Skip to content

Avoid MSB4011 duplicate-import warnings when layering MSTest.Sdk on another base SDK#9565

Open
Evangelink wants to merge 2 commits into
mainfrom
dev/amauryleve/literate-carnival
Open

Avoid MSB4011 duplicate-import warnings when layering MSTest.Sdk on another base SDK#9565
Evangelink wants to merge 2 commits into
mainfrom
dev/amauryleve/literate-carnival

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Fixes #9562.

Problem

MSTest.Sdk implicitly imports Microsoft.NET.Sdk as its base SDK in both Sdk.props and Sdk.targets. When MSTest.Sdk is layered on top of a different base SDK (for example Microsoft.NET.Sdk.Web for an ASP.NET Core integration test project) using manual SDK imports, the base Microsoft.NET.Sdk gets imported twice and the build emits MSB4011 duplicate-import warnings:

warning MSB4011: "...\Microsoft.NET.Sdk\Sdk\Sdk.props" cannot be imported again.
It was already imported at "...\Microsoft.NET.Sdk.Web\Targets\Sdk.Server.props".

Fix

Guard the Microsoft.NET.Sdk import behind a new _MSTestSdkImportsMicrosoftNETSdk property. Microsoft.NET.Sdk sets UsingMicrosoftNETSdk=true very early (before anything else), so MSTest.Sdk can detect whether an outer SDK has already imported the base SDK and skip re-importing it. The decision is recorded in Sdk.props and reused in Sdk.targets so both stay in sync — when layered, the outer SDK owns the base import.

The common <Project Sdk="MSTest.Sdk"> usage is unchanged: UsingMicrosoftNETSdk is unset at that point, so MSTest.Sdk still imports the base SDK exactly as before.

Changes

  • src/Package/MSTest.Sdk/Sdk/Sdk.props.template — compute _MSTestSdkImportsMicrosoftNETSdk and gate the base props import on it.
  • src/Package/MSTest.Sdk/Sdk/Sdk.targets — gate the base targets import on the same property.
  • src/Package/MSTest.Sdk/README.md — document the first-class layering pattern (import the base SDK first).
  • test/IntegrationTests/MSTest.Acceptance.IntegrationTests/SdkTests.cs — new acceptance test MSTestSdk_LayeredOnTopOfWebSdk_BuildsWithoutDuplicateImportWarnings that builds a Microsoft.NET.Sdk.Web + MSTest.Sdk project, asserts it runs, and asserts no MSB4011 in the output.

Verification

Reproduced the scenario with the real base SDKs plus a control:

Scenario ... cannot be imported again (MSB4011)
Mixed Web SDK, unguarded (before) 12
Mixed Web SDK, guarded (after) 0
Normal single-SDK (after) 0 — base SDK still imported, compiles cleanly

…nother base SDK (#9562)

Guard the Microsoft.NET.Sdk import in MSTest.Sdk's Sdk.props/Sdk.targets behind a new _MSTestSdkImportsMicrosoftNETSdk property. MSTest.Sdk now only imports the base .NET SDK when no other SDK has already done so (detected via UsingMicrosoftNETSdk, which Microsoft.NET.Sdk sets very early). This lets MSTest.Sdk be layered on top of a different base SDK such as Microsoft.NET.Sdk.Web via manual SDK imports without emitting MSB4011 warnings.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 3, 2026 09:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR improves MSTest.Sdk’s behavior when it’s layered on top of another SDK (e.g., Microsoft.NET.Sdk.Web) via manual Sdk.props/Sdk.targets imports by preventing duplicate imports of Microsoft.NET.Sdk that would otherwise produce MSB4011 warnings.

Changes:

  • Add _MSTestSdkImportsMicrosoftNETSdk in Sdk.props to detect whether an outer SDK already imported Microsoft.NET.Sdk, and conditionally skip re-importing it.
  • Use the same _MSTestSdkImportsMicrosoftNETSdk decision in Sdk.targets to keep props/targets imports in sync and avoid duplicate-import warnings.
  • Add an acceptance test that builds and runs a Microsoft.NET.Sdk.Web + MSTest.Sdk manually-layered project and asserts no MSB4011 appears in build output; document the layering pattern in the SDK README.
Show a summary per file
File Description
test/IntegrationTests/MSTest.Acceptance.IntegrationTests/SdkTests.cs Adds an acceptance test covering the mixed/manual SDK import scenario and asserting MSB4011 is absent.
src/Package/MSTest.Sdk/Sdk/Sdk.targets Conditionally imports Microsoft.NET.Sdk targets only when MSTest.Sdk performed the base SDK import.
src/Package/MSTest.Sdk/Sdk/Sdk.props.template Computes _MSTestSdkImportsMicrosoftNETSdk and conditionally imports Microsoft.NET.Sdk props to prevent duplicate-import warnings.
src/Package/MSTest.Sdk/README.md Documents the recommended manual layering pattern (base SDK first) and explains the guard behavior.

Review details

  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Low

…l SDK imports

The versioned SDK form (MSTest.Sdk/version) only resolves on the outer <Project Sdk> attribute, not on <Import Sdk=...>, which caused MSB4236 'SDK could not be found' in CI. Pin the version through global.json's msbuild-sdks instead, matching the canonical way to pin MSBuild project SDK versions for manual/mixed imports.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

🧪 Test quality grade — PR #9565

GradeTestNotes
A (90–100) new SdkTests.
MSTestSdk_
LayeredOnTopOfWebSdk_
BuildsWithoutDuplicateImportWarnings
Well-structured acceptance test asserting MSB4011 absence and end-to-end execution of the layered project.

This advisory comment was generated automatically. Grades are heuristic
and informational — they do not block merging. Re-run with
/grade-tests.

🤖 Automated content by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Grade Tests on PR (on open / sync) workflow. · 56.5 AIC · ⌖ 7.79 AIC · ⊞ 9.5K · [◷]( · )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explore reducing MSB4011 duplicate-import warnings when MSTest.Sdk is combined with another base SDK

2 participants