buildGoModule mod.go replace behaviour

I try to package rook but stumbled upon a go module behavior I don’t know how to fix.

{ lib
, pkgs
, buildGoModule
, fetchFromGitHub
}:

buildGoModule rec {
  pname = "rook";
  version = "1.13.3";

  src = fetchFromGitHub {
    owner = "rook";
    repo = "rook";
    rev = "v${version}";
    hash = "sha256-50J0OwuQHHHbAv0qUQNo+2RBFvan4FC+HmqUry+9v0Y=";
  };

  vendorHash = "sha256-JRnhWcf1lnrzvADL7RPwz/sc0xvP8Ne010QT4edbBDw=";
  buildInputs = with pkgs; [
    bash
    iproute
    s5cmd
  ];

  patchPhase = ''
    substituteInPlace pkg/operator/ceph/controller/network.go \
      --replace "bash" "${pkgs.bash}/bin/bash \
      --replace "ip" "${pkgs.iproute}/bin/ip
  '';

  ldflags = [ "-s" "-w" ];

  meta = with lib; {
    description = "Storage Orchestration for Kubernetes";
    homepage = "https://github.com/rook/rook";
    license = licenses.asl20;
    maintainers = with maintainers; [ ];
    mainProgram = "rook";
  };
}

It fail to compile with:

       last 10 log lines:
       > Building subPackage ./cmd/rook
       > Building subPackage ./cmd/rook/ceph
       > Building subPackage ./cmd/rook/rook
       > Building subPackage ./cmd/rook/userfacing
       > Building subPackage ./cmd/rook/userfacing/multus
       > Building subPackage ./cmd/rook/userfacing/multus/validation
       > Building subPackage ./cmd/rook/util
       > Building subPackage ./cmd/rook/version
       > Building subPackage ./pkg/apis/ceph.rook.io
       > main module (github.com/rook/rook) does not contain package github.com/rook/rook/pkg/apis/ceph.rook.io

So, the package github.com/rook/rook/pkg/apis/ceph.rook.io does exist, but it is redirected in go.mod by:

replace github.com/rook/rook/pkg/apis => ./pkg/apis

What is the proper way to fix this ?

The file does exist:

❯ ls -la /nix/store/zzwyzcwdp75g0hmwpdk1p7saasw2k253-source/pkg/apis/
total 69
dr-xr-xr-x 3 nobody nogroup      5 Jan  1  1970 .
dr-xr-xr-x 9 nobody nogroup      9 Jan  1  1970 ..
dr-xr-xr-x 3 nobody nogroup      4 Jan  1  1970 ceph.rook.io
-r--r--r-- 2 nobody nogroup   5067 Jan  1  1970 go.mod
-r--r--r-- 2 nobody nogroup 104895 Jan  1  1970 go.sum

Does it maybe get fixed by proxyVendor = true? This changes the way vendoring is done. You need to update the fixed output hash for this.

Unfortunately not. I tried different combinations of proxyVendor and

patchPhase = ''
substituteInPlace go.mod \
      --replace "=> ./pkg/apis" "=> $src/pkg/apis"
'';

Seems to fix problem when proxyVendor is false, but then complains about vendor/modules.txt beeing desyncd with go.mod, even when I do it in the patchPhase.

I think the goBuildModule is broken for all go.mod that have a replace declaration

Turned out, the buildGoModule default buildPhase tried to compile subpackages that are not compileable.
Using a modified make rule:

  buildPhase = ''
    # make --trace go.build SHELL=${pkgs.bash}/bin/bash VERSION=${version} 
    CGO_ENABLED=0 go build -mod=vendor -v -o _output/bin/linux_amd64/rook \
      -tags "" -pkgdir .work/pkg/linux_amd64_static -installsuffix static \
      -ldflags '-s -w -X github.com/rook/rook/pkg/version.Version=${version}' \
      github.com/rook/rook/cmd/rook
  '';

solved the problem.