Skip to content

Disambiguate Roslyn task cache by compilation mode - #15214

Merged
JanProvaznik merged 3 commits into
mainfrom
copilot/fix-roslyncodetaskfactory-cache-issue
Oct 7, 2026
Merged

JanProvaznik merged 3 commits into
mainfrom
copilot/fix-roslyncodetaskfactory-cache-issue

Conversation

Copilot AI commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #15213

Summary

  • Include the out-of-process compilation requirement in the Roslyn task cache key by overriding Equals and GetHashCode.
  • Keep in-process and multithreaded compilations from reusing incompatible cache entries.
  • Fix case sensitivity of hashing in task names

Customer Impact

  • Prevents MSB0001 when a reused MSBuild process switches from a non-/mt build to /mt.

Testing

  • Added a focused cache-mode regression test.

Risk

  • Low; reuse remains unchanged within each compilation mode.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hello @copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo.

Copilot AI changed the title [WIP] Fix RoslynCodeTaskFactory cache issue causing MSB0001 Disambiguate Roslyn task cache by compilation mode Oct 6, 2026
Copilot AI requested a review from JanProvaznik October 6, 2026 15:19
@JanProvaznik
JanProvaznik requested a review from AR-May October 6, 2026 15:33
@JanProvaznik
JanProvaznik marked this pull request as ready for review October 6, 2026 15:33
Copilot AI balanced review requested due to automatic review settings October 6, 2026 15:33

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

🟢 Approval recommended

The focused change correctly isolates incompatible cache entries and includes regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Disambiguates Roslyn inline-task cache entries by compilation mode, preventing /mt builds from reusing incompatible in-process assemblies.

Changes:

  • Adds compilation mode to cache equality and hashing.
  • Initializes the cache key with the required execution mode.
  • Adds regression coverage for in-process followed by multithreaded initialization.
File Description
RoslynCodeTaskFactoryTaskInfo.cs Adds compilation mode to the cache key.
RoslynCodeTaskFactory.cs Populates the new cache-key field.
RoslynCodeTaskFactory_Tests.cs Tests cache isolation across compilation modes.

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

@github-actions github-actions Bot 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.

ChangeWave Discipline — LGTM

Warning

Firewall blocked 4 domains

The following domains were blocked by the firewall during workflow execution:

  • api.github.com
  • cafe.github.com
  • github.com
  • raw.githubusercontent.com

[!TIP]
api.github.com is blocked because GitHub API access uses the built-in GitHub tools by default. Instead of adding api.github.com to network.allowed, use tools.github.mode: gh-proxy for direct pre-authenticated GitHub CLI access without requiring network access to api.github.com:

tools:
  github:
    mode: gh-proxy

See GitHub Tools for more information on gh-proxy mode.

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "api.github.com"
    - "cafe.github.com"
    - "github.com"
    - "raw.githubusercontent.com"

See Network Configuration for more information.

Generated by Expert Code Review (on open) for #15214 · copilot · gpt56 · 933.6 AIC · ⌖ 5.81 AIC · ⊞ 24.9K

@JanProvaznik

Copy link
Copy Markdown
Member

@copilot

// Include both Name and SourceCode to avoid cache collisions between different task names
return HashCode.Combine(Name?.GetHashCode() ?? 0, SourceCode?.GetHashCode() ?? 0);
return HashCode.Combine(Name?.GetHashCode() ?? 0, SourceCode?.GetHashCode() ?? 0, CompileForOutOfProcess);
@github-actions
github-actions Bot
17 hours ago
Contributor
String Comparison Correctness — MAJOR: This hash still violates Equals: Name and SourceCode are compared with OrdinalIgnoreCase, but hashed case-sensitively. Concrete sequential trigger: initialize CaseTask with public class CaseTask : Microsoft.Build.Utilities.Task { public override bool Execute() => true; }, then initialize casetask with the identical body and the same IsMultiThreadedBuild value. The resulting keys compare equal (task-name case is ignored, source/references/bool match), but case-sensitive name hashes differ, so CompiledAssemblyCache.TryGetValue searches another bucket and recompiles instead of returning the cached assembly. No concurrent interleaving is required; concurrent callers access the same ConcurrentDictionary, whose synchronization cannot repair inconsistent key hashing. Hash both strings with StringComparer.OrdinalIgnoreCase.GetHashCode(...) so hashing matches equality.

Copilot AI commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@copilot

// Include both Name and SourceCode to avoid cache collisions between different task names
...

Updated the hash to use StringComparer.OrdinalIgnoreCase for Name and SourceCode, and added a dictionary lookup regression test for case-only differences. Commit: HEAD.

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.

RoslynCodeTaskFactory cache reused across non-/mt and /mt builds causes MSB0001

4 participants