Improve RootCommand executable name handling (fix #2812)#2820
Draft
jonsequitur wants to merge 2 commits into
Draft
Improve RootCommand executable name handling (fix #2812)#2820jonsequitur wants to merge 2 commits into
jonsequitur wants to merge 2 commits into
Conversation
baronfel
reviewed
Jun 17, 2026
| <_SystemCommandLineGeneratedFile>$(IntermediateOutputPath)System.CommandLine.ExecutableName.g.cs</_SystemCommandLineGeneratedFile> | ||
| </PropertyGroup> | ||
|
|
||
| <Target Name="_GenerateSystemCommandLineExecutableName" |
Member
There was a problem hiding this comment.
this needs to be conditioned on Language or else it'd break C#, VB, etc consumers.
baronfel
requested changes
Jun 17, 2026
baronfel
left a comment
Member
There was a problem hiding this comment.
Logic looks fine but the MSBuild targets need some work to not break incrementality
|
|
||
| <Target Name="_GenerateSystemCommandLineExecutableName" | ||
| BeforeTargets="CoreCompile" | ||
| Inputs="$(MSBuildAllProjects)" |
Member
There was a problem hiding this comment.
IIRC MSBuildAllProjects as an input is an antipattern - if you care about properties as inputs, the pattern today you should use is
- have a target before your main target that writes the properties you care about into a file in the IntermeidateOutputPath with
WriteOnlyWhenDifferent='true' - have that file be the input to your main target
- make sure to add any files you generate to the
FileWritesItem type in MSbuild so thatdotnet cleanclears them out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces several improvements and bug fixes to how
RootCommanddetermines the executable name and path, especially in advanced hosting scenarios like NativeAOT native libraries. It adds comprehensive tests to ensure correct behavior, addresses edge cases with command-line parsing, and improves packaging for build targets.Executable Name/Path Resolution Improvements:
RootCommand.ExecutableNameandRootCommand.ExecutablePathto more robustly determine the executable name and path, including fallback to anAppContextvalue injected by build targets when no command line arguments are available (e.g., in NativeAOT shared library scenarios) (src/System.CommandLine/RootCommand.cs).System.CommandLine.targetsfile to the NuGet package to ensure the executable-name module initializer is available for all consumers, including test projects and native libraries (src/System.CommandLine/System.CommandLine.csproj).NativeAOT and Advanced Hosting Test Coverage:
NativeLibrary) that builds as a NativeAOT shared library, exporting a native symbol to validate thatRootCommand.Namefalls back to the correct value whenEnvironment.GetCommandLineArgs()is empty (src/System.CommandLine.Tests/TestApps/NativeLibrary/NativeLibrary.csproj,src/System.CommandLine.Tests/TestApps/NativeLibrary/Program.cs). [1] [2]CompilationTests.csto verify the fallback logic forRootCommand.Namein native library hosting scenarios, including platform-specific handling and dynamic invocation of the exported native symbol (src/System.CommandLine.Tests/CompilationTests.cs).Command-Line Parsing Bug Fixes and Tests:
/p:Key=something/myapp) could incorrectly be treated as root command names, and added targeted regression tests for these scenarios (src/System.CommandLine/Parsing/StringExtensions.cs,src/System.CommandLine.Tests/ParserTests.RootCommandAndArg0.cs). [1] [2]src/System.CommandLine.Tests/ParserTests.RootCommandAndArg0.cs). [1] [2]General Test Improvements:
RootCommand.ExecutablePathandRootCommand.ExecutableNameare never null or empty (src/System.CommandLine.Tests/RootCommandTests.cs).Build and Target Framework Updates:
System.CommandLine.v3.ncrunchsolution).These changes collectively improve the reliability and correctness of executable name detection across diverse hosting models, strengthen the test suite, and fix subtle parsing bugs.