hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.5k stars 9.52k forks source link

provider_meta typo crashes in terraform init #35835

Open romanini-ciandt opened 1 day ago

romanini-ciandt commented 1 day ago

Terraform Version

Terraform v1.9.1

Terraform Configuration Files

terraform {
  required_version = ">= 1.5.7"

  # Intentional typo below to force a crash. The correct would be "google-beta" instead.
  provider_meta "google_beta" { # 
    hello = "world"
  }
}

Debug Output

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Terraform crashed! This is always indicative of a bug within Terraform.
Please report the crash with Terraform[1] so that we can fix this.

When reporting bugs, please include your terraform version, the stack trace
shown below, and any additional information which may help replicate the issue.

[1]: https://github.com/hashicorp/terraform/issues

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

panic: must contain only letters, digits, and dashes, and may not use leading or trailing dashes
goroutine 1 [running]:
runtime/debug.Stack()
        runtime/debug/stack.go:24 +0x5e
github.com/hashicorp/terraform/internal/logging.PanicHandler()
        github.com/hashicorp/terraform/internal/logging/panic.go:84 +0x18b
panic({0x2e73cc0?, 0xc0003e07d0?})
        runtime/panic.go:770 +0x132
github.com/hashicorp/terraform/internal/addrs.MustParseProviderPart({0xc00074a170?, 0xc0008ca6e8?})
        github.com/hashicorp/terraform/internal/addrs/provider.go:193 +0x45
github.com/hashicorp/terraform/internal/addrs.NewDefaultProvider(...)
        github.com/hashicorp/terraform/internal/addrs/provider.go:89
github.com/hashicorp/terraform/internal/addrs.ImpliedProviderForUnqualifiedType({0xc00074a170?, 0x7bbf85a79ef8?})
        github.com/hashicorp/terraform/internal/addrs/provider.go:81 +0x10e
github.com/hashicorp/terraform/internal/configs.(*Module).ImpliedProviderForUnqualifiedType(...)
        github.com/hashicorp/terraform/internal/configs/module.go:694
github.com/hashicorp/terraform/internal/configs.(*Module).ProviderForLocalConfig(0x0?, {{0xc00074a170?, 0x200025?}, {0x0?, 0x314ab00?}})
        github.com/hashicorp/terraform/internal/configs/module.go:680 +0xaf
github.com/hashicorp/terraform/internal/configs.(*Module).appendFile(0xc0005c2000, 0xc0007681a0)
        github.com/hashicorp/terraform/internal/configs/module.go:269 +0xda8
github.com/hashicorp/terraform/internal/configs.NewModule({0xc000133db0, 0x1, 0x1?}, {0x0, 0x0, 0x1?})
        github.com/hashicorp/terraform/internal/configs/module.go:169 +0x4d5
github.com/hashicorp/terraform/internal/configs.NewModuleWithTests(...)
        github.com/hashicorp/terraform/internal/configs/module.go:102
github.com/hashicorp/terraform/internal/configs.(*Parser).LoadConfigDirWithTests(0xc000a150c0, {0x3dc2468, 0x1}, {0x3524347?, 0x0?})
        github.com/hashicorp/terraform/internal/configs/parser_config_dir.go:73 +0x39b
github.com/hashicorp/terraform/internal/command.(*Meta).loadSingleModuleWithTests(0xc0006828c0, {0xc000082004?, 0xc00042ba70?}, {0x3524347, 0x5})
        github.com/hashicorp/terraform/internal/command/meta_config.go:108 +0xac
github.com/hashicorp/terraform/internal/command.(*InitCommand).Run(0xc0006828c0, {0xc000118030?, 0x0?, 0x0?})
        github.com/hashicorp/terraform/internal/command/init.go:163 +0xe7e
github.com/hashicorp/cli.(*CLI).Run(0xc0003ec280)
        github.com/hashicorp/cli@v1.1.6/cli.go:265 +0x5b8
main.realMain()
        github.com/hashicorp/terraform/main.go:339 +0x1e6b
main.main()
        github.com/hashicorp/terraform/main.go:64 +0x13

Expected Behavior

I would expect a handled error message or a successful run when I run terraform init and the configuration files has a provider_meta with an underline (_), or a dot (.) within a string.

Actual Behavior

Currently, if you update the above's code to provider_meta "google-beta-typooooooo" and run a terraform init, it will return Terraform has been successfully initialized!. A successful run.

But a simple typo, provider_meta "google_beta" instead of provider_meta "google-beta" will cause a terraform crash error, and it won't be clear to users why this crash is happening. In a large codebase it can be hard to figure it out why this crash is happening.

Steps to Reproduce

  1. terraform init

Additional Context

No response

References

No response

crw commented 1 day ago

Thanks for this report!