Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix restore when a package is installed with a version specified in CPM #5982

Merged
merged 8 commits into from
Sep 3, 2024

Conversation

vernou
Copy link
Contributor

@vernou vernou commented Aug 20, 2024

Bug

Fixes: NuGet/Home#13657

Description

When dotnet add package with version specified in Directory.Packages.props, the restored dependency is the last version and no the version specified in Directory.Packages.props.

The modification :
If a CPM version is available for the installed package then uses it.
Else old behavior (last package version)

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.

@dotnet-policy-service dotnet-policy-service bot added the Community PRs created by someone not in the NuGet team label Aug 20, 2024
@vernou vernou changed the title Fix Fix restore when a package is installed with a version specified in CPM Aug 20, 2024
@zivkan
Copy link
Member

zivkan commented Aug 23, 2024

@vernou thanks for the PR. I was hoping it'd be as easy as what this PR currently is.

If you look at the CI build, you'll see there's 2 failing test across multiple platforms: AddPkg_WithCPM_WithPackageReference_WithVersionOverride_WithPackageVersion_NoVersionCLI_NoOps and AddPkg_WithCPM_WhenPackageVersionDoesNotExistAndVersionCLIArgNotPassed_Success

Have a look at what other tests also exist in the same class, and figure out if we need any more test scenarios.

The other tests failing on CI look like they're flakey, so ignore them.

@vernou
Copy link
Contributor Author

vernou commented Aug 24, 2024

The test AddPkg_WithCPM_WithPackageReference_WithVersionOverride_WithPackageVersion_NoVersionCLI_NoOps expects the command add package fails.
The commande fails because the resolved version range from source hasn't OriginalString, so MSBuild fail to update the project with this error :

error: System.AggregateException: One or more errors occurred. (Parameter "Value" cannot be null.)
error:  ---> System.ArgumentNullException: Parameter "Value" cannot be null.
error:    at Microsoft.Build.Shared.ErrorUtilities.ThrowArgumentNull(String parameterName, String resourceName)
error:    at Microsoft.Build.Construction.ProjectMetadataElement.set_Value(String value)
error:    at NuGet.CommandLine.XPlat.MSBuildAPIUtility.UpdateVersionOverride(Project project, ProjectItem packageReference, String versionCLIArgument) in \NuGet.Client\src\NuGet.Core\NuGet.CommandLine.XPlat\Utility\MSBuildAPIUtility.cs:line 575
error:    at NuGet.CommandLine.XPlat.MSBuildAPIUtility.AddPackageReference(Project project, LibraryDependency libraryDependency, IEnumerable`1 existingPackageReferences, Boolean noVersion, String framework) in \NuGet.Client\src\NuGet.Core\NuGet.CommandLine.XPlat\Utility\MSBuildAPIUtility.cs:line 332
error:    at NuGet.CommandLine.XPlat.MSBuildAPIUtility.AddPackageReference(String projectPath, LibraryDependency libraryDependency, Boolean noVersion) in \NuGet.Client\src\NuGet.Core\NuGet.CommandLine.XPlat\Utility\MSBuildAPIUtility.cs:line 258
error:    at NuGet.CommandLine.XPlat.AddPackageReferenceCommandRunner.ExecuteCommand(PackageReferenceArgs packageReferenceArgs, MSBuildAPIUtility msBuild) in \NuGet.Client\src\NuGet.Core\NuGet.CommandLine.XPlat\Commands\PackageReferenceCommands\AddPackageReferenceCommandRunner.cs:line 242

With the PR fix, the resolved version range from CPM has OriginalString, so MSBuild success to update the project (but do nothing, because CPM is enable) and the command success... and the test fails.


I see two possibilities :

  • Adapt the test
  • Force to set OriginalString to null in the version range retrieved from the CPM

I don't undertand why the command is expected to fail and the reason/error sounds dirty.
So I think the test should be modified.

@zivkan, what do you think?

@vernou
Copy link
Contributor Author

vernou commented Aug 25, 2024

I implemented the second to see. When the version is resolved from CPM, the resolved version OriginalString is forced to null.
So AddPkg_WithCPM_WithPackageReference_WithVersionOverride_WithPackageVersion_NoVersionCLI_NoOps success.


I fixed my code and AddPkg_WithCPM_WhenPackageVersionDoesNotExistAndVersionCLIArgNotPassed_Success success.

@vernou vernou marked this pull request as ready for review August 28, 2024 10:29
@vernou vernou requested a review from a team as a code owner August 28, 2024 10:29
@vernou
Copy link
Contributor Author

vernou commented Aug 28, 2024

After going through the tests to be able to add a test, I think I have a better understanding of how they work.
Finally I decided to keep central version range original string and alter the test AddPkg_WithCPM_WithPackageReference_WithVersionOverride_WithPackageVersion_NoVersionCLI_NoOps.

@zivkan, the PR is ready to review.

@zivkan zivkan enabled auto-merge (squash) September 3, 2024 06:40
@zivkan zivkan merged commit 38010e5 into NuGet:dev Sep 3, 2024
28 checks passed
@vernou vernou deleted the bug/13657-add-pkg-with-cpm branch February 23, 2025 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PRs created by someone not in the NuGet team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dotnet add package with CPM installs a different version than what gets restored
4 participants