Skip to content

Poor error when terraform init fails to acquire a lock on the working directory's backend settings file on Windows (e.g. on a WSL share) #35344

Open
@robpomeroy

Description

@robpomeroy

Terraform Version

Terraform v1.8.5
on windows_386

Terraform Configuration Files

Not relevant.

Debug Output

https://gist.github.com/robpomeroy/96161fd561e6a109947b636fcd522567

Expected Behavior

Terraform initialises

Actual Behavior

When running terraform init from Windows on a WSL file path, this fails with an error:

│ Error: Error locking state: [{%!s(tfdiags.Severity=69) Error acquiring the state lock Error message: 2 errors occurred:
│       * Incorrect function.
│       * open .terraform\.terraform.tfstate.lock.info: The system cannot find the file specified.
│
│
│
│ Terraform acquires a state lock to protect the state from being written
│ by multiple users at the same time. Please resolve the issue above and try
│ again. For most commands, you can disable locking with the "-lock=false"
│ flag, but this is not recommended. }]

Running from PowerShell provides a little more insight, adding to the above:

'\\wsl.localhost\AlmaLinux9\mnt\wsl\repos\[REDACTED]\Terraform'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

It looks like terraform init invokes CMD.EXE when run from PowerShell. And CMD.EXE can't handle changing to a UNC path so ends up in the wrong directory. In CMD.EXE, presumably Terraform cannot correctly interpret the UNC path.

Steps to Reproduce

  1. Store a Terraform plan in WSL (e.g. at /tmp/plan or from Windows, e.g. \\wsl.localhost\AlmaLinux9\tmp\plan).
  2. Open PowerShell prompt and navigate to the WSL directory (e.g. Set-Location \\wsl.localhost\AlmaLinux9\tmp\plan)
  3. Run terraform init

Additional Context

In my development environment, I use a junction point to map C:\Repos (link) to \\wsl.localhost\AlmaLinux9\mnt\wsl\repos (target). Hence CMD.EXE allows me to enter the WSL directory using cd C:\Repos, i.e., without needing to use a UNC path. My system is set up for a lot of cross-platform work and I keep my repositories inside WSL for performance reasons (known issue with WSL2 file performance).

Appears to be somewhat related to #29483, albeit in a different context.

References

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcliexplaineda Terraform Core team member has described the root cause of this issue in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions