23

When attempting to run terraform init as a task in an Azure Pipeline, it errors stating

spawn C:\hostedtoolcache\windows\terraform\0.12.7\x64\terraform.exe ENOENT

The installation appears fine, as basic functionality is verified during the install step (terraform version)

Relevant Pipeline Tasks

...
  - task: TerraformInstaller@0
    displayName: 'Install Terraform 0.12.7'
    inputs:
      terraformVersion: 0.12.7
  - task: TerraformTaskV1@0
    displayName: 'Terraform : init'
    inputs:
        command: 'init'
        workingDirectory: '$(System.DefaultWorkingDirectory)/Terraform/terraform'
...

Install Terraform 0.12.7

...
Verifying Terraform installation...
C:\hostedtoolcache\windows\terraform\0.12.7\x64\terraform.exe version
Terraform v0.12.7

Your version of Terraform is out of date! The latest version
is 0.12.19. You can update by downloading from www.terraform.io/downloads.html
Finishing: Install Terraform 0.12.7

Terraform : init

...
C:\hostedtoolcache\windows\terraform\0.12.7\x64\terraform.exe validate
##[error]Error: There was an error when attempting to execute the process 'C:\hostedtoolcache\windows\terraform\0.12.7\x64\terraform.exe'. This may indicate the process failed to start. Error: spawn C:\hostedtoolcache\windows\terraform\0.12.7\x64\terraform.exe ENOENT
Finishing: Terraform : validate

Many other users reported success fixing this by adding a checkout step, but the pipeline automatically does this (presumably previous versions did not), and manually adding it had no effect (actually took 2s longer due to different options).

ti7
  • 16,375
  • 6
  • 40
  • 68

2 Answers2

36

Turns out the working directory path was incorrect, as the directory structure had been changed.

Changing all the named working directories from Terraform/terraform to just terraform corrected the issue.

Presumably both in this and cases where checkout was not performed, Terraform simply cannot locate main.tf, but the error is missing or lost.

ti7
  • 16,375
  • 6
  • 40
  • 68
  • 2
    For me, this occurred when no input artifact was specified on the release pipeline, and also when the working directory on the `Terraform init` task did not match the input artifact's "source alias". I had to set the path to `$(System.DefaultWorkingDirectory)/[InputArtifactSourceAlias]/drop/[TfScriptDir]`. – Timo Oct 15 '20 at 09:45
1

I stumbled upon this error when I renamed the release pipeline artifact and did not re-push the code, hence causing the cache not to invalidate.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/28304949) – Jeroen Steenbeeke Feb 12 '21 at 07:40
  • 1
    @JeroenSteenbeeke: Why do you say that? It clearly suggests a possible cause (renaming the release pipeline) and solution (repushing the code). Seems like an answer to me. – Jeremy Caney Feb 12 '21 at 07:56
  • Your answer showed up in the "low quality post" moderation queue, and in my opinion this should have been a comment on the question ("have you tried renaming?") rather than an answer. The message I posted is one of a set of canned responses offered by Stackoverflow – Jeroen Steenbeeke Feb 12 '21 at 08:08
  • 4
    @JeroenSteenbeeke agree this should have been a comment. I don't have enough reputation to post comments. I just wanted to help, spend an hour figuring this myself. – Filip Výborný Feb 13 '21 at 09:56
  • In my case worked, just changed back the name and it became working – Gleb S Apr 24 '23 at 13:32