Skip to content

golang: remove older releases' go_env dirs before installing - #228

Merged
blairham merged 2 commits into
mainfrom
fix/golang-legacy-env-dirs
Nov 15, 2026
Merged

blairham merged 2 commits into
mainfrom
fix/golang-legacy-env-dirs

Conversation

@blairham

Copy link
Copy Markdown
Owner

Why

The first install of a language: golang hook after upgrading to 4.7.1 from 4.7.0 or older fails:

failed to install environment for hook "gitleaks": go install failed: pattern ./...: directory go_env-default/pkg/mod/github.com/!masterminds/goutils@v1.1.1 outside main module or its selected dependencies

4.7.1 renamed the golang env to upstream's golangenv (#161) but left older releases' go_env-<version> and bare go_env dirs in every hook clone. Those releases never removed GOPATH/pkg, so each clone kept a full module cache (418 MB per gitleaks clone on one machine; 20 such dirs, 8.6 GB in all). InstallEnvironment runs go install ./... in the clone, the pattern walks into that cache, and Go refuses any module there without its own go.mod (goutils@v1.1.1 predates modules). Hit on the first commit after brew upgrade; CI was unaffected only because its cache started cold.

What changed

  • removeLegacyGoEnvs runs before go install ./... and removes only directories named exactly go_env or go_env-* directly in the clone, through fsutil.RemoveAll because the module cache is read-only. Other golangenv-* versions, files with those names, and lookalikes such as go_envoy/ are untouched.
  • TestGolangInstallRemovesLegacyEnvDirs reproduces it with a read-only go_env-default/pkg/mod/.../goutils@v1.1.1 that has no go.mod.
  • TestInstallEnvironmentsWipesStatelessLeftoverEnv could not fail: its junk module had a go.mod, which ./... skips. Without it, the test now fails when the env wipe is removed.
  • CHANGELOG: a Fixed entry under Unreleased.

Parity

Matches upstream. Upstream never names an env go_env (checked every 4.7.0 ENVIRONMENT_DIR), so these dirs exist only because of this port's pre-4.7.1 naming. Removing them leaves the clone as upstream's would be, and a store shared with Python pre-commit loses nothing.

Checklist

  • pre-commit install is set up, so the commit hook (which lints) ran (golangci-lint included; it catches a planted unused func on these files)
  • Tests do not touch real user state (t.TempDir(), GOPROXY=off)
  • If behavior changed, the differential parity suite still passes (not run; behavior changes only when a go_env* dir exists)
  • Docs updated if this changes something a user can observe (CHANGELOG)
  • I have signed the CLA (see CLA.md)

Evidence

  • The new test fails on main with the error above and passes with the fix. Five mutations, each killed:
    • drop the call;
    • os.RemoveAll instead of fsutil.RemoveAll;
    • match go_env without the -;
    • drop the bare go_env case;
    • drop the IsDir check.
  • End to end, on one gitleaks v8.30.1 clone with a real read-only go_env-default/pkg/mod planted:
    • released v4.7.1 fails with the error above and leaves both legacy dirs;
    • this branch installs cleanly, leaves only golangenv-*, and bin/gitleaks version runs.
  • go test -race: internal/hook and internal/fsutil pass. internal/languages passes, apart from intermittent dial tcp 127.0.0.1:N: can't assign requested address in untouched httptest-based tests (TestGolangInstallDownloadsGo, TestRustInstallEnvironment). Those also fail on an untouched main checkout on the same loaded machine.
  • Not covered: Resolve language_version 'default' where upstream does #161 also renamed the conda, coursier, dart, dotnet and r env dirs. Their old copies remain too; whether any of them breaks an install is unchecked.

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-<version> 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.
@blairham
blairham merged commit 5af1773 into main Nov 15, 2026
23 of 24 checks passed
@blairham
blairham deleted the fix/golang-legacy-env-dirs branch November 15, 2026 18:42
@blairham blairham mentioned this pull request Nov 15, 2026
2 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant