diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index c8a67910dcd..5e1244d29bc 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -57,7 +57,7 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, typeConstraint: LibraryDependencyTarget.Package) }; - msBuild.AddPackageReference(packageReferenceArgs.ProjectPath, libraryDependency, packageReferenceArgs.PackageVersion); + msBuild.AddPackageReference(packageReferenceArgs.ProjectPath, libraryDependency, packageReferenceArgs.NoVersion); return 0; } @@ -219,7 +219,7 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, // generate a library dependency with all the metadata like Include, Exlude and SuppressParent var libraryDependency = GenerateLibraryDependency(updatedPackageSpec, packageReferenceArgs, restorePreviewResult, userSpecifiedFrameworks, packageDependency); - msBuild.AddPackageReference(packageReferenceArgs.ProjectPath, libraryDependency, packageReferenceArgs.PackageVersion); + msBuild.AddPackageReference(packageReferenceArgs.ProjectPath, libraryDependency, packageReferenceArgs.NoVersion); } else { @@ -240,7 +240,7 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, msBuild.AddPackageReferencePerTFM(packageReferenceArgs.ProjectPath, libraryDependency, compatibleOriginalFrameworks, - packageReferenceArgs.PackageVersion); + packageReferenceArgs.NoVersion); } // 6. Commit restore result diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs index d9ab83e593a..a67da984da9 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs @@ -125,8 +125,8 @@ public int RemovePackageReference(string projectPath, LibraryDependency libraryD /// /// Path to the csproj file of the project. /// Package Dependency of the package to be added. - /// The version that is passed in as a CLI argument. Null if no version is passed in the command. - public void AddPackageReference(string projectPath, LibraryDependency libraryDependency, string packageVersion) + /// If a version is passed in as a CLI argument. + public void AddPackageReference(string projectPath, LibraryDependency libraryDependency, bool noVersion) { var project = GetProject(projectPath); @@ -134,7 +134,7 @@ public void AddPackageReference(string projectPath, LibraryDependency libraryDep // If the project has a conditional reference, then an unconditional reference is not added. var existingPackageReferences = GetPackageReferencesForAllFrameworks(project, libraryDependency); - AddPackageReference(project, libraryDependency, existingPackageReferences, packageVersion); + AddPackageReference(project, libraryDependency, existingPackageReferences, noVersion); ProjectCollection.GlobalProjectCollection.UnloadProject(project); } @@ -144,9 +144,9 @@ public void AddPackageReference(string projectPath, LibraryDependency libraryDep /// Path to the csproj file of the project. /// Package Dependency of the package to be added. /// Target Frameworks for which the package reference should be added. - /// The version that is passed in as a CLI argument. Null if no version is passed in the command. + /// If a version is passed in as a CLI argument. public void AddPackageReferencePerTFM(string projectPath, LibraryDependency libraryDependency, - IEnumerable frameworks, string packageVersion) + IEnumerable frameworks, bool noVersion) { foreach (var framework in frameworks) { @@ -154,7 +154,7 @@ public void AddPackageReferencePerTFM(string projectPath, LibraryDependency libr { { "TargetFramework", framework } }; var project = GetProject(projectPath, globalProperties); var existingPackageReferences = GetPackageReferences(project, libraryDependency); - AddPackageReference(project, libraryDependency, existingPackageReferences, packageVersion, framework); + AddPackageReference(project, libraryDependency, existingPackageReferences, noVersion, framework); ProjectCollection.GlobalProjectCollection.UnloadProject(project); } } @@ -165,12 +165,12 @@ public void AddPackageReferencePerTFM(string projectPath, LibraryDependency libr /// Project that needs to be modified. /// Package Dependency of the package to be added. /// Package references that already exist in the project. - /// The version that is passed in as a CLI argument. Null if no version is passed in the command. + /// If a version is passed in as a CLI argument. /// Target Framework for which the package reference should be added. private void AddPackageReference(Project project, LibraryDependency libraryDependency, IEnumerable existingPackageReferences, - string packageVersion, + bool noVersion, string framework = null) { // Getting all the item groups in a given project @@ -212,8 +212,9 @@ private void AddPackageReference(Project project, else { // Modify the Directory.Packages.props file with the version that is passed in. - if (packageVersion != null) + if (!noVersion) { + string packageVersion = libraryDependency.LibraryRange.VersionRange.OriginalString; UpdatePackageVersion(project, packageVersionInProps, packageVersion); } }