Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: Go generate fails when net/http is imported in source file #51063

Closed
mpetronic opened this issue Feb 8, 2022 · 2 comments
Closed
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@mpetronic
Copy link

gopls version

golang.org/x/tools/gopls v0.7.5
    golang.org/x/tools/gopls@v0.7.5 h1:8Az52YwcFXTWPvrRomns1C0N+zlgTyyPKWvRazO9GG8=
    github.com/BurntSushi/toml@v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=
    github.com/google/go-cmp@v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/mod@v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
    golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
    golang.org/x/sys@v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
    golang.org/x/text@v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
    golang.org/x/tools@v0.1.9-0.20220114220130-fd7798718afd h1:lTnuArxJC+n54TyvWUPyHhrnGxYvhSi13/aM2Ndr4bs=
    golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
    honnef.co/go/tools@v0.2.1 h1:/EPr//+UMMXwMTkXvCCoaJDq8cpjMO80Ou+L4PDo2mY=
    mvdan.cc/gofumpt@v0.1.1 h1:bi/1aS/5W00E2ny5q65w9SnKpWEF/UIOqDYBILpo9rA=
    mvdan.cc/xurls/v2@v2.3.0 h1:59Olnbt67UKpxF1EwVBopJvkSUBmgtb468E4GVWIZ1I=

go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mpetronic/.cache/go-build"
GOENV="/home/mpetronic/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/mpetronic/go/pkg/mod"
GONOPROXY="bitbucket.org/hnsdevops"
GONOSUMDB="bitbucket.org/hnsdevops"
GOOS="linux"
GOPATH="/home/mpetronic/go"
GOPRIVATE="bitbucket.org/hnsdevops"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2444120814=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I created a wrapper around http.Client in order to mock the client in my code using dependency injection. When I try to generate the mock for my interface, it fails. However, if I remove "import "net/http" and all references to it from my wrapper code, I can generate the mock. Here is the very simple wrapper that fails to generate the mock file:

package httpclient

//go:generate mockgen -destination=mock_httpclient.go -package=httpclient -source=httpclient.go IHttpClient

import "net/http"

type IHttpClient interface {
	Get(url string) (resp *http.Response, err error)
}

var _ IHttpClient = (*HttpClient)(nil)

type HttpClient struct {
	client *http.Client
}

func NewHttpClient() *HttpClient {
	return &HttpClient{
		client: http.DefaultClient,
	}
}

func (c *HttpClient) Get(url string) (*http.Response, error) {
	return c.client.Get(url)
}

Running go generate against this results in errors.

go generate -v 
httpclient.go
/usr/local/go/src/internal/abi/abi.go:70:27: invalid operation: shift count (i % 8) (value of type int) must be unsigned integer
/usr/local/go/src/internal/abi/abi.go:60:24: invalid operation: shift count (i % 8) (value of type int) must be unsigned integer
/usr/local/go/src/runtime/type.go:466:23: invalid operation: shift count (7 * i) (value of type int) must be unsigned integer
/usr/local/go/src/runtime/proc.go:5834:23: invalid operation: shift count (id % 32) (value of type int32) must be unsigned integer
/usr/local/go/src/runtime/proc.go:5827:23: invalid operation: shift count (id % 32) (value of type int32) must be unsigned integer
<------ SNIP tons of errors (see log attached for full details) ------->
/usr/local/go/src/net/http/h2_bundle.go:10189:32: MaxInt32 not declared by package math
/usr/local/go/src/net/http/h2_bundle.go:9188:20: MaxInt32 not declared by package math
/usr/local/go/src/net/http/h2_bundle.go:7425:61: MaxInt32 not declared by package math
/usr/local/go/src/net/http/h2_bundle.go:4042:37: MaxUint32 not declared by package math
2022/02/07 23:41:41 Loading input failed: loading package failed
httpclient.go:3: running "mockgen": exit status 1

However, if I change the code to this, to remove net/http just as a test, it generates the mock file perfectly well...

package httpclient

//go:generate mockgen -destination=mock_httpclient.go -package=httpclient -source=httpclient.go IHttpClient

type IHttpClient interface {
	Get(url string) (resp string, err error)
}

var _ IHttpClient = (*HttpClient)(nil)

type HttpClient struct {
	client string
}

func NewHttpClient() *HttpClient {
	return &HttpClient{
		client: "something",
	}
}

func (c *HttpClient) Get(url string) (string, error) {
	return c.client, nil
}

Another oddity is, if I do generate the mock successfully using the altered code, if I try to regenerate it, that also fails. If I first delete the generated mock file, however, then I can regenerate it.

What did you expect to see?

Properly generated mock files for my interface

What did you see instead?

The errors shown above in context of the code examples, and in attached logs

Editor and settings

{
    "explorer.confirmDelete": false,
    "editor.fontSize": 12,
    "terminal.integrated.fontSize": 12,
    "workbench.editorAssociations": {
        "*.ipynb": "jupyter-notebook"
    },
    "go.toolsManagement.autoUpdate": true,
    "typescript.format.insertSpaceAfterTypeAssertion": true,
    "git.confirmSync": false,
    "debug.console.fontSize": 12,
    "debug.console.lineHeight": 14,
    "scm.inputFontSize": 12,
    "markdown.preview.fontSize": 10,
    "notebook.cellToolbarLocation": {
        "default": "right",
        "jupyter-notebook": "left"
    },
    "security.workspace.trust.untrustedFiles": "open",
    "git.allowForcePush": true,
    "go.useLanguageServer": true,
    "go.buildOnSave": "off",
    "[go]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true,
        },
        "editor.snippetSuggestions": "none",
    },
    "[go.mod]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true,
        },
    },
    "go.languageServerFlags": [
        "serve",
        "-rpc.trace",
        "-debug=localhost:6060",
        "-logfile=/var/tmp/gopls.log"
    ],
    "gopls": {
        "usePlaceholders": true,
    },
    "go.buildFlags": [
        "-tags=unit,integration",
    ],
    "go.delveConfig": {
        "dlvLoadConfig": {
            "followPointers": true,
            "maxVariableRecurse": 1,
            "maxStringLen": 256,
            "maxArrayValues": 64,
            "maxStructFields": -1
        },
        "apiVersion": 2,
        "showGlobalVariables": true,
        "debugAdapter": "legacy",
        "substitutePath": [],
    },
    "go.coverageDecorator": {
        "type": "gutter",
        "coveredGutterStyle": "slashgreen",
        "uncoveredGutterStyle": "slashred"
    },
    "launch": {
        "configurations": [],
        "compounds": []
    },
    "go.coverMode": "atomic",
    "rewrap.wrappingColumn": 100,
    "html.format.extraLiners": "",
    "go.testFlags": [
        "-v"
    ],
    "python.formatting.autopep8Args": [
        "max_line_length = 120"
    ],
    "files.trimTrailingWhitespace": true,
    "git-graph.customPullRequestProviders": [
        {
            "name": "Bitbucket",
            "templateUrl": "$1/$2/$3/pull-requests/new?source=$2/$3::$4&dest=$5/$6::$8"
        }
    ],
    "yaml.schemas": {
        "file:///home/mpetronic/.vscode-server/extensions/atlassian.atlascode-2.10.7/resources/schemas/pipelines-schema.json": "bitbucket-pipelines.yml"
    },
    "redhat.telemetry.enabled": false,
    "atlascode.bitbucket.enabled": false,
    "atlascode.jira.enabled": false,
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "explorer.confirmDragAndDrop": false,
    "python.formatting.provider": "black",
    "diffEditor.ignoreTrimWhitespace": false,
    "atlascode.jira.lastCreateSiteAndProject": {
        "siteId": "",
        "projectKey": ""
    },
    "workbench.startupEditor": "none",
    "rewrap.autoWrap.enabled": true,
    "cSpell.customDictionaries": {
        "custom-dictionary-user": {
            "name": "custom-dictionary-user",
            "path": "~/.cspell/custom-dictionary-user.txt",
            "addWords": true,
            "scope": "user"
        }
    },
    "editor.unicodeHighlight.invisibleCharacters": false,
    "editor.unicodeHighlight.ambiguousCharacters": false,
    "[perl]": {
        "editor.defaultFormatter": "sfodje.perltidy"
    },
    "perltidy.additionalArguments": [
        "-nolq",
        "-l=120"
    ],
    "perl.perlInc": [
        "/home/mpetronic/repos/smarts-collector/perl",
        "/home/mpetronic/perl",
    ],
    "editor.formatOnSave": true,
    "files.autoSave": "afterDelay",
    "perl.cacheDir": "/home/mpetronic/vscode-cache"
}

Logs

gopls.log

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Feb 8, 2022
@gopherbot gopherbot added this to the Unreleased milestone Feb 8, 2022
@mpetronic mpetronic changed the title x/tools/gopls: Go generate fails when net/http in imported in source file x/tools/gopls: Go generate fails when net/http is imported in source file Feb 8, 2022
@seankhliao
Copy link
Member

This looks unrelated to either gopls or go generate and seems to be a failure of mockgen, please report the problem there.

@AndresDuran54
Copy link

Hello @mpetronic , I am having the same problem, did you manage to solve it?

@golang golang locked and limited conversation to collaborators Jul 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants