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

Make error message consistent when invalid script is passed to -File, better error when passed ambiguous arg #4573

Merged
merged 3 commits into from
Aug 17, 2017

Conversation

SteveL-MSFT
Copy link
Member

@SteveL-MSFT SteveL-MSFT commented Aug 14, 2017

Since -File is now the positional parameter for powershell.exe, made the error message consistent with
-Command when passed an invalid file.

If ambiguous arg is passed, give a better error message:

PS C:\users\poker\repos\PowerShell> powershell -no -file foo
Invalid argument '-no', did you mean:

        -nologo
        -noexit
        -noprofile
        -noninteractive
PowerShell v6.0.0-beta.5-42-g3ed9a816b2452551aaad1ac1245a1cbb35e87612-dirty
Copyright (C) Microsoft Corporation. All rights reserved.

Enable -WindowStyle to work

Fix #4351

@@ -252,7 +252,7 @@ EXAMPLES
<value>Processing -File '{0}' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.</value>
</data>
<data name="ArgumentFileDoesNotExist" xml:space="preserve">
<value>The argument '{0}' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.</value>
<value>The term '{0}' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.</value>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe argument is better term and we can be shorter:

The argument '{0}' is not recognized as the name of a script file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will change

StringBuilder possibleParameters = new StringBuilder();
foreach (string validParameter in validParameters)
{
if (validParameter.Contains(param))
Copy link
Member

Choose a reason for hiding this comment

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

Should this be validParameter.StartsWith(param, StringComparer.OrdinalIgnoreCase)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking to cover cases like: -format

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add a test case for this

{
if (validParameter.Contains(param))
{
possibleParameters.Append("\n\t-");
Copy link
Member

@lzybkr lzybkr Aug 15, 2017

Choose a reason for hiding this comment

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

I'd indent with spaces - we don't indent with tabs anywhere else that I know of.

Copy link
Member Author

Choose a reason for hiding this comment

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

will change

WriteCommandLineError(
string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidArgument, args[i]),
showBanner: false);
WriteCommandLineError(possibleParameters.ToString(), showBanner: true);
Copy link
Member

Choose a reason for hiding this comment

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

Showing the banner after the error is weird and not like the other errors I reviewed.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought that was weird in general, but it seems consistent with other uses of WriteCommandLineError. Unless you know the reason it was like this, I'll change all uses to not show the banner.

@SteveL-MSFT SteveL-MSFT changed the title Make error message consistent when invalid script is passed to -File Make error message consistent when invalid script is passed to -File, better error when passed ambiguous arg Aug 16, 2017
@SteveL-MSFT SteveL-MSFT force-pushed the console-no branch 2 times, most recently from a9d0bac to cb97564 Compare August 16, 2017 10:23
Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

Leave a comment

#if !CORECLR
"sta",
"mta",
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

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

#if !CORECLR is no longer relevant. Should we remove the parameters at all?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm fixing that as part of enabling ApartmentState PR and not part of this PR

Copy link
Collaborator

Choose a reason for hiding this comment

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

Closed.

@@ -446,7 +446,6 @@ internal enum KeyboardFlag : uint
internal const int SW_MAX = 11;


#if !CORECLR // ProcessWindowStyle does Not exist on CoreCLR
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe
#if !UNIX

Copy link
Member Author

Choose a reason for hiding this comment

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

makes sense

@@ -269,4 +269,7 @@ EXAMPLES
<data name="MissingConfigurationNameArgument" xml:space="preserve">
<value>Cannot process the command because -Configuration requires an argument that is a remote endpoint configuration name. Specify this argument and try again.</value>
</data>
<data name="InvalidArgument" xml:space="preserve">
<value>Invalid argument '{0}', did you mean:</value>
Copy link
Collaborator

Choose a reason for hiding this comment

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

"did you mean:" - Do you want add something else?

Copy link
Member Author

Choose a reason for hiding this comment

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

The possible matches show up after this. See example above in the PR description.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Closed.

Describe "WindowStyle argument" -Tag Feature {
BeforeAll {
$ExitCodeBadCommandLineParameter = 0xFFFD0000
Add-Type -Name User32 -Namespace Test -MemberDefinition @"
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should exclude this on Unix.

Copy link
Member Author

Choose a reason for hiding this comment

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

will do

"@
}

It "-WindowStyle <WindowStyle> should work on Windows" -Skip:(!$IsWindows) -TestCases @(
Copy link
Collaborator

Choose a reason for hiding this comment

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

All tests is excluded on Unix so we could use our pattern "$PSDefaultParameterValues["it:skip"] = ..."

Copy link
Member Author

Choose a reason for hiding this comment

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

agree

Describe "WindowStyle argument" -Tag Feature {
BeforeAll {
$defaultParamValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = (!$IsWindows)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Parentheses can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will remove

@SteveL-MSFT
Copy link
Member Author

SteveL-MSFT commented Aug 16, 2017

Not sure why some of the new tests fail in AppVeyor but not on my desktop. Figured it out, need to redirect stderr to stdout. However, I noticed the tests fail on Mac because the exit code isn't being set, but still shows as passed overall.

@SteveL-MSFT SteveL-MSFT force-pushed the console-no branch 2 times, most recently from bb83bb5 to ab1862b Compare August 16, 2017 13:01
@SteveL-MSFT SteveL-MSFT added the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Aug 16, 2017
@SteveL-MSFT
Copy link
Member Author

Figured out why it's failing on non-Windows. It appears that exit codes from programs have to be in the range 0-255 and anything greater than 255 is mod 256. I verified this with a simple console c# app. In this case, the value for ExitCodeBadCommandLineParameter mod 256 == 0. Hmmm.

Although a breaking change, I think we should conform to libc standards which means this error code should be 64 "command line usage error". We should also update all the other error codes that powershell console returns.

In general, I don't think this should be a problem as typically 0 means success and non-0 means error so although a breaking change, I suspect it have limited real world impact.

cc @PowerShell/powershell-committee

@SteveL-MSFT SteveL-MSFT added the Breaking-Change breaking change that may affect users label Aug 16, 2017
Copy link
Member

@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

I approve the changes, but there are 2 distinct fixes in the PR, so I think there should be 2 distinct commits.

Interactive add (git add -p) is an easy way to do that.

@SteveL-MSFT
Copy link
Member Author

@lzybkr are you referring to -WindowStyle and everything else as two commits?

@lzybkr
Copy link
Member

lzybkr commented Aug 16, 2017

Yeah, -WindowStyle is buried at the bottom of your PR description, so it felt like it's own thing.

@SteveL-MSFT
Copy link
Member Author

@lzybkr sure, I can make that two commits in the same PR.

@SteveL-MSFT
Copy link
Member Author

Split into 3 commits (Thanks @lzybkr for the tip on git add -p!)

@SteveL-MSFT SteveL-MSFT added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Aug 16, 2017
@SteveL-MSFT
Copy link
Member Author

@PowerShell/powershell-committee reviewed this and is ok with changing the exit codes

@TravisEz13 TravisEz13 removed their request for review August 16, 2017 23:09
@iSazonov iSazonov merged commit 45d1d20 into PowerShell:master Aug 17, 2017
@TravisEz13
Copy link
Member

TravisEz13 commented Aug 17, 2017

@iSazonov, @lzybkr and @SteveL-MSFT thanks as always!

@SteveL-MSFT SteveL-MSFT deleted the console-no branch August 24, 2017 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking-Change breaking change that may affect users Committee-Reviewed PS-Committee has reviewed this and made a decision
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants