Skip to content

Commit

Permalink
golang: fix exception when coverage enabled and external tests import…
Browse files Browse the repository at this point in the history
… the base package (Cherry-pick of #21452) (#21455)

As reported in #21386, Pants
raises an exception when coverage is enabled and a package has external
tests (i.e., "xtests") which import the base package. The bug occurred
because the code which injects the base package dependency did not
properly pass through the coverage flag. Consequently, the compile graph
ended up with two different variants of the base package: one with
coverage codegen and the other without coverage codegen. This resulted
in two different copies of the package archive which Pants cannot merge
together into a single `Digest`.

The solution is to properly pass through the coverage flag when
injecting the base package dependency on the external test package to
ensure there are not multiple variants of the base package in the
compile graph.

Fixes #21386.

Co-authored-by: Tom Dyas <tom@shoalsoftware.com>
  • Loading branch information
WorkerPants and tdyas authored Sep 25, 2024
1 parent 6361831 commit 9254175
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/python/pants/backend/go/goals/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,45 @@ def test_profile_options_write_results(rule_runner: RuleRunner) -> None:
"test_runner",
"trace.out",
]


def test_external_test_with_use_coverage(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
"foo/BUILD": "go_mod(name='mod')\ngo_package()",
"foo/go.mod": "module foo",
"foo/add.go": textwrap.dedent(
"""
package foo
func Add(x, y int) int {
return x + y
}
"""
),
"foo/add_test.go": textwrap.dedent(
"""
package foo_test
import (
"foo"
"testing"
)
func TestAdd(t *testing.T) {
if foo.Add(2, 3) != 5 {
t.Fail()
}
}
"""
),
}
)
tgt = rule_runner.get_target(Address("foo", generated_name="./"))
rule_runner.set_options(
[
"--test-use-coverage",
],
env_inherit={"PATH"},
)
result = rule_runner.request(
TestResult, [GoTestRequest.Batch("", (GoTestFieldSet.create(tgt),), None)]
)
assert result.exit_code == 0
5 changes: 4 additions & 1 deletion src/python/pants/backend/go/util_rules/build_pkg_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ async def setup_build_go_package_target_request(
maybe_base_pkg_dep = await Get(
FallibleBuildGoPackageRequest,
BuildGoPackageTargetRequest(
request.address, for_tests=True, build_opts=request.build_opts
request.address,
for_tests=True,
with_coverage=request.with_coverage,
build_opts=request.build_opts,
),
)
if maybe_base_pkg_dep.request is None:
Expand Down

0 comments on commit 9254175

Please sign in to comment.