Go buildpack failed with `failed to teardown GOPATH`

fly deploy fails with an unusual error: failed to teardown GOPATH: unlinkat /tmp/gopath3041371786/pkg/mod/golang.org/x/sys@v0.2.0/PATENTS: permission denied

Noteworthy:

What could be the reason for this? Google shows essentially nothing – if it was a buildpack issue I’d assume others would have run into this issue. Are there permissions lacking in the builder?

===> DETECTING
4 of 8 buildpacks participating
paketo-buildpacks/ca-certificates 3.5.1
paketo-buildpacks/go-dist         2.2.4
paketo-buildpacks/git             1.0.1
paketo-buildpacks/go-build        2.0.9
===> ANALYZING
Previous image with name "registry.fly.io/XXX:cache" not found
===> RESTORING
===> BUILDING

Paketo Buildpack for CA Certificates 3.5.1
  https://github.com/paketo-buildpacks/ca-certificates
  Launch Helper: Contributing to layer
    Creating /layers/paketo-buildpacks_ca-certificates/helper/exec.d/ca-certificates-helper
Paketo Buildpack for Go Distribution 2.2.4
  Resolving Go version
    Candidate version sources (in priority order):
      BP_GO_VERSION -> "1.19.5"
      <unknown>     -> ""

    Selected Go version (using BP_GO_VERSION): 1.19.5

  Executing build process
    Installing Go 1.19.5
      Completed in 8.899s

  Generating SBOM for /layers/paketo-buildpacks_go-dist/go
      Completed in 0s

Paketo Buildpack for Git 1.0.1
  Configuring build environment
    REVISION -> "ff64078f0feb7021e00a4a95ba3e60e292e9e49d"

  Configuring launch environment
    REVISION -> "ff64078f0feb7021e00a4a95ba3e60e292e9e49d"

Paketo Buildpack for Go Build 2.0.9
  Executing build process
    Running 'go build -o /layers/paketo-buildpacks_go-build/targets/bin -buildmode pie -trimpath ./api'
      Completed in 16.577s

failed to teardown GOPATH: unlinkat /tmp/gopath3041371786/pkg/mod/golang.org/x/sys@v0.2.0/PATENTS: permission denied
ERROR: failed to build: exit status 1
Error failed to fetch an image or build from source: executing lifecycle: failed with status code: 51

fly.toml:

app = XXX
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[build]
builder = "paketobuildpacks/builder:base"
buildpacks = ["gcr.io/paketo-buildpacks/go"]

[build.args]
BP_GO_TARGETS = "./api"
BP_GO_VERSION = "1.19"

[env]
PORT = "8080"

[[services]]
http_checks = []
internal_port = 8080
processes = ["app"]
protocol = "tcp"
script_checks = []
[services.concurrency]
hard_limit = 25
soft_limit = 20
type = "connections"

[[services.ports]]
force_https = true
handlers = ["http"]
port = 80

[[services.ports]]
handlers = ["tls", "http"]
port = 443

Tried using pack directly, and indeed the same error appears locally as well:

pack build pld-api --buildpack paketo-buildpacks/go --builder paketobuildpacks/builder:base --env BP_GO_TARGETS="./api"

So yeah, turns out the go buildpack doesn’t play nice with Go Workspaces, and takes a different build path when go.mod isn’t present in the project root.

Filed go.work without go.mod in workspace fails with `failed to teardown GOPATH: unlinkat ...` · Issue #397 · paketo-buildpacks/go-build · GitHub (contains a workaround)