generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 64
perf: py_venv.bzl optimizations #725
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
Open
dzbarsky
wants to merge
4
commits into
main
Choose a base branch
from
zbarsky/optimize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # tools_telemetry | ||
| bazel_dep(name = "aspect_tools_telemetry", version = "0.2.8") | ||
| bazel_dep(name = "aspect_tools_telemetry", version = "0.3.2") | ||
|
|
||
| tel = use_extension("@aspect_tools_telemetry//:extension.bzl", "telemetry") | ||
| use_repo(tel, "aspect_tools_telemetry_report") | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -28,6 +28,18 @@ def _interpreter_flags(ctx): | |
|
|
||
| return args | ||
|
|
||
| # Forked from bazel-lib to avoid keeping `ctx` alive to execution phase | ||
| def _to_rlocation_path(workspace_name, file): | ||
| if file.short_path.startswith("../"): | ||
| return file.short_path[3:] | ||
| else: | ||
| return workspace_name + "/" + file.short_path | ||
|
|
||
| def _interpreter_path(workspace_name, py_toolchain): | ||
| return ( | ||
| _to_rlocation_path(workspace_name, py_toolchain.python) if py_toolchain.runfiles_interpreter else py_toolchain.python.path | ||
| ) | ||
|
|
||
| # FIXME: This is derived directly from the py_binary.bzl rule and should really | ||
| # be a layer on top of it if we can figure out flowing the data around. This is | ||
| # PoC quality. | ||
|
|
@@ -96,57 +108,74 @@ def _py_venv_base_impl(ctx): | |
|
|
||
| venv_name = ".{}".format(ctx.attr.name) | ||
| venv_dir = ctx.actions.declare_directory(venv_name) | ||
| workspace_name = ctx.workspace_name | ||
|
|
||
| args = ctx.actions.args() | ||
| args.add_all([venv_dir], expand_directories = False, format_each = "--location=%s") | ||
| args.add(py_shim.bin.bin, format = "--venv-shim=%s") | ||
|
|
||
| # Post-bzlmod we need to record the current repository in case the | ||
| # user tries to consume a `py_venv_binary` across repo boundaries | ||
| # which could cause repo mapping to become relevant. | ||
| args.add( | ||
| ctx.label.repo_name or workspace_name, | ||
| format = "--repo=%s", | ||
| ) | ||
| args.add_all( | ||
| [py_toolchain], | ||
| map_each = lambda py_toolchain: _interpreter_path(workspace_name, py_toolchain), | ||
| format_each = "--python=%s", | ||
| allow_closure = True, | ||
| ) | ||
| args.add(site_packages_pth_file, format = "--pth-file=%s") | ||
| args.add(env_file, format = "--env-file=%s") | ||
| args.add_all([ctx.bin_dir], expand_directories = False, format_each = "--bin-dir=%s") | ||
| args.add(ctx.attr.package_collisions, format = "--collision-strategy=%s") | ||
| args.add(venv_name, format = "--venv-name=%s") | ||
| args.add(ctx.attr.mode, format = "--mode=%s") | ||
| args.add( | ||
| "{}.{}".format( | ||
| py_toolchain.interpreter_version_info.major, | ||
| py_toolchain.interpreter_version_info.minor, | ||
| ), | ||
| format = "--version=%s", | ||
| ) | ||
| args.add( | ||
| "true" if ctx.attr.include_system_site_packages else "false", | ||
| format = "--include-system-site-packages=%s", | ||
| ) | ||
| args.add( | ||
| "true" if ctx.attr.include_system_site_packages else "false", | ||
| format = "--include-user-site-packages=%s", | ||
| ) | ||
|
|
||
| if ctx.attr.debug: | ||
| args.add("--debug") | ||
|
Comment on lines
+111
to
+153
Collaborator
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. This seems worthwhile if we can peel it out. |
||
|
|
||
| ctx.actions.run( | ||
| executable = venv_tool, | ||
| arguments = [ | ||
| "--location=" + venv_dir.path, | ||
| "--venv-shim=" + py_shim.bin.bin.path, | ||
| # Post-bzlmod we need to record the current repository in case the | ||
| # user tries to consume a `py_venv_binary` across repo boundaries | ||
| # which could cause repo mapping to become relevant. | ||
| "--repo=" + (ctx.label.repo_name or ctx.workspace_name), | ||
| "--python=" + (to_rlocation_path(ctx, py_toolchain.python) if py_toolchain.runfiles_interpreter else py_toolchain.python.path), | ||
| "--pth-file=" + site_packages_pth_file.path, | ||
| "--env-file=" + env_file.path, | ||
| "--bin-dir=" + ctx.bin_dir.path, | ||
| "--collision-strategy=" + ctx.attr.package_collisions, | ||
| "--venv-name=" + venv_name, | ||
| "--mode=" + ctx.attr.mode, | ||
| "--version={}.{}".format( | ||
| py_toolchain.interpreter_version_info.major, | ||
| py_toolchain.interpreter_version_info.minor, | ||
| ), | ||
| "--include-system-site-packages=" + ({ | ||
| True: "true", | ||
| False: "false", | ||
| }[ctx.attr.include_system_site_packages]), | ||
| "--include-user-site-packages=" + ({ | ||
| True: "true", | ||
| False: "false", | ||
| }[ctx.attr.include_system_site_packages]), | ||
| ] + (["--debug"] if ctx.attr.debug else []), | ||
| arguments = [args], | ||
| inputs = rfs.merge_all([ | ||
| ctx.runfiles(files = [ | ||
| site_packages_pth_file, | ||
| env_file, | ||
| venv_tool, | ||
| ] + py_toolchain.files.to_list()), | ||
|
Collaborator
Author
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. py_toolchain should already be in |
||
| ctx.runfiles(files = [site_packages_pth_file, env_file, venv_tool]), | ||
| py_shim.default_info.default_runfiles, | ||
| ]).files, | ||
| outputs = [ | ||
| venv_dir, | ||
| ], | ||
| execution_requirements = { | ||
| "supports-path-mapping": "1", | ||
| }, | ||
| # TODO: Is this right? The venv toolchain isn't quite in the right | ||
| # configuration (target not exec) so we have to use a different source | ||
| # of the target, but it is (logically) the venv toolchain. | ||
| toolchain = VENV_TOOLCHAIN, | ||
| ) | ||
|
|
||
| return venv_dir, rfs.merge_all([ | ||
| ctx.runfiles(files = [ | ||
| venv_dir, | ||
| ] + py_toolchain.files.to_list()), | ||
| ctx.runfiles( | ||
| files = [venv_dir], | ||
| transitive_files = py_toolchain.files, | ||
| ), | ||
| ctx.attr._runfiles_lib[DefaultInfo].default_runfiles, | ||
| ]) | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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
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.
Drop. Already updated.