@@ -204,18 +204,11 @@ if (-not (Test-Path $helpersPath)) {
204204}
205205Import-Module $helpersPath - Force
206206
207- $worktreeLock = Enter-WorktreeLock - RepoRoot $PSScriptRoot - Context " FieldWorks build" - StartedBy $StartedBy
208-
209- # Worktree-aware cleanup: only stop conflicting processes related to this repo root.
210- Stop-ConflictingProcesses - IncludeOmniSharp - RepoRoot $PSScriptRoot
211-
212- $fwTasksSourcePath = Join-Path $PSScriptRoot " BuildTools/FwBuildTasks/$Configuration /FwBuildTasks.dll"
213- $fwTasksDropPath = Join-Path $PSScriptRoot " BuildTools/FwBuildTasks/$Configuration /FwBuildTasks.dll"
214-
215207# =============================================================================
216208# Environment Setup
217209# =============================================================================
218210
211+ $worktreeLock = $null
219212$cleanupArgs = @ {
220213 IncludeOmniSharp = $true
221214 RepoRoot = $PSScriptRoot
@@ -285,6 +278,14 @@ function Get-BuildStampPath {
285278}
286279
287280try {
281+ $worktreeLock = Enter-WorktreeLock - RepoRoot $PSScriptRoot - Context " FieldWorks build" - StartedBy $StartedBy
282+
283+ # Worktree-aware cleanup: only stop conflicting processes related to this repo root.
284+ Stop-ConflictingProcesses - IncludeOmniSharp - RepoRoot $PSScriptRoot
285+
286+ $fwTasksSourcePath = Join-Path $PSScriptRoot " BuildTools/FwBuildTasks/$Configuration /FwBuildTasks.dll"
287+ $fwTasksDropPath = Join-Path $PSScriptRoot " BuildTools/FwBuildTasks/$Configuration /FwBuildTasks.dll"
288+
288289 Invoke-WithFileLockRetry - Context " FieldWorks build" - IncludeOmniSharp - RepoRoot $PSScriptRoot - Action {
289290 # Initialize Visual Studio Developer environment
290291 Initialize-VsDevEnvironment
@@ -561,7 +562,9 @@ try {
561562finally {
562563 # Kill any lingering build processes that might hold file locks
563564 Stop-ConflictingProcesses @cleanupArgs
564- Exit-WorktreeLock - LockHandle $worktreeLock
565+ if ($worktreeLock ) {
566+ Exit-WorktreeLock - LockHandle $worktreeLock
567+ }
565568}
566569
567570if ($testExitCode -ne 0 ) {
0 commit comments