cachix / devenv

Fast, Declarative, Reproducible, and Composable Developer Environments
https://devenv.sh
Apache License 2.0
3.95k stars 295 forks source link

pre-commit govet uses different go compiler than set #511

Open knl opened 1 year ago

knl commented 1 year ago

Describe the bug

I have the following in my devenv.nix:

{ pkgs, ... }:

{
  languages.go.enable = true;
  languages.go.package = pkgs.go_1_20;

  pre-commit.hooks = {
    govet.enable = true;
    gofmt.enable = true;
    gotest.enable = true;
  };
}

When I run devenv shell pre-commit run -a, I get a bunch of error from govet in the form:

# runtime/internal/atomic
compile: version "go1.20.1" does not match go tool version "go1.19.6"
# sync/atomic
compile: version "go1.20.1" does not match go tool version "go1.19.6"

If I look at the pre-commit script for govet, it has:

#!/nix/store/w9lvybijj4w7ykd2z12zfvg7icjc4q89-bash-5.2-p15/bin/bash
set -e
for dir in $(echo "$@" | xargs -n1 dirname | sort -u); do
  /nix/store/b1axwl8bzxdnk9n13j305wqv72zdsfay-go-1.19.6/bin/go vet ./"$dir"
done

This is clearly not the right go version that I enabled in devenv.nix.

To Reproduce Please provide an sscce by creating a gist using devenv.nix, devenv.yaml and optionally devenv.lock.

Version

Paste here the output of $ devenv version.

devenv: 0.6.2
thenonameguy commented 1 year ago

The go version determined is here: https://github.com/cachix/pre-commit-hooks.nix/blob/29dbe1efaa91c3a415d8b45d62d48325a4748816/modules/hooks.nix#L875 Which in turn, is passed in to the tools map here: https://github.com/cachix/pre-commit-hooks.nix/blob/29dbe1efaa91c3a415d8b45d62d48325a4748816/nix/tools.nix#L64 Which comes from the pkgs.go attribute.

Devenv language modules should probably override the tools.$LANG package, with the .package version specified.

thenonameguy commented 1 year ago

@knl until the above is done, you can proceed with:

{ pkgs, config, lib, ... }:
{
  languages.go.enable = true;
  languages.go.package = pkgs.go_1_20;

  pre-commit = { 
    tools = { go = lib.mkForce config.languages.go.package; };
    hooks = {
      govet.enable = true;
      gofmt.enable = true;
      gotest.enable = true;
    };
  };
}

Proof:

tail -n +3 .pre-commit-config.yaml | jq '.repos[0].hooks[] | .entry' | xargs grep go-1
/nix/store/hjrlnifv8509wy4pyqxnbpin8qyb7ffc-precommit-gofmt:    if ! /nix/store/3wpbn0nv1yhb47njlb3z3vicj20cp2mj-go-1.20.2/bin/gofmt -l -w "$file" 2>&1
/nix/store/5mkhiyab4r3l59lgjlbfrzzs6jrq5rlj-precommit-gotest:    /nix/store/3wpbn0nv1yhb47njlb3z3vicj20cp2mj-go-1.20.2/bin/go test "./$dir"
/nix/store/qsm3h7j7qx6h5finybcz3s8rxfrjgcfc-precommit-govet:  /nix/store/3wpbn0nv1yhb47njlb3z3vicj20cp2mj-go-1.20.2/bin/go vet ./"$dir"
domenkozar commented 1 year ago

Languages should be setting pre-commit.tools to match the versions.

domenkozar commented 5 months ago

It's now possible to set pre-commit.hooks.govet.package = pkgs..., if someone wants to make a PR