Go, go generate, vendoring

I’m trying to package this. With my attempt:

{ lib, buildGoModule, fetchFromGitHub }:

buildGoModule rec {
 pname = "carapace";
 version = "0.8.5";

 src = fetchFromGitHub {
   owner = "rsteube";
   repo = "${pname}-bin";
   rev = "v${version}";
   sha256 = "0pr0ri217n2cjbfn88pqmsbdsbwqpnd173ka4gwhxlpp68jkswlc";
 };

 vendorSha256 = "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5";

 preBuild = ''
   cd cmd/carapace 
   go generate ./...
 '';

I get

go: inconsistent vendoring in /build/source:
        github.com/mitchellh/go-homedir@v1.1.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
...

I tried a few variations of proxyVendor, runVend, and subPackages, but I know close to nothing of go. Any help would be appreciated.

Hey. There are a few things happening here. First I think some of the build requires Go1.17, so I updated that. I’m using the subPackages feature to select which package I want (you can remove that and build each subpackage and all completers if you wish, but it takes a while). The last and probably what made this difficult to debug, is that Go’s generate function calls some go code, and fails silently, here:
carapace-bin/gen.go at 360e541c328e4e14d6889252ba726845f45fd647 · rsteube/carapace-bin · GitHub (notice the if statement, but no handling of the error case)

No error is printed, and no indication something went wrong. The function tries to call git here: carapace-bin/gen.go at 360e541c328e4e14d6889252ba726845f45fd647 · rsteube/carapace-bin · GitHub

That fails because there is no git and no git repo, and so some of the necessary go files are not generated for you. So to make the upstream logic work you can add git into the repo, but Nix destroys the .git folder that when copying the source, so a quick fix is to add a git init -q. With that you have a working derivation:

      buildGo117Module rec {
        pname = "carapace";
        version = "0.8.5";

        src = fetchFromGitHub {
          owner = "rsteube";
          repo = "${pname}-bin";
          rev = "v${version}";
          sha256 = "0pr0ri217n2cjbfn88pqmsbdsbwqpnd173ka4gwhxlpp68jkswlc";
        };

        nativeBuildInputs = [ gitMinimal ];
        vendorSha256 = "sha256-wTvvu1g+OdFFFBu63vsuYcpAE1B26xW3V792m7Y7okU=";

        subPackages = [ "./cmd/carapace" ];
        postConfigure = ''
          git init -q
          go generate ./...
        '';

      };

The build-time dependency on git was never documented or described, but assumed. And it failed silently. I’d recommend that upstream at least try to print something or error out in a more useful fashion during code generation.

1 Like

That’s enlightening, thank you so much for all the trouble.

I’ll try it later this evening

1 Like

Works like a charm. PR coming. Thanks!