treefmt-nix icon indicating copy to clipboard operation
treefmt-nix copied to clipboard

New formatter: Add golangci-lint support

Open Dauliac opened this issue 3 months ago • 4 comments

treefmt-nix currently doesn't support golangci-lint, a popular and comprehensive Go linter. Go developers have to use separate tools or less comprehensive linters.

Proposed Solution

Add programs/golangci-lint.nix with comprehensive configuration options, following patterns from typos.nix and prettier.nix:

  • enableLinters/disableLinters: Control which linters to run
  • configFile: Path to custom config file
  • timeout: Set analysis timeout
  • concurrency: Configure CPU usage
  • buildTags: Specify build tags
  • tests: Control test file analysis
  • fastOnly: Run only fast linters

Implementation

I can implement this new formatter with:

  • Full golangci-lint CLI option support
  • Follows established treefmt-nix patterns
  • Comprehensive configuration options
  • Auto-fixing capabilities (--fix flag)

This would allow users to configure golangci-lint declaratively:

programs.golangci-lint = {
  enable = true;
  enableLinters = [ "govet" "errcheck" "staticcheck" ];
  timeout = "5m";
  tests = false;
};

Dauliac avatar Sep 27 '25 05:09 Dauliac

As treefmt invokes golangci-lint with the individual files to be linted / formatted, I run into errors like this:

level=error msg="[linters_context] typechecking error: named files must all be in one directory; have api-key-service/docs and api-key-service/gen-token"

Perhaps there should be an option to ignore the files passed by treefmt and just lint on ... (which is Go's notation for a recursive tree starting from .)?

msanft avatar Nov 04 '25 15:11 msanft

When wrapping the golangci-lint formatter in a custom script that uses ... as the argument to the golangci-lint binary, I run into the following error:

Error: parallel golangci-lint is running
The command is terminated due to an error: parallel golangci-lint is running

treefmt seems to make the assumption that running its formatters in parallel is safe, which golangci-lint doesn't like.

msanft avatar Nov 04 '25 15:11 msanft

treefmt seems to make the assumption that running its formatters in parallel is safe, which golangci-lint doesn't like.

golangci-lint doesn't comply with the treefmt spec: https://treefmt.com/formatters-spec/

katexochen avatar Nov 04 '25 15:11 katexochen

golangci-lint doesn't comply with the treefmt spec: https://treefmt.com/formatters-spec/

Yeah. I wonder why it's been added then :D

msanft avatar Nov 04 '25 15:11 msanft