Skip to content

Commit a238289

Browse files
gfraiteurclaude
andcommitted
Use calling directory as container working directory in DockerBuild.ps1.
The container's working directory is now set to the directory where the user invoked the script from, rather than the script's location. This allows users to run the script from any directory and have the container's working directory match their expectation. Changes: - Capture calling directory before Set-Location changes it - Transform calling directory for container (drive letter mapping on Windows) - Use calling directory as -w parameter in all docker run/exec commands - Dockerfile path resolution already uses calling directory Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 050b0a3 commit a238289

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/PostSharp.Engineering.BuildTools/Resources/DockerBuild.ps1

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ if ([string]::IsNullOrEmpty($BuildAgentPath))
6262
}
6363
}
6464

65+
# Capture the calling directory (where the user invoked the script from)
66+
# This will be used as the working directory in the container
67+
$CallingDirectory = (Get-Location).Path
68+
6569
# Resolve Dockerfile path relative to original current directory (before changing location)
6670
# This must be done before Set-Location to preserve the user's intended relative path
6771
if ($Dockerfile -and -not [System.IO.Path]::IsPathRooted($Dockerfile))
6872
{
69-
$Dockerfile = Join-Path (Get-Location).Path $Dockerfile
73+
$Dockerfile = Join-Path $CallingDirectory $Dockerfile
7074
}
7175

7276
# Save current location and restore on exit
@@ -542,7 +546,7 @@ if (-not $KeepEnv)
542546
}
543547
}
544548

545-
# Get the source directory name from $PSScriptRoot
549+
# Get the source directory name from $PSScriptRoot (script location)
546550
$SourceDirName = $PSScriptRoot
547551

548552
# Start timing the entire process except cleaning
@@ -871,10 +875,11 @@ if ($IsWindows)
871875
}
872876
$VolumeMappings = $transformedVolumeMappings
873877

874-
# Transform MountPoints, GitDirectories, and SourceDirName for the container
878+
# Transform MountPoints, GitDirectories, SourceDirName, and CallingDirectory for the container
875879
$MountPoints = $MountPoints | ForEach-Object { Get-ContainerPath $_ }
876880
$GitDirectories = $GitDirectories | ForEach-Object { Get-ContainerPath $_ }
877881
$ContainerSourceDir = Get-ContainerPath $SourceDirName
882+
$ContainerCallingDir = Get-ContainerPath $CallingDirectory
878883

879884
# Add both the unmapped (C:\X\...) and mapped (X:\...) paths to GitDirectories for safe.directory
880885
# Git may resolve paths differently depending on how it's invoked
@@ -911,6 +916,7 @@ else
911916
{
912917
# Unix (Linux/macOS): No drive letter mapping needed, paths remain as-is
913918
$ContainerSourceDir = $SourceDirName
919+
$ContainerCallingDir = $CallingDirectory
914920

915921
# Deduplicate (case-sensitive for Unix paths)
916922
$VolumeMappings = $VolumeMappings | Sort-Object -Unique
@@ -1348,13 +1354,13 @@ if (-not $BuildImage)
13481354
if ($existingContainerId)
13491355
{
13501356
# Reuse existing container with docker exec
1351-
Write-Host "Executing: ``docker exec $existingContainerId $dockerArgsAsString -w $ContainerSourceDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
1352-
docker exec $dockerArgs -w $ContainerSourceDir $existingContainerId $pwshPath $pwshArgs -Command $inlineScript
1357+
Write-Host "Executing: ``docker exec $existingContainerId $dockerArgsAsString -w $ContainerCallingDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
1358+
docker exec $dockerArgs -w $ContainerCallingDir $existingContainerId $pwshPath $pwshArgs -Command $inlineScript
13531359
}
13541360
else
13551361
{
13561362
# Start new container with docker run
1357-
Write-Host "Executing: docker run --rm --memory=$Memory --cpus=$Cpus $isolationArg $dockerArgsAsString $VolumeMappingsAsString $envArgsAsString -w $ContainerSourceDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
1363+
Write-Host "Executing: docker run --rm --memory=$Memory --cpus=$Cpus $isolationArg $dockerArgsAsString $VolumeMappingsAsString $envArgsAsString -w $ContainerCallingDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
13581364

13591365
# Build docker command with proper argument handling (avoid empty strings)
13601366
$dockerCmd = @('run', '--rm', "--memory=$Memory", "--cpus=$Cpus")
@@ -1363,9 +1369,9 @@ if (-not $BuildImage)
13631369
$dockerCmd += $volumeArgs
13641370
$dockerCmd += $envArgs
13651371
if ($pwshArgs) {
1366-
$dockerCmd += @('-w', $ContainerSourceDir, $ImageTag, $pwshPath, $pwshArgs, '-Command', $inlineScript)
1372+
$dockerCmd += @('-w', $ContainerCallingDir, $ImageTag, $pwshPath, $pwshArgs, '-Command', $inlineScript)
13671373
} else {
1368-
$dockerCmd += @('-w', $ContainerSourceDir, $ImageTag, $pwshPath, '-Command', $inlineScript)
1374+
$dockerCmd += @('-w', $ContainerCallingDir, $ImageTag, $pwshPath, '-Command', $inlineScript)
13691375
}
13701376

13711377
& docker @dockerCmd
@@ -1438,13 +1444,13 @@ if (-not $BuildImage)
14381444
if ($existingContainerId)
14391445
{
14401446
# Reuse existing container with docker exec
1441-
Write-Host "Executing: ``docker exec $existingContainerId $dockerArgsAsString -w $ContainerSourceDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
1442-
docker exec $dockerArgs -w $ContainerSourceDir $existingContainerId $pwshPath $pwshArgs -Command $inlineScript
1447+
Write-Host "Executing: ``docker exec $existingContainerId $dockerArgsAsString -w $ContainerCallingDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
1448+
docker exec $dockerArgs -w $ContainerCallingDir $existingContainerId $pwshPath $pwshArgs -Command $inlineScript
14431449
}
14441450
else
14451451
{
14461452
# Start new container with docker run
1447-
Write-Host "Executing: ``docker run --rm --memory=$Memory --cpus=$Cpus $isolationArg $dockerArgsAsString $VolumeMappingsAsString $envArgsAsString -w $ContainerSourceDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
1453+
Write-Host "Executing: ``docker run --rm --memory=$Memory --cpus=$Cpus $isolationArg $dockerArgsAsString $VolumeMappingsAsString $envArgsAsString -w $ContainerCallingDir $ImageTag `"$pwshPath`" $pwshArgs -Command `"$inlineScript`"" -ForegroundColor Cyan
14481454

14491455
# Build docker command with proper argument handling (avoid empty strings)
14501456
$dockerCmd = @('run', '--rm', "--memory=$Memory", "--cpus=$Cpus")
@@ -1453,9 +1459,9 @@ if (-not $BuildImage)
14531459
$dockerCmd += $volumeArgs
14541460
$dockerCmd += $envArgs
14551461
if ($pwshArgs) {
1456-
$dockerCmd += @('-w', $ContainerSourceDir, $ImageTag, $pwshPath, $pwshArgs, '-Command', $inlineScript)
1462+
$dockerCmd += @('-w', $ContainerCallingDir, $ImageTag, $pwshPath, $pwshArgs, '-Command', $inlineScript)
14571463
} else {
1458-
$dockerCmd += @('-w', $ContainerSourceDir, $ImageTag, $pwshPath, '-Command', $inlineScript)
1464+
$dockerCmd += @('-w', $ContainerCallingDir, $ImageTag, $pwshPath, '-Command', $inlineScript)
14591465
}
14601466

14611467
& docker @dockerCmd

0 commit comments

Comments
 (0)