Skip to content

4211 - fix: Setting correct exit code for hcl validate #4290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 25, 2025

Conversation

yhakbar
Copy link
Collaborator

@yhakbar yhakbar commented May 15, 2025

Description

Fixes #4211.

Returns a final error for hcl validate so that it users can check the overall exit code.

This is technically a breaking change, but I would advocate that we release this as part of a minor release instead of going through a full strict control process. It should be pretty simple to address, and it's typical behavior of linting tools, etc.

TODOs

Read the Gruntwork contribution guidelines.

  • Update the docs.
  • Run the relevant tests successfully, including pre-commit checks.
  • Ensure any 3rd party code adheres with our license policy or delete this line if its not applicable.
  • Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Added error for overall exit code to be non-zero if any of the files are invalid in hcl validate command.

Migration Guide

If users were previously using hcl validate expecting a zero exit code, they'll need to suppress the exit code using something like || true in their scripts. This is because the command now returns a non-zero exit code if any of the files are invalid.

Summary by CodeRabbit

  • New Features

    • Documentation for CLI flags now displays the default value when available.
  • Bug Fixes

    • The validation command now properly returns an error and a non-zero exit code when HCL validation errors are present.
  • Documentation

    • Improved documentation for the queue-excludes-file flag and --queue-excludes-file CLI option, clarifying usage, terminology, and examples.
    • Added default value information for relevant flags in the documentation.
  • Tests

    • Updated and added tests to ensure the validation command fails as expected on HCL errors and returns appropriate error messages.

Copy link

vercel bot commented May 15, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
terragrunt-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 25, 2025 7:10pm

Copy link
Contributor

coderabbitai bot commented May 15, 2025

📝 Walkthrough

Walkthrough

The changes ensure that the terragrunt hcl validate command returns a non-zero exit code when HCL validation errors are present. This is achieved by updating the error handling logic in the validation command, adjusting related tests to expect errors, and updating documentation and flag schemas to clarify and demonstrate this behavior.

Changes

File(s) Change Summary
cli/commands/hcl/validate/validate.go Modified RunValidate to return a non-nil error if HCL diagnostics are present, ensuring a non-zero exit code.
test/integration_test.go, test/integration_deprecated_test.go Updated tests to expect errors for invalid HCL; added a test to check for non-zero exit code and error message.
docs-starlight/src/components/Flag.astro, docs-starlight/src/content.config.ts Added support for displaying a flag's default value in documentation components and schemas.
docs-starlight/src/data/flags/queue-excludes-file.mdx Updated documentation for queue-excludes-file flag, clarified usage, added default value, and improved examples.
docs/_docs/04_reference/02-cli-options.md Revised CLI option documentation for --queue-excludes-file to reflect updated terminology and usage examples.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant Validator

    User->>CLI: terragrunt hcl validate
    CLI->>Validator: Run HCL validation
    Validator-->>CLI: Return diagnostics (if any)
    alt Diagnostics exist
        CLI-->>User: Output diagnostics, return error (non-zero exit code)
    else No diagnostics
        CLI-->>User: Output success, return nil (zero exit code)
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure terragrunt hcl validate returns non-zero exit code on validation errors (#4211)
Update terminal output and documentation to clarify behavior (#4211)

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Added support for displaying flag default values in documentation components and schemas (docs-starlight/src/components/Flag.astro, docs-starlight/src/content.config.ts, docs-starlight/src/data/flags/queue-excludes-file.mdx) These documentation enhancements are not directly related to the exit code behavior of terragrunt hcl validate.

Possibly related PRs

Suggested reviewers

  • levkohimins

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@yhakbar yhakbar force-pushed the 4211/fix/setting-correct-exit-code-for-validate branch from 38290d5 to 1831163 Compare May 27, 2025 14:34
@yhakbar yhakbar marked this pull request as ready for review June 25, 2025 19:57
@yhakbar yhakbar requested review from denis256 and wakeful as code owners June 25, 2025 19:57
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (8)
cli/commands/hcl/validate/validate.go (2)

110-110: Address lint warning for dynamic error creation.

The linter suggests using wrapped static errors instead of dynamic errors.

Consider refactoring to use a wrapped static error:

-		stackErr = errors.Errorf("%d HCL validation error(s) found", len(diags))
+		stackErr = errors.Wrap(errors.New("HCL validation error(s) found"), fmt.Sprintf("%d validation errors", len(diags)))

Alternatively, you could define a static error variable and wrap it with context.


17-17: Modernize import statement.

The linter suggests replacing the experimental import with the standard library equivalent.

-	"golang.org/x/exp/slices"
+	"slices"

The slices package is now part of the standard library in Go 1.21+.

test/integration_deprecated_test.go (1)

62-62: Address lint warning for fmt.Printf usage.

The linter flags the use of fmt.Printf as forbidden by pattern.

Consider using the test logger instead:

-	fmt.Printf("STDERR is %s.\n STDOUT is %s", errOutput, output)
+	t.Logf("STDERR is %s.\n STDOUT is %s", errOutput, output)

This follows Go testing best practices and avoids the linter warning.

docs/_docs/04_reference/02-cli-options.md (2)

1786-1788: Grammar & clarity polish for the flag description
Minor wording tweaks improve readability and match the phrasing used elsewhere.

-Path to a file with a list of directories that need to be excluded when running `run --all` commands, by default `.terragrunt-excludes`. Units in these directories will be
-excluded during execution of the commands. If a relative path is specified, it should be relative from
-[--working-dir](#working-dir). This will only exclude the module, not its dependencies.
+Path to a file that lists directories to exclude when running `run --all` commands.  
+The default file is `.terragrunt-excludes`.  
+Units in these directories will be excluded during execution.  
+If a relative path is specified, it must be relative **to** [--working-dir](#working-dir).  
+This flag excludes only the module itself, not its dependencies.

1790-1794: Example block: explicitly mention non-zero exit code
The surrounding text explains why || true is needed, but calling it out in the example comment will save copy-pasting readers a subtle gotcha.

-terragrunt run --all plan --queue-excludes-file <(terragrunt hcl validate --show-config-path || true)
+# `hcl validate` returns a non-zero exit code on errors, so we append `|| true`
+terragrunt run --all plan \
+  --queue-excludes-file <(terragrunt hcl validate --show-config-path || true)
docs-starlight/src/data/flags/queue-excludes-file.mdx (3)

3-5: Mirror wording changes made in the Jekyll docs
Keeps both documentation paths in sync and fixes the same minor grammatical issue.

-description: Path to a file with a list of directories that need to be excluded when running `run --all` commands.
+description: Path to a file that lists directories to exclude when running `run --all` commands.

10-11: “relative from” → “relative to”
Same grammar fix as in the primary docs.

-If a relative path is specified, it should be relative from
+If a relative path is specified, it should be relative to

15-17: Optional: clarify why || true is present
Adding a short inline comment keeps the Starlight snippet self-explanatory, matching the suggestion in the Jekyll docs.

-terragrunt run --all plan --queue-excludes-file <(terragrunt hcl validate --show-config-path || true)
+# hcl validate exits non-zero on validation failures, so we ignore the code here
+terragrunt run --all plan --queue-excludes-file <(terragrunt hcl validate --show-config-path || true)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4b8a253 and 4924202.

📒 Files selected for processing (7)
  • cli/commands/hcl/validate/validate.go (1 hunks)
  • docs-starlight/src/components/Flag.astro (2 hunks)
  • docs-starlight/src/content.config.ts (1 hunks)
  • docs-starlight/src/data/flags/queue-excludes-file.mdx (1 hunks)
  • docs/_docs/04_reference/02-cli-options.md (1 hunks)
  • test/integration_deprecated_test.go (1 hunks)
  • test/integration_test.go (3 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
`docs-starlight/**/*.astro`: Review the Astro code in the `docs-starlight` direc...

docs-starlight/**/*.astro: Review the Astro code in the docs-starlight directory for quality and correctness. Make sure that the Astro code follows best practices and is easy to understand, maintain, and follows best practices. When possible, suggest improvements to the Astro code to make it better.

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • docs-starlight/src/components/Flag.astro
`**/*.go`: Review the Go code for quality and correctness. Make sure that the Go code follows best practices, is performant, and is easy to understand and maintain.

**/*.go: Review the Go code for quality and correctness. Make sure that the Go code follows best practices, is performant, and is easy to understand and maintain.

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • cli/commands/hcl/validate/validate.go
  • test/integration_deprecated_test.go
  • test/integration_test.go
`docs-starlight/**/*.md*`: Review the documentation for clarity, grammar, and sp...

docs-starlight/**/*.md*: Review the documentation for clarity, grammar, and spelling. Make sure that the documentation is easy to understand and follow. There is currently a migration underway from the Jekyll based documentation in docs to the Starlight + Astro based documentation in docs-starlight. Make sure that the docs-starlight documentation is accurate and up-to-date with the docs documentation, and that any difference between them results in an improvement in the docs-starlight documentation.

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • docs-starlight/src/data/flags/queue-excludes-file.mdx
`docs/**/*.md`: Review the documentation for clarity, grammar, and spelling. Mak...

docs/**/*.md: Review the documentation for clarity, grammar, and spelling. Make sure that the documentation is easy to understand and follow. There is currently a migration underway from the Jekyll based documentation in docs to the Starlight + Astro based documentation in docs-starlight. Whenever changes are made to the docs directory, ensure that an equivalent change is made in the docs-starlight directory to keep the docs-starlight documentation accurate.

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • docs/_docs/04_reference/02-cli-options.md
🧬 Code Graph Analysis (1)
test/integration_test.go (2)
test/helpers/package.go (3)
  • CleanupTerraformFolder (713-720)
  • CopyEnvironment (84-97)
  • RunTerragruntCommandWithOutput (828-832)
util/file.go (1)
  • JoinPath (480-482)
🪛 GitHub Actions: Strict Lint
cli/commands/hcl/validate/validate.go

[warning] 455-455: Lint warning (err113): do not define dynamic errors, use wrapped static errors instead.


[warning] 17-17: Lint warning (exptostd): Import statement 'golang.org/x/exp/slices' can be replaced by 'slices'.

test/integration_deprecated_test.go

[warning] 62-903: Lint warning (forbidigo): use of fmt.Printf forbidden by pattern.

test/integration_test.go

[warning] 3804-3804: Lint warning (dogsled): declaration has 3 blank identifiers.


[warning] 1407-2492: Lint warning (forcetypeassert): type assertion must be checked in multiple locations.

🔇 Additional comments (8)
docs-starlight/src/content.config.ts (1)

55-55: LGTM! Schema extension follows best practices.

The addition of the optional defaultVal property to the flags schema is well-implemented, following Zod conventions and maintaining consistency with the existing schema structure.

docs-starlight/src/components/Flag.astro (2)

14-14: LGTM! Proper extraction of new property.

The defaultVal property is correctly extracted from the flag entry data alongside other properties.


24-28: LGTM! Well-implemented conditional rendering.

The default value badge is properly implemented with:

  • Appropriate conditional rendering using {defaultVal && (...)}
  • Consistent styling with existing badges using "tip" variant
  • Logical positioning between Type and Aliases sections
cli/commands/hcl/validate/validate.go (1)

104-111: LGTM! Core logic correctly implements the PR objective.

The error handling logic properly ensures a non-zero exit code when HCL validation errors are present. The error message format is clear and informative, showing the count of validation errors found.

test/integration_deprecated_test.go (1)

33-33: LGTM! Test correctly updated to match new validation behavior.

The change from require.NoError(t, err) to require.Error(t, err) properly aligns with the new HCL validation behavior that returns errors when validation fails.

test/integration_test.go (3)

705-705: LGTM: Correctly updated test expectation for validation failures.

The change from require.NoError(t, err) to require.Error(t, err) is appropriate since the terragrunt hcl validate command should now return a non-zero exit code when HCL validation errors are present, aligning with the PR objective.


715-731: LGTM: Excellent addition of explicit exit code verification test.

This new test function provides dedicated coverage for the core PR objective - ensuring that terragrunt hcl validate returns a non-zero exit code when HCL validation errors are found. The test structure is solid:

  • Uses the same test fixture with known invalid HCL
  • Explicitly verifies that an error is returned (non-zero exit code)
  • Validates the error message contains the expected text
  • Includes helpful comments explaining the test's purpose

The test name clearly describes its intent and the implementation is straightforward and focused.


746-746: LGTM: Consistent test expectation update.

This change maintains consistency with the other HCL validation test updates, correctly expecting an error when running terragrunt hcl validate on invalid HCL configuration with the --show-config-path flag.

@yhakbar yhakbar merged commit e7bce62 into main Jun 25, 2025
69 of 75 checks passed
@yhakbar yhakbar deleted the 4211/fix/setting-correct-exit-code-for-validate branch June 25, 2025 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terragrunt hcl validate returns zero exit code on errors?
2 participants