Skip to content

Fix temporary WPF assembly identity evaluation - #11995

Open
amarinov-msft wants to merge 10 commits into
dotnet:mainfrom
amarinov-msft:amarinov-fix/5711-preserve-temp-assembly-identity
Open

amarinov-msft wants to merge 10 commits into
dotnet:mainfrom
amarinov-msft:amarinov-fix/5711-preserve-temp-assembly-identity

Conversation

@amarinov-msft

@amarinov-msft amarinov-msft commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Keep path properties and _TargetAssemblyProjectName available during props evaluation, then restore the evaluated AssemblyName and RootNamespace before SDK targets derive output identity.

Fixes #5711

Description

WPF markup compilation creates a temporary project when XAML references types from the same assembly. Although the evaluated AssemblyName was copied into that project, it was placed before Sdk.props. An AssemblyName defined in Directory.Build.props or a <Choose> block could therefore be evaluated again using the temporary project name, causing markup compilation to fail with MC3074.

This change keeps path properties and _TargetAssemblyProjectName available before props evaluation, while adding a separate late property group for the evaluated AssemblyName and RootNamespace. The identity group is inserted immediately before the generated Sdk.targets imports, ensuring TargetName and related properties are derived from the original project identity.

The insertion logic supports multiple SDKs and projects without an implicit Sdk attribute, without using a global property that could flow into referenced projects.

Customer Impact

Affected WPF projects can build successfully without hard-coding AssemblyName or adding temporary-project-specific workarounds. The change applies only during temporary markup compilation and does not alter the final assembly's name or behavior of unaffected projects.

Regression

Yes

Risk

Low. The change is limited to property placement in the temporary project used for WPF markup compilation. It preserves the existing early evaluation of path properties and _TargetAssemblyProjectName, while restoring the already evaluated AssemblyName and RootNamespace before SDK targets derive output identity.

Projects whose temporary assembly identity was already correct retain the same evaluated values. The change does not modify the final project’s assembly identity or pass AssemblyName as a global property to referenced projects.

Microsoft Reviewers: Open in CodeFlow

Keep path properties and _TargetAssemblyProjectName available during props evaluation, then restore the evaluated AssemblyName and RootNamespace before SDK targets derive output identity.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: b9298a94-6480-4b7c-b75d-4be15d13253f
@dotnet-policy-service dotnet-policy-service Bot added PR metadata: Label to tag PRs, to facilitate with triage draft labels Oct 5, 2026
Keep AssemblyName and RootNamespace available while the temporary project evaluates SDK props, then retain the late identity override. Use the first targets import as the fallback insertion anchor.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: b9298a94-6480-4b7c-b75d-4be15d13253f
Use the first .targets import as the fallback insertion anchor without a redundant Sdk.targets special case.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: b9298a94-6480-4b7c-b75d-4be15d13253f
@amarinov-msft
amarinov-msft marked this pull request as ready for review October 5, 2026 10:41
@amarinov-msft
amarinov-msft requested review from a team and a balanced review from Copilot October 5, 2026 10:41
@amarinov-msft
amarinov-msft requested a review from a team as a code owner October 5, 2026 10:41

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.

Copilot review overview

🟡 Changes recommended

Explicit SDK imports can still cause identity restoration to occur too early or too late.

Review effort: Balanced
Findings: 2 High severity

Open (2)
What changed in this PR

Addresses #5711 by restoring the original assembly identity during WPF temporary-project evaluation, before SDK targets derive output names.

Changes:

  • Preserves properties needed before SDK props evaluation.
  • Adds a late property group for AssemblyName and RootNamespace.
  • Adds import lookup to position identity restoration.
File Description
src/​Microsoft.DotNet.Wpf/​src/​PresentationBuildTasks/​Microsoft/​Build/​Tasks/​Windows/​GenerateTemporaryTargetAssembly.cs Separates early properties from late identity restoration and locates targets imports.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

amarinov-msft and others added 2 commits October 5, 2026 13:54
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Prefer explicit SDK target imports when positioning the temporary identity properties. Split import groups at the SDK target import so the root-level identity group preserves import order and conditions.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: b9298a94-6480-4b7c-b75d-4be15d13253f
Snapshot an ImportGroup condition before splitting it so imports before Sdk.targets cannot change whether the second half executes.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: b9298a94-6480-4b7c-b75d-4be15d13253f
subhajitm
subhajitm previously approved these changes Nov 10, 2026

@subhajitm subhajitm 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.

looks safe.

continue;
}

if (string.Equals(element.Name, "Import", StringComparison.OrdinalIgnoreCase))

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.

element.Name is a qualified name, if I understand correctly this would not work for project files with explicit namespace

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In practice Name and LocalName cannot differ here: MSBuild rejects prefixed elements, and this task only runs on projects MSBuild has already loaded. I switched the new checks to LocalName anyway for clarity. RemoveEntityByName uses the same .Name pattern and could get the same change in a separate PR.
Addressed in 53a06a8

string project = import.GetAttribute("Project");
return string.Equals(project, "Sdk.targets", StringComparison.OrdinalIgnoreCase) ||
project.EndsWith(@"\Sdk.targets", StringComparison.OrdinalIgnoreCase) ||
project.EndsWith("/Sdk.targets", StringComparison.OrdinalIgnoreCase);

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.

Slightly nervous about making a filename part of the contract, the build system does not really depend on filenames. Why is just checking the Sdk attribute not enough?

@amarinov-msft amarinov-msft Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Checking only the Sdk attribute would also match , which comes first. The properties would then go before Sdk.props, which is the scenario this PR is trying to fix. The file name is what tells the two apart, and it isn't ours: MSBuild hardcodes Sdk.props/Sdk.targets for implicit SDK imports (ProjectRootElement), and ReplaceImplicitImports already writes them. For the usual case this lookup isn't used. We insert before the node ReplaceImplicitImports creates. I've tightened it to an exact Sdk.targets match and added a comment.
Commit 091da63

string condition = importGroup.GetAttribute("Condition");
if (!string.IsNullOrEmpty(condition))
{
string conditionPropertyName = "_WpfTempProjectImportGroupCondition_" + Guid.NewGuid().ToString("N");

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.

I think this whole block deserves some comment or example of what's going on and what it is trying to achieve

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added an explanation comment with 3395233 with a before-and-after example explaining the split and why the condition is saved first. I also dropped the GUID suffix, since only one group is ever split and the generated project is the same on every build.

@miloush

miloush commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

You mentioned this is a regression. Do we know what caused it?

@ThomasGoulet73 mentioned in the past he had a working fix, want to review this one?

@lindexi

lindexi commented Nov 20, 2026

Copy link
Copy Markdown
Member

@miloush Btw, I think the source generators are the only real way to solve this properly.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 43891a2e-162f-488b-ab67-0b34ea7aecaf
@amarinov-msft

Copy link
Copy Markdown
Contributor Author

You mentioned this is a regression. Do we know what caused it?

@ThomasGoulet73 mentioned in the past he had a working fix, want to review this one?

Yes. #3846 (5.0.2) added the source-generator-aware temp project path. Unlike the legacy path, which passes AssemblyName as a global property, it prepends AssemblyName before Sdk.props, so any later definition re-evaluates it against the _wpftmp project name. #4089 made that path the default in .NET 6, which is when #5711 was reported. #7557 covered the case where the project file itself sets AssemblyName, but not Directory.Build.props or <Choose>. A global property isn't an option on the new path, because it would flow into referenced projects through ResolveProjectReferences.

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

Labels

Included in test pass PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression for custom AssemblyName in WPF apps

5 participants