Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 17, 2025

The $os variable was accessed before initialization in terraform-installer.yaml, causing Windows systems to use Unix PATH separators (: instead of ;), preventing terraform.exe from being found.

Changes

  • Moved OS detection before first variable usage (lines 40-49)
  • Consolidated PATH configuration to single location after installation logic (lines 113-118)
  • Refactored control flow to eliminate duplicate PATH-setting code across early-return and installation paths

Before/After

Before: Variable accessed uninitialized

if (Test-Path $unzipdir) {
  if($os -eq "windows") {  # $os is empty here
    $env:PATH = "$($unzipdir);$env:PATH"
  } else {
    $env:PATH = "$($unzipdir):$env:PATH"  # Always executes
  }
  return
}
$os = ""  # Initialized too late
if ($IsWindows) { $os = "windows" }

After: Variable initialized first, PATH set once

$os = ""
if ($IsWindows) { $os = "windows" }
if ($IsLinux) { $os = "linux" }
if ($IsMacOS) { $os = "darwin" }

if (Test-Path $unzipdir) {
  Write-Host "Already installed."
} else {
  # Installation logic
}
# Single PATH configuration for both paths
if($os -eq "windows") {
  $env:PATH = "$($unzipdir);$env:PATH"
} else {
  $env:PATH = "$($unzipdir):$env:PATH"
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Bug: terraform.exe location cannot be found</issue_title>
<issue_description>### Is there an existing issue for this?

  • I have searched the existing issues

Infrastructure as Code Type? (Required)

terraform

PowerShell Module Version (Optional)

No response

Bootstrap Module Version (Optional)

No response

Starter Module? (Required)

terraform - platform_landing_zone

Starter Module Version (Optional)

No response

Input arguments of the ALZ-PowerShell-Module (Optional)

No response

Debug Output/Panic Output (Optional)

Expected Behaviour (Required)

In the accelerator, I corrected the following in the script "Get-TerraformTool.ps1":
In this script, starting at line 42, the downloaded ZIP file containing the terraform.exe file is extracted, and then the PATH variable is set depending on the OS (Windows or Linux). However, the variable '$os' used for this is only set in line 53 via the function "Get-OSArchitecture". This causes the PATH variable to be extended with the same format as for Linux, i.e., with the ":" character, even for Windows. As a result, when running terraform.exe, the location cannot be found.
I simply copied the command line "$osArchitecture = Get-OSArchitecture" to before the unzip step, i.e., to line 42...
I also noticed the same error in the YAML file 'terraform-installer.yaml' of the CI pipeline templates

  $unzipdir = Join-Path -Path $TOOLS_PATH -ChildPath "terraform_$TF_VERSION"
 if (Test-Path $unzipdir) {
   Write-Host "Terraform $TF_VERSION already installed."
   if($os -eq "windows") {
     $env:PATH = "$($unzipdir);$env:PATH"
   } else {
     $env:PATH = "$($unzipdir):$env:PATH"
   }
   Write-Host "##vso[task.setvariable variable=PATH]$env:PATH"
   return
 }

 $os = ""
 if ($IsWindows) {
   $os = "windows"
 }
 if($IsLinux) {
   $os = "linux"
 }
 if($IsMacOS) {
   $os = "darwin"
 }

Here too, the '$os' variable is accessed, even though it has not been initialized yet."

Actual Behaviour (Required)

the '$os' variable is accessed, even though it has not been initialized yet."

Steps to Reproduce (Optional)

No response

Important Factoids (Optional)

No response

References (Optional)

No response</issue_description>

<agent_instructions>Also consolidate the code that sets the path to only call it once.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Dec 17, 2025
…e and consolidating PATH setting

Co-authored-by: jaredfholgate <1612200+jaredfholgate@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bug where terraform.exe location cannot be found Fix uninitialized $os variable causing incorrect PATH separator on Windows Dec 17, 2025
Copilot AI requested a review from jaredfholgate December 17, 2025 12:02
@jaredfholgate jaredfholgate marked this pull request as ready for review December 17, 2025 12:11
@jaredfholgate jaredfholgate changed the title Fix uninitialized $os variable causing incorrect PATH separator on Windows fix: uninitialized $os variable causing incorrect PATH separator on Windows Dec 17, 2025
@jaredfholgate jaredfholgate merged commit d6b9bce into main Dec 17, 2025
9 of 10 checks passed
@jaredfholgate jaredfholgate deleted the copilot/fix-terraform-exe-location-bug branch December 17, 2025 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: terraform.exe location cannot be found

3 participants