Conversation
b64ea51 to
b602fbe
Compare
Szelethus
left a comment
There was a problem hiding this comment.
Bazel 7, during unittest, seems to prefer our own, while if included by another project, it seems to prefer the rules_python toolchain.
What does "prefer" mean? Does this mean that we are committing to supporting both WORKSPACE and Module system for bazel 7?
I want to know whether this is an actual requirement, because the way you explain it, this seems to be a non-trivial complexity. Would the code look significantly cleaner if we dropped WORKSPACE support for bazel 7?
src/common.bzl
Outdated
| elif hasattr(py_toolchain, "py3_runtime"): | ||
| py_runtime = py_toolchain.py3_runtime | ||
| python_path = py_runtime.interpreter_path | ||
| if python_path == None: # Bazel 8 |
There was a problem hiding this comment.
Thats a vague comment on its own.
src/common.bzl
Outdated
|
|
||
| def python_interpreter_tool(ctx): | ||
| """ | ||
| Returns version specific Python interpreter object in a list |
There was a problem hiding this comment.
Do we have to return a list?
| Returns version specific Python interpreter object in a list | |
| Returns version specific Python interpreter object that is appropriate for the Bazel version that is running |
There was a problem hiding this comment.
We don't have to return a list, but handling the case where there is no tool to return (Bazel 6) will become very ugly.
Now we have (removing unimportant parts):
ctx.actions.run(
executable = python_path(ctx),
tools = python_interpreter_tool(ctx),
arguments = [
...
],
use_default_shell_env = True,
)
This would look like:
ctx.actions.run(
executable = python_path(ctx),
tools = [python_interpreter_tool(ctx)] if python_interpreter_tool(ctx) != None else [],
arguments = [
...
],
use_default_shell_env = True,
)
or we calculate tools ahead of time:
tools = [python_interpreter_tool(ctx)] if python_interpreter_tool(ctx) != None else [],
ctx.actions.run(.....)
| #python = use_extension("@rules_python//python/extensions:python.bzl", "python") | ||
| #python.toolchain( | ||
| # python_version = "3.10", | ||
| # is_default = True, | ||
| #) |
There was a problem hiding this comment.
What is the story with these?
There was a problem hiding this comment.
I tried everything to leave the toolchain registration for the old toolchain in the MODULE file, and have rules_python have a higher priority, but I failed.
These options I have come across during that. (I left them in thinking they might be interesting)
|
My explanation might not been the best, mostly it was just noting my experience trying this change out in different environments. The strange behaviour, while I'm not completely sure I understand (which we should before merging), is most likely due to the way the rule is included in a project.
This theory holds up in FOSS tests:
This would also mean that Bazel 8 will fail on tests using WORKSPACE files (Bazel 8 can be forced to read WORKSPACE files) |
e947e0b to
e11868f
Compare
a40e8a0 to
276963e
Compare
366aef9 to
410c09f
Compare
| ctx.actions.write( | ||
| output = ctx.outputs.test_script_wrapper, | ||
| is_executable = True, | ||
| content = """ | ||
| {} {} | ||
| """.format( | ||
| python_interpreter_path, | ||
| ctx.outputs.codechecker_test_script.short_path, | ||
| ), | ||
| ) | ||
|
|
||
| # Return test script and all required files | ||
| run_files = default_runfiles + [ctx.outputs.codechecker_test_script] | ||
| run_files = default_runfiles + [ | ||
| ctx.outputs.codechecker_test_script, | ||
| ctx.outputs.test_script_wrapper, | ||
| ] + python_interpreter_tool(ctx) |
There was a problem hiding this comment.
@nettle I understand you find this solution inelegant, and I won't contest it. However, would it be agreeable for you if we pushed this PR as-is, and found a nicer solution after the fact? This code is not the architectural backbone of the patch, so I think it is fine to improve it later.
There was a problem hiding this comment.
What is the problem with current implementation?
Well, it is also not nice :) but I dont think we should change "not nice" with another "not nice" :)
There was a problem hiding this comment.
We cannot use the Python binary provided by rules_python with the old implementation (shebangs).
This is because a shebang must be an absolute path.
I'm not aware of any way to get the absolute path for the rules_python provided binary (remote executors further complicate this).
Also, doing this is not completely unheard of:
- actions.run
argumentsare not passed when executing a test rule bazelbuild/bazel#19661 (comment) - https://github.com/keith/rules_multirun/blob/e2245c6a4e1fb26cea7db6fac16d856693cf1ec1/multirun.bzl#L124
I guess a nicer solution would be if the script were a py_binary target in the BUILD file.
We could also just hardcode
#!/usr/bin/env python3.
There was a problem hiding this comment.
Okay, I have tried out the py_binary solution in #199.
If we agree with that, then we should merge that one and return here afterwards.
| # If we use our custom toolchain, the path will be an absolute path, | ||
| # executable by bazel | ||
| python_interpreter_path = python_path(ctx) | ||
|
|
||
| # If we can find an interpreter tool provided by bazel, | ||
| # use that as an executable | ||
| if python_interpreter_tool(ctx) != []: | ||
| python_interpreter_path = python_interpreter_tool(ctx)[0].short_path | ||
|
|
||
| # Since we cannot give parameters to the runfiles | ||
| # we wrap the executable (a python binary) in a script. | ||
| # In the script we give the CodeChecker script as an argument. | ||
| ctx.actions.write( | ||
| output = ctx.outputs.test_script_wrapper, | ||
| is_executable = True, | ||
| content = """ | ||
| {} {} | ||
| """.format( | ||
| python_interpreter_path, | ||
| ctx.outputs.codechecker_test_script.short_path, | ||
| ), | ||
| ) | ||
|
|
There was a problem hiding this comment.
This part is exactly why I dont like this change.
Do we have any good reasons for this?
| ctx.actions.write( | ||
| output = ctx.outputs.test_script_wrapper, | ||
| is_executable = True, | ||
| content = """ | ||
| {} {} | ||
| """.format( | ||
| python_interpreter_path, | ||
| ctx.outputs.codechecker_test_script.short_path, | ||
| ), | ||
| ) | ||
|
|
||
| # Return test script and all required files | ||
| run_files = default_runfiles + [ctx.outputs.codechecker_test_script] | ||
| run_files = default_runfiles + [ | ||
| ctx.outputs.codechecker_test_script, | ||
| ctx.outputs.test_script_wrapper, | ||
| ] + python_interpreter_tool(ctx) |
There was a problem hiding this comment.
What is the problem with current implementation?
Well, it is also not nice :) but I dont think we should change "not nice" with another "not nice" :)
b9d43b6 to
34c5855
Compare
Co-authored-by: Kristóf Umann <dkszelethus@gmail.com>
34c5855 to
2f1f0b3
Compare
Why:
Necessary for Bazel 8 support.
What:
rules_pythonas a dependencyMODULE.bazel; if left there, its priority would shadow the rules_python toolchain. Bazel 7, during unittest, seems to prefer our own, while if included by another project, it seems to prefer the rules_python toolchain. (Adding the following flag will reveal the process of toolchain selection:--toolchain_resolution_debug=.*)(The logic behind this:
MODULEfile, e.g., zlib-modules, it will use the rules_python toolchain. (My theory for this is that register_toolchains takes the highest precedence in the order of toolchain resolution, and in other projects, we don't register the toolchain))
use_default_shell_envis enabled, the path to an executable is accepted as executable.codechecker_test- I have no better idea of how I return an executable that, without any parameters, would execute the script using the interpreter, that maybe inside the sandbox, maybe not. I'm open to any better ideas. (The only idea I have is dropping the template expansion entirely, and using py_binary targets instead)Addresses:
#108