git-hooks.nix icon indicating copy to clipboard operation
git-hooks.nix copied to clipboard

golangci-lint fails because it cannot find `go` in $PATH

Open shackra opened this issue 1 year ago • 5 comments

This happens when running nix flake check.

golangci-lint............................................................Failed
- hook id: golangci-lint
- exit code: 3

WARN Failed to discover go env: failed to run 'go env -json GOCACHE GOROOT': exec: "go": executable file not found in $PATH
ERRO Running error: context loading failed: failed to load packages: failed to load packages: failed to load with go/packages: err: go command required, not found: exec: "go": executable file not found in $PATH: stderr:

~~This may be related to the fact that flakes do not admit environment variables~~. It goes well as a pre-commit-hook, thought.

shackra avatar Jun 20 '24 22:06 shackra

still failing :(

shackra avatar Oct 18 '24 23:10 shackra

same issue here

using

$ nix develop .#git-hooks --ignore-environment

I get into an env that has go, but I'm fairly certain it's from the previous shell

using

$ nix develop .#git-hooks --unset PATH

I can no longer use go

for reference, the git-hooks dev shell is just an alias for the one from pre-commit:

devShells.git-hooks = config.pre-commit.devShell;

matdibu avatar Nov 12 '24 21:11 matdibu

using

golangci-lint = {
  enable = true;
  extraPackages = [ pkgs.go pkgs.which ];
};

I get the go binary which looks correct

$ which go
/nix/store/jyhpd4hgq672bm4m4s1r25zm1bx8vd44-go-1.23.2/bin/go

but nix flake check still fails because it can't find go during golangci-lint

matdibu avatar Nov 12 '24 21:11 matdibu

I ended up writing a custom hook for all go tools, since I also needed to run some codegen before running golangci-lint

x-pre-commit-go =
  let
    pkg = pkgs.writeShellApplication {
      name = "x-pre-commit-go";
      runtimeInputs = [
        pkgs.flatbuffers
        pkgs.go
        pkgs.golangci-lint
        pkgs.just
        pkgs.jq
        pkgs.protobuf
        pkgs.protoc-gen-go
        pkgs.protoc-gen-go-grpc
        pkgs.revive
      ];
      text = ''
        just gen-go
        just golangci-lint
        # just go-test
        # just go-vet
        # just go-revive
        # just go-staticcheck
      '';
    };
  in
  {
    enable = true;
    package = pkg;
    entry = "${lib.getExe pkg}";
    pass_filenames = false;
  };

I'm using just because I'm working with a big and complex multi-language monorepo and I like to avoid writing bespoke scripts in configs

@shackra maybe this will be useful to you too

matdibu avatar Jan 28 '25 14:01 matdibu

extraPackages = [ pkgs.go pkgs.which ]; but nix flake check still fails because it can't find go during golangci-lint

@matdibu Adding it to extraPackages works fine for me, as it just adds it to the PATH of the check derivation. Can you still reproduce the failure with that specifically? I would be interested in figuring out what's going on. (Especially if it works now but not in November.)

winterqt avatar Apr 01 '25 23:04 winterqt