From c98f0c9089e68d961b7f8854a1faa6ded4eb4ae7 Mon Sep 17 00:00:00 2001 From: Blair Hamilton Date: Sun, 15 Nov 2026 12:44:38 -0500 Subject: [PATCH 1/2] golang: remove older releases' go_env dirs before installing The first install of a golang hook after upgrading to 4.7.1 from 4.7.0 or older failed with `pattern ./...: directory go_env-default/pkg/mod/... outside main module or its selected dependencies`. 4.7.1 renamed the env to upstream's golangenv but left the old go_env- and bare go_env dirs in each hook clone, and those releases never removed GOPATH/pkg, so a full module cache sat inside the clone. `go install ./...` runs in the clone and refuses any module there without a go.mod of its own. Seen on gitleaks (goutils@v1.1.1 predates modules) on the first commit after `brew upgrade`. Nothing has read those dirs since the rename and no upstream language uses the name, so the install now removes them first. The wipe test for the env's own leftovers could not fail (its junk module had a go.mod, which the pattern skips); without that go.mod it now fails when the wipe is removed. --- CHANGELOG.md | 10 ++++ internal/hook/runner_test.go | 9 ++-- internal/languages/golang.go | 43 +++++++++++++++ internal/languages/golang_test.go | 89 +++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0324e6..e619729 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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-` 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. ## [4.7.1] - 2026-11-02 diff --git a/internal/hook/runner_test.go b/internal/hook/runner_test.go index 28142d1..7993111 100644 --- a/internal/hook/runner_test.go +++ b/internal/hook/runner_test.go @@ -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) } diff --git a/internal/languages/golang.go b/internal/languages/golang.go index 1d3df6a..402fd5e 100644 --- a/internal/languages/golang.go +++ b/internal/languages/golang.go @@ -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 @@ -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-, 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, diff --git a/internal/languages/golang_test.go b/internal/languages/golang_test.go index 738baa8..97ce1aa 100644 --- a/internal/languages/golang_test.go +++ b/internal/languages/golang_test.go @@ -18,6 +18,8 @@ import ( "slices" "strings" "testing" + + "github.com/blairham/go-pre-commit/v4/internal/fsutil" ) // --------------------------------------------------------------------------- @@ -151,6 +153,93 @@ func TestGolangInstallRemovesModuleCache(t *testing.T) { } } +// Releases before 4.7.1 named the env go_env- (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. From 01d44d4cd7f3147911d07e75239c5b1bdc9009a4 Mon Sep 17 00:00:00 2001 From: Blair Hamilton Date: Sun, 15 Nov 2026 12:45:33 -0500 Subject: [PATCH 2/2] CHANGELOG: link the go_env fix to #228 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e619729..274c3ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,7 +37,7 @@ file was introduced; the full notes are on the 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. + has read them since the rename, and no upstream language uses the name. (#228) ## [4.7.1] - 2026-11-02