Improve missing Terraform config errors#12070
Open
preko-p wants to merge 1 commit into
Open
Conversation
Signed-off-by: preko-p <278021202+preko-p@users.noreply.github.com>
Contributor
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. |
Comment on lines
+160
to
+162
| 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)) |
Comment on lines
+187
to
+204
| 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 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.