Skip to content

Disallow duplicate directives across #:included files too#54101

Open
jjonescz wants to merge 3 commits intodotnet:release/10.0.4xxfrom
jjonescz:sprint-include-duplicate-directives
Open

Disallow duplicate directives across #:included files too#54101
jjonescz wants to merge 3 commits intodotnet:release/10.0.4xxfrom
jjonescz:sprint-include-duplicate-directives

Conversation

@jjonescz
Copy link
Copy Markdown
Member

Related to #54090.

jjonescz and others added 2 commits April 27, 2026 19:09
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
@jjonescz jjonescz added the Area-run-file Items related to the "dotnet run <file>" effort label Apr 27, 2026
@jjonescz jjonescz marked this pull request as ready for review April 28, 2026 09:34
@jjonescz jjonescz requested review from a team and Copilot April 28, 2026 09:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Extends file-based app directive validation so duplicate directives are detected across #:include boundaries, providing deterministic failure behavior in multi-file compositions (per #54090).

Changes:

  • Adds cross-file duplicate directive detection during virtual project construction (evaluated directives across all included files).
  • Updates directive parsing helpers to optionally skip per-file duplicate checks, enabling centralized cross-file deduplication.
  • Adds/updates tests to cover duplicate directives across included files for both dotnet run and dotnet project convert.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/dotnet.Tests/CommandTests/Run/RunFileTests_Directives.cs New test asserting duplicate directives across included files fail dotnet run with the expected diagnostic.
test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs New conversion test for duplicates across included files; updates diagnostic assertions to include file path.
src/Microsoft.DotNet.ProjectTools/VirtualProjectBuilder.cs Centralizes duplicate detection across all evaluated directives in the include closure.
src/Cli/Microsoft.DotNet.FileBasedPrograms/InternalAPI.Unshipped.txt Updates internal API surface for new optional parameters and the new deduplicator type.
src/Cli/Microsoft.DotNet.FileBasedPrograms/FileLevelDirectiveHelpers.cs Adds checkDuplicates parameter and introduces DirectiveDeduplicator utility used by both parsing and project evaluation.

Comment thread test/dotnet.Tests/CommandTests/Run/RunFileTests_Directives.cs Outdated
Co-authored-by: Copilot <copilot@github.com>

_seen ??= new(NamedDirectiveComparer.Instance);

if (_seen.TryGetValue(directive, out var existingDirective))
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.

Should we allow (or warn-and-ignore) identical values? Asking because in C/C++, you can have same #define twice with same value but it errors on different values:

$ printf '%s\n%s' '#define Foo 1' '#define Foo 1' | gcc -xc -Werror - -shared
$ printf '%s\n%s' '#define Foo 1' '#define Foo 1' | clang -xc -Werror - -shared

vs.

$ printf '%s\n%s' '#define Foo 1' '#define Foo 2' | gcc -xc -Werror - -shared
<stdin>:2: error: "Foo" redefined [-Werror]
<stdin>:1: note: this is the location of 
cc1: all warnings being treated as errors

$ printf '%s\n%s' '#define Foo 1' '#define Foo 2' | clang -xc -Werror - -shared
<stdin>:2:9: error: 'Foo' macro redefined [-Werror,-Wmacro-redefined]
    2 | #define Foo 2
      |         ^
<stdin>:1:9: note: previous definition is here
    1 | #define Foo 1
      |         ^
1 error generated.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I think we could do that. This PR doesn't try to change the logic though, it just makes it apply to some unintentionally missed cases. The error for duplicate directives is actually meant to be temporary - until we design how to deduplicate the directives properly. And we can probably relax the restriction incrementally; i.e., I can follow up with your suggestion (allowing duplicate directives if they have the same value too) after this PR.

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

Labels

Area-run-file Items related to the "dotnet run <file>" effort

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants