Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ file was introduced; the full notes are on the
recognized, so upgrading reinstalls nothing. The SQLite driver is
`modernc.org/sqlite`, which is pure Go, so releases still build with
`CGO_ENABLED=0`; the binary is about 4 MB larger. (#165)
- The first install of a `language: golang` hook after upgrading from 4.7.0
or older no longer fails with `go install failed: ... pattern ./...:
directory go_env-default/pkg/mod/... outside main module or its selected
dependencies`. 4.7.1 renamed the env directory to upstream's `golangenv`
but left older releases' `go_env-<version>` and bare `go_env` directories
in each hook repo's clone, holding the module cache those releases never
removed. `go install ./...` runs in the clone, so it walked into that cache
and refused any module there without a `go.mod` of its own. The install now
removes those directories first, which also frees their disk space: nothing
has read them since the rename, and no upstream language uses the name. (#228)

## [4.7.1] - 2026-11-02

Expand Down
9 changes: 4 additions & 5 deletions internal/hook/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,15 +760,14 @@ func TestInstallEnvironmentsWipesStatelessLeftoverEnv(t *testing.T) {
}

// Simulate the poisoned state: env dir with a populated module cache but
// no install state. The junk module has its own go.mod, which is what
// breaks the `./...` pattern walk when it survives into the install.
// no install state. The junk module has no go.mod of its own (a module
// that predates modules), which is what breaks the `./...` pattern walk
// when it survives into the install; one with a go.mod is a nested module
// the walk skips, so it would pass with or without the wipe.
junk := filepath.Join(repoDir, "golangenv-system", "pkg", "mod", "example.com", "junk@v1.0.0")
if err := os.MkdirAll(junk, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(junk, "go.mod"), []byte("module example.com/junk\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(junk, "junk.go"), []byte("package junk\n"), 0o644); err != nil {
t.Fatal(err)
}
Expand Down
43 changes: 43 additions & 0 deletions internal/languages/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ func (g *Golang) InstallEnvironment(prefix, version string, additionalDeps []str
goBin = "go"
}

if err := removeLegacyGoEnvs(prefix); err != nil {
return err
}

// Install the hook package.
cmd := command(goBin, "install", "./...")
cmd.Dir = prefix
Expand All @@ -255,6 +259,45 @@ func (g *Golang) InstallEnvironment(prefix, version string, additionalDeps []str
return fsutil.RemoveAll(filepath.Join(envDir, "pkg"))
}

// legacyGoEnvDir is the env directory's name before 4.7.1 took upstream's.
const legacyGoEnvDir = "go_env"

// removeLegacyGoEnvs removes the env dirs this port's releases before 4.7.1
// left in a hook's clone, which no release since reads: go_env-<version>, and
// the bare go_env that held their install state through 4.6.4. They are not
// inert. Those releases also kept the module cache in GOPATH/pkg, and `go
// install ./...` runs in the clone, so its pattern walks into that cache and
// refuses any module there with no go.mod of its own ("directory
// go_env-default/pkg/mod/... outside main module or its selected
// dependencies"). The first install after an upgrade failed that way until
// the dirs were deleted by hand. No upstream language names its env go_env,
// so a store shared with Python pre-commit loses nothing.
//
// Only directories with those names are removed, and only in the clone.
// Another version's golangenv is left alone even when a crashed install left
// its pkg behind, as upstream leaves it: one clone's versions install in
// parallel here, so that pkg may be a cache another install is filling. The
// env being installed was already cleared by hook.InstallEnvironments, as by
// upstream's _hook_install. A module cache is read-only, hence
// fsutil.RemoveAll.
func removeLegacyGoEnvs(prefix string) error {
entries, err := os.ReadDir(prefix)
if err != nil {
return err
}
for _, e := range entries {
name := e.Name()
if !e.IsDir() || (name != legacyGoEnvDir && !strings.HasPrefix(name, legacyGoEnvDir+"-")) {
continue
}
path := filepath.Join(prefix, name)
if err := fsutil.RemoveAll(path); err != nil {
return fmt.Errorf("failed to remove %s, an env from an older release: %w", path, err)
}
}
return nil
}

// Run implements Language.
func (g *Golang) Run(
ctx context.Context,
Expand Down
89 changes: 89 additions & 0 deletions internal/languages/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"slices"
"strings"
"testing"

"github.com/blairham/go-pre-commit/v4/internal/fsutil"
)

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -151,6 +153,93 @@ func TestGolangInstallRemovesModuleCache(t *testing.T) {
}
}

// Releases before 4.7.1 named the env go_env-<version> (through 4.6.4 its
// install state went in a bare go_env), and they left the module cache in
// GOPATH/pkg. The rename left those dirs in the clone, where
// `go install ./...` walks into the cache and refuses any module there with
// no go.mod of its own: "pattern ./...: directory go_env-default/pkg/mod/...
// outside main module or its selected dependencies". So the first install
// after an upgrade failed until they were deleted by hand. They are removed
// before installing, read-only as Go leaves a module cache, and nothing else
// in the clone is touched.
func TestGolangInstallRemovesLegacyEnvDirs(t *testing.T) {
if _, err := exec.LookPath("go"); err != nil {
t.Skip("go not available")
}
prefix := t.TempDir()
// Registered after TempDir, so it runs first: if the install leaves the
// read-only cache behind, TempDir's own cleanup could not remove it.
t.Cleanup(func() { _ = fsutil.RemoveAll(prefix) })
t.Setenv("GOFLAGS", "-mod=mod")
t.Setenv("GOPROXY", "off")
for name, body := range map[string]string{
"go.mod": "module example.com/hook\n\ngo 1.21\n",
"main.go": "package main\n\nfunc main() {}\n",
// The hook's own source, under names that start like a legacy env.
filepath.Join("go_envoy", "envoy.go"): "package envoy\n",
"go_env-unix.go": "package main\n",
} {
path := filepath.Join(prefix, name)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
// A module cache as 4.7.0 left one: goutils@v1.1.1 predates modules, so
// its directory has no go.mod.
legacy := filepath.Join(prefix, "go_env-default")
module := filepath.Join(legacy, "pkg", "mod", "github.com", "!masterminds", "goutils@v1.1.1")
if err := os.MkdirAll(module, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(module, "goutils.go"), []byte("package goutils\n"), 0o444); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(legacy, "bin"), 0o755); err != nil {
t.Fatal(err)
}
stateDir := filepath.Join(prefix, "go_env")
if err := os.MkdirAll(stateDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(stateDir, "install_state_v2"), []byte("{}\n"), 0o644); err != nil {
t.Fatal(err)
}
// Go makes its module cache read-only, directories included.
for _, dir := range []string{
module, filepath.Dir(module), filepath.Dir(filepath.Dir(module)),
filepath.Join(legacy, "pkg", "mod"), filepath.Join(legacy, "pkg"),
} {
if err := os.Chmod(dir, 0o555); err != nil {
t.Fatal(err)
}
}

if err := (&Golang{}).InstallEnvironment(prefix, "system", nil); err != nil {
t.Fatalf("InstallEnvironment beside a legacy env: %v", err)
}

for _, gone := range []string{legacy, stateDir} {
if _, err := os.Lstat(gone); !os.IsNotExist(err) {
t.Errorf("legacy env %s should be removed, stat: %v", gone, err)
}
}
for _, kept := range []string{filepath.Join("go_envoy", "envoy.go"), "go_env-unix.go"} {
if _, err := os.Stat(filepath.Join(prefix, kept)); err != nil {
t.Errorf("the hook's own %s should be left alone: %v", kept, err)
}
}
bin := "hook"
if runtime.GOOS == "windows" {
bin += ".exe"
}
if _, err := os.Stat(filepath.Join(prefix, "golangenv-system", "bin", bin)); err != nil {
t.Errorf("hook binary not installed into GOPATH/bin: %v", err)
}
}

// goDistServer serves a go.dev release list naming version and a Go
// distribution whose go/bin/go is a script that records its environment and
// "installs" by writing $GOBIN/hook.
Expand Down
Loading