Skip to content

Commit

Permalink
Add erro to invalid targetFramework with semicolon
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrrm committed Oct 29, 2024
1 parent fac3557 commit 6abdca3
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,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(';'))
{
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 @@ -73,6 +73,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)
{
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

0 comments on commit 6abdca3

Please sign in to comment.