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

Add a Windows mode for native commands that allows some commands to use legacy argument passing #15408

Merged
merged 25 commits into from Jul 20, 2021

Conversation

JamesWTruher
Copy link
Member

@JamesWTruher JamesWTruher commented May 14, 2021

PR Summary

This PR targets a consistent default behavior between PowerShell 5 and PowerShell 7 on Windows for some executables and file types. The behavior is now update to use the legacy behavior for the following files:

  • cmd.exe
  • cscript.exe
  • wscript.exe
  • ending with .bat
  • ending with .cmd
  • ending with .vbs

This list is table driven, and can be easily altered. I have started with this list as I am familiar with real world issues with them.

The PSNativeArgumentPassing preference value has been expanded to include the new value Windows. When the preference variable is set to Windows the above table will be in effect, meaning that invocations of those files will automatically use the Legacy style argument passing. If the PSNativeArgumentPassing is set to either Legacy or Standard, then the additional checks will not take place.

The default behavior is now also platform specific. On Windows platforms, the default setting will be Windows and non-Windows platforms will be Standard.

PR Context

PR Checklist

@ghost ghost assigned iSazonov May 14, 2021
@JamesWTruher JamesWTruher force-pushed the NativeArgPassing002 branch 2 times, most recently from 2fe31da to 8ed04ee Compare May 17, 2021 23:01
@iSazonov iSazonov assigned daxian-dbw and unassigned iSazonov May 18, 2021
@JamesWTruher JamesWTruher changed the title WIP: Native argument passing adjustments for Windows systems Native argument passing adjustments for Windows systems May 20, 2021
@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels May 20, 2021
@SteveL-MSFT
Copy link
Member

I believe this PR addresses part of #15143

@JamesWTruher
Copy link
Member Author

@SteveL-MSFT - I think I've addressed your issues, please let me know

@SteveL-MSFT
Copy link
Member

@JamesWTruher one comment was not addressed

return false;
}

string commandPath = filePath.ToLowerInvariant();
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need to allocate. We can do if (filePath.EndsWith(exception, StringComparison.OrdinalIgnoreVase)) in line 1302.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jun 3, 2021

I'm not sure this is working completely for cmd.exe - ie excluding cmd.exe. preview.6 breaks the Invoke-BatchFile command we have implemented in PSCX:

PS> invoke-batchfile 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat' amd64
'\"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat\"' is not recognized as an internal or external command,
operable program or batch file.

This is basically Lee Holmes old function that we have implemented in PSCX:

function Invoke-BatchFile
{
    param([string]$Path, [string]$Parameters)

    $tempFile = [IO.Path]::GetTempFileName()

    ## Store the output of cmd.exe.  We also ask cmd.exe to output
    ## the environment table after the batch file completes
    cmd.exe /c " `"$Path`" $Parameters && set " > $tempFile
    ...
}

Reverting to Legacy makes this function work again but at this rate I suspect I'll be running Legacy on all my Windows installs. :-(

@ghost
Copy link

ghost commented Jun 11, 2021

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

@JamesWTruher
Copy link
Member Author

@rkeithhill wrt the pscx issue that you mentioned. Have you tried running this branch against your scenario? This PR is supposed to automatically put the arg passing into legacy mode for cmd.exe invocations, so it should be fine (but I would really like confirmation on that). I've added validation for that very scenario, but I might have missed something.

@rkeithhill
Copy link
Collaborator

I built this branch and yes, that command works now while I still have the mode set to Windows:

image

@JamesWTruher
Copy link
Member Author

@rkeithhill - excellent - the default setting for windows will be "Windows", which is the hybrid model where certain commands will run as Legacy while others will use "Standard". This should hopefully be better and allow the wacky parsing that happens for batch files, but enable more modern apps to use the new style.

Copy link
Member

@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

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

Please take a look at Rob's suggestion

src/System.Management.Automation/engine/CommandBase.cs Outdated Show resolved Hide resolved
@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed labels Jun 15, 2021
@ghost ghost removed the Review - Needed The PR is being reviewed label Jul 19, 2021
@SteveL-MSFT
Copy link
Member

Reassigning to @rjmholt as @daxian-dbw is taking time off right now

@rjmholt rjmholt self-requested a review July 20, 2021 16:14
@rjmholt
Copy link
Collaborator

rjmholt commented Jul 20, 2021

Opened JamesWTruher#15 to add remaining suggested changes

rjmholt and others added 2 commits July 20, 2021 14:21
@rjmholt
Copy link
Collaborator

rjmholt commented Jul 20, 2021

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@rjmholt rjmholt enabled auto-merge (squash) July 20, 2021 22:29
@rjmholt rjmholt changed the title Native argument passing adjustments for Windows systems Add a Windows mode for native commands that allows some commands to use legacy argument passing Jul 20, 2021
@rjmholt rjmholt disabled auto-merge July 20, 2021 22:30
@rjmholt rjmholt enabled auto-merge (squash) July 20, 2021 22:30
@rjmholt rjmholt merged commit ac762c1 into PowerShell:master Jul 20, 2021
@adityapatwardhan adityapatwardhan added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Jul 21, 2021
@adityapatwardhan adityapatwardhan added this to the 7.2.0-preview.8 milestone Jul 21, 2021
xtqqczze pushed a commit to xtqqczze/PowerShell that referenced this pull request Jul 22, 2021
@ghost
Copy link

ghost commented Jul 22, 2021

🎉v7.2.0-preview.8 has been released which incorporates this pull request.:tada:

Handy links:

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 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

9 participants