@@ -90,18 +90,19 @@ function NormalizeArg {
90
90
$Items -join " ,"
91
91
}
92
92
93
- function QuotedString {
93
+ function QuoteString {
94
94
param (
95
- [Parameter (Mandatory = $true , ValueFromPipeline = $true )]
95
+ [Parameter (ValueFromPipeline = $true )]
96
96
[string ] $Item
97
97
)
98
98
99
99
# We surround the string with double quotes, in order to preserve its contents as
100
100
# only one command arg.
101
- # This is needed because PowerShell will consider a new argument when it sees a
102
- # space char.
103
- if (-not $Item.StartsWith (" `" " ) -and $Item.Contains (" " )) {
104
- " `" $Item `" "
101
+ # This is needed because PowerShell consider spaces as separator of arguments.
102
+ # The double quotes around will be removed when PowerShell process the argument.
103
+ # See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.4#passing-quoted-strings-to-external-commands
104
+ if ($Item.Contains (" " )) {
105
+ ' "' + $Item + ' "'
105
106
}
106
107
else {
107
108
$Item
@@ -121,7 +122,7 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
121
122
122
123
switch ($Arg ) {
123
124
{ $_ -in @ (" -e" , " -r" , " -pr" , " -pa" , " -pz" , " --eval" , " --remsh" , " --dot-iex" , " --dbg" ) } {
124
- $private :NextArg = $Args [++ $i ] | NormalizeArg | QuotedString
125
+ $private :NextArg = NormalizeArg( $Args [++ $i ])
125
126
126
127
$ElixirParams.Add ($Arg )
127
128
$ElixirParams.Add ($NextArg )
@@ -235,7 +236,7 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
235
236
236
237
$ElixirParams.Add (" --rpc-eval" )
237
238
$ElixirParams.Add ($Key )
238
- $ElixirParams.Add (" $ ( QuotedString $ Value) " )
239
+ $ElixirParams.Add ($ Value )
239
240
break
240
241
}
241
242
@@ -255,7 +256,7 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
255
256
256
257
$ElixirParams.Add (" -boot_var" )
257
258
$ElixirParams.Add ($Key )
258
- $ElixirParams.Add (" $ ( QuotedString $ Value) " )
259
+ $ElixirParams.Add ($ Value )
259
260
break
260
261
}
261
262
@@ -283,12 +284,7 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
283
284
284
285
Default {
285
286
$private :Normalized = NormalizeArg $Arg
286
- if ($Normalized.StartsWith (" -" )) {
287
- $AllOtherParams.Add ($Normalized )
288
- }
289
- else {
290
- $AllOtherParams.Add (" $ ( QuotedString $Normalized ) " )
291
- }
287
+ $AllOtherParams.Add ($Normalized )
292
288
break
293
289
}
294
290
}
@@ -331,7 +327,8 @@ if ($ERTS_BIN) {
331
327
}
332
328
333
329
if ($null -eq $RunErlPipe ) {
334
- $ParamsPart = $AllParams -join " "
330
+ # We double the double-quotes because they are going to be escaped by arguments parsing.
331
+ $ParamsPart = $AllParams | ForEach-Object - Process { QuoteString($_ -replace " `" " , " `"`" " ) }
335
332
}
336
333
else {
337
334
$private :OrigBinPath = $BinPath
@@ -344,10 +341,12 @@ else {
344
341
345
342
$AllParams.Insert (0 , $OrigBinPath )
346
343
347
- # We only scape double-quotes because the command will be surrounded by double-quotes.
348
- $private :Escaped = $AllParams | ForEach-Object - Process { $_ -replace " `" " , " \$&" }
344
+ # We scape special chars using the Unix style of scaping, with "\".
345
+ # But first we escape the double-quotes.
346
+ $private :Escaped = $AllParams | ForEach-Object - Process { ($_ -replace " `" " , " \`" " ) -replace " [^a-zA-Z0-9_/-]" , " \$&" }
349
347
350
- $ParamsPart = " -daemon `" $RunErlPipe /`" `" $RunErlLog /`" `" $ ( [string ]::Join(" " , $Escaped )) `" "
348
+ # The args are surrounded here for the same reasons
349
+ $ParamsPart = @ (" -daemon" , " `" $RunErlPipe /`" " , " `" $RunErlLog /`" " , " `" $ ( $Escaped -join " " ) `" " )
351
350
}
352
351
353
352
if ($Env: ELIXIR_CLI_DRY_RUN ) {
@@ -358,6 +357,6 @@ else {
358
357
$null = New-Item - Path " ." - ItemType " directory" - Name " $RunErlPipe " - Force
359
358
$null = New-Item - Path " ." - ItemType " directory" - Name " $RunErlLog " - Force
360
359
}
361
- $Output = Start-Process - FilePath $BinPath - ArgumentList " $ParamsPart " - NoNewWindow - Wait - PassThru
360
+ $Output = Start-Process - FilePath $BinPath - ArgumentList $ParamsPart - NoNewWindow - Wait - PassThru
362
361
exit $Output.ExitCode
363
362
}
0 commit comments