Skip to content

Commit fe5390c

Browse files
committed
Ensure that GOBIN is not set when installing a golang hook
If GOBIN is set, it will be used as the install path instead of the first item from GOPATH followed by "/bin". If it is used, commands will not be isolated between different repos.
1 parent 6bc7b91 commit fe5390c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pre_commit/languages/golang.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pre_commit.constants as C
88
from pre_commit import git
99
from pre_commit.envcontext import envcontext
10+
from pre_commit.envcontext import UNSET
1011
from pre_commit.envcontext import Var
1112
from pre_commit.languages import helpers
1213
from pre_commit.util import clean_path_on_failure
@@ -21,6 +22,7 @@
2122

2223
def get_env_patch(venv):
2324
return (
25+
('GOBIN', UNSET),
2426
('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))),
2527
)
2628

@@ -69,6 +71,7 @@ def install_environment(prefix, version, additional_dependencies):
6971
else:
7072
gopath = directory
7173
env = dict(os.environ, GOPATH=gopath)
74+
env.pop('GOBIN', None)
7275
cmd_output('go', 'get', './...', cwd=repo_src_dir, env=env)
7376
for dependency in additional_dependencies:
7477
cmd_output('go', 'get', dependency, cwd=repo_src_dir, env=env)

tests/repository_test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pre_commit import parse_shebang
1515
from pre_commit.clientlib import CONFIG_SCHEMA
1616
from pre_commit.clientlib import load_manifest
17+
from pre_commit.envcontext import envcontext
1718
from pre_commit.languages import golang
1819
from pre_commit.languages import helpers
1920
from pre_commit.languages import node
@@ -71,7 +72,7 @@ def _test_hook_repo(
7172
path = make_repo(tempdir_factory, repo_path)
7273
config = make_config_from_repo(path, **(config_kwargs or {}))
7374
ret = _get_hook(config, store, hook_id).run(args)
74-
assert ret[0] == expected_return_code
75+
assert ret[0] == expected_return_code, "output was: {}".format(ret[1])
7576
assert _norm_out(ret[1]) == expected
7677

7778

@@ -267,6 +268,16 @@ def test_golang_hook(tempdir_factory, store):
267268
)
268269

269270

271+
def test_golang_hook_still_works_when_gobin_is_set(tempdir_factory, store):
272+
gobin_dir = tempdir_factory.get()
273+
with envcontext([('GOBIN', gobin_dir)]):
274+
_test_hook_repo(
275+
tempdir_factory, store, 'golang_hooks_repo',
276+
'golang-hook', [], b'hello world\n',
277+
)
278+
assert os.listdir(gobin_dir) == [], "hook should not be installed in $GOBIN"
279+
280+
270281
def test_rust_hook(tempdir_factory, store):
271282
_test_hook_repo(
272283
tempdir_factory, store, 'rust_hooks_repo',

0 commit comments

Comments
 (0)