Skip to content

Add support for Go 1.16 #1815

Closed
Closed

Description

@rafikdraoui

Go 1.16 now rejects empty go.mod files (c.f. golang/go@d4986e0). This means that golang hooks cannot be installed anymore:

$ pre-commit install-hooks
[INFO] Initializing environment for local.
[INFO] Installing environment for local.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/usr/local/bin/go', 'get', './...')
return code: 1
expected return code: 0
stdout: (none)
stderr:
    go: no module declaration in go.mod. To specify the module path:
    	go mod edit -module=example.com/mod
    
Check the log at /Users/rdraoui/.cache/pre-commit/pre-commit.log

I could get it to work by changing pre_commit/resources/empty_template_go.mod from an empty file to:

module example/mod

But I'm not all that familiar with how modules work in different Go versions, so maybe making that change has some other implications that I don't know about. This Go Wiki entry seems to suggest that empty go.mod files are legal, but maybe it's outdated... (c.f: #1428 (review))

The fix is compatible with Go 1.15.

I tried validating the fix by running the test suite (pytest tests -k golang), but I can't get it to pass with Go 1.15 and current master branch either.

Activity

  1. asottile commented on Feb 24, 2021

    @asottile
    Member

    hmmm what an annoying change, why would they do this -- the patch seems unrelated to the issue it closes

  2. asottile commented on Feb 24, 2021

    @asottile
    Member

    this is the minimal patch to fix repo: local for golang (as far as I can tell, all other usages of go work already):

    $ git diff
    diff --git a/pre_commit/resources/empty_template_go.mod b/pre_commit/resources/empty_template_go.mod
    index e69de29..fc0ab8c 100644
    --- a/pre_commit/resources/empty_template_go.mod
    +++ b/pre_commit/resources/empty_template_go.mod
    @@ -0,0 +1,3 @@
    +go 1.11
    +
    +module pre-commit-dummy-empty-module

    the testsuite however uses classical gopath-based modules which apparently are outright broken by go 1.16 unless GO111MODULE=auto is set (which is outside the control of pre-commit)

  3. rafikdraoui commented on Feb 25, 2021

    @rafikdraoui
    ContributorAuthor

    In addition, adding the file testing/resources/golang_hooks_repo/go.mod with content module golang-hello-world makes the tests pass under all combinations of Go [1.15|1.16] + GO111MODULE=[""|"on"|"auto"]

    I can make a PR if you want.

  4. asottile commented on Feb 25, 2021

    @asottile
    Member

    sure! it seems surprising that such a testing file is necessary -- makes me think that all hook-producers will need to do the same as well

    I wonder if there's something with our temporary GOPATH setup that might need adjusting as well

    but yeah feel free to send a PR to get the ball rolling!

  5. asottile commented on Feb 25, 2021

    @asottile
    Member

    hmm yeah it might just be that we need to copy the repo remote along here:

    cmd = ('git', 'clone', '--recursive', '.', repo_src_dir)
    helpers.run_setup_cmd(prefix, cmd)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions