Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement managed SegmentCommandLine #82883

Merged
merged 5 commits into from
Mar 3, 2023

Conversation

huoyaoyuan
Copy link
Member

Fixes #77644
Ported from the code deleted by #71111

How should we test it? Can we mark the method internal and test in some unit test?

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Mar 2, 2023
@ghost
Copy link

ghost commented Mar 2, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #77644
Ported from the code deleted by #71111

How should we test it? Can we mark the method internal and test in some unit test?

Author: huoyaoyuan
Assignees: -
Labels:

area-System.Runtime

Milestone: -

Comment on lines 225 to 240
//---------------------------------------------------------------------
// Splits a command line into argc/argv lists, using the VC7 parsing rules.
//
// This functions interface mimics the CommandLineToArgvW api.
//
//---------------------------------------------------------------------
// NOTE: Implementation-wise, once every few years it would be a good idea to
// compare this code with the C runtime library's parse_cmdline method,
// which is in vctools\crt\crtw32\startup\stdargv.c. (Note we don't
// support wild cards, and we use Unicode characters exclusively.)
// We are up to date as of ~6/2005.
//---------------------------------------------------------------------

// The C# version is ported from C++ version in coreclr
// The behavior mimics what MSVCRT does before main,
// which is slightly different with CommandLineToArgvW
Copy link
Member

Choose a reason for hiding this comment

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

I don't think these comments are useful as written, especially since we're adding this specifically to not be in sync with MSVC's CommandLineToArgvW API.

Comment on lines 225 to 240
//---------------------------------------------------------------------
// Splits a command line into argc/argv lists, using the VC7 parsing rules.
//
// This functions interface mimics the CommandLineToArgvW api.
//
//---------------------------------------------------------------------
// NOTE: Implementation-wise, once every few years it would be a good idea to
// compare this code with the C runtime library's parse_cmdline method,
// which is in vctools\crt\crtw32\startup\stdargv.c. (Note we don't
// support wild cards, and we use Unicode characters exclusively.)
// We are up to date as of ~6/2005.
//---------------------------------------------------------------------

// The C# version is ported from C++ version in coreclr
// The behavior mimics what MSVCRT does before main,
// which is slightly different with CommandLineToArgvW
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
//---------------------------------------------------------------------
// Splits a command line into argc/argv lists, using the VC7 parsing rules.
//
// This functions interface mimics the CommandLineToArgvW api.
//
//---------------------------------------------------------------------
// NOTE: Implementation-wise, once every few years it would be a good idea to
// compare this code with the C runtime library's parse_cmdline method,
// which is in vctools\crt\crtw32\startup\stdargv.c. (Note we don't
// support wild cards, and we use Unicode characters exclusively.)
// We are up to date as of ~6/2005.
//---------------------------------------------------------------------
// The C# version is ported from C++ version in coreclr
// The behavior mimics what MSVCRT does before main,
// which is slightly different with CommandLineToArgvW
// Parse command line arguments using the rules documented at
// https://learn.microsoft.com/cpp/cpp/main-function-command-line-args#parsing-c-command-line-arguments
//
// CommandLineToArgvW API cannot be used here since
// it has slightly different behavior.

Simplify this comment

@jkotas
Copy link
Member

jkotas commented Mar 3, 2023

How should we test it? Can we mark the method internal and test in some unit test?

You can access the method via private reflection (call GetMethod with BindingFlags.NonPublic). We have number of tests like that.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks!

@jkotas
Copy link
Member

jkotas commented Mar 3, 2023

Known failure according to the Build Analysis

@jkotas jkotas merged commit 732ae12 into dotnet:main Mar 3, 2023
radical pushed a commit to radical/runtime that referenced this pull request Mar 4, 2023
* Implement managed version of SegmentCommandLine

* Remove P/Invoke for CommandLineToArgv

* Update parsing to latest UCRT

* Add test and fix trailing space in command

* Fix test cases
var method = typeof(Environment).GetMethod("SegmentCommandLine", BindingFlags.Static | BindingFlags.NonPublic);
Assert.NotNull(method);

var span = cmdLine.AsSpan(); // Workaround
Copy link
Member Author

Choose a reason for hiding this comment

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

Here is workaround for xUnit issue xunit/xunit#1969

@huoyaoyuan huoyaoyuan deleted the segment-command-line branch March 4, 2023 03:17
@ghost ghost locked as resolved and limited conversation to collaborators Apr 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CommandLineToArgvW has different behavior than SegmentCommandLine
3 participants