Go: Unable to build snap when main package is not in the root


After adding support for go mod in 3.10.1, we are no longer able to build new packages for https://github.com/travis-ci/worker

Snapcraft runs go build -o <out dir>, which builds main package only if it’s inside the root, but in our case main package is located in another dir ./cmd/travis-worker/main.go (which is a common pattern for go applications) and the command fails.

Before introducing go mod support, snapcraft would scan all dirs for main package and build all of them. Unfortunately, I haven’t found a way to restore the old behaviour.

Possible way to fix is to tell go build where main package is located (e.g. go build -o <output dir> ./cmd/travis-worker), or use ./... pattern (e.g. go build -o <output dir> ./...), so that compiler will build all main packages in the repo. I think the 2nd option is preferable as it will not require any additional configuration.

I’ve prepared a minimal example where the issue is reproducible:

  1. Given a go module, where main package is located in ./cli subdirectory
  2. Clone https://github.com/pavel-d/snaptest
  3. cd snaptest
  4. Run snapcraft build --use-lxd --debug


snapcraft --use-lxd  --debug
The LXD provider is offered as a technology preview for early adopters.
The command line interface, container names or lifecycle handling may change in upcoming releases.
Launching a container.
Waiting for container to be ready
status: done
snapd 2.43.3 from Canonical✓ refreshed
Skipping pull snaptest (already ran)
Building snaptest 
go build -o /root/parts/snaptest/install/bin
go build: no main packages to build
Failed to run 'go build -o /root/parts/snaptest/install/bin' for 'snaptest': Exited with code 1.
Verify that the part is using the correct parameters and try again.
snapcraft-snaptest # snapcraft --version
snapcraft, version 3.10.1

I’ve sent a PR, please let me know what you think! https://github.com/snapcore/snapcraft/pull/2962

1 Like

Also, it’s not possible to disable go mod, when GO111MODULE=off is set, build fails as well https://build.snapcraft.io/user/travis-ci/worker/862268

The issue has been fixed in master

1 Like