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 invalid target framework error for Solution restore #6091

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,11 @@ internal static string[] GetTargetFrameworkStrings(IMSBuildProject project)
if (string.IsNullOrEmpty(targetFrameworks))
{
targetFrameworks = project.GetProperty("TargetFramework");
if (targetFrameworks.Contains(';'))
{
var error = RestoreLogMessage.CreateError(NuGetLogCode.NU1001, string.Format(CultureInfo.CurrentCulture, Strings.Error_TargetFrameworkWithSemicolon, targetFrameworks));
throw new RestoreCommandException(error);
}
}
var projectFrameworkStrings = MSBuildStringUtility.Split(targetFrameworks);
return projectFrameworkStrings;
Expand Down
9 changes: 9 additions & 0 deletions src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/NuGet.Core/NuGet.Build.Tasks/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,8 @@
<value>An error occurred starting static graph-based restore. {0}. Please file an issue at https://github.com/NuGet/Home</value>
<comment>0 - The exception message</comment>
</data>
<data name="Error_TargetFrameworkWithSemicolon" xml:space="preserve">
<value>The TargetFramework value '{0}' is not valid. To multi-target, use the 'TargetFrameworks' property instead.</value>
<comment>0 - Is a string with a list of target frameworks separated by a semi colon</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ private static IEnumerable<TargetFrameworkInformation> GetTargetFrameworkInforma
foreach (var item in GetItemByType(items, "TargetFrameworkInformation"))
{
var frameworkString = item.GetProperty("TargetFramework");
if (frameworkString.Contains(';'))
Copy link
Member

@nkolev92 nkolev92 Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing from here probably leads to a bad failure in NuGet.exe?

In general, NuGet.exe expects a dg spec to create an this fails that.

Can you double check NuGet.exe?

{
var error = RestoreLogMessage.CreateError(NuGetLogCode.NU1001, string.Format(CultureInfo.CurrentCulture, Strings.Error_TargetFrameworkWithSemicolon, frameworkString));
throw new RestoreCommandException(error);
}

var targetFrameworkMoniker = item.GetProperty("TargetFrameworkMoniker");
var targetPlatforMoniker = item.GetProperty("TargetPlatformMoniker");
var targetPlatformMinVersion = item.GetProperty("TargetPlatformMinVersion");
Expand Down
9 changes: 9 additions & 0 deletions src/NuGet.Core/NuGet.Commands/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/NuGet.Core/NuGet.Commands/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1127,4 +1127,8 @@ NuGet requires HTTPS sources. Refer to https://aka.ms/nuget-https-everywhere for
<value>Audit source '{0}' did not provide any vulnerability data.</value>
<comment>{0} is the source name</comment>
</data>
<data name="Error_TargetFrameworkWithSemicolon" xml:space="preserve">
<value>The TargetFramework value '{0}' is not valid. To multi-target, use the 'TargetFrameworks' property instead.</value>
<comment>0 - Is a string with a list of target frameworks separated by a semi colon</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public void GetOriginalTargetFrameworks_WhenTargetFramworksSpecified_HasCorrectT
actual.Should().BeEquivalentTo(expected);
}

[Theory]
[InlineData("net40;net45")]
[InlineData("net40;net45 ; netstandard2.0 ")]
public void GetOriginalTargetFramework_WhenTargetFramworkSpecified_HasCorrectTargetFramework(string targetFrameworks)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void GetOriginalTargetFramework_WhenTargetFramworkSpecified_HasCorrectTargetFramework(string targetFrameworks)
public void GetOriginalTargetFramework_WhenMultipleTargetFramworkSpecified_ThrowRestoreCommandException(string targetFrameworks)

Might want to change the name of this test.

{
var project = new MockMSBuildProject(new Dictionary<string, string>
{
["TargetFramework"] = targetFrameworks
});

Assert.Throws<RestoreCommandException>(() => MSBuildStaticGraphRestore.GetTargetFrameworkStrings(project));
}

[Fact]
public void GetPackageDownloads_WhenDuplicatesExist_DuplicatesIgnored()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3399,6 +3399,36 @@ public void MSBuildRestoreUtility_AddPackageDownloads_InvalidVersionRange(string
var exception = Assert.Throws<ArgumentException>(() => MSBuildRestoreUtility.AddPackageDownloads(spec, msbuildItems));
}

[Fact]
public void MSBuildRestoreUtility_GetPackageSpec_InvalidTargetFramework()
{
// Arrange
var targetFramework = "netstandard2.0;net7.0";
var items = new[] {
CreateItems(new Dictionary<string, string>()
{
{ "Type", "ProjectSpec" },
{ "ProjectName", "a" },
{ "ProjectStyle", "PackageReference" },
{ "ProjectUniqueName", "a" },
}),
CreateItems(new Dictionary<string, string>()
{
{ "Type", "TargetFrameworkInformation" },
{ "AssetTargetFallback", "" },
{ "PackageTargetFallback", "" },
{ "ProjectUniqueName", "a" },
{ "TargetFramework", targetFramework },
{ "TargetPlatformIdentifier", "" },
{ "TargetPlatformMoniker", "" },
{ "TargetPlatformVersion", "" },
})
};

// Act & Assert
var exception = Assert.Throws<RestoreCommandException>(() => MSBuildRestoreUtility.GetPackageSpec(items));
}

[Fact]
public void MSBuildRestoreUtility_AddPackageDownloads_NoVersion_ThrowsException()
{
Expand Down
Loading