Cgo packages cannot have a package name different than its folder name
See original GitHub issueGo allows a package name different from its folder name. However, we have to tell cgo the package name so it generates .go files with right package declaration. It was possible to do that at revision fdb121f6f803ad14c7ab77c0381e81da6ae06fab, but after #1994, it is no longer possible. As a result, we cannot build github.com/mattn/go-sqlite3 with Buck anymore.
Steps to reproduce the error:
- Checkout the sqlite branch of my fork of buck
- Run this test
As a comparison, Go toolchain is able to build the same package:
cd test/com/facebook/buck/features/go/testdata/cgo
GOPATH=$(pwd) go run src/different_package/cli/main.go
In order for the Go toolchain to build the package, we cannot change the import statements in main.go
.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
go - How to specify the package name if multiple files exist in ...
The answer is yes , move the "hello" package files to their own folder or , use the same package name for all...
Read more >cgo dependencies are no longer referenced via "cgo ...
It always had to be a part of either go_library or go_binary. ... Cgo packages cannot have a package name different than its...
Read more >multiple packages in the same folder and package keyword ...
Now that the go tool requires each directory to be one package and doesn't allow to have files with different package names inside...
Read more >Style guideline for Go packages - rakyll.org
Renames should follow the same rules ... If you are importing more than one packages with the same name, you can locally rename...
Read more >Package names - The Go Programming Language
Just as types in different packages can have the same name without ambiguity, packages in different directories can have the same name.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After long discussion with @linzhp on slack we got to the root of the problem, which is frankly speaking very non-obvious.
Because cgo package is always embedded in go binary generated by “go build” the package name is not expected to be the same as the binary package name.
“go build” would infer the package name from the source files (not the location / directory name) and use it as “cgo” package_name.
For example:
This used to work before, because the package_name is different for cgo and go libraries. The former is not the import path (ex. github.com/whatever/go-sqlite3) but sqlite3 which “go build” would infer from the source files.
The solution is to add extra argument such as:
to cgo_library
It’s used in only one place (CGoGenImport.java) so likely it can