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
Add python_resolve
field to protobuf_source
and thrift_source
to support multiple resolves with codegen
#14693
Merged
Eric-Arellano
merged 1 commit into
pantsbuild:2.10.x
from
Eric-Arellano:codegen-py-resolve-10
Mar 3, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/python/pants/backend/codegen/thrift/apache/python/additional_fields.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from pants.backend.codegen.thrift.target_types import ( | ||
ThriftSourcesGeneratorTarget, | ||
ThriftSourceTarget, | ||
) | ||
from pants.backend.python.target_types import PythonResolveField | ||
|
||
|
||
class ThriftPythonResolveField(PythonResolveField): | ||
alias = "python_resolve" | ||
|
||
|
||
def rules(): | ||
return [ | ||
ThriftSourceTarget.register_plugin_field(ThriftPythonResolveField), | ||
ThriftSourcesGeneratorTarget.register_plugin_field(ThriftPythonResolveField), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,39 +24,64 @@ def find_python_runtime_library_or_raise_error( | |
codegen_address: Address, | ||
runtime_library_module: str, | ||
*, | ||
resolve: str, | ||
resolves_enabled: bool, | ||
recommended_requirement_name: str, | ||
recommended_requirement_url: str, | ||
disable_inference_option: str, | ||
) -> Address: | ||
addresses = [ | ||
module_provider.addr | ||
for module_provider in module_mapping.providers_for_module( | ||
runtime_library_module, resolve=None | ||
runtime_library_module, resolve=resolve | ||
) | ||
if module_provider.typ == ModuleProviderType.IMPL | ||
] | ||
if len(addresses) == 1: | ||
return addresses[0] | ||
|
||
for_resolve_str = f" for the resolve '{resolve}'" if resolves_enabled else "" | ||
if not addresses: | ||
resolve_note = ( | ||
( | ||
"Note that because `[python].enable_resolves` is set, you must specifically have a " | ||
f"`python_requirement` target that uses the same resolve '{resolve}' as the target " | ||
f"{codegen_address}. Alternatively, update {codegen_address} to use a different " | ||
"resolve.\n\n" | ||
) | ||
if resolves_enabled | ||
else "" | ||
) | ||
raise MissingPythonCodegenRuntimeLibrary( | ||
f"No `python_requirement` target was found with the module `{runtime_library_module}` " | ||
f"in your project, so the Python code generated from the target {codegen_address} will " | ||
f"not work properly. See {doc_url('python-third-party-dependencies')} for how to " | ||
f"in your project{for_resolve_str}, so the Python code generated from the target " | ||
f"{codegen_address} will not work properly. See " | ||
f"{doc_url('python-third-party-dependencies')} for how to " | ||
"add a requirement, such as adding to requirements.txt. Usually you will want to use " | ||
f"the `{recommended_requirement_name}` project at {recommended_requirement_url}.\n\n" | ||
f"{resolve_note}" | ||
f"To ignore this error, set `{disable_inference_option} = false` in `pants.toml`." | ||
) | ||
|
||
if len(addresses) > 1: | ||
raise AmbiguousPythonCodegenRuntimeLibrary( | ||
"Multiple `python_requirement` targets were found with the module " | ||
f"`{runtime_library_module}` in your project, so it is ambiguous which to use for the " | ||
f"runtime library for the Python code generated from the the target {codegen_address}: " | ||
f"{sorted(addr.spec for addr in addresses)}\n\n" | ||
"To fix, remove one of these `python_requirement` targets so that there is no " | ||
"ambiguity and Pants can infer a dependency. Alternatively, if you do want to have " | ||
alternative_solution = ( | ||
( | ||
f"Alternatively, change the resolve field for {codegen_address} to use a different " | ||
"resolve from `[python].resolves`." | ||
) | ||
if resolves_enabled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If resolves are enabled, then you must not have two |
||
else ( | ||
"Alternatively, if you do want to have " | ||
f"multiple conflicting versions of the `{runtime_library_module}` requirement, set " | ||
f"`{disable_inference_option} = false` in `pants.toml`. " | ||
f"Then manually add a dependency on the relevant `python_requirement` target to each " | ||
"target that directly depends on this generated code (e.g. `python_source` targets)." | ||
) | ||
return addresses[0] | ||
) | ||
raise AmbiguousPythonCodegenRuntimeLibrary( | ||
"Multiple `python_requirement` targets were found with the module " | ||
f"`{runtime_library_module}` in your project{for_resolve_str}, so it is ambiguous which to " | ||
f"use for the runtime library for the Python code generated from the the target " | ||
f"{codegen_address}: {sorted(addr.spec for addr in addresses)}\n\n" | ||
f"To fix, remove one of these `python_requirement` targets{for_resolve_str} so that " | ||
f"there is no ambiguity and Pants can infer a dependency. {alternative_solution}" | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminder that if
--no-enable-resolves
, Pants will have set the resolve name to<ignore-me>
or something like that. So everything ends up being in the same resolve.