Skip to content

Report CS8802 on every file with top-level statements - #83916

Merged
333fred merged 7 commits into
dotnet:mainfrom
64bitsDuck:fix/cs8802-show-all-toplevel-files-main
Aug 7, 2026
Merged

Report CS8802 on every file with top-level statements#83916
333fred merged 7 commits into
dotnet:mainfrom
64bitsDuck:fix/cs8802-show-all-toplevel-files-main

Conversation

@64bitsDuck

@64bitsDuck 64bitsDuck commented May 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #83691

Summary

When multiple compilation units have top-level statements, the CS8802 error
message previously only reported the offending file without indicating which
file was already accepted as the entry point. This made it harder to locate
the conflict, especially in larger projects.

This change reports the diagnostic on every compilation unit that has top-level
statements, so clicking each entry in the error list takes you straight to a file
you have to edit.

Changes

  • SourceMemberContainerSymbol.cs: emit CS8802 for every declaration with
    top-level statements when more than one exists, instead of skipping the first
  • TopLevelStatementsTests.cs: existing tests now expect the additional
    diagnostic on the first file
  • RemoveUnusedMembersTests.cs: same, for the two analyzer tests that compile
    multiple files with top-level statements
  • Simplified an operation tree assertion in Simple_06_02 that was conditional
    on which tree was reported; both are now always invalid
  • Added MultipleTopLevelStatements_ReportedInEveryFile and
    MultipleTopLevelStatements_SourceGeneratedFile
Microsoft Reviewers: Open in CodeFlow

@64bitsDuck
64bitsDuck requested a review from a team as a code owner May 27, 2026 22:04
@dotnet-policy-service dotnet-policy-service Bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label May 27, 2026
@64bitsDuck

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@333fred 333fred 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.

Overall this looks like an acceptable approach, but I do want to understand what the UX looks like for source generated files.

Comment thread src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs Outdated
Comment thread src/Compilers/CSharp/Test/Semantic/Semantics/TopLevelStatementsTests.cs Outdated
Comment thread src/Compilers/CSharp/Test/Semantic/Semantics/TopLevelStatementsTests.cs Outdated

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.

Think we should probably test what this looks like with source generated entry point files.

@64bitsDuck 64bitsDuck May 29, 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.

Good point, from what I understand source generated files are susceptible to having a null filepath, should we fall back to a generic label like generated, or omit the path entirely?

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.

Sorry, missed that you asked a question and it fell off my radar on vacation. I wouldn't do anything special, I just wanted to see what it looked like and document in a test.

@RikkiGibson

Copy link
Copy Markdown
Member

Perhaps when multiple files have top-level statements, then we should report a diagnostic in all such files, instead of reporting for "all the files after the first one". That will make it so clicking each diagnostic in the error list will just take you to the right place, rather than having to look at the filename in the message and navigate to it, in order to decide which top-level statements you intended to keep.

@64bitsDuck

Copy link
Copy Markdown
Contributor Author

@RikkiGibson please excuse the late response, some comments escaped my attention.
I'd like to be sure of what to modify before I make any change:
The idea would be to also emit CS8802 on the first file (the one accepted as entry point), so that all conflicting files show up in the error list? If we do this, should we keep the file name in the message or is navigating via the error list enough?
Thanks

@RikkiGibson

Copy link
Copy Markdown
Member

should we keep the file name in the message or is navigating via the error list enough?

No, if you report an error on every file which had top-level statements, then, I would just restore the original message which lacks the file name.

@64bitsDuck

Copy link
Copy Markdown
Contributor Author

I would just restore the original message which lacks the file name.

That makes sense, so we'd emit CS8802 on every file including the first one right? It does seem like a more user friendly approach. Once that's confirmed I'll gladly implement this version of the fix

@RikkiGibson

Copy link
Copy Markdown
Member

That makes sense, so we'd emit CS8802 on every file including the first one right? It does seem like a more user friendly approach. Once that's confirmed I'll gladly implement this version of the fix

Yes, please do.

@64bitsDuck
64bitsDuck requested a review from a team as a code owner August 5, 2026 20:38
@64bitsDuck 64bitsDuck changed the title Improve CS8802 error message to include the conflicting file name Report CS8802 on every file with top-level statements Aug 5, 2026
@333fred
333fred merged commit 8cd4945 into dotnet:main Aug 7, 2026
28 checks passed
@333fred

333fred commented Aug 7, 2026

Copy link
Copy Markdown
Member

Thanks @64bitsDuck! Apologies this took so long to review.

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

Labels

Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee. VSCode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compilation error for multiple top-level statements

4 participants