hashicorp / hashicorp/terraform

`-chdir` is not honored when attempting to use `terraform.d/plugins` directory in the current working directory

Open
#31,442 7 comments 1 reaction 0 assignees View on GitHub
cli enhancement
Dominant language
Go
Stars
49.7k
Forks
10.6k
Avg merge
21h 30m
Merged PRs (30d)
100

Description

### Terraform Version

```
$ terraform --version
Terraform v1.2.5
on linux_amd64
```

### Terraform Configuration Files

https://github.com/fnickels/terraform-plugin-bug

### Debug Output

### Expected Behavior

Terraform provider versions saved under `terraform.d/plugins` directory should be picked up when the current working directory is specified with the command line option `-chdir`.

ie:
```bash
terraform -chdir=./app1 init
```
should behave the same as
```bash
cd ./app1 && terraform init
```
when there are valid provider files under `./app1/terraform.d/plugins`

### Actual Behavior

```bash
cd ./app1 && terraform init
```
returns the expected results

```bash
terraform -chdir=./app1 init
```
gets the most recent version on the public mirror and appears to ignore the local mirror

### Steps to Reproduce

Create a file `./app1/main.tf` with the contents such as:

```
terraform {
required_providers {
# standard form
aws = {
source = "registry.terraform.io/hashicorp/aws"
version = ">= 1.0"
}
external = {
source = "registry.terraform.io/hashicorp/external"
version = ">= 1.0"
}
}
}

variable test {
type = bool
default = true
}

output mytest {
value = var.test
}
```

Create a file `./providermirror1/mirrorsource.tf` with the contents such as:

```
terraform {
required_providers {
# standard form
aws = {
source = "registry.terraform.io/hashicorp/aws"
version = "4.21.0"
}
}
}
```

Run:

```bash
#
# Build Local Mirror
#
rm -rf ./mirror1 ./app1/terraform.d
terraform -chdir=./providermirror1 \
providers mirror -platform=linux_amd64 ../mirror1/terraform/plugins
#
# copy mirror files to local default plugin directories
#
cp -r ./mirror1/terraform ./app1/terraform.d
#
# Test without -chdir and local plugin directory
#
rm -rf ./app1/.terraform ./app1/terraform.tfstate ./app1/.terraform.tfstate.lock.info ./app1/.terraform.lock.hcl
cd ./app1 && terraform init
#
# Test with -chdir and local plugin directory
#
rm -rf ./app1/.terraform ./app1/terraform.tfstate ./app1/.terraform.tfstate.lock.info ./app1/.terraform.lock.hcl
terraform -chdir=./app1 init

```

### Additional Context

### References

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior using ./app1/main.tf and ./providermirror1/mirrorsource.tf, comparing `cd ./app1 && terraform init` with `terraform -chdir=./app1 init`. Start by tracing Terraform's local provider plugin discovery and how `-chdir` changes path handling. Done means both commands select the provider files under `./app1/terraform.d/plugins` rather than the public mirror.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.