Skip to content

Conversation

@majiayu000
Copy link

@majiayu000 majiayu000 commented Jan 2, 2026

Fixes #5286

Changes

  • Check for TFLINT_CONFIG_FILE environment variable when determining tflint config file path
  • Add debug logging when using config file from environment variable
  • Add integration test to verify environment variable is properly recognized

Config file lookup priority

  1. --config argument in hook execute command
  2. TFLINT_CONFIG_FILE environment variable
  3. .tflint.hcl file search in project directories

TODOs

Read the Gruntwork contribution guidelines.

  • I authored this code entirely myself
  • I am submitting code based on open source software (e.g. MIT, MPL-2.0, Apache) (n/a)
  • I am adding or upgrading a dependency or adapted code and confirm it has a compatible open source license (n/a)
  • Update the docs. (not needed)
  • Run the relevant tests successfully, including pre-commit checks. (CI)
  • Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Updated TFLint before_hook to honor TFLINT_CONFIG_FILE env var.

Migration Guide

N/A

When running tflint as a before_hook, the TFLINT_CONFIG_FILE environment
variable was not being recognized. This fix adds a check for the environment
variable after checking for --config argument and before searching for
.tflint.hcl files in the project hierarchy.

The config file lookup now follows this priority:
1. --config argument in hook execute command
2. TFLINT_CONFIG_FILE environment variable
3. .tflint.hcl file in project directories

Closes gruntwork-io#5286

Signed-off-by: majiayu000 <[email protected]>
@vercel
Copy link

vercel bot commented Jan 2, 2026

@majiayu000 is attempting to deploy a commit to the Gruntwork Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 2, 2026

📝 Walkthrough

Walkthrough

Added environment variable support for TFLint configuration files in Terragrunt's before_hook. The implementation introduces a new constant TFLintConfigEnvName and modifies the RunTflintWithOpts function to check the TFLINT_CONFIG_FILE environment variable when no config file is provided via hook parameters. A corresponding test validates this behavior.

Changes

Cohort / File(s) Summary
TFLint Configuration Environment Variable Support
tflint/tflint.go
Added constant TFLintConfigEnvName defining the environment variable name. Modified RunTflintWithOpts to check TFLINT_CONFIG_FILE env var as a fallback when no config file is provided in hook arguments, prioritizing it over the default project file search (.tflint.hcl).
Integration Test
test/integration_tflint_test.go
Added TestTflintConfigFileEnvVar test function that verifies Terragrunt correctly recognizes and uses the TFLINT_CONFIG_FILE environment variable when executing tflint before_hook operations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing TFLINT before_hook to recognize the TFLINT_CONFIG_FILE environment variable.
Linked Issues check ✅ Passed The PR successfully addresses issue #5286 by implementing environment variable detection, debug logging, and adding integration tests as required.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing TFLINT_CONFIG_FILE environment variable support with no out-of-scope modifications detected.
Description check ✅ Passed The pull request description covers all critical sections with specific, actionable details about changes, config file lookup priority, and test coverage.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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 (1)
tflint/tflint.go (1)

56-56: Consider making the log message more generic.

The log message says "Using .tflint.hcl file" but configFile might not be named .tflint.hcl when sourced from the environment variable or --config argument. Consider a more generic message like "Using tflint config file at %s" for clarity.

🔎 Proposed fix
-	l.Debugf("Using .tflint.hcl file in %s", configFile)
+	l.Debugf("Using tflint config file at %s", configFile)
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5a1d401 and 46d0d08.

📒 Files selected for processing (2)
  • test/integration_tflint_test.go
  • tflint/tflint.go
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go

⚙️ CodeRabbit configuration file

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.

Files:

  • tflint/tflint.go
  • test/integration_tflint_test.go
🧬 Code graph analysis (2)
tflint/tflint.go (1)
pkg/log/log.go (1)
  • Debugf (72-74)
test/integration_tflint_test.go (2)
test/integration_windows_test.go (1)
  • CopyEnvironmentWithTflint (239-264)
test/helpers/package.go (2)
  • RemoveFolder (981-989)
  • RunTerragruntCommand (1043-1047)
🔇 Additional comments (3)
tflint/tflint.go (2)

30-31: LGTM! Good practice to define the environment variable name as an exported constant.

This makes the code more maintainable and provides clear documentation of the environment variable name.


38-44: LGTM! Environment variable check correctly implements the priority order.

The logic correctly positions the environment variable check between the hook argument check and the project file search, matching the priority described in the PR objectives:

  1. --config argument (line 37)
  2. TFLINT_CONFIG_FILE environment variable (lines 39-43)
  3. Project file search (lines 46-54)

The implementation properly validates the environment variable value is non-empty and provides helpful debug logging.

test/integration_tflint_test.go (1)

199-231: LGTM! Comprehensive test validates the environment variable feature.

The test is well-structured and effectively validates the TFLINT_CONFIG_FILE environment variable support:

  1. Smart fixture choice: Uses testFixtureTflintNoConfigFile which lacks a .tflint.hcl file and would normally fail, proving that the environment variable is the source of the config.

  2. Good isolation: Uses t.Setenv to ensure the environment variable is scoped to this test only, preventing interference with other tests.

  3. Comprehensive assertions: Verifies the debug log message, the config path in the command arguments, and successful execution.

The test coverage aligns well with the implementation in tflint/tflint.go and confirms the feature works as intended.

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.

TFLINT before_hook is not recognizing TFLINT_CONFIG_FILE environment variable

1 participant