Skip to content

dagger mod init fails with confusing error message when there is already a go.mod #6329

Closed

Description

@samalba

How to reproduce

go mod init github.com/samalba/sandbox/test
dagger mod init --name=test --sdk=go

Output

✘ generatedCode ERROR [0.57s]
✘ exec /usr/local/bin/codegen --module . --propagate-logs=true --introspection-json-path /schema.json ERROR [0.34s]
┃ Error: load package ".": err: exit status 1: stderr: go: go.mod requires go >= 1.21.5 (running go 1.21.3; GOTOOLCHAIN=local)
✘ generating go module: test ERROR [0.04s]
├ [0.00s] go mod tidy
┃ writing dagger.gen.go
┃ writing go.mod
┃ writing go.sum
┃ writing main.go
┃ creating directory querybuilder
┃ writing querybuilder/marshal.go
┃ writing querybuilder/querybuilder.go
┃ go: go.mod requires go >= 1.21.5 (running go 1.21.3; GOTOOLCHAIN=local)
┃ needs another pass...
• Cloud URL: https://dagger.cloud/runs/0bafbb50-94f4-49e4-8cfc-3e8f28582770
• Engine: 580198e00e2f (version v0.9.5)
⧗ 1.70s ✔ 18 ∅ 2 ✘ 3
Error: failed to automate vcs: failed to get vcs ignored paths: input:1: host.directory.asModule.generatedCode failed to get modified source directory for go module sdk codegen: process "/usr/local/bin/codegen --module . --propagate-logs=true --introspection-json-path /schema.json" did not complete successfully: exit code: 1

Info

dagger 0.9.5
go 1.21.5

Expected behavior

It's normal to fail when a go mod already exists, however the error is confusing and useless. go.mod requires go >= 1.21.5 -> I am already using 1.21.5

Instead, a message saying something like: cannot initialize dagger module: a go module already exists.

Activity

  1. sipsma commented on Jan 2, 2024

    @sipsma
    Contributor

    @samalba To clarify, you are allowed to have an existing go.mod and that's handled in general (there's integ test coverage for this). The problem here is that Go in 1.21 started creating go.mod by default with directives like go 1.21.5 which result in these errors if there's a mismatch between the version of go being used by the engine the and the version of go you used to run go mod init on your host.

    So you don't get this error if your host is using go 1.21.3 (what the engine is currently on).


    You can easily fix this right now by just editing the go.mod created by go mod init to just say go 1.21 rather than go 1.21.5.

    I'm sure there's got to be some better way of handling this in general since there's no way we actually care about the patch version of Go.

  2. samalba commented on Jan 4, 2024

    @samalba
    ContributorAuthor

    Thanks for the context. Maybe a good way to solve this is to detect if there is a minor version in go.mod and hint the user to remove it. If I learned this from the output, I would have done this manually to fix it.

  3. sipsma commented on Jan 10, 2024

    @sipsma
    Contributor

    As a note, Nipuna mentioned that he also needed to make the same adjustment to his go.work file to remove the minor version in order for this to work fully

  4. vbehar commented on Jan 12, 2024

    @vbehar
    Contributor

    Hi, I had a look at this issue, and found out that it only happens if you run go mod init before dagger mod init, and if you have go > 1.21.3 locally. because go mod init will create a go.mod file with your local version of go (say 1.21.5), but when running dagger mod init, it will run in a container with go 1.21.3 in local mode, so it won't be able to do anything

    the "quick" fix is to avoid running go mod init, because dagger mod init will take care of creating the go.mod file anyway if it doesn't exists, and it will have the "right" go version (the one used by the dagger container)

    the real fix is to ensure that the container is properly configured, with GOTOOLCHAIN=auto. by default, the official go images have GOTOOLCHAIN=local, which is ok because you want a controlled env. but in the dagger context, I believe it makes sense to be able to run the code if even it requires a newer version of go. just adding the GOTOOLCHAIN=auto env to the container will do it:

    $ echo $(docker run --rm -it golang:1.21.3 go env GOTOOLCHAIN)
    local
    $ echo $(docker run --rm -it -e GOTOOLCHAIN=auto golang:1.21.3 go env GOTOOLCHAIN)
    auto
  5. added
    kind/bug
    and removed on Apr 24, 2024
  6. added and removed
    kind/bug
    on May 31, 2024
  7. helderco commented on Aug 1, 2024

    @helderco
    Contributor

    This is no longer relevant because for a while now, the Go SDK's codegen merges the new modules go.mod with the existing one, so no longer returns an error.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions