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/imports: imports.Process chooses wrong import paths when v2 module is used #44737

Open
IlyaFloppy opened this issue Mar 2, 2021 · 3 comments

Comments

@IlyaFloppy
Copy link

@IlyaFloppy IlyaFloppy commented Mar 2, 2021

What version of Go are you using (go version)?

go version go1.16 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ilya/Library/Caches/go-build"
GOENV="/Users/ilya/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/ilya/go/pkg/mod"
GONOPROXY="gitlab.mstar.site"
GONOSUMDB="gitlab.mstar.site"
GOOS="darwin"
GOPATH="/Users/ilya/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ilya/Development/importsbuglib/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/vs/hy8s9kz12dngnkz0gf49k0040000gp/T/go-build4119139561=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Run imports.Process("filename.go", filedata, nil) with file filename.go containing:

package libuserv2

import "github.com/IlyaFloppy/importsbuglib/v2"

func SomeFunc() importsbuglib.SomeType {
	typev2 := importsbuglib.SomeType{
		Value:        "v2",
		AnotherValue: "v2",
	}

	return typev2
}

and filedata containing:

package libuserv2


func SomeFunc() importsbuglib.SomeType {
	typev2 := importsbuglib.SomeType{
		Value:        "v2",
		AnotherValue: "v2",
	}

	return typev2
}

(same but no import)

What did you expect to see?

Original file with original import:

package libuserv2

import "github.com/IlyaFloppy/importsbuglib/v2"

func SomeFunc() importsbuglib.SomeType {
	typev2 := importsbuglib.SomeType{
		Value:        "v2",
		AnotherValue: "v2",
	}

	return typev2
}

What did you see instead?

Import without /v2 part:

package libuserv2

import "github.com/IlyaFloppy/importsbuglib"

func SomeFunc() importsbuglib.SomeType {
        typev2 := importsbuglib.SomeType{
                Value:        "v2",
                AnotherValue: "v2",
        }

        return typev2
}

Aliased imports like import alias "github.com/IlyaFloppy/importsbuglib" are not present after using imports.Process.

Full example is available at https://github.com/IlyaFloppy/importsbug which uses https://github.com/IlyaFloppy/importsbuglib

@heschi
Copy link
Contributor

@heschi heschi commented Mar 2, 2021

Sorry, maybe I'm being dense, but I don't understand. Once you remove the v2 import, how is goimports supposed to know to use v2 rather than v1? And once you remove the named import, how is it supposed to guess that importsbuglibalias refers to importsbuglib?

@IlyaFloppy
Copy link
Author

@IlyaFloppy IlyaFloppy commented Mar 2, 2021

It is stated that filename's directory influences which imports can be chosen. Adding another file with v2 import makes Process substitute correct v2 import (added this case to the linked example). So I guess I was expecting Process to read imports from all files in the filename's directory, including original filename.

@heschi
Copy link
Contributor

@heschi heschi commented Mar 2, 2021

No, sibling imports are only loaded from other files. That seems right to me; if I remove an import from a file, then process it in-memory before saving it, the fact that there's an old version of it on disk shouldn't affect results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants