Skip to content

Can terraform init fully initialize (for tests) if no .tf files exist the current working directory? #35040

@brettcurtis

Description

@brettcurtis

Terraform Version

Terraform v1.8.1
on linux_amd64
+ provider registry.terraform.io/hashicorp/google v5.25.0

Use Cases

With terraform test you can now run tests from the root of a repo even if the root has no .tf files. A directory structure like this can't run tests unless an empty.tf file is added because we can't fully initialize with terraform init.

image

This is a current test/WA:

❯ terraform test -verbose
╷
│ Error: Module not installed
│ 
│   on tests/default.tftest.hcl line 4, in run "test":
│    4:   module {
│ 
│ This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
╵
❯ terraform init
Terraform initialized in an empty directory!

The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.
❯ terraform test -verbose
╷
│ Error: Module not installed
│ 
│   on tests/default.tftest.hcl line 4, in run "test":
│    4:   module {
│ 
│ This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
╵
❯ touch empty.tf
❯ terraform init

Initializing the backend...
Initializing modules...
- test.tests.default.test in tests/fixtures/default
- test.tests.default.test.test in global

Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Installing hashicorp/google v5.25.0...
- Installed hashicorp/google v5.25.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
❯ terraform test
tests/default.tftest.hcl... in progress
  run "test"... pass
tests/default.tftest.hcl... tearing down
tests/default.tftest.hcl... pass

Success! 1 passed, 0 failed.

Attempted Solutions

add empty.tf, change directory structure.

Proposal

No response

References

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions