GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(env): preserve PowerShell argument separators (#3083) · voidzero-dev/vite-plus@46e4849

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • crates
    • vp_cli_snapshots/tests/cli_snapshots/fixtures
      • command_env_powershell
        • assert-arguments.ps1
        • assert.ps1
        • print-args.cjs
      • shell_integration_cwd_templates/snapshots
        • shell_integration_cwd_templates.md
    • vp_global_cli/src/commands/env
      • setup.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_powershell/assert-arguments.ps1‎

Copy file name to clipboard
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# The PowerShell script binder consumes a bare -- before populating $args.
2+
# Exercise the generated wrapper with a real native child, without a registry.
3+
function Assert-Arguments($actual, $expected) {
4+
if ($LASTEXITCODE -ne 0 -or ($actual -join "`n") -ne $expected) {
5+
throw "Expected arguments $expected, got $actual (exit $LASTEXITCODE)"
6+
}
7+
}
8+
9+
Assert-Arguments (vp env exec node print-args.cjs -- --template react-ts) '["--","--template","react-ts"]'
10+
Assert-Arguments (vp env exec node print-args.cjs '--' --template react-ts) '["--","--template","react-ts"]'
11+
Assert-Arguments (vp env exec node print-args.cjs "--" --template react-ts) '["--","--template","react-ts"]'
12+
Assert-Arguments (vp env exec node print-args.cjs '--' -- --template react-ts) '["--","--","--template","react-ts"]'
13+
Assert-Arguments (vp env exec node print-args.cjs -- -- --template react-ts) '["--","--","--template","react-ts"]'
14+
Assert-Arguments (vp env exec node print-args.cjs --) '["--"]'
15+
Assert-Arguments (vp env exec node print-args.cjs 'value with spaces' -- --template 'react ts') '["value with spaces","--","--template","react ts"]'
16+
17+
# The stop-parsing token preserves its literal remainder without consuming --.
18+
# Capture on a separate line because --% makes the rest of the line literal.
19+
$literalArguments = vp env exec node print-args.cjs --% --
20+
Assert-Arguments $literalArguments '["--"]'
21+
$literalArguments = vp env exec node print-args.cjs --% -- --
22+
Assert-Arguments $literalArguments '["--","--"]'
23+
$literalArguments = vp env exec node print-args.cjs -- --% --
24+
Assert-Arguments $literalArguments '["--","--"]'
25+
26+
# A quoted --% allows recovery; escaping its first dash still stops parsing.
27+
$literalArguments = vp env exec node print-args.cjs '--%' -- after
28+
Assert-Arguments $literalArguments '["--","after"]'
29+
$literalArguments = vp env exec node print-args.cjs `--% --
30+
Assert-Arguments $literalArguments '["--"]'
31+
32+
$prefix = @('env', 'exec', 'node', 'print-args.cjs')
33+
$suffix = @('--template', 'react-ts')
34+
$allArguments = $prefix + @('--') + $suffix
35+
Assert-Arguments (vp env exec node print-args.cjs -- @suffix) '["--","--template","react-ts"]'
36+
Assert-Arguments (vp @allArguments) '["--","--template","react-ts"]'
37+
Assert-Arguments (vp @prefix '--' @suffix) '["--","--template","react-ts"]'
38+
Assert-Arguments (vp env exec node print-args.cjs @('before', 'separator') -- @('after', 'separator')) '["before","separator","--","after","separator"]'
39+
40+
# A splat before -- makes its position unknowable from syntax alone. Preserve
41+
# PowerShell's bound arguments in that case instead of guessing an insertion point.
42+
Assert-Arguments (vp @prefix -- --template react-ts) '["--template","react-ts"]'
43+
Assert-Arguments (vp @prefix -- @suffix) '["--template","react-ts"]'
44+
$emptyArguments = @()
45+
Assert-Arguments (vp @prefix @emptyArguments -- @emptyArguments @suffix) '["--template","react-ts"]'
46+
$script:scopedPrefix = $prefix
47+
$script:scopedSuffix = $suffix
48+
Assert-Arguments (vp @script:scopedPrefix -- @script:scopedSuffix) '["--template","react-ts"]'
49+
Assert-Arguments (vp @script:scopedPrefix '--' @script:scopedSuffix) '["--","--template","react-ts"]'
50+
Assert-Arguments (vp env exec node print-args.cjs -- @script:scopedSuffix) '["--","--template","react-ts"]'
51+
52+
# Argument expressions can change a splatted variable after it has been expanded.
53+
Assert-Arguments (vp @prefix -- @script:scopedSuffix $($script:scopedSuffix = @(); 'last')) '["--template","react-ts","last"]'
54+
55+
# The caller AST must identify the invocation inside a function or on a later line.
56+
function Invoke-ArgumentCheck {
57+
Assert-Arguments (vp env exec node print-args.cjs -- @suffix) '["--","--template","react-ts"]'
58+
}
59+
Invoke-ArgumentCheck
60+
Assert-Arguments (& vp env exec node print-args.cjs `
61+
-- `
62+
--template react-ts) '["--","--template","react-ts"]'
63+
Assert-Arguments (vp env exec node print-args.cjs first -- second | ForEach-Object { $_ }) '["first","--","second"]'
64+
$first = vp env exec node print-args.cjs one -- two; $second = vp env exec node print-args.cjs three -- four
65+
Assert-Arguments $first '["one","--","two"]'
66+
Assert-Arguments $second '["three","--","four"]'
67+
68+
# Recovering the separator must not evaluate argument expressions a second time.
69+
$script:argumentEvaluations = 0
70+
Assert-Arguments (vp env exec node print-args.cjs -- $($script:argumentEvaluations++; 'value with spaces')) '["--","value with spaces"]'
71+
if ($script:argumentEvaluations -ne 1) { throw 'An argument expression ran more than once' }
72+
73+
Assert-Arguments (vp env exec node print-args.cjs 'text -- with spaces' --template react-ts) '["text -- with spaces","--template","react-ts"]'
74+
Assert-Arguments (vp env exec node print-args.cjs `-- --template react-ts) '["--","--template","react-ts"]'
75+
Assert-Arguments (vp env exec node print-args.cjs --template react-ts) '["--template","react-ts"]'
Collapse file

‎crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_powershell/assert.ps1‎

Copy file name to clipboardExpand all lines: crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_powershell/assert.ps1
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ if ($env:VP_NODE_VERSION -ne "22.18.0") {
117117
throw "file-based VP_NODE_VERSION mismatch: expected 22.18.0, got $env:VP_NODE_VERSION"
118118
}
119119

120+
. (Join-Path $PSScriptRoot "assert-arguments.ps1")
120121
. (Join-Path $PSScriptRoot "assert-status.ps1")
121122

122123
Write-Output "PowerShell environment checks passed"
Collapse file

‎crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/command_env_powershell/print-args.cjs‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(JSON.stringify(process.argv.slice(2)));
Collapse file

‎crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/shell_integration_cwd_templates/snapshots/shell_integration_cwd_templates.md‎

Copy file name to clipboardExpand all lines: crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/shell_integration_cwd_templates/snapshots/shell_integration_cwd_templates.md
+42Lines changed: 42 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,48 @@ PowerShell wrapper keeps global -C before env use and propagates the native exit
344344
```
345345
# Vite+ PowerShell wrapper (https://viteplus.dev)
346346
$__vp_bin = '<workspace>/home/bin'
347+
# PowerShell consumes the first bare -- when calling a script. Recover its
348+
# position from the caller's AST, without evaluating argument expressions again.
349+
# The call stack also works on Windows PowerShell 5.1, which has no Statement.
350+
$__vp_stack = @(Get-PSCallStack)
351+
$__vp_command = $null
352+
if ($__vp_stack.Count -gt 1) {
353+
$__vp_ast = $__vp_stack[1].InvocationInfo.MyCommand.ScriptBlock.Ast
354+
$__vp_line = $MyInvocation.ScriptLineNumber
355+
$__vp_column = $MyInvocation.OffsetInLine
356+
if ($__vp_ast) {
357+
$__vp_command = $__vp_ast.Find({ param($node)
358+
$node -is [System.Management.Automation.Language.CommandAst] -and
359+
$node.Extent.StartLineNumber -eq $__vp_line -and $node.Extent.StartColumnNumber -eq $__vp_column
360+
}, $true)
361+
}
362+
}
363+
if ($__vp_command) {
364+
$__vp_index = 0
365+
$__vp_elements = $__vp_command.CommandElements
366+
for ($__vp_i = 1; $__vp_i -lt $__vp_elements.Count; $__vp_i++) {
367+
$__vp_element = $__vp_elements[$__vp_i]
368+
# A preceding splat has an unknown length. Leave $args unchanged rather
369+
# than reading caller variables, which may have changed or belong to another scope.
370+
if ($__vp_element -is [System.Management.Automation.Language.VariableExpressionAst] -and $__vp_element.Splatted) {
371+
break
372+
}
373+
# Only a parameter AST can be consumed. After --%, the separator is literal.
374+
if ($__vp_element -is [System.Management.Automation.Language.CommandParameterAst] -and $__vp_element.Extent.Text -ceq '--') {
375+
if ($__vp_index -le $args.Count) {
376+
$__vp_arguments = [System.Collections.Generic.List[object]]::new()
377+
$__vp_arguments.AddRange($args)
378+
$__vp_arguments.Insert($__vp_index, '--')
379+
$args = $__vp_arguments.ToArray()
380+
}
381+
break
382+
}
383+
$__vp_index++
384+
if ($__vp_element -is [System.Management.Automation.Language.CommandParameterAst] -and $__vp_element.Argument) {
385+
$__vp_index++
386+
}
387+
}
388+
}
347389
$__vp_command_index = 0
348390
if ($args.Count -ge 1) {
349391
if ($args[0] -eq "-C") {
Collapse file

‎crates/vp_global_cli/src/commands/env/setup.rs‎

Copy file name to clipboardExpand all lines: crates/vp_global_cli/src/commands/env/setup.rs
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,52 @@ Register-ArgumentCompleter -Native -CommandName vpr -ScriptBlock $__vpr_comp
10951095
// Keep the wrapper outside bin so it cannot shadow the native executable.
10961096
// Unlike an advanced function, this script forwards all arguments without
10971097
// PowerShell consuming common parameters such as -Verbose or its -V alias.
1098+
// Bare -- recovery is inspired by mise's __mise_restore_double_dash:
1099+
// https://github.com/jdx/mise/blob/34ec0439c868379be87a28591f50d41f84fb8d02/src/shell/pwsh.rs#L45-L103
10981100
const VP_WRAPPER_PS1: &str = r#"# Vite+ PowerShell wrapper (https://viteplus.dev)
10991101
$__vp_bin = '__VP_BIN_WIN__'
1102+
# PowerShell consumes the first bare -- when calling a script. Recover its
1103+
# position from the caller's AST, without evaluating argument expressions again.
1104+
# The call stack also works on Windows PowerShell 5.1, which has no Statement.
1105+
$__vp_stack = @(Get-PSCallStack)
1106+
$__vp_command = $null
1107+
if ($__vp_stack.Count -gt 1) {
1108+
$__vp_ast = $__vp_stack[1].InvocationInfo.MyCommand.ScriptBlock.Ast
1109+
$__vp_line = $MyInvocation.ScriptLineNumber
1110+
$__vp_column = $MyInvocation.OffsetInLine
1111+
if ($__vp_ast) {
1112+
$__vp_command = $__vp_ast.Find({ param($node)
1113+
$node -is [System.Management.Automation.Language.CommandAst] -and
1114+
$node.Extent.StartLineNumber -eq $__vp_line -and $node.Extent.StartColumnNumber -eq $__vp_column
1115+
}, $true)
1116+
}
1117+
}
1118+
if ($__vp_command) {
1119+
$__vp_index = 0
1120+
$__vp_elements = $__vp_command.CommandElements
1121+
for ($__vp_i = 1; $__vp_i -lt $__vp_elements.Count; $__vp_i++) {
1122+
$__vp_element = $__vp_elements[$__vp_i]
1123+
# A preceding splat has an unknown length. Leave $args unchanged rather
1124+
# than reading caller variables, which may have changed or belong to another scope.
1125+
if ($__vp_element -is [System.Management.Automation.Language.VariableExpressionAst] -and $__vp_element.Splatted) {
1126+
break
1127+
}
1128+
# Only a parameter AST can be consumed. After --%, the separator is literal.
1129+
if ($__vp_element -is [System.Management.Automation.Language.CommandParameterAst] -and $__vp_element.Extent.Text -ceq '--') {
1130+
if ($__vp_index -le $args.Count) {
1131+
$__vp_arguments = [System.Collections.Generic.List[object]]::new()
1132+
$__vp_arguments.AddRange($args)
1133+
$__vp_arguments.Insert($__vp_index, '--')
1134+
$args = $__vp_arguments.ToArray()
1135+
}
1136+
break
1137+
}
1138+
$__vp_index++
1139+
if ($__vp_element -is [System.Management.Automation.Language.CommandParameterAst] -and $__vp_element.Argument) {
1140+
$__vp_index++
1141+
}
1142+
}
1143+
}
11001144
$__vp_command_index = 0
11011145
if ($args.Count -ge 1) {
11021146
if ($args[0] -eq "-C") {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.