Skip to content

Commit 77b66cc

Browse files
authored
fix(core): Fix arguments parsing method of Invoke-ExternalCommand() (#5839)
1 parent 9770c86 commit 77b66cc

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- **config:** Warn users about misconfigured GitHub token ([#5777](https://github.com/ScoopInstaller/Scoop/issues/5777))
4646
- **update/uninstall:** Remove items from PATH correctly ([#5833](https://github.com/ScoopInstaller/Scoop/issues/5833))
4747
- **shim:** Allow GUI applications to attach to the shell's console when launched using the GUI shim ([#5721](https://github.com/ScoopInstaller/Scoop/issues/5721))
48+
- **core:** Fix arguments parsing method of `Invoke-ExternalCommand()` ([#5839](https://github.com/ScoopInstaller/Scoop/issues/5839))
4849

4950
### Performance Improvements
5051

lib/core.ps1

+11-10
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,7 @@ function Invoke-ExternalCommand {
600600
[CmdletBinding(DefaultParameterSetName = "Default")]
601601
[OutputType([Boolean])]
602602
param (
603-
[Parameter(Mandatory = $true,
604-
Position = 0)]
603+
[Parameter(Mandatory = $true, Position = 0)]
605604
[Alias("Path")]
606605
[ValidateNotNullOrEmpty()]
607606
[String]
@@ -651,6 +650,7 @@ function Invoke-ExternalCommand {
651650
$Process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
652651
}
653652
if ($ArgumentList.Length -gt 0) {
653+
$ArgumentList = $ArgumentList | ForEach-Object { [regex]::Split($_.Replace('"', ''), '(?<=(?<![:\w])[/-]\w+) | (?=[/-])') }
654654
if ($FilePath -match '^((cmd|cscript|wscript|msiexec)(\.exe)?|.*\.(bat|cmd|js|vbs|wsf))$') {
655655
$Process.StartInfo.Arguments = $ArgumentList -join ' '
656656
} elseif ($Process.StartInfo.ArgumentList.Add) {
@@ -661,14 +661,15 @@ function Invoke-ExternalCommand {
661661
} else {
662662
# escape arguments manually in lower versions, refer to https://docs.microsoft.com/en-us/previous-versions/17w5ykft(v=vs.85)
663663
$escapedArgs = $ArgumentList | ForEach-Object {
664-
# escape N consecutive backslash(es), which are followed by a double quote, to 2N consecutive ones
665-
$s = $_ -replace '(\\+)"', '$1$1"'
666-
# escape N consecutive backslash(es), which are at the end of the string, to 2N consecutive ones
667-
$s = $s -replace '(\\+)$', '$1$1'
668-
# escape double quotes
669-
$s = $s -replace '"', '\"'
670-
# quote the argument
671-
"`"$s`""
664+
# escape N consecutive backslash(es), which are followed by a double quote or at the end of the string, to 2N consecutive ones
665+
$s = $_ -replace '(\\+)(""|$)', '$1$1$2'
666+
# quote the path if it contains spaces and is not NSIS's '/D' argument
667+
# ref: https://nsis.sourceforge.io/Docs/Chapter3.html
668+
if ($s -match ' ' -and $s -notmatch '/D=[A-Z]:[\\/].*') {
669+
$s -replace '([A-Z]:[\\/].*)', '"$1"'
670+
} else {
671+
$s
672+
}
672673
}
673674
$Process.StartInfo.Arguments = $escapedArgs -join ' '
674675
}

0 commit comments

Comments
 (0)