Skip to content

fix: load format from config file #2306

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

bendrucker
Copy link
Member

Fixes handling of format inside a config {} block in a TFLint configuration file. Previously, cli.formatter.Format was set to options.Format and never considered the configuration file.

Now, NewCLI parses the config too and uses the merged config and options to set the format. The implementation is a bit hacky because a formatter needs to be constructed early to print errors.

Closes #2305

@bendrucker bendrucker requested a review from wata727 May 12, 2025 20:07
Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

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

Thank you for working on this.

This issue seems a bit trickier than we thought. As I left in the comments, we can't simply load the config first. It looks like we need to consider a different approach.

}

// Setup config
cfg, err := tflint.LoadConfig(afero.Afero{Fs: afero.NewOsFs()}, opts.Config)
Copy link
Member

Choose a reason for hiding this comment

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

We must set the log level before loading the config, otherwise debug logs will always be shown.
Doing this will remove the hack in integrationtest/recursive/recursive_test.go.

}

// Setup config
cfg, err := tflint.LoadConfig(afero.Afero{Fs: afero.NewOsFs()}, opts.Config)
Copy link
Member

Choose a reason for hiding this comment

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

If we load a config outside of withinChangedDir, --chdir will be ignored.

$ tree -a
.
├── .tflint.hcl
└── work
    └── .tflint.hcl
$ cat .tflint.hcl
config {
  format = "json"
}
$ cat work/.tflint.hcl
config {
  format = "compact"
}
$ tflint --chdir work
{"issues":[],"errors":[]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

format in config files is ignored
2 participants