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

cmd/go: go:binary-only-package not working in go 1.10 #24318

Closed
hilyjiang opened this issue Mar 9, 2018 · 7 comments
Closed

cmd/go: go:binary-only-package not working in go 1.10 #24318

hilyjiang opened this issue Mar 9, 2018 · 7 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@hilyjiang
Copy link

hilyjiang commented Mar 9, 2018

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

go version go1.10 linux/amd64

Does this issue reproduce with the latest release?

yes

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

# go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/godev"
GORACE=""
GOROOT="/root/go"
GOTMPDIR=""
GOTOOLDIR="/root/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build085446828=/tmp/go-build"

What did you do?

test-binary-package/main.go

package main

import (
        "fmt"
        "test-binary-package/myframework"
)

func main() {
        fmt.Println("test:", myframework.Hello("golang"))
}

test-binary-package/myframework/hello.go

package myframework

import "fmt"
import "test-binary-package/myname"

func Hello(name string) string {
        return fmt.Sprintf("Hello, %s! I am %s", name, myname.MYNAME)
}

test-binary-package/myname/myname.go

package myname

const MYNAME = "golang"

I run go install in test-binary-package/myframework and test-binary-package/myname dir then replace the content code with:

//go:binary-only-package

package myframework

and

//go:binary-only-package

package myname

Then go build the test program.

What did you expect to see?

The test code works well with go1.9:

# go build -v -x
WORK=/tmp/go-build397500380
test-binary-package
mkdir -p $WORK/test-binary-package/_obj/
mkdir -p $WORK/test-binary-package/_obj/exe/
cd /root/godev/src/test-binary-package
/root/go/pkg/tool/linux_amd64/compile -o $WORK/test-binary-package.a -trimpath $WORK -goversion go1.9 -p main -complete -buildid 1fe88b276335f95449725f3118d43fc70f104d57 -D _/root/godev/src/test-binary-package -I $WORK -I /root/godev/pkg/linux_amd64 -pack ./main.go
cd .
/root/go/pkg/tool/linux_amd64/link -o $WORK/test-binary-package/_obj/exe/a.out -L $WORK -L /root/godev/pkg/linux_amd64 -extld=gcc -buildmode=exe -buildid=1fe88b276335f95449725f3118d43fc70f104d57 $WORK/test-binary-package.a
mv $WORK/test-binary-package/_obj/exe/a.out test-binary-package

What did you see instead?

Error with go1.10:

# go build -v -x
WORK=/tmp/go-build337943129
test-binary-package/myframework
test-binary-package
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg << 'EOF' # internal
# import config
packagefile fmt=/root/go/pkg/linux_amd64/fmt.a
packagefile test-binary-package/myframework=/root/godev/pkg/linux_amd64/test-binary-package/myframework.a
packagefile runtime=/root/go/pkg/linux_amd64/runtime.a
EOF
cd /root/godev/src/test-binary-package
/root/go/pkg/tool/linux_amd64/compile -o $WORK/b001/_pkg_.a -trimpath $WORK/b001 -p main -complete -buildid BIGdoXiIkuDDnICLqC05/BIGdoXiIkuDDnICLqC05 -goversion go1.10 -D "" -importcfg $WORK/b001/importcfg -pack -c=4 ./main.go
/root/go/pkg/tool/linux_amd64/buildid -w $WORK/b001/_pkg_.a # internal
cp $WORK/b001/_pkg_.a /root/.cache/go-build/94/9484f19284c521d729eebdc97357259fc03d90c12eb4217a7d3625fffcbacb4f-d # internal
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile test-binary-package=$WORK/b001/_pkg_.a
packagefile fmt=/root/go/pkg/linux_amd64/fmt.a
packagefile test-binary-package/myframework=/root/godev/pkg/linux_amd64/test-binary-package/myframework.a
packagefile runtime=/root/go/pkg/linux_amd64/runtime.a
packagefile errors=/root/go/pkg/linux_amd64/errors.a
packagefile io=/root/go/pkg/linux_amd64/io.a
packagefile math=/root/go/pkg/linux_amd64/math.a
packagefile os=/root/go/pkg/linux_amd64/os.a
packagefile reflect=/root/go/pkg/linux_amd64/reflect.a
packagefile strconv=/root/go/pkg/linux_amd64/strconv.a
packagefile sync=/root/go/pkg/linux_amd64/sync.a
packagefile unicode/utf8=/root/go/pkg/linux_amd64/unicode/utf8.a
packagefile runtime/internal/atomic=/root/go/pkg/linux_amd64/runtime/internal/atomic.a
packagefile runtime/internal/sys=/root/go/pkg/linux_amd64/runtime/internal/sys.a
packagefile sync/atomic=/root/go/pkg/linux_amd64/sync/atomic.a
packagefile internal/cpu=/root/go/pkg/linux_amd64/internal/cpu.a
packagefile internal/poll=/root/go/pkg/linux_amd64/internal/poll.a
packagefile internal/testlog=/root/go/pkg/linux_amd64/internal/testlog.a
packagefile syscall=/root/go/pkg/linux_amd64/syscall.a
packagefile time=/root/go/pkg/linux_amd64/time.a
packagefile unicode=/root/go/pkg/linux_amd64/unicode.a
packagefile internal/race=/root/go/pkg/linux_amd64/internal/race.a
EOF
mkdir -p $WORK/b001/exe/
cd .
/root/go/pkg/tool/linux_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=VRqrpvfeQkTyAVOZ5Wtp/BIGdoXiIkuDDnICLqC05/2AsWVWQk98D3OTEb1GW0/VRqrpvfeQkTyAVOZ5Wtp -extld=gcc $WORK/b001/_pkg_.a
# test-binary-package
cannot find package test-binary-package/myname (using -importcfg)
/root/go/pkg/tool/linux_amd64/link: cannot open file : open : no such file or directory

It seems there miss packagefile define for the nested package test-binary-package/myname.

@hilyjiang hilyjiang reopened this Mar 9, 2018
@andybons andybons changed the title build: go:binary-only-package not work for go 1.10 cmd/go: go:binary-only-package not working in go 1.10 Mar 9, 2018
@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 9, 2018
@andybons andybons added this to the Go1.11 milestone Mar 9, 2018
@andybons
Copy link
Member

andybons commented Mar 9, 2018

/cc @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

This is likely due to a documented change in binary packages in 1.10. They must now explicitly list their imports. See the last paragraph of https://golang.org/doc/go1.10#build .

@hilyjiang
Copy link
Author

hilyjiang commented Mar 12, 2018

@ianlancetaylor Thanks.

Is there any way that I can manualy export imports from .a without source codes?

@ianlancetaylor
Copy link
Contributor

I believe it should be possible to do that using the go/importer package.

I don't think there is anything for us to change here, so closing this issue.

@triztian
Copy link

Hello, I know the issue is closed but haven't figured out how to fix the issue and be able to use BOP packages.

The new docs say:

One new requirement implied by these changes is that binary-only packages must now declare accurate import blocks in their stub source code, so that those imports can be made available when linking a program using the binary-only package.

Source: https://golang.org/doc/go1.10#build

However, personally that does not seem to convey exactly what actions the developer should take, it points you to https://golang.org/cmd/go/#hdr-File_types, however again I feel that it just mentions the following:

Non-test Go source files can also include a //go:binary-only-package comment, indicating that the package sources are included for documentation only and must not be used to build the package binary. This enables distribution of Go packages in their compiled form alone. Even binary-only packages require accurate import blocks listing required dependencies, so that those dependencies can be supplied when linking the resulting command.

But no hint towards what to make of accurate import blocks, furthermore the Binary-Only Package documentation does not mention how the go/importer package would be used for the BOP packages.

Any example of how to use it in 1.10 would be greatly appreciated!, thanks!, sorry for the trouble

@ianlancetaylor
Copy link
Contributor

Thanks for the note. This issue is closed, and we don't use issues for questions. You will get better answers using a forum. Please see https://golang.org/wiki/Questions . Thanks.

@triztian
Copy link

Thank you for the quick reply, I figured out what it meant from this issue that you encountered: #23473

@golang golang locked and limited conversation to collaborators Jul 17, 2019
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

5 participants