Skip to content

Commit 1408fff

Browse files
EvangelinkCopilot
andauthored
Avoid MSB4011 duplicate-import warnings when layering MSTest.Sdk on another base SDK (#9565)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 9f8dd5e commit 1408fff

4 files changed

Lines changed: 140 additions & 3 deletions

File tree

‎src/Package/MSTest.Sdk/README.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,41 @@ Instead, the `.targets` files should split the version specification depending o
1818
matching `PackageVersion` item carrying the `Version` metadata so CPM resolves the version.
1919

2020
This approach works regardless of `CentralPackageVersionOverrideEnabled` and does not produce `NU1009` warnings.
21+
22+
## Layering on top of a different base SDK
23+
24+
`MSTest.Sdk` implicitly imports `Microsoft.NET.Sdk` as its base SDK, so `<Project Sdk="MSTest.Sdk">`
25+
is enough for the common case. To combine it with a different base SDK (for example
26+
`Microsoft.NET.Sdk.Web` for an ASP.NET Core integration test project), import both SDKs manually and
27+
list `Microsoft.NET.Sdk.Web` first so it owns the base `Microsoft.NET.Sdk` import:
28+
29+
```xml
30+
<Project>
31+
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk.Web" />
32+
<Import Project="Sdk.props" Sdk="MSTest.Sdk" />
33+
34+
<PropertyGroup>
35+
<TargetFramework>net10.0</TargetFramework>
36+
</PropertyGroup>
37+
38+
<Import Project="Sdk.targets" Sdk="MSTest.Sdk" />
39+
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
40+
</Project>
41+
```
42+
43+
Because the version cannot be specified on an `<Import>`'s `Sdk` attribute the same way it can on
44+
`<Project Sdk="MSTest.Sdk/x.y.z">`, pin the `MSTest.Sdk` version through `global.json`:
45+
46+
```json
47+
{
48+
"msbuild-sdks": {
49+
"MSTest.Sdk": "x.y.z"
50+
}
51+
}
52+
```
53+
54+
`Sdk.props`/`Sdk.targets` guard their `Microsoft.NET.Sdk` import behind the
55+
`_MSTestSdkImportsMicrosoftNETSdk` property: `MSTest.Sdk` only imports the base SDK when no other SDK
56+
has already done so (detected via the `UsingMicrosoftNETSdk` property that `Microsoft.NET.Sdk` sets
57+
very early). This keeps the manual/mixed layering scenario free of `MSB4011` duplicate-import
58+
warnings.

‎src/Package/MSTest.Sdk/Sdk/Sdk.props.template‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,33 @@
88
<UsingMSTestSdk>true</UsingMSTestSdk>
99
</PropertyGroup>
1010

11+
<!--
12+
MSTest.Sdk layers on top of Microsoft.NET.Sdk. In the common case, MSTest.Sdk is the outer
13+
SDK (`<Project Sdk="MSTest.Sdk">`) and is responsible for importing the base .NET SDK.
14+
15+
However, MSTest.Sdk can also be layered on top of a different base SDK (for example
16+
Microsoft.NET.Sdk.Web) using manual SDK imports:
17+
18+
<Project>
19+
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk.Web" />
20+
<Import Project="Sdk.props" Sdk="MSTest.Sdk" />
21+
...
22+
<Import Project="Sdk.targets" Sdk="MSTest.Sdk" />
23+
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
24+
</Project>
25+
26+
In that case the outer SDK has already imported Microsoft.NET.Sdk (which sets
27+
'UsingMicrosoftNETSdk' to 'true' before anything else), so importing it again here would emit
28+
MSB4011 duplicate-import warnings. We only import the base SDK when nobody else has, and we
29+
remember that decision so Sdk.targets knows whether it should also import the base SDK targets.
30+
-->
31+
<PropertyGroup>
32+
<_MSTestSdkImportsMicrosoftNETSdk Condition=" '$(_MSTestSdkImportsMicrosoftNETSdk)' == '' AND '$(UsingMicrosoftNETSdk)' != 'true' ">true</_MSTestSdkImportsMicrosoftNETSdk>
33+
<_MSTestSdkImportsMicrosoftNETSdk Condition=" '$(_MSTestSdkImportsMicrosoftNETSdk)' == '' ">false</_MSTestSdkImportsMicrosoftNETSdk>
34+
</PropertyGroup>
35+
1136
<!-- Implicit top import -->
12-
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
37+
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" Condition=" '$(_MSTestSdkImportsMicrosoftNETSdk)' == 'true' " />
1338

1439
<PropertyGroup>
1540
<EnableAspireTesting Condition=" '$(EnableAspireTesting)' == '' ">false</EnableAspireTesting>

‎src/Package/MSTest.Sdk/Sdk/Sdk.targets‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
<!-- Import VSTest.targets -->
1414
<Import Project="$(MSBuildThisFileDirectory)VSTest/VSTest.targets" Condition=" '$(UseVSTest)' == 'true' " />
1515

16-
<!-- Implicit bottom import -->
17-
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
16+
<!--
17+
Implicit bottom import.
18+
19+
Only import the base .NET SDK targets when MSTest.Sdk was the SDK that imported the base .NET
20+
SDK props (see the detailed comment in Sdk.props). When MSTest.Sdk is layered on top of another
21+
base SDK, that outer SDK imports the base targets itself, so importing them here would emit
22+
MSB4011 duplicate-import warnings.
23+
-->
24+
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" Condition=" '$(_MSTestSdkImportsMicrosoftNETSdk)' == 'true' " />
1825
</Project>

‎test/IntegrationTests/MSTest.Acceptance.IntegrationTests/SdkTests.cs‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,4 +672,71 @@ public void TestMethod1()
672672
testHostResult.AssertOutputContains("Test Parallelization enabled");
673673
testHostResult.AssertOutputContains("(Workers: 3, Scope: MethodLevel)");
674674
}
675+
676+
// Verifies that MSTest.Sdk can be layered on top of a different base SDK (here Microsoft.NET.Sdk.Web)
677+
// using manual SDK imports without emitting MSB4011 duplicate-import warnings. See https://github.com/microsoft/testfx/issues/9562.
678+
[TestMethod]
679+
public async Task MSTestSdk_LayeredOnTopOfWebSdk_BuildsWithoutDuplicateImportWarnings()
680+
{
681+
const string MixedSdkSource = """
682+
#file MSTestWeb.csproj
683+
<Project>
684+
685+
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk.Web" />
686+
<Import Project="Sdk.props" Sdk="MSTest.Sdk" />
687+
688+
<PropertyGroup>
689+
<EnableMicrosoftTestingPlatform>true</EnableMicrosoftTestingPlatform>
690+
<TargetFramework>$TargetFramework$</TargetFramework>
691+
<PlatformTarget>x64</PlatformTarget>
692+
<NoWarn>$(NoWarn);NU1507</NoWarn>
693+
</PropertyGroup>
694+
695+
<Import Project="Sdk.targets" Sdk="MSTest.Sdk" />
696+
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
697+
698+
</Project>
699+
700+
#file global.json
701+
{
702+
"msbuild-sdks": {
703+
"MSTest.Sdk": "$MSTestVersion$"
704+
}
705+
}
706+
707+
#file UnitTest1.cs
708+
using Microsoft.AspNetCore.Builder;
709+
using Microsoft.VisualStudio.TestTools.UnitTesting;
710+
711+
namespace MSTestWebTest
712+
{
713+
[TestClass]
714+
public class UnitTest1
715+
{
716+
[TestMethod]
717+
public void TestMethod1()
718+
{
719+
WebApplicationBuilder builder = WebApplication.CreateBuilder();
720+
Assert.IsNotNull(builder);
721+
}
722+
}
723+
}
724+
""";
725+
726+
const string MixedAssetName = "MSTestWeb";
727+
728+
using TestAsset testAsset = await TestAsset.GenerateAssetAsync(
729+
MixedAssetName,
730+
MixedSdkSource
731+
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion)
732+
.PatchCodeWithReplace("$TargetFramework$", TargetFrameworks.NetCurrent));
733+
734+
DotnetMuxerResult compilationResult = await DotnetCli.RunAsync($"build -c {BuildConfiguration.Release} {testAsset.TargetAssetPath}", cancellationToken: TestContext.CancellationToken);
735+
compilationResult.AssertExitCodeIs(0);
736+
compilationResult.AssertOutputDoesNotContain("MSB4011");
737+
738+
var testHost = TestHost.LocateFrom(testAsset.TargetAssetPath, MixedAssetName, TargetFrameworks.NetCurrent, buildConfiguration: BuildConfiguration.Release);
739+
TestHostResult testHostResult = await testHost.ExecuteAsync(cancellationToken: TestContext.CancellationToken);
740+
testHostResult.AssertOutputContainsSummary(0, 1, 0);
741+
}
675742
}

0 commit comments

Comments
 (0)