KEMBAR78
disable automatic toolchain switching for golang hooks by AleksaC · Pull Request #3304 · pre-commit/pre-commit · GitHub
Skip to content

Conversation

@AleksaC
Copy link
Contributor

@AleksaC AleksaC commented Sep 18, 2024

Resolves #3300

I only disabled the toolchain switching if the version is not system. I did this because system is the default if go is installed globally and no language_version is explicitly specified. In this case users likely doesn't care which version of go is used as long as their hooks are working, so I think it makes more sense to allow the toolchain switching since it doesn't break user expectations and other than a small performance hit on hook installation doesn't have negative impact.

If we were to disable toolchain switching regardless of the version, many hooks would break since many environments lag with their go version. For example, in GitHub-hosted runners for GitHub Actions the pre-installed go version is 1.21 which is two versions behind the current version, so it's likely that a lot of hooks work thanks to automatic toolchain switching without even knowing.

@AleksaC
Copy link
Contributor Author

AleksaC commented Sep 19, 2024

Unrelated to this PR, but mypy fails with Module has no attribute "sched_getaffinity" likely because it's not available on MacOS:

return len(os.sched_getaffinity(0))

Replacing try/except with hasattr would fix it, but I didn't want to change it because it's outside the scope of the current PR.

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.

exe='golang-version-test',
)

assert 'go.mod requires go >= 1.23.1' in excinfo.value
Copy link
Member

Choose a reason for hiding this comment

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

this line is never run because it is indented

language=golang,
version='1.22.0',
exe='go fmt',
file_args=(str(main_file)),
Copy link
Member

Choose a reason for hiding this comment

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

this is just a parenthesized string and should be a tuple I believe -- I'm kinda surprised the type checker was ok with this? (oh duh, str is a Sequence[str] so it just kinda lets it happen)


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.

Comment on lines 225 to 226
with monkeypatch.context() as m:
m.chdir(str(test_dir))
Copy link
Member

Choose a reason for hiding this comment

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

there's a helper for this I believe (I think it's called with chdir(...)? or with cwd(...))

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

@asottile asottile enabled auto-merge November 25, 2024 23:48
@asottile asottile merged commit cb14bc2 into pre-commit:main Nov 25, 2024
24 checks passed
Copy link

@WojasKrk WojasKrk left a comment

Choose a reason for hiding this comment

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

X

@pre-commit pre-commit locked as off-topic and limited conversation to collaborators Jan 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Disable automatic toolchain switching in golang

3 participants