Skip to content

[CLI] Reference output of projects that fail to load (e.g. F#) - #1833

Merged
josefpihrt merged 2 commits into
dotnet:mainfrom
helgeu:fix/analyze-metadata-for-unsupported-refs
Sep 2, 2026
Merged

[CLI] Reference output of projects that fail to load (e.g. F#)#1833
josefpihrt merged 2 commits into
dotnet:mainfrom
helgeu:fix/analyze-metadata-for-unsupported-refs

Conversation

@helgeu

@helgeu helgeu commented Sep 2, 2026

Copy link
Copy Markdown
Contributor
  • I've read the contributing guide
  • Bug fixes / features have an issue, or new analyzers / refactorings / fixes were discussed first
  • Unit tests added for bug fixes / features (see note below)
  • Changelog updated
  • Build passes locally for net8.0, net9.0 and net10.0

Summary

When a C#/VB project references a project in a language MSBuildWorkspace cannot load — most commonly F# — the reference is dropped, and every type defined in that project is reported as missing during analyze / fix:

CS0246: The type or namespace name '...' could not be found
CS0103: The name '...' does not exist in the current context

These are false positives; the solution compiles fine with dotnet build.

Cause

MSBuildWorkspace does not support F#, so the referenced project is not added to the workspace and its project-to-project reference is lost, leaving the referencing project unable to see its types.

Fix

Set MSBuildWorkspace.LoadMetadataForReferencedProjects = true. When a referenced project cannot be loaded as a source project, the workspace then falls back to its compiled output as a metadata reference, so the types resolve. Projects that can be loaded are still loaded as source (project-to-project), so there is no change for ordinary C#/VB-only solutions. The referenced project must have been built.

Verification

  • Repro: a net10.0 solution with a C# project referencing an F# .fsproj. Before: roslynator analyze reports CS0103/CS0246 for the F# types. After (with the F# project built): those are gone.
  • A real 9-project net10.0 solution: 112 false diagnostics (44× CS0103 'StatusRules' plus CS0246 for other F# rule modules) drop to 0; total goes 1671 → 1558 real diagnostics.
  • Builds clean for net8.0, net9.0 and net10.0.

Note on tests

This is MSBuildWorkspace load behaviour with no natural home in the existing test projects, and there is no CLI test project (the analogous CLI infrastructure fix #1783 shipped without one). Verified end to end as above; happy to add a test if you would like one.

Related issue

Fixes #1832


Investigated and implemented with AI assistance (opencode).

MSBuildWorkspace cannot load F# (and other unsupported-language) projects, so a C#/VB project that references one loses access to its types and reports them as missing (CS0103/CS0246) during analyze/fix. Set MSBuildWorkspace.LoadMetadataForReferencedProjects so the referenced project's compiled output is used as a metadata reference when the project itself cannot be loaded.
@josefpihrt
josefpihrt merged commit b5bd067 into dotnet:main Sep 2, 2026
17 checks passed
@josefpihrt

Copy link
Copy Markdown
Collaborator

@helgeu Thanks for the contribution!

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.

[CLI] False CS0103/CS0246 for types defined in referenced F# (unsupported-language) projects

2 participants