myitcv / gopherjs

A compiler from Go to JavaScript for running Go code in a browser
BSD 2-Clause "Simplified" License
21 stars 0 forks source link

package in GOPATH shadows module #27

Closed antong closed 5 years ago

antong commented 5 years ago

On the go1.11 branch, packages within the module are shadowed by packages happening to be in GOPATH as well. This does not happen for regular Go, only GopherJS.

Consider the following layout, with GOPATH set to "/.../topdir/gopath".

topdir
├───foomod
│   └───bar
└───gopath
    └───src
        └───foo
            └───bar

$ cat foomod/go.mod
module foo

$ cat foomod/main.go
package main // import "foo"

import "foo/bar"

func main() {
        bar.Bar()
}

$ cat foomod/bar/bar.go
package bar

import "fmt"

func Bar() {
        fmt.Println("Hello from module foo!")
}

$ cat gopath/src/foo/bar/bar.go
package bar

import "fmt"

func Bar() {
        fmt.Println("Hello from GOPATH!")
}

With Go:

$ cd foomod/
$ go run .
Hello from module foo!

With gopherjs on branch go1.11 I get "Hello from GOPATH!". If I remove the version checked out in GOPATH, I get the expected "Hello from module foo!".

myitcv commented 5 years ago

Thanks very much for the repro @antong. Here is a copy-paste repro script for reference:

export GOPATH=$(mktemp -d)
export PATH=$GOPATH/bin:$PATH

# block: output
go env GOPATH
cd $GOPATH
git clone --depth=1 https://github.com/myitcv/gopherjs "$(cut -d':' -f1 <<< $GOPATH)/src/github.com/gopherjs/gopherjs"
go get -u github.com/gopherjs/gopherjs
cd /tmp
mkdir foomood
cd foomood
go mod init foo
cat <<EOD > main.go
package main

import "foo/bar"

func main() {
   bar.Bar()
}
EOD
mkdir bar
cat <<EOD > bar/bar.go
package bar

import "fmt"

func Bar() {
   fmt.Println("Hello from module foo/bar!")
}
EOD
cd $GOPATH
mkdir -p src/foo/bar
cat <<EOD > src/foo/bar/bar.go
package bar

import "fmt"

func Bar() {
   fmt.Println("Hello from GOPATH foo/bar!")
}
EOD
cd /tmp/foomood
go run .
gopherjs run *.go

gives:

$ go run .
Hello from module foo/bar!
$ gopherjs run *.go
Hello from GOPATH foo/bar!

I suspect this is somewhat related to #24... but in a strange way.

I'll investigate. Thanks again

myitcv commented 5 years ago

Per discussions with @marwan-at-work, a temporary fix for this (and @antong points this out above) is to set your GOPATH to something other than its current/default value. Something like /path/to/module/.gopath should suffice.

myitcv commented 5 years ago

e.g.

export PATH=$GOPATH/bin:$PATH
git clone https://github.com/myitcv/gopherjs /tmp/gopherjs
cd /tmp/gopherjs
go install
git clone https://github.com/marwan-at-work/marwanio /tmp/marwanio
cd /tmp/marwanio
export GOPATH=$PWD/.gopath
go mod init
gopherjs build
myitcv commented 5 years ago

Finally getting around to this.

Now that #29 has landed, it's trivial to create repros for this sort of thing.

I've pushed up https://github.com/myitcv/gopherjs/pull/32 as a starting point with a testscript repro. Fix hopefully to follow shortly.