Skip to content

Commit 25b360d

Browse files
authored
Add filtering to SdkSupportedTargetPlatformVersion validation. Fixes #38016. (#38017)
1 parent 849d677 commit 25b360d

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.TargetFrameworkInference.targets

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ Copyright (c) .NET Foundation. All rights reserved.
222222
BeforeTargets="ProcessFrameworkReferences"
223223
Condition="'$(TargetPlatformVersion)' != '' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), 5.0)) and ('$(Language)' != 'C++' or '$(_EnablePackageReferencesInVCProjects)' == 'true')">
224224
<ItemGroup>
225-
<_ValidTargetPlatformVersion Include="@(SdkSupportedTargetPlatformVersion)" Condition="'@(SdkSupportedTargetPlatformVersion)' != '' and $([MSBuild]::VersionEquals(%(Identity), $(TargetPlatformVersion)))" />
225+
<_ApplicableTargetPlatformVersion Include="@(SdkSupportedTargetPlatformVersion)" Condition="'@(SdkSupportedTargetPlatformVersion)' != '' and '%(SdkSupportedTargetPlatformVersion.DefineConstantsOnly)' != 'true'" RemoveMetadata="DefineConstantsOnly" />
226+
<_ValidTargetPlatformVersion Include="@(_ApplicableTargetPlatformVersion)" Condition="'@(_ApplicableTargetPlatformVersion)' != '' and $([MSBuild]::VersionEquals(%(Identity), $(TargetPlatformVersion)))" />
226227
</ItemGroup>
227228

228229
<PropertyGroup>
@@ -236,8 +237,8 @@ Copyright (c) .NET Foundation. All rights reserved.
236237
Condition="'$(TargetPlatformVersion)' != '' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), 5.0)) and ('$(Language)' != 'C++' or '$(_EnablePackageReferencesInVCProjects)' == 'true')">
237238
<PropertyGroup>
238239
<TargetPlatformVersionSupported Condition="'$(TargetPlatformVersionSupported)' == '' and '@(_ValidTargetPlatformVersion)' != ''" >true</TargetPlatformVersionSupported>
239-
<_ValidTargetPlatformVersions Condition="'@(SdkSupportedTargetPlatformVersion)' != ''" >@(SdkSupportedTargetPlatformVersion, '%0a')</_ValidTargetPlatformVersions>
240-
<_ValidTargetPlatformVersions Condition="'@(SdkSupportedTargetPlatformVersion)' == ''" >None</_ValidTargetPlatformVersions>
240+
<_ValidTargetPlatformVersions Condition="'@(_ApplicableTargetPlatformVersion)' != ''" >@(_ApplicableTargetPlatformVersion, '%0a')</_ValidTargetPlatformVersions>
241+
<_ValidTargetPlatformVersions Condition="'@(_ApplicableTargetPlatformVersion)' == ''" >None</_ValidTargetPlatformVersions>
241242
</PropertyGroup>
242243

243244
<NetSdkError Condition="'$(TargetPlatformVersionSupported)' != 'true'"

test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildWithATargetPlatform.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.NET.Build.Tasks;
5+
46
namespace Microsoft.NET.Build.Tests
57
{
68
public class GivenThatWeWantToBuildWithATargetPlatform : SdkTest
@@ -90,5 +92,82 @@ public void It_fails_on_unsupported_os()
9092
.And
9193
.HaveStdOutContaining("NETSDK1139");
9294
}
95+
96+
[Fact]
97+
public void It_fails_if_targetplatformversion_is_constant_only()
98+
{
99+
var testProject = new TestProject()
100+
{
101+
Name = "It_fails_if_targetplatformversion_is_constant_only",
102+
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
103+
};
104+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
105+
106+
107+
string DirectoryBuildTargetsContent = $@"
108+
<Project>
109+
<ItemGroup>
110+
<SdkSupportedTargetPlatformVersion Include=""111.0"" DefineConstantsOnly=""true"" />
111+
<SdkSupportedTargetPlatformVersion Include=""222.0"" />
112+
</ItemGroup>
113+
<PropertyGroup>
114+
<TargetPlatformVersion>111.0</TargetPlatformVersion>
115+
<TargetPlatformIdentifier>ios</TargetPlatformIdentifier>
116+
<TargetPlatformSupported>true</TargetPlatformSupported>
117+
</PropertyGroup>
118+
</Project>
119+
";
120+
121+
File.WriteAllText(Path.Combine(testAsset.TestRoot, "Directory.Build.targets"), DirectoryBuildTargetsContent);
122+
123+
var buildCommand = new BuildCommand(testAsset);
124+
buildCommand.Execute()
125+
.Should()
126+
.Fail()
127+
.And
128+
.HaveStdOutContaining("NETSDK1140")
129+
.And
130+
.HaveStdOutContaining(string.Format(Strings.InvalidTargetPlatformVersion, "111.0", "ios", "222.0").Split ('\n', '\r') [0])
131+
.And
132+
.HaveStdOutContaining("222.0");
133+
}
134+
135+
[Fact]
136+
public void It_fails_if_targetplatformversion_is_invalid()
137+
{
138+
var testProject = new TestProject()
139+
{
140+
Name = "It_fails_if_targetplatformversion_is_invalid",
141+
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
142+
};
143+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
144+
145+
146+
string DirectoryBuildTargetsContent = $@"
147+
<Project>
148+
<ItemGroup>
149+
<SdkSupportedTargetPlatformVersion Include=""222.0"" />
150+
</ItemGroup>
151+
<PropertyGroup>
152+
<TargetPlatformVersion>111.0</TargetPlatformVersion>
153+
<TargetPlatformIdentifier>ios</TargetPlatformIdentifier>
154+
<TargetPlatformSupported>true</TargetPlatformSupported>
155+
</PropertyGroup>
156+
</Project>
157+
";
158+
159+
File.WriteAllText(Path.Combine(testAsset.TestRoot, "Directory.Build.targets"), DirectoryBuildTargetsContent);
160+
161+
var buildCommand = new BuildCommand(testAsset);
162+
buildCommand.Execute()
163+
.Should()
164+
.Fail()
165+
.And
166+
.HaveStdOutContaining("NETSDK1140")
167+
.And
168+
.HaveStdOutContaining(string.Format(Strings.InvalidTargetPlatformVersion, "111.0", "ios", "222.0").Split ('\n', '\r') [0])
169+
.And
170+
.HaveStdOutContaining("222.0");
171+
}
93172
}
94173
}

0 commit comments

Comments
 (0)