question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cgo packages cannot have a package name different than its folder name

See original GitHub issue

Go 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:

  1. Checkout the sqlite branch of my fork of buck
  2. 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:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mkaczanowskicommented, Aug 13, 2018

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:

go_library(
    name='lib',
    package_name="github.com/mattn/go-sqlite3",
    srcs=['doc.go', 'sqlite3_func_crypt.go', 'sqlite3_go18.go'],
    cgo=':cgo_lib',
    visibility=['PUBLIC']
)

cgo_library(
    name='cgo_lib',
    package_name = "sqlite3",

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:

declared_package_name = "sqlite3"

to cgo_library

0reactions
mkaczanowskicommented, Aug 13, 2018

It’s used in only one place (CGoGenImport.java) so likely it can

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found