Skip to content

Refactor command line parser to do early parsing - #11482

Merged
Dongbo Wang (daxian-dbw) merged 44 commits into
PowerShell:masterfrom
iSazonov:cpp-early
Aug 12, 2020
Merged

Refactor command line parser to do early parsing#11482
Dongbo Wang (daxian-dbw) merged 44 commits into
PowerShell:masterfrom
iSazonov:cpp-early

Conversation

@iSazonov

@iSazonov Ilya (iSazonov) commented Jan 3, 2020

Copy link
Copy Markdown
Collaborator

PR Summary

Previously we had to make workarounds in #6025 and #7631 to enable logging as early as possible.
A root of the problem is that command line parser has a dependency on ConsoleHost being initialized late.

Main change in the PR is to refactor CommandLineParameterParser and remove the dependency on ConsoleHost.
Now we create CommandLineParameterParser at startup as static and can run the parser immediately after pwsh launch. Later, after ConsoleHost is initialized, we write the parser's errors, banner and helps.

Now we call PSEtwLog.LogConsoleStartup() early in one place for both Unix and Windows.

While refactoring and testing the parser I discovered NRE in an edge case: pwsh -removeworkingdirectorytrailingcharacter throws NRE. As result I added #nullable enable in some files to make the code more reliable.

UnmanagedPSEntry.Start() uses still old signature. It was refactored too: new method create with removed unneeded parameters and old one was marked with Obsolete attribute.

CommandLineParameterParser.WriteCommandLineError() method was renamed to SetCommandLineError().

In CommandLineParameterParser.ParseHelper() was added fast return if pwsh started with no parameters (args.Length == 0).

Unused parameters was removed from the parser - iss, isswait and modules.

Waiting a debugger in remoting scenario was moved in WaitingRemoteDebugger() method that called later after ConsoleHost initialized.

CommandLineParameterParser.EarlyParse() was removed - now main parser is called in the place.

NullHostUserInterface was not fully refactored for nullable reference types because PSHostUserInterface base class must be refactored first. We can defer the work.

After the change that we can finally add xUnix tests in follow PR.

PR Context

PR Checklist

@iSazonov Ilya (iSazonov) added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Jan 3, 2020
@iSazonov Ilya (iSazonov) added this to the 7.1.0-preview.1 milestone Jan 3, 2020
@iSazonov Ilya (iSazonov) self-assigned this Jan 3, 2020
@iSazonov
Ilya (iSazonov) force-pushed the cpp-early branch 2 times, most recently from bd2928e to bd527ba Compare January 3, 2020 19:03
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs Outdated
@iSazonov
Ilya (iSazonov) marked this pull request as ready for review January 3, 2020 20:32
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs Outdated
@iSazonov

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) Rob Holt (@rjmholt) Dongbo Wang (@daxian-dbw) It seems MSFT team has plans for the area. I believe the PR should be merged and then xUnit tests should be created.

@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Mainainer, Please provide feedback and/or mark it as Waiting on Author

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) Rob Holt (@rjmholt) Dongbo Wang (@daxian-dbw) Please review.

@ghost ghost removed the Review - Needed The PR is being reviewed label Jun 2, 2020
@iSazonov

Ilya (iSazonov) commented Aug 9, 2020

Copy link
Copy Markdown
Collaborator Author

Dongbo Wang (@daxian-dbw) I added some commits and don't see more issues I can fix.

@daxian-dbw

Dongbo Wang (daxian-dbw) commented Aug 10, 2020

Copy link
Copy Markdown
Member

Ilya (@iSazonov) Why mixing style/formatting changes with functionality changes? This makes it harder to review and thus the risk of regression is higher, especially for a large refactoring PR like this one ... Can you please revert the style/formatting changes?

It looks some of confusing changes were from other merged/reverted PRs, so never mind reverting the style/formatting changes. But please avoid mixing functionality changes with unrelated style changes in future PRs.

Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs
@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Aug 10, 2020
@daxian-dbw

Copy link
Copy Markdown
Member

Ilya (@iSazonov) I depend on a tool called CodeFlow to review the changes in this PR and it won't work if there are more than 63 commits in the PR. So please don't use too many commits when addressing my comments. I would prefer 1 commit to address all my comments if possible. Also, please don't make changes to the existing commits and force push, or I will lose track of my review state.

@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Aug 11, 2020
@iSazonov

Copy link
Copy Markdown
Collaborator Author

Why mixing style/formatting changes with functionality changes?

Sorry, I misunderstand your previous request. :-(

Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs Outdated
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs Outdated
@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Aug 11, 2020
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs Outdated
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Aug 11, 2020

@daxian-dbw Dongbo Wang (daxian-dbw) 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.

LGTM. Oh man, it's not easy for both you and me 😄

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Oh man, it's not easy for both you and me

Sorry, again. As I said the original plan was only to get rid of the workarounds with "early parsing"...
On the other hand, it is good that this work is done - it will save us a lot of time in the future as PowerShell becomes more popular and requires more launching features (like pwshw, login shell and so on).

Additional notices:

  • I see I skipped some code paths in xUnit tests (but Pester tests exist). But it is minor and new tests can be added later.
  • If you think we can/should investigate performance of the parser please ask me or open new tracking issue.

@daxian-dbw

Copy link
Copy Markdown
Member

Ilya (@iSazonov) Don't get me wrong, I appreciate you taking on refactoring work like this one, which usually would require a lot patience to get through the long review process. To be honest, I admit that I'm kinda afraid of PRs like this one -- it obviously has values but also has a high risk of regression -- which would take me a lot efforts to review to get to the point where I'm sort of confident to accept the PR. That's why I said "it's not easy for both you and me" 😄

I see I skipped some code paths in xUnit tests (but Pester tests exist). But it is minor and new tests can be added later.

Agreed.

If you think we can/should investigate performance of the parser please ask me or open new tracking issue.

No, I don't think the argument parsing is the bottleneck, so don't waste your time there.

@ghost

Copy link
Copy Markdown

🎉v7.1.0-preview.6 has been released which incorporates this pull request.:tada:

Handy links:

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants