Skip to content

Go SDK issues with newer go.mod than packaged into engine #6706

Closed

Description

@jedevc

Writing up a brain dump, so might be a bit messy.

Currently the go version used by the Go SDK is 1.21.3. However, a user module can set a custom go version (using go.mod/go.work) to a higher version, like 1.22.0.

Now, this can cause fun issues it seems? I'm not quite sure what happens, but essentially in

pkgs, err := packages.Load(&packages.Config{
Context: ctx,
Dir: dir,
Tests: false,
Fset: fset,
Mode: packages.NeedName |
packages.NeedTypes |
packages.NeedSyntax |
packages.NeedTypesInfo |
packages.NeedModule,
}, ".")

We can get a package returned that:

{
  "ID": "foobar",
  "Name": "main",
  "PkgPath": "foobar",
}

for a go.mod that looks like this:

module foobar

go 1.22.0

This silent failure is caused by https://github.com/golang/tools/blob/a821e617f30094a078bfdc1d63623b93365b3130/go/packages/packages.go#L1039 - Checker.Files *can` return an error if we try and type-check for a go version that is greater than the currently installed version.

This behavior makes sense (though the silent failure really doesn't). Note: GOTOOLCHAIN=auto (as added in #6652 doesn't help for this, it only helps for compiling, not parsing+processing the package).

However, this leaves us with the problem of how to manage this from the point-of-view of the Go SDK (cc @vito @sipsma). A few options:

  • We simply explicitly ban doing this - if the go version bundled into the Go SDK tarball is X, then we don't allow >X with a nice error message.
    • Note, we could potentially decouple the codegen go version from the engine go version to be higher with very little risk.
    • However, since the Go SDK is built into the engine, we'd have to re-release every time we want to bump go version, which is a pain (but not unavoidable at our pace of releases).
  • We rework package loading significantly, instead somehow custom compiling a binary at runtime to do this (with the right go-version), then serializing + deserializing this into the codegen binary.
    • This would go poorly if the types structure changes, not sure if the go compatibility requirement allows this.
    • Also, complexity 😭
  • Or (as a variation) recompile the codegen binary itself if it asks for a different go version.
  • Just attempt to parse it as best we can, by explicitly overriding the go version for typechecking - if we fail, append a warning about the upgrade, but otherwise continue on!
    • This could just work, there are really minor type differences between versions anyways.
    • Not quite sure how we implement this though, hopefully we don't need to mess around with any x/tools internals.

Open to other suggestions 🎉

Also see: https://github.com/golang/tools/blob/a821e617f30094a078bfdc1d63623b93365b3130/go/packages/packages.go#L930-L945

Activity

  1. added this to the v0.10.0 milestone on Feb 21, 2024
  2. jedevc commented on Feb 21, 2024

    @jedevc
    ContributorAuthor

    Also gonna milestone this one, because this is a supreme pain (I spent a whole day tracking out what was really going on), at the very least we should have a nice error message for this.

  3. sipsma commented on Feb 22, 2024

    @sipsma
    Contributor

    This silent failure

    What's the failure? That Name is main rather than foobar? Just double checking.

    In terms of handling this better, the fact that the go sdk is builtin to the engine is indeed a pretty big wrench here. We do that to avoid hard deps on the internet and to avoid problems in corp envs around need to use registry proxies. I can see some routes to do something like:

    1. use builtin sdk when at user module version or older
    2. fallback to dynamically pulling+building if user module is at newer version

    Which, if I understand the issue correctly, would probably be one possible reasonable next step. And in the long term I have some ideas on making it easier for users to customize what specifically gets builtin to the engine (or equivalent).

    This coincidentally overlaps quite a bit with what I was doing in #6714 today, I will think a bit more tomorrow and see what can be done for v0.10.

  4. jedevc commented on Feb 22, 2024

    @jedevc
    ContributorAuthor

    Ahh, sorry, such a brain dump I didn't bother to write down the actual error.

    ❯ ls
    go.mod
    
    ❯ cat go.mod
    module foobar
    
    go 1.22.0
    
    ❯ dagger-0.9.11 init --sdk=go --source=.
    ● dagger init 5.9s
    ● ModuleSource.asModule: Module! 5.6s
      ● Module.withSource(
          source: ✔ ModuleSource.resolveFromCaller: ModuleSource! 0.0s
        ): Module! 5.6s
    ✘ ModuleSource.asModule: Module! 16.3s
      ✘ Module.withSource(
          source: ✔ ModuleSource.resolveFromCaller: ModuleSource! 0.0s
        ): Module! 16.3s
        ✘ Container.directory(path: "/src"): Directory! 14.6s
          ✘ exec /usr/local/bin/codegen --module-context /src --module-name playground --propagate-logs=true --introspection-json-path /schema.json 12.3s
          ┃ Error: generate code: template: alias.go.tmpl:56:3: executing "_dagger.gen.go/alias.go.tmpl" at <ModuleMainSrc>: error calling ModuleMainSrc: cannot find default codegen DaggerObjec
          ┃ ace                                                                                                                                                                                        ✘ generating go module: playground 11.5s
          ┃ go: downloading github.com/vektah/gqlparser/v2 v2.5.6                                                                                                                                
          ┃ go: downloading golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa                                                                                                                  
          ┃ go: downloading github.com/Khan/genqlient v0.6.0                                                                                                                                     
          ┃ go: downloading golang.org/x/sync v0.6.0                                                                                                                                             
          ┃ go: downloading github.com/99designs/gqlgen v0.17.31                                                                                                                                 
          ┃ go: downloading github.com/stretchr/testify v1.8.3                                                                                                                                   
          ┃ go: downloading gopkg.in/yaml.v3 v3.0.1                                                                                                                                              
          ┃ go: downloading github.com/davecgh/go-spew v1.1.1                                                                                                                                    
          ┃ go: downloading github.com/pmezard/go-difflib v1.0.0                                                                                                                                 
          ┃ go: downloading github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883                                                                                                         
          ┃ go: downloading github.com/sergi/go-diff v1.3.1                                                                                                                                      
          ┃ needs another pass...                                                                                                                                                                
    Error: failed to generate code: input: resolve: moduleSource: withName: withSDK: withSourceSubpath: resolveFromCaller: asModule: failed to create module: failed to update codegen and runtime: failed to generate code: failed to get modified source directory for go module sdk codegen: process "/usr/local/bin/codegen --module-context /src --module-name playground --propagate-logs=true --introspection-json-path /schema.json" did not complete successfully: exit code: 1
    
    Stderr:
    Error: generate code: template: alias.go.tmpl:56:3: executing "_dagger.gen.go/alias.go.tmpl" at <ModuleMainSrc>: error calling ModuleMainSrc: cannot find default codegen DaggerObject interface
    

    We get cannot find default codegen DaggerObject interface because no type-checking is done so no types can be found (but instead of erroring, we get back an invalid package struct).

  5. modified the milestones: v0.10.0, v0.10.x on Feb 26, 2024
  6. locked and limited conversation to collaborators on Mar 24, 2026
  7. converted this issue into a discussion #12349 on Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions