Improve missing Terraform config errors#12070
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR tightens Terraform module inspection by detecting missing Terraform configuration files up front and improving test coverage for missing/invalid module scenarios.
Changes:
- Added a pre-check for presence of
.tf/.tf.jsonfiles before callingtfconfig.LoadModule. - Introduced a standardized error message for missing module configuration.
- Expanded unit tests to cover missing submodules, missing config, and invalid config cases with optional exact error matching.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pkg/recipes/terraform/module.go | Adds hasTerraformConfigFiles and returns a clearer “config not found” error before attempting module load. |
| pkg/recipes/terraform/module_test.go | Extends table-driven tests with setup hooks and exact error assertions for new failure modes. |
| moduleDir := filepath.Join(workingDir, moduleRootDir, "test-module-no-config") | ||
| require.NoError(t, os.MkdirAll(filepath.Join(moduleDir, "main.tf"), 0755)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(moduleDir, "README.md"), []byte("no terraform configuration"), 0644)) |
| entries, err := os.ReadDir(moduleDir) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| for _, entry := range entries { | ||
| info, err := entry.Info() | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if !info.Mode().IsRegular() { | ||
| continue | ||
| } | ||
|
|
||
| name := entry.Name() | ||
| if strings.HasSuffix(name, ".tf") || strings.HasSuffix(name, ".tf.json") { | ||
| return true, nil | ||
| } | ||
| } |
Signed-off-by: preko-p <278021202+preko-p@users.noreply.github.com>
6d225dc to
03c5165
Compare
|
Thanks, addressed both review comments in a follow-up push. What changed:
Verification run locally through the Go 1.26.4 Docker image:
|
Signed-off-by: preko-p <278021202+preko-p@users.noreply.github.com>
1b8c09b to
89ad95a
Compare
Radius functional test overviewClick here to see the test run details
Test Status⌛ Building Radius and pushing container images for functional tests... |
DariuszPorowski
left a comment
There was a problem hiding this comment.
good, targeted fix! thank you
@willtsai @zachcasper @nicolejms The hardcoded .terraform/modules breaks if TF_DATA_DIR is set (tfexec inherits the pod env). Pre-existing, out of scope here, but flagging as latent tech debt.
Description
This improves Terraform recipe inspection so a missing downloaded module directory, missing subdirectory, or module directory without Terraform configuration files returns an actionable error:
The Terraform configuration in location <templatePath> is not found.Malformed Terraform configuration still flows through the existing
tfconfig.LoadModulediagnostics aserror loading the module: ....Type of change
Fixes: #9477
Contributor checklist
Please verify that the PR meets the following requirements, where applicable:
eng/design-notes/in this repository, if new APIs are being introduced.Verification
git diff --checkdocker run --rm golang:1.26.4 go versiondocker run --rm --user "$(id -u):$(id -g)" -v "$PWD":/repo -w /repo golang:1.26.4 gofmt -w pkg/recipes/terraform/module.go pkg/recipes/terraform/module_test.godocker run --rm --user "$(id -u):$(id -g)" -v "$PWD":/repo -w /repo -e HOME=/tmp -e GOCACHE=/tmp/gocache -e GOMODCACHE=/tmp/gomodcache golang:1.26.4 go test ./pkg/recipes/terraform -run Test_InspectTFModuleConfigdocker run --rm --user "$(id -u):$(id -g)" -v "$PWD":/repo -w /repo -e HOME=/tmp -e GOCACHE=/tmp/gocache -e GOMODCACHE=/tmp/gomodcache golang:1.26.4 go test ./pkg/recipes/terraform ./pkg/recipes/driver/terraformAI assistance disclosure
This PR was prepared with AI assistance and reviewed locally before submission.