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

plugin: different version of package main #31048

Closed
lxstorm opened this issue Mar 26, 2019 · 4 comments
Closed

plugin: different version of package main #31048

lxstorm opened this issue Mar 26, 2019 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@lxstorm
Copy link

lxstorm commented Mar 26, 2019

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

$ go version
go version go1.12.1 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
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/lx/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/lx/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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/7k/r2p50mb149ngfkbmdw9tdqhc0000gp/T/go-build367373739=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I write a demo to test the go plugin, My work directory structure is

|____gen
| |____src
| | |____main
| | | |____main.go
|____plugin_name.so
|____build.sh
|____main.go

main.go

package main

import "plugin"

func main() {
	p, err := plugin.Open("plugin_name.so")
	if err != nil {
		panic(err)
	}
	v, err := p.Lookup("V")
	if err != nil {
		panic(err)
	}
	f, err := p.Lookup("F")
	if err != nil {
		panic(err)
	}
	*v.(*int) = 7
	f.(func())()
}

gen/src/main/main.go

package main

import "fmt"

var V int
func F() { fmt.Printf("Hello, number %d\n", V) }

build.sh

DIRPATH=$(dirname $0)
GOPATH=$(cd "${DIRPATH}/gen"; pwd)

export GOPATH
cd ${GOPATH}/src/main
go build -v -o plugin_name.so  -buildmode=plugin
mv plugin_name.so ${GOPATH}/..

What did you expect to see?

sh ./build.sh
go run main.go

I expect to see the main.go load the plugin successfully and print line.

What did you see instead?

panic: plugin.Open("plugin_name"): plugin was built with a different version of package main

goroutine 1 [running]:

main.main()
	/Users/lx/go/src/testZoom/tplugin/main.go:8 +0x38b
exit status 2

Why the main package is a different version, howerver if I modify the build.sh to

go build -v -o plugin_name.so  -buildmode=plugin main.go

Then go run main.go load so successfully.

How can the main package be a different version and why the second solution success?

@Kings-gee
Copy link

@lxstorm lxstorm changed the title plugin different version of package main plugin: different version of package main Mar 26, 2019
@katiehockman katiehockman added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 29, 2019
@katiehockman
Copy link
Contributor

/cc @ianlancetaylor

@ianlancetaylor ianlancetaylor added this to the Go1.13 milestone Apr 29, 2019
@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@seankhliao
Copy link
Member

The plugin has a full package path of main (it's directly under GOPATH/src)
go run . uses main as the package name since it's a main package. This conflicts with the plugin.
go run main.go uses command-line-arguments as the package name.

I think this is working as intended, and easy to work around (make sure each package has a unique import path).

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 15, 2022
@ZhengHe-MD
Copy link

@seankhliao Thanks for pointing this out, this saves my day. I struggled with this for several hours, checked many times to make sure both environments are exactly the same, and that's intimidating. But yes, as you said, this is working as intended.

@golang golang locked and limited conversation to collaborators Jan 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

9 participants