NuGet / NuGet/Home

Changing the version for dotnet pack does not change the dependency version

Open
#9,596 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Functionality:Pack Priority:3 Resolution:Question
Dominant language
HTML
Stars
1.6k
Forks
276
Avg merge
6d 1h
Merged PRs (30d)
1

Description

When defining a version for a package inside a custom target the package version and the reference version inside the package generated through dontet pack are mismatched.

I change the version and packVersion within a Target running before GenerateNuSpec (in Directory.Build.targets) like this:

<Target Name="SetVersion" BeforeTargets="GenerateNuspec">
		<PropertyGroup>
			<Version>$(MyProjectSpecificCalculatedVersion)</Version>
			<PackageVersion>$(MyProjectSpecificCalculatedVersion)</PackageVersion>
		</PropertyGroup>
</Target>

This is picked up during pack and correctly set as the version for the nupkg filename and inside the nupkg as the nuspec-version, as expected.

When referencing project B from project A both are packed with the correct version but the dependency inside the nupkg for project B is always set to 1.0.0

It seems the version used is whatever is written inside the obj/project.assets.json which is generated during dotnet restore. But even if I set BeforeTargets="Restore" that version is never populated correctly.

Is there another way to write that target in order to have the same version for both the package and the reference?

This is using dotnet version 3.1.300, a minimal reproduction is attached:

DotNetPackTest.zip

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the attached DotNetPackTest.zip to reproduce the mismatch with dotnet pack and restore. Start with Directory.Build.targets, the SetVersion target, GenerateNuspec, and obj/project.assets.json; compare the package version with the project dependency version. Done means the generated package and its dependency reference use the same calculated version.

Written by the indexing model from the issue text.

Assessment

Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.