Skip to content

fix(sdk): Let projects remove the Uno implicit usings - #25567

Open
mikernet wants to merge 5 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/sdk-implicit-using-remove
Open

mikernet wants to merge 5 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/sdk-implicit-using-remove

Conversation

@mikernet

@mikernet mikernet commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #18525

PR Type:

🐞 Bugfix

What changed? 🚀

The Uno.Sdk added its implicit Using items in Uno.Implicit.Namespaces.targets, which Uno.Common.targets imports from Sdk.targets, after the project body. A <Using Remove="Uno.UI" /> in the project therefore ran before the item it was meant to remove and did nothing. DisableImplicitUnoNamespaces, which drops them all, was the only way out.

  • The items are now defined before the project body, in Uno.Implicit.Namespaces.props, imported right after the .NET SDK's Sdk.props, where the .NET SDK defines its own implicit usings. The ImplicitUsings and DisableImplicitUnoNamespaces checks are item conditions, evaluated after every property, so values set in the project body or in a Directory.Build.targets apply. Before, DisableImplicitUnoNamespaces set after the project body, in a Directory.Build.targets for example, was ignored; it now applies.
  • A WinAppSDK-only library gets no Uno.UI using on Windows. With DisableImplicitUnoWinAppSdkPackages, that target has no Uno.WinUI, which provides the namespace there, so global using global::Uno.UI; failed with CS0246 whenever ImplicitUsings was on. The using now follows the condition that strips the packages, computed once in _UnoIsWinAppSdkOnlyLibrary. It is defined last, in Uno.Sdk.After.targets, so the property still counts when set in a Directory.Build.targets. The other Uno usings stay: the Windows App SDK, the Windows SDK and the BCL provide them.
  • Docs: a new "Implicit usings" section in using-the-uno-sdk.md. It also says that a static <PackageReference Remove> cannot remove an implicit package, even in a Directory.Build.targets: the SDK adds them after the body on purpose, so that an explicit PackageReference replaces one, and its UnoImplicitPackages target adds them again before restore. It lists the supported opt-outs instead.

This is a deliberate trade-off for one case: a WinAppSDK-only library that gets Uno.WinUI back on Windows, explicitly or through a feature package such as Toolkit, and relies on the Uno.UI using there now adds <Using Include="Uno.UI" />. The property only exists since 6.7, and without such a package the same library never compiled.

Tests

Given_UnoSdkImplicitUsings (Uno.UI.Tasks.Tests, 15 cases) evaluates the in-repo SDK around a project body, with its own Directory.Build.props and Directory.Build.targets. It runs dotnet msbuild through the shared DotnetMSBuild runner from the repository root, so the repo's global.json picks the SDK, and parses only its standard output. The Remove cases fail with the items imported back where they were, the WinAppSDK-only cases fail without the Uno.UI condition, and the Directory.Build.targets case fails with the property defined in Uno.Common.targets. Uno.UI.Tasks.Tests: 211/211 pass.

Runtime, with locally packed SDKs: a WinAppSDK-only library, opting in from the project or from a Directory.Build.targets, fails with CS0246 on master (as it does with Uno.Sdk 6.7.30) and builds with this change, and a <Using Remove> in the project takes the namespace out of GlobalUsings.g.cs.

PR Checklist ✅

Copilot AI balanced review requested due to automatic review settings October 18, 2026 05:42
@github-actions github-actions Bot added area/code-generation Categorizes an issue or PR as relevant to code generation kind/documentation labels Oct 18, 2026

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.

🟢 Approval recommended

The focused import-ordering fix, consistent filtering, and regression coverage address the reported issue with no unresolved findings.

No open issues 🎉

What changed in this PR

Fixes #18525 by letting projects remove individual Uno SDK implicit usings and omitting Uno.UI for WinAppSDK-only libraries.

Changes:

  • Moves implicit usings before the project body so Using Remove works.
  • Shares the WinAppSDK-only condition between package and namespace filtering.
  • Adds regression coverage and documents supported opt-outs.
File Description
src/​Uno.Sdk/​targets/​Uno.Sdk.After.targets Defines the shared library-filtering condition.
src/​Uno.Sdk/​targets/​Uno.Implicit.Packages.ProjectSystem.targets Reuses the condition and explains package ordering.
src/​Uno.Sdk/​targets/​Uno.Implicit.Namespaces.targets Removes the late namespace definitions.
src/​Uno.Sdk/​targets/​Uno.Implicit.Namespaces.props Defines removable, conditional implicit usings.
src/​Uno.Sdk/​targets/​Uno.Common.targets Removes the old namespace import.
src/​Uno.Sdk/​Sdk/​Sdk.props.buildschema.json Documents namespace suppression.
src/​Uno.Sdk/​Sdk/​Sdk.props Imports implicit usings before the project body.
src/​SourceGenerators/​Uno.UI.Tasks.Tests/​Given_UnoSdkImplicitUsings.cs Adds 15 SDK evaluation cases.
doc/​articles/​features/​using-the-uno-sdk.md Explains implicit usings and package opt-outs.

Review effort: Balanced

@unodevops

Copy link
Copy Markdown
Contributor

@mergify

mergify Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

The Uno.Sdk added its implicit Using items in
Uno.Implicit.Namespaces.targets, which Uno.Common.targets imports from
Sdk.targets, after the project body. A <Using Remove="..." /> in the
project was evaluated before the item it was meant to remove, so it had
no effect, and DisableImplicitUnoNamespaces was the only way out.

Define the items in a props file imported right after the .NET SDK's
Sdk.props, as the .NET SDK does with its own implicit usings. The
DisableImplicitUnoNamespaces check moves from the import to the item
group. Item conditions are evaluated after all properties, so a value
set in the project body still applies, and one set later, in a
Directory.Build.targets for example, now applies too: the import
condition was evaluated before that file.

Fixes unoplatform#18525
With DisableImplicitUnoWinAppSdkPackages, a library's Windows App SDK
target no longer references Uno.WinUI, which is what provides the Uno.UI
namespace there, but the Uno.Sdk still added global using Uno.UI. With
ImplicitUsings on, that target failed with CS0246.

Skip that using under the same condition that strips the Uno packages,
now computed once in _UnoIsWinAppSdkOnlyLibrary so the two cannot drift.
Both readers are item conditions, so the property is defined last, in
Uno.Sdk.After.targets: the property is still honored when set in a
Directory.Build.targets, as the strip's own item condition did. The other
Uno implicit usings stay: the Windows App SDK, the Windows SDK and the
BCL provide them on that target.
Evaluates the in-repo Uno.Sdk around a project body, the way SamplesApp
imports it, in a temporary directory with its own Directory.Build.props
and Directory.Build.targets, and checks the Using items: a Remove in the
body applies, ImplicitUsings and DisableImplicitUnoNamespaces apply from
the body and from a Directory.Build.targets, and a WinAppSDK-only
library gets neither Uno.WinUI nor the Uno.UI using on Windows, whether
it opts in from the body, from an inner build or from a
Directory.Build.targets, while its other targets and application heads
keep both. Also checks that leaving a feature out of UnoFeatures on
Windows leaves its packages out there.
Lists the Uno.Sdk implicit usings and how to remove one or all of them,
and notes that a WinAppSDK-only library gets no Uno.UI using on Windows,
and what to add when Uno.WinUI reaches that target anyway.

A static <PackageReference Remove="..." /> does not remove an implicit
package: the SDK adds them after the project body on purpose, so a
PackageReference the project declares replaces the implicit one instead
of duplicating it, and UnoImplicitPackages adds them again before
restore. Say so where the packages are added and in the docs, and point
to the supported routes: the DisableImplicitUno* properties and a
per-target UnoFeatures.
@mikernet
mikernet force-pushed the dev/mikernet/sdk-implicit-using-remove branch from 3501969 to df3ad24 Compare November 12, 2026 19:58
@unodevops

Copy link
Copy Markdown
Contributor

@MartinZikmund MartinZikmund left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@mikernet thanks, this is a really clean fix, and the write-up made it easy to review. The SDK side checks out: the Uno usings now sit right after the .NET SDK's Sdk.props, same as the .NET SDK's own, so a <Using Remove> in the body works. The item conditions read final property values, so DisableImplicitUnoNamespaces from a Directory.Build.targets now counts too. Moving _UnoIsWinAppSdkOnlyLibrary into Uno.Sdk.After.targets is the right spot. The new .props is covered by the targets/**/*.props glob in Uno.Sdk.csproj, so it gets packed. Default experience: same set of usings, same Sdk="Uno" metadata, and workarounds that removed usings inside a target keep working.

One thing to change before merge, in the test harness only:

  • Given_UnoSdkImplicitUsings brings its own MSBuild runner, but Uno.UI.Tasks.Tests/DotnetMSBuild.cs already exists for exactly this, and Given_UnoSdkLateBrowserWasmTargetFramework uses it with the same import-the-SDK-by-path pattern. The local copy is missing a few things that matter on CI (details inline). Could you switch to DotnetMSBuild.Run(RepositoryPaths.Root, ...), parse StandardOutput only, and use DotnetMSBuild.TryDeleteDirectory in the finally?

Nit, optional: the test file starts with two BOMs (EF BB BF EF BB BF). It compiles fine, but it's worth dropping the extra one.

Heads-up: the main CI pipeline hasn't run on this yet, so the new tests haven't been run on CI agents.

The test brought its own dotnet msbuild runner: no time limit, no
DOTNET_NOLOGO, and run from the temp folder, so global.json did not
pick the SDK. Parsing stdout with stderr appended could also break on
anything MSBuild writes to stderr. Use DotnetMSBuild from the repo
root, parse stdout only, and drop the doubled BOM.
@mikernet

Copy link
Copy Markdown
Contributor Author

Switched Given_UnoSdkImplicitUsings to the shared runner in 0781116: it now calls DotnetMSBuild.Run(RepositoryPaths.Root, ...), so the repo's global.json picks the SDK and the run gets the timeout and DOTNET_NOLOGO. It parses StandardOutput only, keeps Output as the exit-code assertion message, and cleans up with DotnetMSBuild.TryDeleteDirectory. The local runner is gone, and so is the second BOM.

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

Labels

area/code-generation Categorizes an issue or PR as relevant to code generation kind/documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

<Using Remove..../> does not work in Uno.Sdk projects

4 participants