-
Notifications
You must be signed in to change notification settings - Fork 257
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
dotnet add package updates version in-place even if it was initially defined in an MSBuild property #14041
Comments
Our code will need to parse the existing unexpanded value of <PropertyGroup>
<SomePackageVersion Condition="'$(TargetFramework)' == 'net472'">1.0.0</SomePackageVersion>
<SomePackageVersion Condition="'$(TargetFramework)' != 'net472'">2.0.0</SomePackageVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SomePackage" Version="$(SomePackageVersion)" />
</ItemGroup> The We could also log an error if we detect that the user has done something like this and indicate its not supported, if we don't think we can get it right. |
Here is a prototype I made which makes it work at https://github.com/NuGet/NuGet.Client/blob/313eecd3af442ee2eeed2e6decf310858934ab21/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs?plain=1#L546C1-L547C1: foreach (var packageReferenceItem in packageReferencesItems)
{
var packageVersion = libraryDependency.LibraryRange.VersionRange.OriginalString ??
libraryDependency.LibraryRange.VersionRange.MinVersion.ToString();
ProjectMetadata metadatum = packageReferenceItem.GetMetadata(VERSION_TAG);
// Verify that the evaluated and unevaluated values are the same, meaning it is just a plain value like:
// <PackageReference Include="SomePackage" Version="1.2.3" />
if (string.Equals(metadatum.EvaluatedValue, metadatum.UnevaluatedValue))
{
packageReferenceItem.SetMetadataValue(VERSION_TAG, packageVersion);
UpdateExtraMetadataInProjectItem(libraryDependency, packageReferenceItem);
Logger.LogInformation(string.Format(CultureInfo.CurrentCulture,
Strings.Info_AddPkgUpdated,
libraryDependency.Name,
packageVersion,
packageReferenceItem.Xml.ContainingProject.FullPath));
return;
}
// The declared Version attribute is different after evaluation, it might be something like:
// <PackageReference Include="SomePackage" Version="$(SomeProperty)" />
var match = Regex.Match(metadatum.UnevaluatedValue, @"^\$\((?<PropertyName>[\w-]+)\)$");
if (!match.Groups["PropertyName"].Success)
{
// The Version metadata is something other than a simple property reference. It could be something like:
// <PackageReference Include="SomePackage" Version="1.2.$(MinorVersion)" />
// Setting the version in this case would be unsafe, log an error. Maybe we could add a --force argument to override this?
return;
}
string propertyName = match.Groups["PropertyName"].Value;
ProjectProperty property = packageReferenceItem.Project.GetProperty(propertyName);
if (property == null)
{
// The property is not defined in the project, log an error
return;
}
if (property.IsImported)
{
// The property is defined in some other imported file. It could be unsafe to edit some random .props file, maybe have a --force argument?
// Otherwise, log an error
return;
}
if (!string.Equals(property.UnevaluatedValue, property.EvaluatedValue))
{
// The property is not just plain text and is defined in terms of other properties, log an error
// <SomeProperty>$(MinorVersion)</SomeProperty>
// We could see if its just another property and keep looking but that's probably too advanced, just log an error that its unsupported
return;
}
// Update the in-memory property value, calling project.Save() will persist the change
packageReferenceItem.Project.SetProperty(propertyName, packageVersion);
} |
NuGet Product Used
dotnet.exe
Product Version
I believe this issue exists in all the versions.
Worked before?
No response
Impact
It bothers me. A fix would be nice
Repro Steps & Context
Repro steps
dotnet add .\classlib.csproj package newtonsoft.json -v 13.0.3
command.Actual
The version number is updated directly under the
PackageReference
element instead of updating theNewtonsoftJsonVersion
MSBuild property value.Expected
The version number should be updated in the corresponding MSBuild property.
Verbose Logs
The text was updated successfully, but these errors were encountered: