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

Closed
IlyaFloppy opened this issue Mar 2, 2021 · 4 comments
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@IlyaFloppy
Copy link

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

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Mar 2, 2021
@gopherbot gopherbot added this to the Unreleased milestone Mar 2, 2021
@heschi
Copy link
Contributor

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?

@heschi heschi added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 2, 2021
@IlyaFloppy
Copy link
Author

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 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.

@seankhliao seankhliao added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Jun 12, 2022
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Jul 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants