Skip to content

Conversation

@mothzarella
Copy link

Both parse and fn provider functions now receive fname and cwd parameters to provide file path and working directory context.

Copilot AI review requested due to automatic review settings January 28, 2026 14:07
@mothzarella mothzarella changed the title feat: Pass fname and cwd to parse function (#246) feat: Pass fname and cwd to parse function Jan 28, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds file path and working directory context to linter provider functions by passing fname and cwd parameters to both parse and fn functions. This enhancement allows custom linters to access file location information when processing lint results or generating lint output.

Changes:

  • Updated lint.parse() calls to include fname and cwd parameters (lines 107, 122)
  • Updated lint.fn() call to include fname and cwd parameters (line 120)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@xiaoshihou514
Copy link
Member

Instead of fname, would bufid be a better choice? 🤔 Seems much more natural as an api.

Could you remind me again why you need cwd?

@mothzarella
Copy link
Author

As I wrote in the issue: "parse runs inside async.run, which means it's in a fast event context where you can't call neovim api." By passing only bufnr, you wouldn't be able to obtain the filepath that the linter is actually operating on, you'd need to call vim.api.nvim_buf_get_name(bufnr) inside the parser, which isn't allowed in that context.

Example with terraform validate -json

{
  "format_version": "1.0",
  "valid": true,
  "error_count": 0,
  "warning_count": 2075,
  "diagnostics": [
    {
      "severity": "warning",
      "summary": "Deprecated Resource",
      "detail": "This resource is deprecated...",
      "address": "module.some.module.address.abcd.name",
      "range": {
        "filename": ".terraform/modules/some.address/main.tf",
        ...
      }
    }
    ... other 2074 warnings
  ]
}
  • NOTE: Without fname filtering, all 2075 warnings would show up in every buffer.

Regarding cwd: some linters output paths relative to the working directory (especially in CI or when run recursively). Having cwd available allows the parser to resolve relative paths correctly and match them against the buffer's absolute path:
local rpath = fname:sub(#cwd + 2), but you can just match fname with diagnostics.range.filename ig.

@xiaoshihou514
Copy link
Member

xiaoshihou514 commented Jan 28, 2026

How about a get state function that runs outside of the coroutine?

@mothzarella
Copy link
Author

You cannot safely call neovim api inside the fast event context vim.system/async.run cuz it causes E5560 or undefined behaviour.

A callable outside the coroutine only helps if the state is already available or the parsing logic is refactored into a pure sync function. Trying to obtain the path with vim.schedule + coroutine yield is technically possible but fragile cuz it changes execution context/timing and introduces race issues.

@mothzarella
Copy link
Author

Act I think you can do it with a small cache populated by autocmds (e.g. BufEnter/BufWritePre) and have parse read that, but the downsides are race/timing conditions where the cache may not be populated before the linter runs, stale state that needs invalidation on writes/renames/cwd changes and in multiroot projects, and potential memory leaks from forgotten bufnr entries. 🙏🏻

@xiaoshihou514
Copy link
Member

Ah you are right it's a bit awkward given the current impl

@xiaoshihou514
Copy link
Member

xiaoshihou514 commented Jan 30, 2026

Can you update the docs? Just change the type signature. (optionally add how you used this feature for tf)

Will merge afterwards.

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.

2 participants