Skip to content

Commit 63ac1d5

Browse files
authored
Transitive pinning and pack (#3388)
1 parent 778b8d4 commit 63ac1d5

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

docs/consume-packages/Central-Package-Management.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ For an idea of how central package management may look like, refer to our [sampl
122122

123123
You can automatically override a transitive package version even without an explicit top-level `<PackageReference />` by opting into a feature known as
124124
transitive pinning. This promotes a transitive dependency to a top-level dependency implicitly on your behalf when necessary.
125+
Note that downgrades are allowed when transitive pinning a package. If you attempt to pin a package to a lower version than the one requested by your dependencies, restore will raise a [NU1109](../reference/errors-and-warnings/NU1109.md) error.
125126

126127
You can enable this feature by setting the MSBuild property `CentralPackageTransitivePinningEnabled` to `true` in a project or in a `Directory.Packages.props`
127128
or `Directory.Build.props` import file:
@@ -132,6 +133,45 @@ or `Directory.Build.props` import file:
132133
</PropertyGroup>
133134
```
134135

136+
### Transitive pinning and pack
137+
138+
When a package is transitively pinned, your project uses a higher than the one requested by your dependencies.
139+
If you create a package from your project, in order to ensure that your package will work, NuGet will promote the transitively pinned dependencies to explicit dependencies in the nuspec.
140+
141+
In the following example, `PackageA 1.0.0` has a dependency on `PackageB 1.0.0`.
142+
143+
```xml
144+
<Project>
145+
<ItemGroup>
146+
<PackageVersion Include="PackageA" Version="1.0.0" />
147+
<PackageVersion Include="PackageB" Version="2.0.0" />
148+
</ItemGroup>
149+
</Project>
150+
```
151+
152+
```xml
153+
<Project Sdk="Microsoft.NET.Sdk">
154+
<PropertyGroup>
155+
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
156+
<TargetFramework>net6.0</TargetFramework>
157+
</PropertyGroup>
158+
<ItemGroup>
159+
<PackageReference Include="PackageA" />
160+
</ItemGroup>
161+
</Project>
162+
```
163+
164+
When you use the pack command to create a package, both packages will appear in the dependency group.
165+
166+
```xml
167+
<group targetFramework="net6.0">
168+
<dependency id="PackageA" version="6.12.1" exclude="Build,Analyzers" />
169+
<dependency id="PackageB" version="6.12.1" exclude="Build,Analyzers" />
170+
</group>
171+
```
172+
173+
Because of this, the use of transitive pinning should be carefully evaluated when authoring a library as it may lead to dependencies you did not expect.
174+
135175
## Overriding package versions
136176

137177
You can override an individual package version by using the `VersionOverride` property on a `<PackageReference />` item. This overrides any `<PackageVersion />`
@@ -181,6 +221,7 @@ If you'd like to disable central package management for any a particular project
181221
```
182222

183223
## Global Package References
224+
184225
> [!Note]
185226
> This feature is only available in Visual Studio 2022 17.4 or higher, .NET SDK 7.0.100.preview7 or higher, and NuGet 6.4 or higher.
186227

0 commit comments

Comments
 (0)