Skip to content

Commit

Permalink
[internal] Stop setting import_path="main" for Go packages (pantsbu…
Browse files Browse the repository at this point in the history
…ild#13113)

Given a project with `module = example.com/foo` in `go.mod`, and `hello.go` with `package main` in it, `go list -json` will report the `ImportPath` as `example.com/foo`, _not_ `main`. So it was wrong for us to be overriding the `import_path` to be `main`.

This is important for dependency inference to work properly when there are multiple `go.mod`s in the project. Dependency inference maps import paths to targets, and we'd have ambiguity if there were multiple `main` import paths.

This also fixes our calculation of `import_path` to not include a `/` suffix when the `package` is in the same directory as the `go.mod`, i.e. we should use `example.com/foo`, not `example.com/foo/`.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano authored Oct 5, 2021
1 parent 45d376b commit 8f918bf
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
24 changes: 15 additions & 9 deletions src/python/pants/backend/go/util_rules/build_go_pkg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ def test_build_internal_pkg(rule_runner: RuleRunner) -> None:
"BUILD": dedent(
"""\
go_mod(name="mod")
go_package(name="pkg", import_path='main')
go_package(name="pkg")
"""
),
}
)
assert_built(rule_runner, Address("", target_name="pkg"), expected_import_paths=["main"])
assert_built(
rule_runner, Address("", target_name="pkg"), expected_import_paths=["example.com/greeter"]
)


def test_build_external_pkg(rule_runner: RuleRunner) -> None:
Expand Down Expand Up @@ -144,7 +146,7 @@ def test_build_dependencies(rule_runner: RuleRunner) -> None:
import (
"fmt"
"example.com/greeter/quoter"
"example.com/project/greeter/quoter"
"golang.org/x/xerrors"
)
Expand All @@ -159,7 +161,7 @@ def test_build_dependencies(rule_runner: RuleRunner) -> None:
"""\
package main
import "example.com/greeter"
import "example.com/project/greeter"
func main() {
greeter.QuotedHello()
Expand All @@ -168,7 +170,7 @@ def test_build_dependencies(rule_runner: RuleRunner) -> None:
),
"go.mod": dedent(
"""\
module example.com
module example.com/project
go 1.17
require golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
"""
Expand All @@ -182,7 +184,7 @@ def test_build_dependencies(rule_runner: RuleRunner) -> None:
"BUILD": dedent(
"""\
go_mod(name='mod')
go_package(name='pkg', import_path='main')
go_package(name='pkg')
"""
),
}
Expand All @@ -202,14 +204,18 @@ def test_build_dependencies(rule_runner: RuleRunner) -> None:
expected_import_paths=xerrors_import_paths,
)

quoter_import_path = "example.com/greeter/quoter"
quoter_import_path = "example.com/project/greeter/quoter"
assert_built(rule_runner, Address("greeter/quoter"), expected_import_paths=[quoter_import_path])

greeter_import_paths = ["example.com/greeter", quoter_import_path, *xerrors_import_paths]
greeter_import_paths = [
"example.com/project/greeter",
quoter_import_path,
*xerrors_import_paths,
]
assert_built(rule_runner, Address("greeter"), expected_import_paths=greeter_import_paths)

assert_built(
rule_runner,
Address("", target_name="pkg"),
expected_import_paths=["main", *greeter_import_paths],
expected_import_paths=["example.com/project", *greeter_import_paths],
)
9 changes: 4 additions & 5 deletions src/python/pants/backend/go/util_rules/go_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ async def resolve_go_package(
go_mod_spec_path = owning_go_mod.address.spec_path
assert request.address.spec_path.startswith(go_mod_spec_path)
spec_subpath = request.address.spec_path[len(go_mod_spec_path) :]
print(request.address.spec_path, go_mod_spec_path, spec_subpath)

go_mod_info, pkg_sources = await MultiGet(
Get(GoModInfo, GoModInfoRequest(owning_go_mod.address)),
Expand All @@ -217,11 +218,9 @@ async def resolve_go_package(
# Otherwise infer the import path from the owning `go_mod` target. The inferred import
# path will be the module's import path plus any subdirectories in the spec_path
# between the go_mod and go_package target.
import_path = f"{go_mod_info.import_path}/"
if spec_subpath.startswith("/"):
import_path += spec_subpath[1:]
else:
import_path += spec_subpath
import_path = f"{go_mod_info.import_path}"
if spec_subpath:
import_path += spec_subpath if spec_subpath.startswith("/") else f"/{spec_subpath}"

result = await Get(
ProcessResult,
Expand Down
6 changes: 1 addition & 5 deletions testprojects/src/go/pants_test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

go_package()

go_binary(
name="bin",
main=":pants_test",
)
go_binary(name="bin", main=":pants_test")
4 changes: 1 addition & 3 deletions testprojects/src/go/pants_test/bar/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

go_package(
import_path="github.com/toolchainlabs/toolchain/src/go/src/toolchain/pants_test/bar",
)
go_package()

6 changes: 6 additions & 0 deletions testprojects/src/go/pants_test/bar/bar.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package bar

import "github.com/google/uuid"

func GenUuid() string {
return uuid.NewString()
}

func Quote(s string) string {
return ">> " + s + " <<"
}
3 changes: 1 addition & 2 deletions testprojects/src/go/pants_test/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package main
import (
"fmt"
"os"

"github.com/toolchainlabs/toolchain/src/go/src/toolchain/pants_test/bar"
"github.com/pantsbuild/pants/testprojects/src/go/pants_test/bar"
)

func main() {
Expand Down

0 comments on commit 8f918bf

Please sign in to comment.