-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(powershell): use Invoke-Expression to pass args #8267
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
Conversation
Co-authored-by: noseratio <[email protected]>
Hi Alex, Any change here should go along with new tests that prove what's not working (before the change) but working after. This includes executing I have updated Please include this in the PR. On my machine (pwsh 7.5.0, npm 10.9.2), all parameters are passed correctly with the current Do you have other tests that fail? Please add them. |
…ipts Co-authored-by: @mbtools
@mbtools thanks! I'll work on new tests for this PR later today |
|
This PR is ready for review |
Below "Output" is from running the This should pass before adding my PR changes @mbtools Ok I have an issue running the tests on my Windows machine Without my PR, running line number 240 and line number 253 from https://github.com/npm/cli/blob/latest/test/bin/windows-shims.js
|
This is not the same Your Log: |
Right...I meant I'm getting 2 failing tests before adding my PR |
That's with Windows PowerShell? I'm not sure if this ever was an option (or tested). Here it's about cmd, pwsh, and bash. If we can make it work for the older Windows PowerShell as well, why not? |
My 2c. If it's possible to make it work for both legacy (v5.0 and below) and modern (v6+) PowerShell with little efforts, that's great. Otherwise, I'd say specifying |
I manually tested that |
the 2 failing tests I get on my machine do not appear in GitHub Actions CI so it must be my setup issue |
Thanks for the feedback! I'll look into the not so good part tomorrow I also just made the ps1 files simpler by going back to the old logic if the grave key is found in the command....too many edge cases with that one |
Ok this PR looks complete to me, but if there's anything else to change please let me know |
Letting CI run. Will let @mbtools weigh in here before landing. Node 24 is now live with npm 11 so it will be good to get this landed. |
👍 Let's add single quoted parameters to the test:
I'm not sure how you came up with the $NPM_OG_COMMAND syntax but it works like a charm. Thank you! Just a bit of styling to keep "exit" at the end: if {
#...
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS"
} else {
Invoke-Expression "& `"$NODE_EXE`" `"$NPM_CLI_JS`" $NPM_ARGS"
}
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $NODE_EXE $NPM_CLI_JS $args
} else {
& $NODE_EXE $NPM_CLI_JS $args
}
}
exit $LASTEXITCODE |
CMD treats single-quotes as literals - output on my machine
Don't know why pwsh7 is not passing the test |
It almost feels like pwsh7 and cmd output always match with spawnSync ?? const { spawnSync } = require('child_process')
const cmd = 'pwsh'
const result = spawnSync(`"${cmd}"`, [`.\\abc.ps1 "hello world" 'hi world' a=1,b=2,c=3`], {
shell: true,
cwd: __dirname,
})
if(result.stderr.length > 0) throw new Error(result.stderr.toString().trim())
console.log(result.stdout.toString().trim())
const { spawnSync } = require('child_process')
const cmd = 'powershell'
const result = spawnSync(`"${cmd}"`, [`.\\abc.ps1 "hello world" 'hi world' a=1,b=2,c=3`], {
shell: true,
cwd: __dirname,
})
if(result.stderr.length > 0) throw new Error(result.stderr.toString().trim())
console.log(result.stdout.toString().trim())
|
Let's remove the single quote tests for now. It doesn't look related to the This PR is hard to parse now. Could you create a new PR with a single commit and summary of the change (co-pilot 😉), please? |
Continuing in #8278 |
PS: Set |
ahhhh
EDIT: I updated the new PR |
@mbtools with the single-quote tests which I'm not merging into my branch.....we now get this https://github.com/alexsch01/cli/actions/runs/14887289051/job/41810161408 Only CMD is now failing!!! which is expected for CMD |
Yeah, |
yes the test is good that's what I meant |
Continuation of #8267 @mbtools --- This fixes the command `npm test -- hello -p1 world -p2 "hello world" --q1=hello world --q2="hello world"` in Windows PowerShell and pwsh7 - where the "test" script prints all the arguments passed after the first "--" in the command above Before this change ``` PS> npm test -- hello -p1 world -p2 "hello world" --q1=hello world --q2="hello world" npm warn "world" is being parsed as a normal command line argument. npm warn "hello world" is being parsed as a normal command line argument. npm warn Unknown cli config "--p1". This will stop working in the next major version of npm. npm warn Unknown cli config "--p2". This will stop working in the next major version of npm. npm warn Unknown cli config "--q1". This will stop working in the next major version of npm. npm warn Unknown cli config "--q2". This will stop working in the next major version of npm. > [email protected] test > node args.js hello world hello world world hello world hello world world ``` With this change ``` PS> npm test -- hello -p1 world -p2 "hello world" --q1=hello world --q2="hello world" > [email protected] test > node args.js hello -p1 world -p2 hello world --q1=hello world --q2=hello world hello -p1 world -p2 hello world --q1=hello world --q2=hello world ``` --- Also, fixes comma-separated values in Windows PowerShell and pwsh7 Before this change ``` PS> npm help a=1,b=2,c=3 No matches in help for: a=1 b=2 c=3 ``` With this change ``` PS> npm help a=1,b=2,c=3 No matches in help for: a=1,b=2,c=3 ```
Reopening of #7458