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

cmd/go, x/tools/gopls: misleading errors for invalid import #59433

Open
findleyr opened this issue Apr 4, 2023 · 3 comments
Open

cmd/go, x/tools/gopls: misleading errors for invalid import #59433

findleyr opened this issue Apr 4, 2023 · 3 comments
Assignees
Labels
GoCommand cmd/go gopls/incremental gopls/metadata Issues related to metadata loading in gopls 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

@findleyr
Copy link
Contributor

findleyr commented Apr 4, 2023

Just noticed this while refactoring import paths:

image

In this case, I'm in the process of moving the 'tag' package, and the old location no longer exists. I believe the error message is due to the logic here: https://cs.opensource.google/go/x/tools/+/master:internal/gcimporter/iimport.go;l=218;drc=fa556487c5c2be818dd2bab43e16f1afa06f8f89

The problem is that we're now importing packages with broken metadata, and invalid imports appear to have package path "".

@findleyr findleyr added this to the gopls/v0.12.0 milestone Apr 4, 2023
@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 Apr 4, 2023
@findleyr findleyr added release-blocker okay-after-beta1 Used by release team to mark a release-blocker issue as okay to resolve either before or after beta1 labels Apr 17, 2023
@findleyr findleyr self-assigned this Apr 24, 2023
@findleyr
Copy link
Contributor Author

Oh, this appears to simply be a go/packages bug, as I can reproduce without gopls, using x/tools/go/packages/gopackages.

Note that after removing the tag package, the "CompiledGoFiles" field is simply wrong. It picks up the first compiled go file of the jsonrpc2 package.

CC @adonovan @matloob @bcmills

I have not yet reproduced this with go list alone, so it may be a bug in the go list driver)

> gopackages -json golang.org/x/tools/internal/jsonrpc2 golang.org/x/tools/internal/event/tag
{
	"ID": "golang.org/x/tools/internal/event/tag",
	"PkgPath": "golang.org/x/tools/internal/event/tag",
	"Errors": [
		{
			"Pos": "../internal/jsonrpc2/conn.go:16:2",
			"Msg": "no required module provides package golang.org/x/tools/internal/event/tag; to add it:\n\tgo get golang.org/x/tools/internal/event/tag",
			"Kind": 1
		}
	],
	"GoFiles": [
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/conn.go"
	],
	"CompiledGoFiles": [
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/conn.go"
	]
}{
	"ID": "golang.org/x/tools/internal/jsonrpc2",
	"Name": "jsonrpc2",
	"PkgPath": "golang.org/x/tools/internal/jsonrpc2",
	"GoFiles": [
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/conn.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/handler.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/jsonrpc2.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/messages.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/serve.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/stream.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/wire.go"
	],
	"CompiledGoFiles": [
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/conn.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/handler.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/jsonrpc2.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/messages.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/serve.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/stream.go",
		"/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2/wire.go"
	],
	"Imports": {
		"bufio": "bufio",
		"context": "context",
		"encoding/json": "encoding/json",
		"errors": "errors",
		"fmt": "fmt",
		"golang.org/x/tools/internal/event": "golang.org/x/tools/internal/event",
		"golang.org/x/tools/internal/event/label": "golang.org/x/tools/internal/event/label",
		"golang.org/x/tools/internal/event/tag": "golang.org/x/tools/internal/event/tag",
		"io": "io",
		"math": "math",
		"net": "net",
		"os": "os",
		"strconv": "strconv",
		"strings": "strings",
		"sync": "sync",
		"sync/atomic": "sync/atomic",
		"time": "time"
	}
}

@findleyr
Copy link
Contributor Author

findleyr commented May 1, 2023

Actually, this is a Go command bug. Compare the two go list invocations below. When the jsonrpc2 package is included, the error for internal/event/tag erroneously uses a position from the jsonrpc2 package.

go/packages interprets this error to synthesize missing GoFiles, due to #39986.

> go list -e -json -compiled=true -test=false -export=false -find=false -- golang.org/x/tools/internal/event/tag
{
	"ImportPath": "golang.org/x/tools/internal/event/tag",
	"Match": [
		"golang.org/x/tools/internal/event/tag"
	],
	"Incomplete": true,
	"Error": {
		"ImportStack": [],
		"Pos": "",
		"Err": "no required module provides package golang.org/x/tools/internal/event/tag; to add it:\n\tgo get golang.org/x/tools/internal/event/tag"
	}
}

vs.

> go list -e -json -compiled=true -test=false -export=false -find=false -- golang.org/x/tools/internal/jsonrpc2 golang.org/x/tools/internal/event/tag
{
	"Dir": "/usr/local/google/home/rfindley/src/tools2/internal/jsonrpc2",
	"ImportPath": "golang.org/x/tools/internal/jsonrpc2",
	"Name": "jsonrpc2",
	"Doc": "Package jsonrpc2 is a minimal implementation of the JSON RPC 2 spec.",
	"Root": "/usr/local/google/home/rfindley/src/tools2",
	"Module": {
		"Path": "golang.org/x/tools",
		"Main": true,
		"Dir": "/usr/local/google/home/rfindley/src/tools2",
		"GoMod": "/usr/local/google/home/rfindley/src/tools2/go.mod",
		"GoVersion": "1.18"
	},
	"Match": [
		"golang.org/x/tools/internal/jsonrpc2"
	],
	"Incomplete": true,
	"Stale": true,
	"StaleReason": "stale dependency: golang.org/x/tools/internal/event/tag",
	"GoFiles": [
		"conn.go",
		"handler.go",
		"jsonrpc2.go",
		"messages.go",
		"serve.go",
		"stream.go",
		"wire.go"
	],
	"CompiledGoFiles": [
		"conn.go",
		"handler.go",
		"jsonrpc2.go",
		"messages.go",
		"serve.go",
		"stream.go",
		"wire.go"
	],
	"Imports": [
		"bufio",
		"context",
		"encoding/json",
		"errors",
		"fmt",
		"golang.org/x/tools/internal/event",
		"golang.org/x/tools/internal/event/label",
		"golang.org/x/tools/internal/event/tag",
		"io",
		"math",
		"net",
		"os",
		"strconv",
		"strings",
		"sync",
		"sync/atomic",
		"time"
	],
	"Deps": [
		"bufio",
		"bytes",
		"context",
		"encoding",
		"encoding/base64",
		"encoding/binary",
		"encoding/json",
		"errors",
		"fmt",
		"golang.org/x/tools/internal/event",
		"golang.org/x/tools/internal/event/core",
		"golang.org/x/tools/internal/event/keys",
		"golang.org/x/tools/internal/event/label",
		"golang.org/x/tools/internal/event/tag",
		"internal/abi",
		"internal/bytealg",
		"internal/coverage/rtcov",
		"internal/cpu",
		"internal/fmtsort",
		"internal/goarch",
		"internal/godebug",
		"internal/goexperiment",
		"internal/goos",
		"internal/intern",
		"internal/itoa",
		"internal/nettrace",
		"internal/oserror",
		"internal/poll",
		"internal/race",
		"internal/reflectlite",
		"internal/safefilepath",
		"internal/singleflight",
		"internal/syscall/execenv",
		"internal/syscall/unix",
		"internal/testlog",
		"internal/unsafeheader",
		"io",
		"io/fs",
		"math",
		"math/bits",
		"net",
		"net/netip",
		"os",
		"path",
		"reflect",
		"runtime",
		"runtime/cgo",
		"runtime/internal/atomic",
		"runtime/internal/math",
		"runtime/internal/sys",
		"runtime/internal/syscall",
		"sort",
		"strconv",
		"strings",
		"sync",
		"sync/atomic",
		"syscall",
		"time",
		"unicode",
		"unicode/utf16",
		"unicode/utf8",
		"unsafe",
		"vendor/golang.org/x/net/dns/dnsmessage"
	],
	"DepsErrors": [
		{
			"ImportStack": [
				"golang.org/x/tools/internal/event/tag"
			],
			"Pos": "internal/jsonrpc2/conn.go:16:2",
			"Err": "no required module provides package golang.org/x/tools/internal/event/tag; to add it:\n\tgo get golang.org/x/tools/internal/event/tag"
		}
	],
	"TestGoFiles": [
		"serve_test.go"
	],
	"TestImports": [
		"context",
		"golang.org/x/tools/internal/stack/stacktest",
		"golang.org/x/tools/internal/testenv",
		"net",
		"sync",
		"testing",
		"time"
	],
	"XTestGoFiles": [
		"jsonrpc2_test.go",
		"wire_test.go"
	],
	"XTestImports": [
		"bytes",
		"context",
		"encoding/json",
		"flag",
		"fmt",
		"golang.org/x/tools/internal/event/export/eventtest",
		"golang.org/x/tools/internal/jsonrpc2",
		"golang.org/x/tools/internal/stack/stacktest",
		"net",
		"path",
		"reflect",
		"testing"
	]
}
{
	"ImportPath": "golang.org/x/tools/internal/event/tag",
	"Match": [
		"golang.org/x/tools/internal/event/tag"
	],
	"Incomplete": true,
	"Stale": true,
	"StaleReason": "build ID mismatch",
	"Error": {
		"ImportStack": [
			"golang.org/x/tools/internal/event/tag"
		],
		"Pos": "internal/jsonrpc2/conn.go:16:2",
		"Err": "no required module provides package golang.org/x/tools/internal/event/tag; to add it:\n\tgo get golang.org/x/tools/internal/event/tag"
	}
}

@findleyr findleyr added the GoCommand cmd/go label May 1, 2023
@findleyr findleyr changed the title x/tools/gopls: misleading errors for invalid import cmd/go, x/tools/gopls: misleading errors for invalid import May 1, 2023
@findleyr findleyr removed release-blocker okay-after-beta1 Used by release team to mark a release-blocker issue as okay to resolve either before or after beta1 labels May 1, 2023
@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 2, 2023
@findleyr findleyr modified the milestones: gopls/v0.12.0, gopls/v0.12.1 May 23, 2023
@findleyr findleyr modified the milestones: gopls/v0.12.1, gopls/v0.12.2 Jun 1, 2023
@findleyr findleyr modified the milestones: gopls/v0.12.3, gopls/v0.12.4 Jun 14, 2023
@findleyr findleyr modified the milestones: gopls/v0.12.4, gopls/v0.12.5 Jun 22, 2023
@findleyr findleyr modified the milestones: gopls/v0.12.5, gopls/v0.13.0 Jul 18, 2023
@adonovan adonovan added the gopls/metadata Issues related to metadata loading in gopls label Aug 31, 2023
@findleyr findleyr modified the milestones: gopls/v0.14.0, gopls/v0.15.0 Oct 9, 2023
@findleyr
Copy link
Contributor Author

findleyr commented Jan 6, 2024

Moving this to the backlog, since it's a Go command bug and therefore not particularly actionable for gopls.

@findleyr findleyr modified the milestones: gopls/v0.15.0, gopls/backlog Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go gopls/incremental gopls/metadata Issues related to metadata loading in gopls 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

4 participants