Skip to content

Commit bf601a2

Browse files
niheavenrashil2000
authored andcommitted
fix(shim): Fix PS1 shim error when in different drive in PS7 (ScoopInstaller#4614)
Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com>
1 parent 3de0161 commit bf601a2

2 files changed

Lines changed: 79 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
- **depends:** Prevent error on no URL ([#4595](https://github.com/ScoopInstaller/Scoop/issues/4595))
77
- **decompress:** Fix nested Zstd archive extraction ([#4608](https://github.com/ScoopInstaller/Scoop/issues/4608))
8+
- **shim:** Fix PS1 shim error when in different drive in PS7 ([#4614](https://github.com/ScoopInstaller/Scoop/issues/4614))
89

910
### Styles
1011

lib/core.ps1

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -577,90 +577,114 @@ function shim($path, $global, $name, $arg) {
577577
} elseif ($path -match '\.(bat|cmd)$') {
578578
# shim .bat, .cmd so they can be used by programs with no awareness of PSH
579579
warn_on_overwrite "$shim.cmd" $path
580-
"@rem $resolved_path
581-
@`"$resolved_path`" $arg %*" | Out-File "$shim.cmd" -Encoding ASCII
580+
@(
581+
"@rem $resolved_path",
582+
"@`"$resolved_path`" $arg %*"
583+
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
582584

583585
warn_on_overwrite $shim $path
584-
"#!/bin/sh
585-
# $resolved_path
586-
MSYS2_ARG_CONV_EXCL=/C cmd.exe /C `"$resolved_path`" $arg `"$@`"" | Out-File $shim -Encoding ASCII
586+
@(
587+
"#!/bin/sh",
588+
"# $resolved_path",
589+
"MSYS2_ARG_CONV_EXCL=/C cmd.exe /C `"$resolved_path`" $arg `"$@`""
590+
) -join "`n" | Out-File $shim -Encoding ASCII
587591
} elseif ($path -match '\.ps1$') {
588592
# if $path points to another drive resolve-path prepends .\ which could break shims
589593
warn_on_overwrite "$shim.ps1" $path
590-
$ps1text = if ($relative_path -match '^(.\\[\w]:).*$') {
591-
"# $resolved_path
592-
`$path = `"$path`"
593-
if(`$myinvocation.expectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }
594-
exit `$lastexitcode"
594+
$ps1text = if ($relative_path -match '^(\.\\)?\w:.*$') {
595+
@(
596+
"# $resolved_path",
597+
"`$path = `"$path`"",
598+
"if (`$MyInvocation.ExpectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }",
599+
"exit `$LASTEXITCODE"
600+
)
595601
} else {
596602
# Setting PSScriptRoot in Shim if it is not defined, so the shim doesn't break in PowerShell 2.0
597-
"# $resolved_path
598-
if (!(Test-Path Variable:PSScriptRoot)) { `$PSScriptRoot = Split-Path `$MyInvocation.MyCommand.Path -Parent }
599-
`$path = join-path `"`$psscriptroot`" `"$relative_path`"
600-
if(`$myinvocation.expectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }
601-
exit `$lastexitcode"
603+
@(
604+
"# $resolved_path",
605+
"if (!(Test-Path Variable:PSScriptRoot)) { `$PSScriptRoot = Split-Path `$MyInvocation.MyCommand.Path -Parent }",
606+
"`$path = Join-Path `"`$PSScriptRoot`" `"$relative_path`"",
607+
"if (`$MyInvocation.ExpectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }",
608+
"exit `$LASTEXITCODE"
609+
)
602610
}
603-
$ps1text | Out-File "$shim.ps1" -Encoding ASCII
611+
$ps1text -join "`r`n" | Out-File "$shim.ps1" -Encoding ASCII
604612

605613
# make ps1 accessible from cmd.exe
606614
warn_on_overwrite "$shim.cmd" $path
607-
"@rem $resolved_path
608-
@echo off
609-
setlocal enabledelayedexpansion
610-
set args=%*
611-
:: replace problem characters in arguments
612-
set args=%args:`"='%
613-
set args=%args:(=``(%
614-
set args=%args:)=``)%
615-
set invalid=`"='
616-
if !args! == !invalid! ( set args= )
617-
where /q pwsh.exe
618-
if %errorlevel% equ 0 (
619-
pwsh -noprofile -ex unrestricted -command `"& '$resolved_path' $arg %args%;exit `$lastexitcode`"
620-
) else (
621-
powershell -noprofile -ex unrestricted -command `"& '$resolved_path' $arg %args%;exit `$lastexitcode`"
622-
)" | Out-File "$shim.cmd" -Encoding ASCII
615+
@(
616+
"@rem $resolved_path",
617+
"@echo off",
618+
"setlocal enabledelayedexpansion",
619+
"set args=%*",
620+
":: replace problem characters in arguments",
621+
"set args=%args:`"='%",
622+
"set args=%args:(=``(%",
623+
"set args=%args:)=``)%",
624+
"set invalid=`"='",
625+
"if !args! == !invalid! ( set args= )",
626+
"where /q pwsh.exe",
627+
"if %errorlevel% equ 0 (",
628+
" pwsh -noprofile -ex unrestricted -command `"& '$resolved_path' $arg %args%;exit `$lastexitcode`"",
629+
") else (",
630+
" powershell -noprofile -ex unrestricted -command `"& '$resolved_path' $arg %args%;exit `$lastexitcode`"",
631+
")"
632+
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
623633

624634
warn_on_overwrite $shim $path
625-
"#!/bin/sh
626-
# $resolved_path
627-
if command -v pwsh.exe &> /dev/null; then
628-
pwsh.exe -noprofile -ex unrestricted -command `"& '$resolved_path' $arg $@;exit \`$lastexitcode`"
629-
else
630-
powershell.exe -noprofile -ex unrestricted -command `"& '$resolved_path' $arg $@;exit \`$lastexitcode`"
631-
fi" | Out-File $shim -Encoding ASCII
635+
@(
636+
"#!/bin/sh",
637+
"# $resolved_path",
638+
"if command -v pwsh.exe &> /dev/null; then",
639+
" pwsh -noprofile -ex unrestricted -command `"& '$resolved_path' $arg $@;exit \`$lastexitcode`"",
640+
"else",
641+
" powershell -noprofile -ex unrestricted -command `"& '$resolved_path' $arg $@;exit \`$lastexitcode`"",
642+
"fi"
643+
) -join "`n" | Out-File $shim -Encoding ASCII
632644
} elseif ($path -match '\.jar$') {
633645
warn_on_overwrite "$shim.cmd" $path
634-
"@rem $resolved_path
635-
@java -jar `"$resolved_path`" $arg %*" | Out-File "$shim.cmd" -Encoding ASCII
646+
@(
647+
"@rem $resolved_path",
648+
"@java -jar `"$resolved_path`" $arg %*"
649+
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
636650

637651
warn_on_overwrite $shim $path
638-
"#!/bin/sh
639-
# $resolved_path
640-
java -jar `"$resolved_path`" $arg `"$@`"" | Out-File $shim -Encoding ASCII
652+
@(
653+
"#!/bin/sh",
654+
"# $resolved_path",
655+
"java -jar `"$resolved_path`" $arg `"$@`""
656+
) -join "`n" | Out-File $shim -Encoding ASCII
641657
} elseif ($path -match '\.py$') {
642658
warn_on_overwrite "$shim.cmd" $path
643-
"@rem $resolved_path
644-
@python `"$resolved_path`" $arg %*" | Out-File "$shim.cmd" -Encoding ASCII
659+
@(
660+
"@rem $resolved_path",
661+
"@python `"$resolved_path`" $arg %*"
662+
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
645663

646664
warn_on_overwrite $shim $path
647-
"#!/bin/sh
648-
# $resolved_path
649-
python `"$resolved_path`" $arg `"$@`"" | Out-File $shim -Encoding ASCII
665+
@(
666+
"#!/bin/sh",
667+
"# $resolved_path",
668+
"python `"$resolved_path`" $arg `"$@`""
669+
) -join "`n" | Out-File $shim -Encoding ASCII
650670
} else {
651671
warn_on_overwrite "$shim.cmd" $path
652672
# find path to Git's bash so that batch scripts can run bash scripts
653673
$gitdir = (Get-Item (Get-Command git -ErrorAction:Stop).Source -ErrorAction:Stop).Directory.Parent
654674
if ($gitdir.FullName -imatch 'mingw') {
655675
$gitdir = $gitdir.Parent
656676
}
657-
"@rem $resolved_path
658-
@`"$(Join-Path (Join-Path $gitdir.FullName 'bin') 'bash.exe')`" `"$resolved_path`" $arg %*" | Out-File "$shim.cmd" -Encoding ASCII
677+
@(
678+
"@rem $resolved_path",
679+
"@`"$(Join-Path (Join-Path $gitdir.FullName 'bin') 'bash.exe')`" `"$resolved_path`" $arg %*"
680+
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
659681

660682
warn_on_overwrite $shim $path
661-
"#!/bin/sh
662-
# $resolved_path
663-
`"$resolved_path`" $arg `"$@`"" | Out-File $shim -Encoding ASCII
683+
@(
684+
"#!/bin/sh",
685+
"# $resolved_path",
686+
"`"$resolved_path`" $arg `"$@`""
687+
) -join "`n" | Out-File $shim -Encoding ASCII
664688
}
665689
}
666690

0 commit comments

Comments
 (0)