-
-
Notifications
You must be signed in to change notification settings - Fork 642
refactor(core): get_zip_runfiles_path should call startswith less #3442
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
Merged
rickeylev
merged 1 commit into
bazel-contrib:main
from
aignas:exp.aignas.get_zip_runfiles_path
Dec 7, 2025
+23
−17
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,7 @@ load(":venv_runfiles.bzl", "create_venv_app_files") | |
| _py_builtins = py_internal | ||
| _EXTERNAL_PATH_PREFIX = "external" | ||
| _ZIP_RUNFILES_DIRECTORY_NAME = "runfiles" | ||
| _INIT_PY = "__init__.py" | ||
| _LAUNCHER_MAKER_TOOLCHAIN_TYPE = "@bazel_tools//tools/launcher:launcher_maker_toolchain_type" | ||
|
|
||
| # Non-Google-specific attributes for executables | ||
|
|
@@ -834,14 +835,14 @@ def _create_zip_file(ctx, *, output, zip_main, runfiles): | |
| manifest.add("__main__.py={}".format(zip_main.path)) | ||
| manifest.add("__init__.py=") | ||
| manifest.add( | ||
| "{}=".format( | ||
| _get_zip_runfiles_path("__init__.py", workspace_name, legacy_external_runfiles), | ||
| ), | ||
| "{}=".format(_get_zip_runfiles_path(_INIT_PY, workspace_name)), | ||
| ) | ||
|
|
||
| def map_zip_empty_filenames(list_paths_cb): | ||
| return [ | ||
| _get_zip_runfiles_path(path, workspace_name, legacy_external_runfiles) + "=" | ||
| # FIXME @aignas 2025-12-06: what kind of paths do we expect here? Will they | ||
| # ever start with `../` or `external`? | ||
| _get_zip_runfiles_path_legacy(path, workspace_name, legacy_external_runfiles) + "=" | ||
| for path in list_paths_cb().to_list() | ||
| ] | ||
|
|
||
|
|
@@ -856,7 +857,7 @@ def _create_zip_file(ctx, *, output, zip_main, runfiles): | |
| def map_zip_runfiles(file): | ||
| return ( | ||
| # NOTE: Use "+" for performance | ||
| _get_zip_runfiles_path(file.short_path, workspace_name, legacy_external_runfiles) + | ||
| _get_zip_runfiles_path_legacy(file.short_path, workspace_name, legacy_external_runfiles) + | ||
| "=" + file.path | ||
| ) | ||
|
|
||
|
|
@@ -893,23 +894,28 @@ def _create_zip_file(ctx, *, output, zip_main, runfiles): | |
| progress_message = "Building Python zip: %{label}", | ||
| ) | ||
|
|
||
| def _get_zip_runfiles_path(path, workspace_name, legacy_external_runfiles): | ||
| maybe_workspace = "" | ||
| if legacy_external_runfiles and path.startswith(_EXTERNAL_PATH_PREFIX): | ||
| zip_runfiles_path = path.removeprefix(_EXTERNAL_PATH_PREFIX) | ||
| def _get_zip_runfiles_path(path, workspace_name = ""): | ||
| # NOTE @aignas 2025-12-06: This is to avoid the prefix checking in the very | ||
| # trivial case that is always happening once per this function call | ||
|
|
||
| # NOTE: Use "+" for performance | ||
| if workspace_name: | ||
| # NOTE: Use "+" for performance | ||
| return _ZIP_RUNFILES_DIRECTORY_NAME + "/" + workspace_name + "/" + path | ||
| else: | ||
| return _ZIP_RUNFILES_DIRECTORY_NAME + "/" + path | ||
|
Comment on lines
+901
to
+906
Contributor
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 function can be slightly simplified to reduce code duplication and improve readability by building the path prefix incrementally. This change maintains the use of |
||
|
|
||
| def _get_zip_runfiles_path_legacy(path, workspace_name, legacy_external_runfiles): | ||
| if legacy_external_runfiles and path.startswith(_EXTERNAL_PATH_PREFIX): | ||
| return _get_zip_runfiles_path(path.removeprefix(_EXTERNAL_PATH_PREFIX)) | ||
| elif path.startswith("../"): | ||
| # NOTE: External runfiles (artifacts in other repos) will have a leading | ||
| # path component of "../" so that they refer outside the main workspace | ||
| # directory and into the runfiles root. So we simplify it, e.g. | ||
| # "workspace/../foo/bar" to simply "foo/bar". | ||
| if path.startswith("../"): | ||
| zip_runfiles_path = path[3:] | ||
| else: | ||
| zip_runfiles_path = path | ||
| maybe_workspace = workspace_name + "/" | ||
|
|
||
| # NOTE: Use "+" for performance | ||
| return _ZIP_RUNFILES_DIRECTORY_NAME + "/" + maybe_workspace + zip_runfiles_path | ||
| return _get_zip_runfiles_path(path[3:]) | ||
| else: | ||
| return _get_zip_runfiles_path(path, workspace_name) | ||
|
|
||
| def _create_executable_zip_file( | ||
| ctx, | ||
|
|
||
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.
I would expect the paths to match what you'd see for a file in
runfiles.files.e.g., if a file in the runfiles comes from an external repo (and would have a short path of
external/repo/foo.pyor../repo/foo.py), then there would be an empty file with a similar short path.