Skip to content

Commit

Permalink
[internal] Error when using cgo files (#13172)
Browse files Browse the repository at this point in the history
Cgo files are `.go` files that use C: https://pkg.go.dev/cmd/cgo. We do not have the infrastructure to support them yet, so we instead want to eagerly redirect users to file a feature request. 

Note that users cannot use other file types like `.h` and `.m` files already because the Sources field bans them. Only cgo files are possible because they end in `.go`.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano authored Oct 8, 2021
1 parent d37d1e8 commit a192f3e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/python/pants/backend/go/target_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ class GoModPackageSourcesField(StringSequenceField, AsyncFieldMixin):
default = ("**/*.go", "**/*.s")
help = (
"What sources to generate `go_first_party_package` targets for.\n\n"
"Pants will generate one target per matching directory."
"Pants will generate one target per matching directory.\n\n"
"Pants does not yet support some file types like `.c` and `.h` files, along with cgo "
"files. If you need to use these files, please open a feature request at "
"https://github.com/pantsbuild/pants/issues/new/choose so that we know to "
"prioritize adding support."
)

def _prefix_glob_with_address(self, glob: str) -> str:
Expand Down
9 changes: 9 additions & 0 deletions src/python/pants/backend/go/util_rules/first_party_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ async def compute_first_party_package_info(
),
)
metadata = json.loads(result.stdout)

if "CgoFiles" in metadata:
raise NotImplementedError(
f"The first-party package {request.address} includes `CgoFiles`, 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."
)

return FirstPartyPkgInfo(
digest=pkg_sources.snapshot.digest,
imports=tuple(metadata.get("Imports", [])),
Expand Down
40 changes: 38 additions & 2 deletions src/python/pants/backend/go/util_rules/first_party_pkg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pants.engine.addresses import Address
from pants.engine.fs import PathGlobs, Snapshot
from pants.engine.rules import QueryRule
from pants.testutil.rule_runner import RuleRunner
from pants.testutil.rule_runner import RuleRunner, engine_error


@pytest.fixture
Expand All @@ -34,7 +34,7 @@ def rule_runner() -> RuleRunner:
return rule_runner


def test_resolve_go_package(rule_runner: RuleRunner) -> None:
def test_package_info(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
"foo/BUILD": "go_mod()\n",
Expand Down Expand Up @@ -118,3 +118,39 @@ def assert_info(
test_files=["bar_test.go"],
xtest_files=[],
)


def test_cgo_not_supported(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
"BUILD": "go_mod(name='mod')\n",
"go.mod": dedent(
"""\
module go.example.com/foo
go 1.17
"""
),
"hello.go": dedent(
"""\
package main
// int fortytwo()
// {
// return 42;
// }
import "C"
import "fmt"
func main() {
f := C.intFunc(C.fortytwo)
fmt.Println(C.intFunc(C.fortytwo))
}
"""
),
}
)
with engine_error(NotImplementedError):
rule_runner.request(
FirstPartyPkgInfo,
[FirstPartyPkgInfoRequest(Address("", target_name="mod", generated_name="./"))],
)

0 comments on commit a192f3e

Please sign in to comment.