nvim-lint icon indicating copy to clipboard operation
nvim-lint copied to clipboard

terraform/tofu linter sets chdir argument to a submodule instead of terraform root dir

Open serpro69 opened this issue 2 weeks ago • 0 comments

Hi,

After enabling terraform/tofu linters I started seeing a lot of diagnostic errors saying: This module is not yet installed. Run "terraform init" to install all modules required by this configuration. terraform validate, whereas simply running terraform validate from the root of the config directory worked just fine w/o reporting any errors.

I thought it might be because I'm actually using tofu, so then I changed to use tofu linter in the for terraform files, but the error was essentially the same.

I then looked at the output of :lua print(vim.inspect(require("lint").linters.tofu())), and noticed that the linter command was using the current file path for chdir argument, instead of e.g. using root-dir as defined by Lsp.

{
  append_fname = false,
  args = { "-chdir=terraform/shared/commons", "validate", "-json" },
  cmd = "tofu",
  ignore_exitcode = true,
  parser = <function 1>,
  stdin = false,
  stream = "both"
}

Running the same command locally naturally produced the same error, because commons in this case is a sub-module and does not contain a .terraform directory.

The lsp, for example, correctly detects the root dir:

vim.lsp: Active Clients ~
- tflint (id: 1)
  - Version: ? (no serverInfo.version response)
  - Root directory: ~/Projects/test/terraform/shared
  - Command: { "tflint", "--langserver" }
  - Settings: {}
  - Attached buffers: 8
- terraformls (id: 2)
  - Version: 0.38.3
  - Root directory: ~/Projects/test/terraform/shared
  - Command: { "terraform-ls", "serve" }
  - Settings: {}
  - Attached buffers: 8

but it seems like it's not used by the linter: https://github.com/mfussenegger/nvim-lint/blob/ebe535956106c60405b02220246e135910f6853d/lua/lint/linters/tofu.lua#L10

Is this something that can be fixed in this plugin, or is the only option I have is to override the linter args myself?

serpro69 avatar Dec 08 '25 11:12 serpro69