Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go module workspaces #203039

Open
urandom2 opened this issue Nov 26, 2022 · 9 comments
Open

go module workspaces #203039

urandom2 opened this issue Nov 26, 2022 · 9 comments

Comments

@urandom2
Copy link
Contributor

Describe the bug

while attempting to package atlas for #197774, it became apparent that buildGoModule is not capable of handling the new go module workspace mode.

technically the versions of go we have supports it, but because workspaces contradict -mod=vendor, see golang/go#54130, we may need to add buildGoWorkspace?

Steps To Reproduce

Steps to reproduce the behavior:

  1. have a repo with go.mod and go.work
  2. use buildGoModule
go: -mod may only be set to readonly when in workspace mode, but it is set to "vendor"
	Remove the -mod flag to use the default readonly value,
	or set GOWORK=off to disable workspace mode.

Expected behavior

derivation builds correctly

Additional context

since we are using GOPROXY=file:// to inject dependencies, we should not need to use -mod=vendor.

Notify maintainers

@c00w @kalbasit @Mic92 @zowoq

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 5.15.79, NixOS, 22.11 (Raccoon), 22.11.20221121.af50806`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.11.0`
 - channels(root): `""`
 - channels(Colin Arnott): `"nixos-22.05-22.05"`
 - channels(colin): `""`
 - nixpkgs: `/nix/store/lwnrrndriqm2d588lgd6184bcawj3b0z-source`
@zowoq
Copy link
Contributor

zowoq commented Nov 26, 2022

since we are using GOPROXY=file:// to inject dependencies, we should not need to use -mod=vendor.

Don't understand what you mean?

GOFLAGS = lib.optionals (!proxyVendor) [ "-mod=vendor" ] ++ lib.optionals (!allowGoReference) [ "-trimpath" ];

${if proxyVendor then ''
export GOPROXY=file://${go-modules}

@urandom2
Copy link
Contributor Author

urandom2 commented Nov 26, 2022

error: builder for '/nix/store/6mx0zawbpkdrlsl8y1y5v33lm2dzmmz5-atlas-0.8.0.drv' failed with exit code 1;
       last 10 log lines:
       > unpacking sources
       > unpacking source archive /nix/store/alkc8id9xjs3aas1jm2qfskcfkywcxhk-source
       > source root is source
       > patching sources
       > configuring
       > building
       > Building subPackage .
       > go: -mod may only be set to readonly when in workspace mode, but it is set to "vendor"
       >     Remove the -mod flag to use the default readonly value,
       >        or set GOWORK=off to disable workspace mode.
       For full logs, run 'nix log /nix/store/6mx0zawbpkdrlsl8y1y5v33lm2dzmmz5-atlas-0.8.0.drv'.
error: 1 dependencies of derivation '/nix/store/9fj79dfz1i8pplpg9h2ym4ksr9p0yxhh-review-shell.drv' failed to build

master...urandom2:nixpkgs:atlas

EDIT: let me know what additional context you are curious about?

@zowoq
Copy link
Contributor

zowoq commented Nov 26, 2022

master...urandom2:nixpkgs:atlas

That isn't using GOPROXY=file:// ?

@zowoq
Copy link
Contributor

zowoq commented Nov 26, 2022

atlas has an open PR that builds: #201531

@urandom2
Copy link
Contributor Author

sorry, misread the source; but you piqued my interest and proxyVendor = true; made it work. should we document this better?

meta question, why is proxyVendor ? true not default?

@MatrixManAtYrService
Copy link
Contributor

I think I'm seeing this bug (here's the flake with the problem: https://gist.github.com/MatrixManAtYrService/ac040f60d3602fc2df871623b1d09bf7).

In my case, adding proxyVendor = true; resulted in a different error:

../kyaml/openapi/openapi.go:22:2: github.com/evanphx/json-patch@v4.11.0+incompatible: reading file:///nix/store/nx9lf6v3pbbv1140i112xvzr818pmz3z-kustomize-4.5.4-go-modules/github.com/evanphx/json-patch/@v/v4.11.0+incompatible.mod: no such file or directory

I'm posting 90% because I want to help collect data that helps with fixing bugs and 10% because maybe sombody can tell me how to work around it in my flake 😉 . Thanks all.

@jalaziz
Copy link
Contributor

jalaziz commented Jul 20, 2023

UPDATE: I got it working using this tip to force regenerating the vendor has.

Ran into this myself while trying to upgrade a package :(

Unfortunately, the export GOWORK=off simply resulted in a new issue.

Given this:

{ lib, buildGoModule, fetchFromGitHub, go-mockery, runCommand, go }:

buildGoModule rec {
  pname = "go-mockery";
  version = "2.32.0";

  src = fetchFromGitHub {
    owner = "vektra";
    repo = "mockery";
    rev = "v${version}";
    sha256 = "sha256-fQzXgCRMIcGQRCnKn/vu3GzNrx4/xrMVmzqjOujyNNE=";
  };

  preCheck = ''
    substituteInPlace ./pkg/generator_test.go --replace 0.0.0-dev ${version}
  '';

  preBuild = ''
    export GOWORK=off
  '';

  ldflags = [
    "-s" "-w"
    "-X" "github.com/vektra/mockery/v2/pkg/config.SemVer=v${version}"
  ];

  CGO_ENABLED = false;

  vendorHash = "sha256-ALcozrbP8vj3CEZkUVpeVPdi2ok89F9qK62xaVf2E0c=";

  passthru.tests = {
    generateMock = runCommand "${pname}-test" {
      nativeBuildInputs = [ go-mockery ];
      buildInputs = [ go ];
    } ''
      if [[ $(mockery --version) != *"${version}"* ]]; then
        echo "Error: program version does not match package version"
        exit 1
      fi

      export HOME=$TMPDIR

      cat <<EOF > foo.go
      package main

      type Foo interface {
        Bark() string
      }
      EOF

      mockery --name Foo --dir .

      if [[ ! -f "mocks/Foo.go" ]]; then
        echo "Error: mocks/Foo.go was not generated by ${pname}"
        exit 1
      fi

      touch $out
    '';
  };

  meta = with lib; {
    homepage = "https://github.com/vektra/mockery";
    description = "A mock code autogenerator for Golang";
    maintainers = with maintainers; [ fbrs ];
    mainProgram = "mockery";
    license = licenses.bsd3;
  };
}

The build fails with:

main module (github.com/vektra/mockery/v2) does not contain package github.com/vektra/mockery/v2/tools

tools here is a separate module in the same workspace, which is properly handled by Go workspaces.

If I remove the GOWORK=off workaround, I end up with all the issues mentioned in this ticket. Not sure what else to do.

@katexochen
Copy link
Contributor

I'm not sure we should add a builder for Go workspaces, but in case we decide to do so, there is an interesting change in Go 1.22 that allows to vendor dependencies of a Go workspace:

Commands in workspaces can now use a vendor directory containing the dependencies of the workspace. The directory is created by go work vendor, and used by build commands when the -mod flag is set to vendor, which is the default when a workspace vendor directory is present.

Note that the vendor directory's contents for a workspace are different from those of a single module: if the directory at the root of a workspace also contains one of the modules in the workspace, its vendor directory can contain the dependencies of either the workspace or of the module, but not both.

https://tip.golang.org/doc/go1.22#go-command

stuebinm added a commit to stuebinm/nixpkgs that referenced this issue Feb 25, 2024
This jumps Mattermost ESR Versions (see [1] for their release cycle). The
new version makes use of Go's workspace feature, which unfortunately the
buildGoModule function does not (yet?) support [2], which breaks the
previous build process for mattermost.

Further, the new release also makes use of private modules only included
in the (non-free) enterprise version of mattermost which makes it impossible
to build in the usual way even outside of nixpkgs's build abstractions [3].

Both issues can be solved by using Go 1.22, which has added support for
vendoring when using workspaces, and instructing it to ignore errors with
the -e flag. This requires overriding the go-modules derivation's
buildPhase, and still produces a functional binary.

[1] https://docs.mattermost.com/upgrade/extended-support-release.html
[2] NixOS#203039
[3] mattermost/mattermost#26221
@katexochen
Copy link
Contributor

for cross-reference: #299096

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants