Skip to content

Commit cb14bc2

Browse files
authored
Merge pull request pre-commit#3304 from AleksaC/go-toolchain
disable automatic toolchain switching for golang hooks
2 parents 74233a1 + 109628c commit cb14bc2

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

pre_commit/languages/golang.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def get_env_patch(venv: str, version: str) -> PatchesT:
7575

7676
return (
7777
('GOROOT', os.path.join(venv, '.go')),
78+
('GOTOOLCHAIN', 'local'),
7879
(
7980
'PATH', (
8081
os.path.join(venv, 'bin'), os.pathsep,
@@ -145,6 +146,7 @@ def install_environment(
145146
env = no_git_env(dict(os.environ, GOPATH=gopath))
146147
env.pop('GOBIN', None)
147148
if version != 'system':
149+
env['GOTOOLCHAIN'] = 'local'
148150
env['GOROOT'] = os.path.join(env_dir, '.go')
149151
env['PATH'] = os.pathsep.join((
150152
os.path.join(env_dir, '.go', 'bin'), os.environ['PATH'],

tests/languages/golang_test.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
from pre_commit.envcontext import envcontext
1212
from pre_commit.languages import golang
1313
from pre_commit.store import _make_local_repo
14+
from pre_commit.util import CalledProcessError
1415
from pre_commit.util import cmd_output
1516
from testing.fixtures import add_config_to_repo
1617
from testing.fixtures import make_config_from_repo
1718
from testing.language_helpers import run_language
1819
from testing.util import cmd_output_mocked_pre_commit_home
20+
from testing.util import cwd
1921
from testing.util import git_commit
2022

2123

@@ -165,3 +167,70 @@ def test_during_commit_all(tmp_path, tempdir_factory, store, in_git_dir):
165167
fn=cmd_output_mocked_pre_commit_home,
166168
tempdir_factory=tempdir_factory,
167169
)
170+
171+
172+
def test_automatic_toolchain_switching(tmp_path):
173+
go_mod = '''\
174+
module toolchain-version-test
175+
176+
go 1.23.1
177+
'''
178+
main_go = '''\
179+
package main
180+
181+
func main() {}
182+
'''
183+
tmp_path.joinpath('go.mod').write_text(go_mod)
184+
mod_dir = tmp_path.joinpath('toolchain-version-test')
185+
mod_dir.mkdir()
186+
main_file = mod_dir.joinpath('main.go')
187+
main_file.write_text(main_go)
188+
189+
with pytest.raises(CalledProcessError) as excinfo:
190+
run_language(
191+
path=tmp_path,
192+
language=golang,
193+
version='1.22.0',
194+
exe='golang-version-test',
195+
)
196+
197+
assert 'go.mod requires go >= 1.23.1' in excinfo.value.stderr.decode()
198+
199+
200+
def test_automatic_toolchain_switching_go_fmt(tmp_path, monkeypatch):
201+
go_mod_hook = '''\
202+
module toolchain-version-test
203+
204+
go 1.22.0
205+
'''
206+
go_mod = '''\
207+
module toolchain-version-test
208+
209+
go 1.23.1
210+
'''
211+
main_go = '''\
212+
package main
213+
214+
func main() {}
215+
'''
216+
hook_dir = tmp_path.joinpath('hook')
217+
hook_dir.mkdir()
218+
hook_dir.joinpath('go.mod').write_text(go_mod_hook)
219+
220+
test_dir = tmp_path.joinpath('test')
221+
test_dir.mkdir()
222+
test_dir.joinpath('go.mod').write_text(go_mod)
223+
main_file = test_dir.joinpath('main.go')
224+
main_file.write_text(main_go)
225+
226+
with cwd(test_dir):
227+
ret, out = run_language(
228+
path=hook_dir,
229+
language=golang,
230+
version='1.22.0',
231+
exe='go fmt',
232+
file_args=(str(main_file),),
233+
)
234+
235+
assert ret == 1
236+
assert 'go.mod requires go >= 1.23.1' in out.decode()

0 commit comments

Comments
 (0)