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: incorrect rewrite import for "error" #63592

Closed
pierrre opened this issue Oct 17, 2023 · 3 comments
Closed

x/tools/gopls: incorrect rewrite import for "error" #63592

pierrre opened this issue Oct 17, 2023 · 3 comments
Assignees
Labels
gopls/imports gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@pierrre
Copy link

pierrre commented Oct 17, 2023

gopls version

➜  ~ gopls version
golang.org/x/tools/gopls v0.13.2
    golang.org/x/tools/gopls@v0.13.2 h1:Pyvx6MKvatbX3zzZmdGiFRfQZl0ohPlt2sFxO/5j6Ro=

go env

go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/pierre/.cache/go-build'
GOENV='/home/pierre/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/pierre/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/pierre/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/pierre/.gimme/versions/go1.21.3.src'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/pierre/.gimme/versions/go1.21.3.src/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/pierre/Git/pierrre/assert/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1722284209=/tmp/go-build -gno-record-gcc-switches'

What did you do?

My code contains this global variable:

var ErrorStringer = error.Error

error refers to the pre-declared error interface.
So error.Error is the function implemented by this interface that returns a string.

What did you expect to see?

When I save my code in my editor, it should save it without changing my imports.

What did you see instead?

When I save the file, gopls rewrites my imports and adds github.com/coreos/etcd/error, which breaks my code.

Editor and settings

{
	"window.titleBarStyle": "custom",

	"workbench.startupEditor": "none",
	"workbench.settings.editor": "json",
	"workbench.settings.useSplitJSON": true,
	"workbench.list.horizontalScrolling": true,
	"workbench.colorTheme": "Deepdark Material Theme | Full Black Version",

	"editor.fontFamily": "Monospace",
	"editor.fontSize": 12,
	"editor.lineHeight": 14,
	"editor.fontLigatures": "'ccmp' off", // Fix for https://github.com/microsoft/vscode/issues/106583
	"editor.renderWhitespace": "all",
	"editor.wordWrap": "on",
	"editor.insertSpaces": false,
	"editor.smoothScrolling": true,
	"editor.minimap.enabled": false,
	"editor.minimap.autohide": true,
	"editor.minimap.renderCharacters": false,
	"editor.inlineSuggest.enabled": true,
	"editor.stickyScroll.enabled": true,

	"files.enableTrash": false,
	"files.autoSave": "onFocusChange",
	"files.trimTrailingWhitespace": true,
	"files.insertFinalNewline": true,
	"files.trimFinalNewlines": true,
	"files.associations": {
		"Dockerfile*": "dockerfile"
	},

	"terminal.integrated.fontSize": 12,
	"terminal.integrated.scrollback": 100000,

	"search.showLineNumbers": true,

	"git.autofetch": true,
	"git.allowForcePush": true,
	"git.confirmForcePush": false,
	"git.enableSmartCommit": true,
	"git.pruneOnFetch": true,

	"redhat.telemetry.enabled": false,

	"github.copilot.enable": {
		"*": true,
		"yaml": true,
		"plaintext": true,
		"markdown": true
	},

	"go.buildFlags": [
		"-gcflags=-e"
	],
	"go.testOnSave": true,
	"go.testExplorer.showOutput": false,
	"go.testFlags": [
		"-v",
		"-fullpath",
		"-short"
	],
	"go.testEnvVars": {
		"ASSERTAUTO_UPDATE": "true",
		"UPDATE_GOLDENS": "true",
	},
	"go.coverOnSave": true,
	"go.coverageDecorator": {
		"type": "gutter",
		"coveredGutterStyle": "blockgreen",
		"uncoveredGutterStyle": "blockred",
		"coveredHighlightColor": "rgba(0,255,0,0.08)",
		"uncoveredHighlightColor": "rgba(255,0,0,0.08)"
	},
	"go.lintOnSave": "package",
	"go.lintTool": "golangci-lint",
	"go.lintFlags": [
		"--fix"
	],
	"gopls": {
		"verboseOutput": true,
		"ui.completion.usePlaceholders": true,
		"ui.codelenses": {
			"gc_details": true
		},
		"ui.semanticTokens": true,
	},
	"go.toolsManagement.autoUpdate": true,
	"go.toolsEnvVars": {
		"GOPRIVATE": "",
		"GOGC":"10",
	},
	/*
	"go.inlayHints.assignVariableTypes": true,
	"go.inlayHints.compositeLiteralFields": true,
	"go.inlayHints.compositeLiteralTypes": true,
	"go.inlayHints.constantValues": true,
	"go.inlayHints.functionTypeParameters": true,
	"go.inlayHints.parameterNames": true,
	"go.inlayHints.rangeVariableTypes": true,
	*/
	"[python]": {
		"editor.formatOnType": true
	},
}

Logs

[Info  - 15:34:21] 2023/10/17 15:34:21 fixImports(filename="/home/pierre/Git/pierrre/assert/error.go"), abs="/home/pierre/Git/pierrre/assert/error.go", srcDir="/home/pierre/Git/pierrre/assert" ...

[Info  - 15:34:21] 2023/10/17 15:34:21 loading package names for 48 packages

[Info  - 15:34:21] 2023/10/17 15:34:21 done loading package names for 48 packages

[Info  - 15:34:21] 2023/10/17 15:34:21 error candidate 1/1: github.com/coreos/etcd/error in /home/pierre/go/pkg/mod/github.com/coreos/etcd@v3.3.10+incompatible/error

[Info  - 15:34:21] 2023/10/17 15:34:21 loading exports in dir /home/pierre/go/pkg/mod/github.com/coreos/etcd@v3.3.10+incompatible/error (seeking package error)

[Info  - 15:34:23] 2023/10/17 15:34:23 fixImports(filename="/home/pierre/Git/pierrre/assert/error.go"), abs="/home/pierre/Git/pierrre/assert/error.go", srcDir="/home/pierre/Git/pierrre/assert" ...

[Info  - 15:34:23] 2023/10/17 15:34:23 loading package names for 48 packages

[Info  - 15:34:23] 2023/10/17 15:34:23 done loading package names for 48 packages

[Info  - 15:34:23] 2023/10/17 15:34:23 loading exports in dir /home/pierre/go/pkg/mod/github.com/coreos/etcd@v3.3.10+incompatible/error (seeking package error)

[Info  - 15:34:23] 2023/10/17 15:34:23 error candidate 1/1: github.com/coreos/etcd/error in /home/pierre/go/pkg/mod/github.com/coreos/etcd@v3.3.10+incompatible/error
@pierrre pierrre added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Oct 17, 2023
@gopherbot gopherbot added this to the Unreleased milestone Oct 17, 2023
@pierrre
Copy link
Author

pierrre commented Oct 17, 2023

If I remove /home/pierre/go/pkg/mod/github.com/coreos/etcd@v3.3.10+incompatible/error, gopls doesn't rewrite my import.

@pierrre pierrre changed the title x/tools/gopls: automatic import rewrite for "error" x/tools/gopls: incorrect rewrite import for "error" Oct 23, 2023
@ansaba ansaba added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 23, 2023
@findleyr
Copy link
Contributor

Thanks for the report. This is an unfortunate edge case, and my guess is that it has been a bug for a long time.

We will fix, though I would also advise against using package names that shadow the 'error' type.

I suspect that the fix is to teach goimports not to shadow predeclared names.

@findleyr findleyr modified the milestones: Unreleased, gopls/v0.15.0 Oct 24, 2023
@adonovan adonovan self-assigned this Jan 19, 2024
@gopherbot
Copy link

Change https://go.dev/cl/557216 mentions this issue: internal/imports: don't add imports that shadow predeclared names

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/imports gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants