diff --git a/tasks/winbuildscripts/common.ps1 b/tasks/winbuildscripts/common.ps1 index 35ef863d57535..d1e51fc5b2b1d 100644 --- a/tasks/winbuildscripts/common.ps1 +++ b/tasks/winbuildscripts/common.ps1 @@ -42,6 +42,23 @@ function Exit-BuildRoot() { Pop-Location -StackName AgentBuildRoot } +<# +.SYNOPSIS +Sets the current directory to the root of the repository. +#> +function Enter-RepoRoot() { + # Expected PSScriptRoot: datadog-agent\tasks\winbuildscripts\ + Push-Location "$PSScriptRoot\..\.." -ErrorAction Stop -StackName AgentRepoRoot | Out-Null +} + +<# +.SYNOPSIS +Leaves the repository root directory and returns to the original working directory. +#> +function Exit-RepoRoot() { + Pop-Location -StackName AgentRepoRoot +} + <# .SYNOPSIS Expands the Go module cache from an archive file. @@ -209,6 +226,8 @@ function Invoke-BuildScript { if ($BuildOutOfSource) { Enter-BuildRoot + } else { + Enter-RepoRoot } Expand-ModCache -modcache modcache @@ -234,9 +253,11 @@ function Invoke-BuildScript { # This finally block is executed regardless of whether the try block completes successfully, throws an exception, # or uses `exit` to terminate the script. + # Restore the original working directory if ($BuildOutOfSource) { - # Restore the original working directory Exit-BuildRoot + } else { + Exit-RepoRoot } } }