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

go: fix cgo bugs preventing kafka module from working with cgo #17592

Closed
Tracked by #17447
clsx524 opened this issue Nov 18, 2022 · 11 comments · Fixed by #17780
Closed
Tracked by #17447

go: fix cgo bugs preventing kafka module from working with cgo #17592

clsx524 opened this issue Nov 18, 2022 · 11 comments · Fixed by #17780
Labels
backend: Go Go backend-related issues bug

Comments

@clsx524
Copy link

clsx524 commented Nov 18, 2022

Is your feature request related to a problem? Please describe.
currently pants doesn't support .h files in the repo and compilation would result in errors

GoThirdPartyPkgError: The third-party package github.com/confluentinc/confluent-kafka-go/kafka includes `HFiles`, which Pants does not yet support. Please open a feature request at https://github.com/pantsbuild/pants/issues/new/choose so that we know to prioritize adding support. Please include this error message and the version of the third-party module.

for example, it would encounter this error when depending on this error. https://github.com/confluentinc/confluent-kafka-go/blob/master/kafka/glue_rdkafka.h

Describe the solution you'd like
support .h file

Describe alternatives you've considered
NA

Additional context
NA

@kaos kaos added the backend: Go Go backend-related issues label Nov 18, 2022
@kaos kaos changed the title support .h files Go: support .h files Nov 18, 2022
@tdyas
Copy link
Contributor

tdyas commented Nov 29, 2022

What version of Pants are you using? You will need to upgrade to a version of Pants with cgo support.

@tdyas
Copy link
Contributor

tdyas commented Nov 29, 2022

Probably the latest 2.15.0 alpha release. I do not believe the cgo support landed on main in time to make the 2.14.0 releases.

@Eric-Arellano
Copy link
Contributor

Going to close as completed. @clsx524 please let us know if you have a chance to try out the new Cgo support in 2.15 and if you have any questions!

@clsx524
Copy link
Author

clsx524 commented Dec 2, 2022

my use case is related to this package and it has a header file to select rdkafka. It provides a local version of it under the same directory or I can install it separately. When I used it with pants 2.15.0a1, it complained

gopath/pkg/mod/github.com/confluentinc/[email protected]/kafka/select_rdkafka.h:26:10: fatal error: 'librdkafka_vendor/rdkafka.h' file not found
#include "librdkafka_vendor/rdkafka.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

Can you provide some suggestions on how to use it with pants?

@tdyas tdyas reopened this Dec 2, 2022
@tdyas
Copy link
Contributor

tdyas commented Dec 2, 2022

I reproduced the failure locally. There are some bugs in the Pants cgo support to fix related to what files are inferred to be in a package and how references to "SRCDIR" in include and link flags in the kafka source code are resolved by Pants.

@tdyas tdyas changed the title Go: support .h files go: fix cgo bugs preventing kafka module from working with cgo Dec 2, 2022
@tdyas tdyas added bug and removed enhancement labels Dec 2, 2022
@tdyas
Copy link
Contributor

tdyas commented Dec 6, 2022

#17728 fixes part of this issue by making all files in a module's sources available to package compilation as packages are permitted to expect. I still need to solve howSRC_DIR in -I and -L options are resolved.

tdyas added a commit that referenced this issue Dec 9, 2022
## Problem

As reported in #17592, third-party Go modules are allowed to assume that all files packaged with the module will be visible during compilation. In that particular issue, the [Confluent Kafka client library](https://github.com/confluentinc/confluent-kafka-go) uses Cgo and expected a subdirectory with files used during C compilation will be visible to the package using Cgo.

Pants, however, subsets the modules sources digest to just the files in the exact subdirectory for each of the module's packages. This means that the [subdirectory files](https://github.com/confluentinc/confluent-kafka-go/tree/master/kafka/librdkafka_vendor) were not visible when compiling [the package in confluent-kafka-go in the immediate parent directory](https://github.com/confluentinc/confluent-kafka-go/tree/master/kafka/).

## Solution

Since Pants cannot predict how a third party package will use the files in the modules sources, Pants cannot reasonably subset the modules source digest. Solve the issue by not subsetting the module sources digest to produce a package-specific digest.


This is a partial fix for #17592.
tdyas added a commit that referenced this issue Dec 10, 2022
The `go-export-cgo-codegen` debug goal was unnecessarily limited to exporting just Cgo codegen for first-party `go_package` targets. As part of fixing #17592, it is useful to also be able to dump Cgo codegen for any third-party packages using Cgo. This PR teaches `go-export-cgo-codegen` how to do that.
@tdyas
Copy link
Contributor

tdyas commented Dec 11, 2022

I was able to get a simple Kafka client example to link successfully in this branch when run with ./pants package. I will need to cleanup those changes and submit some PRs.

@tdyas
Copy link
Contributor

tdyas commented Dec 11, 2022

Split out the removal of the __obj__ directories to #17775.

tdyas added a commit that referenced this issue Dec 12, 2022
Remove the notion of a separate `__obj__` directory (`obj_dir_path` in the code) for compiling object files (e.g., Cgo and assembly) and just generate and capture object files in the same directory as the sources they are built from.

The issue is that the user can configure references to the source directory in `-I` and other compiler/assembler options using the `${SRCDIR}` syntax. With a separate object directory, the rules would need additional code to either copy files around or duplicate `-I` and other options  to account for both the original sources directory and the objects directory if code wanted to be agnostic to the location of files that it expected to be in the package directory.

The idea for an objects tmp directory originally came from the `go` toolchain. `go` copies source files into the objects directory to avoid the duplication problem mentioned above. It makes sense for the Go toolchain to do this because the original sources are the user's actual source repository. Writing temporary object files into the original repository would make for a bad user experience.

Pants, on the other hand, uses an execution sandbox and does not have the same issue about modifying the original repository during a build. Thus, it makes sense in Pants to avoid copying files and just put object files next to the sources in the execution sandbox they are built from. This means the Cgo rules can avoid having to parse and duplicate options in which `${SRCDIR}` is used.

This is a partial fix for #17592. It fixes errors due to the mishandling of `${SRCDIR}` by the Cgo rules in not duplicating options that had `${SRCDIR}` in them to account for both the original sources directory and the objects directory.
@tdyas
Copy link
Contributor

tdyas commented Dec 12, 2022

The final fix is in #17780.

tdyas added a commit that referenced this issue Dec 14, 2022
#17780)

Some Go modules, e.g. https://github.com/confluentinc/confluent-kafka-go, try to link a static archive embedded in the module into the package archive using Cgo. If so, mark this package so that the module sources are included in the output digest for the build of this package.

The need for this inclusion is assumed if the Cgo rules observe the `${SRCDIR}` replacement in any linker option. The `${SRCDIR}` string will be replaced with the string `__PANTS_SANDBOX_ROOT__` which will then be replaced during linking with the actual sandbox path.

This is accomplished by overriding the "external linker" binary used by `go tool link`. As an additional benefit, this also allows Pants to directly control which external linker binary is actually used so the link `Process` can be properly invalidated if the external linker changes.

Fixes #17592. With this, a simple client binary using https://github.com/confluentinc/confluent-kafka-go links and can be executed.
@clsx524
Copy link
Author

clsx524 commented Dec 15, 2022

Thanks. I will have a try once there is a release.

@clsx524
Copy link
Author

clsx524 commented Dec 19, 2022

I verified it works on 2.16.0.dev3. Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend: Go Go backend-related issues bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants