Skip to content

x/tools/cmd/goimports: incorrectly assumes module cache directory paths match module paths #29550

@subash-a

Description

@subash-a

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

$ go version
go version go1.12beta1 darwin/amd64

Does this issue reproduce with the latest release?

Yes the issue is reproducible with the latest version of go (go1.12beta1)

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/subhash_sharma/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/subhash_sharma/go"
GOPROXY=""
GORACE=""
GOROOT="/Users/subhash_sharma/.goversions/go1.12beta1"
GOTMPDIR=""
GOTOOLDIR="/Users/subhash_sharma/.goversions/go1.12beta1/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/subhash_sharma/subash/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 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/w0/d6l19r7j3dbg7fq56_nn4l340000gp/T/go-build083506298=/tmp/go-build -gno-record-
gcc-switches -fno-common"

What did you do?

The following testscript script reproduces the issue:

# install goimports
env GOMODPROXY=$GOPROXY
env GOPROXY=
go install golang.org/x/tools/cmd/goimports

# test goimports
env GOPROXY=$GOMODPROXY
cd mod
go mod download fruit.com@v1.0.0
go mod download
exec goimports -l main.go
stdout main.go

-- go.mod --
module goimports

require golang.org/x/tools v0.0.0-20181221235234-d00ac6d27372

-- mod/go.mod --
module mod

require fruit.com v1.0.0

replace fruit.com => github.com/user/banana v1.0.0

-- mod/main.go --
package main

import (
    "fmt"

    // we are missing an import to satisfy core
)

func main() {
    fmt.Println(core.Orange)
}
-- mod/core/core.go --
package core

type T struct{}
var Orange T

-- .gomodproxy/fruit.com_v1.0.0/.mod --
module fruit.com

-- .gomodproxy/fruit.com_v1.0.0/.info --
{"Version":"v1.0.0","Time":"2018-10-22T18:45:39Z"}

-- .gomodproxy/fruit.com_v1.0.0/go.mod --
module fruit.com

-- .gomodproxy/fruit.com_v1.0.0/fruit/fruit.go --
package fruit

const Apple = "apple"
-- .gomodproxy/fruit.com_v1.0.0/coretest/coretest.go --
// package coretest becomes a candidate for the missing
// core import in main above
package coretest

const Mandarin = "mandarin"

-- .gomodproxy/github.com_user_banana_v1.0.0/.mod --
module fruit.com

-- .gomodproxy/github.com_user_banana_v1.0.0/.info --
{"Version":"v1.0.0","Time":"2018-10-22T18:45:39Z"}

-- .gomodproxy/github.com_user_banana_v1.0.0/go.mod --
module fruit.com

-- .gomodproxy/github.com_user_banana_v1.0.0/fruit/fruit.go --
package fruit

const Apple = "apple"
-- .gomodproxy/github.com_user_banana_v1.0.0/coretest/coretest.go --
// package coretest becomes a candidate for the missing
// core import in main above
package coretest

const Mandarin = "mandarin"

What did you expect to see?

A successful run (the stdout main.go check should succeed because main.go is missing an import to satisfy the qualified identifier core.).

What did you see instead?

$ testscript goimports_directory_walking.txt
# install goimports (5.447s)
# test goimports (0.314s)
> env GOPROXY=$GOMODPROXY
> cd mod
$WORK/mod
> go mod download fruit.com@v1.0.0
[stderr]
go: finding github.com/user/banana v1.0.0
go: finding fruit.com v1.0.0

> go mod download
> exec goimports -l main.go
[stderr]
querying module cache matches: go [list -e -json -compiled -test=false -export=false -deps=false -find=true -- fruit.com/coretest golang.org/x/tools/cmd/heapview/internal/core github.com/user/banana/coretest]: exit status 1: go: github.com/user/banana@v1.0.0: parsing go.mod: unexpected module path "fruit.com"
go: error loading module requirements

[exit status 2]
FAIL: /tmp/testscript845630670/0/script.txt:11: unexpected command failure
error running goimports_directory_walking.txt in /tmp/testscript845630670/0

Our main module includes a replace directive; therefore our module cache contains a directory structure where the path does not match the contained module's path (the directory $GOPATH/pkg/mod/github.com/user/banana@v1.0.0 contains the module fruit.com@v1.0.0).

goimports appears to assume that directories within the module cache contain modules with the same path (which results in the above error message). But with replacements that will not be the case.

cc @heschik @bradfitz @ianthehat @myitcv

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.modules

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions