@@ -229,18 +229,11 @@ if (-not (Test-Path $helpersPath)) {
229229}
230230Import-Module $helpersPath - Force
231231
232- $worktreeLock = Enter-WorktreeLock - RepoRoot $PSScriptRoot - Context " FieldWorks build" - StartedBy $StartedBy
233-
234- # Worktree-aware cleanup: only stop conflicting processes related to this repo root.
235- Stop-ConflictingProcesses - IncludeOmniSharp - RepoRoot $PSScriptRoot
236-
237- $fwTasksSourcePath = Join-Path $PSScriptRoot " BuildTools/FwBuildTasks/$Configuration /FwBuildTasks.dll"
238- $fwTasksDropPath = Join-Path $PSScriptRoot " BuildTools/FwBuildTasks/$Configuration /FwBuildTasks.dll"
239-
240232# =============================================================================
241233# Environment Setup
242234# =============================================================================
243235
236+ $worktreeLock = $null
244237$cleanupArgs = @ {
245238 IncludeOmniSharp = $true
246239 RepoRoot = $PSScriptRoot
@@ -310,6 +303,14 @@ function Get-BuildStampPath {
310303}
311304
312305try {
306+ $worktreeLock = Enter-WorktreeLock - RepoRoot $PSScriptRoot - Context " FieldWorks build" - StartedBy $StartedBy
307+
308+ # Worktree-aware cleanup: only stop conflicting processes related to this repo root.
309+ Stop-ConflictingProcesses - IncludeOmniSharp - RepoRoot $PSScriptRoot
310+
311+ $fwTasksSourcePath = Join-Path $PSScriptRoot " BuildTools/FwBuildTasks/$Configuration /FwBuildTasks.dll"
312+ $fwTasksDropPath = Join-Path $PSScriptRoot " BuildTools/FwBuildTasks/$Configuration /FwBuildTasks.dll"
313+
313314 Invoke-WithFileLockRetry - Context " FieldWorks build" - IncludeOmniSharp - RepoRoot $PSScriptRoot - Action {
314315 # Initialize Visual Studio Developer environment
315316 Initialize-VsDevEnvironment
@@ -635,7 +636,9 @@ try {
635636finally {
636637 # Kill any lingering build processes that might hold file locks
637638 Stop-ConflictingProcesses @cleanupArgs
638- Exit-WorktreeLock - LockHandle $worktreeLock
639+ if ($worktreeLock ) {
640+ Exit-WorktreeLock - LockHandle $worktreeLock
641+ }
639642}
640643
641644if ($testExitCode -ne 0 ) {
0 commit comments