diff --git a/lib/install.ps1 b/lib/install.ps1 index 67f5249dfb..b87dcc8de2 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -956,8 +956,7 @@ function link_current($versiondir) { Remove-Item $currentdir -Recurse -Force -ErrorAction Stop } - New-Item -Path $currentdir -ItemType Junction -Value $versiondir | Out-Null - attrib $currentdir +R /L + New-DirectoryJunction($currentdir, $versiondir) return $currentdir } @@ -1207,8 +1206,7 @@ function persist_data($manifest, $original_dir, $persist_dir) { # create link if (is_directory $target) { # target is a directory, create junction - New-Item -Path $source -ItemType Junction -Value $target | Out-Null - attrib $source +R /L + New-DirectoryJunction($source, $target) } else { # target is a file, create hard link New-Item -Path $source -ItemType HardLink -Value $target | Out-Null @@ -1270,3 +1268,31 @@ function test_running_process($app, $global) { return $false } } + +# test if this script is being executed inside a docker container +function Test-IsInsideContainer { + $foundService = Get-Service -Name cexecsvc -ErrorAction SilentlyContinue + if ( $foundService -eq $null ) { + $false + } + else { + $true + } +} + +# wrapper function to create junction links +# Required to handle docker/for-win#12240 +function New-DirectoryJunction($source, $target) { + if (Test-Path $target) { + warn "Target directory `$target` already exists." + return $false + } + + if (Test-IsInsideContainer) { + & "$env:COMSPEC" /c "mklink /j `"$source`" `"$target`"" | out-null + } else { + New-Item -Path $source -ItemType Junction -Value $target | Out-Null + } + attrib $source +R /L +} + diff --git a/lib/psmodules.ps1 b/lib/psmodules.ps1 index fc32e99425..beac908d7e 100644 --- a/lib/psmodules.ps1 +++ b/lib/psmodules.ps1 @@ -26,7 +26,7 @@ function install_psmodule($manifest, $dir, $global) { Remove-Item -Path $linkfrom -Force -ErrorAction SilentlyContinue } - New-Item -Path $linkfrom -ItemType Junction -Value $dir | Out-Null + New-DirectoryJunction($linkfrom, $dir) } function uninstall_psmodule($manifest, $dir, $global) {