-
Notifications
You must be signed in to change notification settings - Fork 556
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
feat: provide access to arbitrary interpreters #2507
base: main
Are you sure you want to change the base?
Conversation
Run a specific interpreter: * `bazel run @rules_python//tools/run --@rules_python//python/config_settings:python_version=3.12` Run interpreter from a binary: * `bazel run @rules_python//tools/run --@rules_python//tools/run:bin=//my:binary`
$ bazel run //python/bin:repl $ bazel run //python/bin:repl --//python/bin:repl_dep=//python/runfiles
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.
I know this is still a draft, so take my suggestions with a grain of salt :)
self._run_module_test("3.11") | ||
|
||
def test_run_module_3_12(self): | ||
self._run_module_test("3.12") |
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.
It would be great to add a test that imports:
- The code that is in the
//python/bin:interpreter_src
- The code that is in the transitive deps of the
//python/bin:interpreter_src
. Maybe depending ontwine
could be an option in the tests and then we could attempt importing one of its deps?
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.
Unless I'm misunderstanding something, that's currently not possible with the current implementation. The current implementation is purely there to get the interpreter, nothing else.
I'd like to follow this PR up with another PR that enables the use of the REPL with a specific py_*
target (and its dependencies) importable.
I noticed that my `$(location //path/to:target)` wasn't getting expanded when writing a test. This patch fixes the issue by forwarding the already-expanded environment from the inner target to the outer target.
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.
re: the change the the env
attribute in sh_py_run_test
: Please partially undo this change.
The key thing is that I don't want the inner py binary to contaminate the outer sh binary's environment. One of the purpose's of the sh-side of sh_py_run_test is to run a py_binary with a particular environment.
So I think it's fine if sh_py_run_test.env
is passed to only the inner py_binary and not the outer sh_binary. Similarly, the outer sh_binary shouldn't inherit TestEnvironment from the inner py binary. If there are separate env vars that should be set only in the sh-side of things, then either (a) create a sh_env arg to pass to sh, or (b) create a .sh file that sets the desired env prior to invoking the inner py binary.
Almost LGTM. Just that and the visibility comment.
|
||
To run the interpreter that Bazel will use, you can use the | ||
`@rules_python//python/bin:python` target. This is a binary target with | ||
the executable pointing at the `python3` binary plus the relevent runfiles. |
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.
the executable pointing at the `python3` binary plus the relevent runfiles. | |
the executable pointing at the `python3` binary plus its relevant runfiles. |
|
||
:::{note} | ||
The `python` target does not provide access to any modules from `py_*` | ||
targets on its own. Work is ongoing to support that. |
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.
targets on its own. Work is ongoing to support that. | |
targets on its own. Please file a feature request if this is desired. |
Just encouraging users to give feedback
@@ -0,0 +1,21 @@ | |||
load(":interpreter.bzl", "interpreter") | |||
|
|||
package(default_visibility = ["//visibility:public"]) |
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.
package(default_visibility = ["//visibility:public"]) |
Leave visibility as private (default). Explicitly add visbility=public where necessary. This just helps make it clear about what is actually public.
filegroup( | ||
name = "distribution", | ||
srcs = glob(["**"]), | ||
visibility = ["//python:__pkg__"], |
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.
visibility = ["//python:__pkg__"], | |
visibility = ["//:__subpackages__"], |
For simplicity, we generally just grant visibility to our whole repo or public. It just makes it easier to reuse other parts within our codebase without having to go edit visibility stuff frequently.
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.
I think only distribution was following this pedantic visibility setting.
|
||
interpreter( | ||
name = "python", | ||
binary = ":python_src", |
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.
binary = ":python_src", | |
binary = ":python_src", | |
visibility = ["//visibility:public"], |
|
||
load("//python/private:interpreter.bzl", _interpeter = "interpreter") | ||
|
||
interpreter = _interpeter |
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.
Hm. Should this be public?
What use case would users have for defining their own interpreter target?
|
||
load("//python/private:interpreter.bzl", _interpeter = "interpreter") | ||
|
||
interpreter = _interpeter |
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.
Please name this interpreter_binary
. This is to match the Bazel convention that runnable things are suffixed with _binary
.
), | ||
] | ||
|
||
interpreter = rule( |
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.
If this is public, then we'll need to add docs to the rule impl. Eventually, anyways -- I'm fine with docs being done in a separate PR.
|
||
|
||
class InterpreterTest(unittest.TestCase): | ||
def setUp(self): |
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.
def setUp(self): | |
def setUp(self): | |
super().setUp() |
Best practice: call the superclass unless there's a specific reason the parent functionality shouldn't be invoked.
Doh, actually, I was mistaken. The RunEnvironmentInfo propagation is fine. I misread the diff and thought the |
Whew. Okay. I was not sure how I was going to accomplish that otherwise 🙂 Will clean up the rest this weekend (and also figure out how to fix the RBE failures). |
There are some use cases that folks want to cover here. They are
discussed in this Slack thread. The high-level summary is:
to minimize environmental issues.
so that they can use the correct interpreter.
This patch adds to @rickeylev's work from #2359 by adding docs
and a few integration tests.