pkg/refs/refs_template.go ships in pkg/refs/, breaking Go module build
Bug Report: pkg/refs/refs_template.go ships in pkg/refs/, causing Go module build failure
Summary
Starting with the gvisor commit 50e1502a95d3 (module pseudo-version
v0.0.0-20260815053812-50e1502a95d3), the file
pkg/refs/refs_template.go is shipped in the pkg/refs/ directory of
the Go module. The file declares package refs_template, but the
directory's package is refs. This causes a found packages error
for every non-Bazel consumer of any gvisor package — the TCP/IP
stack (pkg/tcpip/...) is the most commonly affected.
Environment
- gvisor:
gvisor.dev/[email protected](latest as of filing) - Go: 1.26.3 darwin/arm64
- Build system: standard
go test/go build(the bug only surfaces outside Bazel; Bazel'sgo_templaterule splits the generated file into a separate package target)
Steps to reproduce
mkdir repro && cd repro
go mod init repro
go get gvisor.dev/gvisor@latest
go get -t gvisor.dev/gvisor/pkg/tcpip/transport/tcp@latest
go test gvisor.dev/gvisor/pkg/tcpip/transport/tcp
Expected
Tests run, OR at minimum the package compiles. The build system should be self-consistent.
Actual
# gvisor.dev/gvisor/pkg/tcpip/transport/tcp
found packages refs (refcounter.go) and refs_template (refs_template.go)
in /Users/willi/go/pkg/mod/gvisor.dev/[email protected]/pkg/refs
FAIL gvisor.dev/gvisor/pkg/tcpip/transport/tcp [setup failed]
FAIL
go build on the affected packages fails with the same error:
$ go build gvisor.dev/gvisor/pkg/refs
found packages refs (refcounter.go) and refs_template (refs_template.go) in ...
Root cause
pkg/refs/refs_template.go declares package refs_template but
lives in the pkg/refs/ directory, which is otherwise
package refs.
In the Bazel build (see pkg/refs/BUILD), the go_template rule
generates this file and the rule's name = "refs_template" causes
Bazel to address it as //pkg/refs_template:refs_template — a
separate Go package from //pkg/refs:refs. The two packages
share the same source directory only on the Bazel side; they
should be in separate directories when distributed as a Go
module.
For comparison, the previous release's pkg/refs/ shipped
refs_state_autogen.go instead, declared package refs, and
compiled cleanly. The commit that switched to the go_template
output did not update the Go module distribution accordingly.
The only other *_template.go file in the module,
pkg/bits/bits_template.go, is package bits and lives in the
pkg/bits/ directory — it extends the existing package rather
than declaring a new one, so it does not cause the same conflict.
Impact
Every non-Bazel Go consumer of gvisor is blocked. In particular,
the pkg/tcpip/... stack (the most commonly used surface — a
userspace TCP/IP stack for use in Go applications) transitively
imports pkg/refs, so any project depending on it via go get
is affected.
Suggested fix
Exclude pkg/refs/refs_template.go from the Go module
distribution. The file is Bazel-only scaffolding for the
go_template rule; the runtime code it generates
(refs_state_autogen.go-style state) should be re-emitted as a
regular package refs file, or the directory layout should be
reconciled so the template's package name matches its directory.
The smallest patch is to add the file to whatever list gvisor
already uses to control which files ship in the module
distribution.
Reproduction environment details
$ go version
go version go1.26.3 darwin/arm64
$ go list -m gvisor.dev/gvisor
gvisor.dev/gvisor v0.0.0-20260815053812-50e1502a95d3
$ cat go.mod
module repro
go 1.26.3
require gvisor.dev/gvisor v0.0.0-20260815053812-50e1502a95d3
$ ls $GOMOD/pkg/mod/gvisor.dev/[email protected]/pkg/refs/
BUILD README.md refcounter.go refs_map.go refs_template.go
$ head -20 $GOMOD/pkg/mod/gvisor.dev/[email protected]/pkg/refs/refs_template.go
// Copyright 2020 The gVisor Authors.
// ...
// Package refs_template defines a template that can be used by reference
// counted objects. The template comes with leak checking capabilities.
package refs_template <-- conflict: directory is package refs
$ ls $GOMOD/pkg/mod/gvisor.dev/[email protected]/pkg/refs/
refcounter.go refs_map.go refs_state_autogen.go
Workaround
Pin to the last release that ships pkg/refs/refs_state_autogen.go:
require gvisor.dev/gvisor v0.0.0-20260413194555-9680d69bf798
Hi! I'd like to work on this. I'll first reproduce the Go module build failure and study how pkg/refs is generated and packaged. Then I'll make the smallest fix that keeps Bazel working while ensuring the published Go module contains a valid pkg/refs package, and I'll verify it with both Bazel and standard Go tooling.