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
2 changes: 2 additions & 0 deletions pre_commit/languages/golang.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def get_env_patch(venv: str, version: str) -> PatchesT:

return (
('GOROOT', os.path.join(venv, '.go')),
('GOTOOLCHAIN', 'local'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this break with language_version: system? this seems different than the install code below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the language_version is system the function is going to return before reaching that line, so it's consistent with the install code.

(
'PATH', (
os.path.join(venv, 'bin'), os.pathsep,
Expand Down Expand Up @@ -145,6 +146,7 @@ def install_environment(
env = no_git_env(dict(os.environ, GOPATH=gopath))
env.pop('GOBIN', None)
if version != 'system':
env['GOTOOLCHAIN'] = 'local'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (?) this should be part of get_env_patch ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can see get_env_patch is only called from in_env which is used when running the hook and not during the installation. This should only be relevant during installation because that's when the build happens.

I ran the test to make sure it works this way and it breaks when I move it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm I guess? but what about for hooks that just call gofmt -- I know the rust toolchain gets grumbly when the versions mismatch -- does something like this survive?

-   repo: local
    hooks:
    -   id: gofmt
        name: gofmt
        language: golang
        entry: gofmt -l -w
        types: [go]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gofmt doesn't seem to care about this option, but go fmt performs the toolchain switch. Looking at the code it seems that any go command will try to perform a toolchain switch.

I guess the variable should be set in both places then.

env['GOROOT'] = os.path.join(env_dir, '.go')
env['PATH'] = os.pathsep.join((
os.path.join(env_dir, '.go', 'bin'), os.environ['PATH'],
Expand Down
69 changes: 69 additions & 0 deletions tests/languages/golang_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
from pre_commit.envcontext import envcontext
from pre_commit.languages import golang
from pre_commit.store import _make_local_repo
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
from testing.fixtures import add_config_to_repo
from testing.fixtures import make_config_from_repo
from testing.language_helpers import run_language
from testing.util import cmd_output_mocked_pre_commit_home
from testing.util import cwd
from testing.util import git_commit


Expand Down Expand Up @@ -165,3 +167,70 @@ def test_during_commit_all(tmp_path, tempdir_factory, store, in_git_dir):
fn=cmd_output_mocked_pre_commit_home,
tempdir_factory=tempdir_factory,
)


def test_automatic_toolchain_switching(tmp_path):
go_mod = '''\
module toolchain-version-test

go 1.23.1
'''
main_go = '''\
package main

func main() {}
'''
tmp_path.joinpath('go.mod').write_text(go_mod)
mod_dir = tmp_path.joinpath('toolchain-version-test')
mod_dir.mkdir()
main_file = mod_dir.joinpath('main.go')
main_file.write_text(main_go)

with pytest.raises(CalledProcessError) as excinfo:
run_language(
path=tmp_path,
language=golang,
version='1.22.0',
exe='golang-version-test',
)

assert 'go.mod requires go >= 1.23.1' in excinfo.value.stderr.decode()


def test_automatic_toolchain_switching_go_fmt(tmp_path, monkeypatch):
go_mod_hook = '''\
module toolchain-version-test

go 1.22.0
'''
go_mod = '''\
module toolchain-version-test

go 1.23.1
'''
main_go = '''\
package main

func main() {}
'''
hook_dir = tmp_path.joinpath('hook')
hook_dir.mkdir()
hook_dir.joinpath('go.mod').write_text(go_mod_hook)

test_dir = tmp_path.joinpath('test')
test_dir.mkdir()
test_dir.joinpath('go.mod').write_text(go_mod)
main_file = test_dir.joinpath('main.go')
main_file.write_text(main_go)

with cwd(test_dir):
ret, out = run_language(
path=hook_dir,
language=golang,
version='1.22.0',
exe='go fmt',
file_args=(str(main_file),),
)

assert ret == 1
assert 'go.mod requires go >= 1.23.1' in out.decode()