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

Simplify v1 Go buildgen's use of source roots. #9694

Merged
merged 3 commits into from May 5, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 33 additions & 12 deletions contrib/go/src/python/pants/contrib/go/tasks/go_buildgen.py
Expand Up @@ -34,9 +34,6 @@ class WrongLocalSourceTargetTypeError(GenerationError):
For example, a Go main package was defined as a GoLibrary instead of a GoBinary.
"""

class RemoteRequired(GenerationError):
"""Indicates that the --remote-root was required but not specified."""

class NewRemoteEncounteredButRemotesNotAllowedError(GenerationError):
"""Indicates a new remote library dependency was found but --remote was not enabled."""

Expand All @@ -47,14 +44,14 @@ def __init__(
local_root,
fetcher_factory,
generate_remotes=False,
remote_root=None,
remote_root_func=None,
):
self._import_oracle = import_oracle
self._build_graph = build_graph
self._local_source_root = local_root
self._fetcher_factory = fetcher_factory
self._generate_remotes = generate_remotes
self._remote_source_root = remote_root
self._remote_source_root_func = remote_root_func

def generate(self, local_go_targets):
"""Automatically generates a Go target graph for the given local go targets.
Expand Down Expand Up @@ -90,11 +87,9 @@ def _generate_missing(self, gopath, local_address, import_listing, visited):
remote_root, import_path
)
name = remote_pkg_path or os.path.basename(import_path)
if not self._remote_source_root:
raise self.RemoteRequired(
"You must provide the remote source root with --remote-root"
)
address = Address(os.path.join(self._remote_source_root, remote_root), name)
address = Address(
os.path.join(self._remote_source_root_func(), remote_root), name
)
try:
self._build_graph.inject_address_closure(address)
except AddressLookupError:
Expand Down Expand Up @@ -422,19 +417,45 @@ def generate_targets(self, local_go_targets=None):
f"multiple build roots: {', '.join(sorted(local_roots))}"
)
local_root = local_roots.pop()
remote_root = self.get_options().remote_root

def infer_remote_root():
benjyw marked this conversation as resolved.
Show resolved Hide resolved
inferrable_roots = ["3rdparty/go", "3rd_party/go", "thirdparty/go", "third_party/go"]
msg = (
f"You can provide an explicit remote source root by setting remote_root in the "
f"[{self.options_scope}] section of your config file."
)
rr = None
for path in inferrable_roots:
if os.path.isdir(path):
if rr:
raise self.InvalidRemoteRootsError(
f"Couldn't infer a single remote root. Found {rr} and {path}. {msg}"
)
rr = path
if not rr:
raise self.InvalidRemoteRootsError(
f"Couldn't infer remote root. Found none of: {','.join(inferrable_roots)}. {msg}"
)
benjyw marked this conversation as resolved.
Show resolved Hide resolved

remote_root_func = lambda: self.get_options().remote_root or infer_remote_root()

generator = GoTargetGenerator(
self.import_oracle,
self.context.build_graph,
local_root,
self.get_fetcher_factory(),
generate_remotes=self.get_options().remote,
remote_root=remote_root,
remote_root_func=remote_root_func,
)
with self.context.new_workunit("go.buildgen", labels=[WorkUnitLabel.MULTITOOL]):
try:
generated = generator.generate(local_go_targets)
benjyw marked this conversation as resolved.
Show resolved Hide resolved
try:
remote_root = remote_root_func()
except self.InvalidRemoteRootsError:
# The result may have been generated without any remote root, so it's
# not an error for there not to be one here.
remote_root = None
return self.GenerationResult(
generated=generated, local_root=local_root, remote_root=remote_root
)
Expand Down