Skip to content

Optimize copy_file - #565

Merged
tetromino merged 1 commit into
bazelbuild:mainfrom
fmeum:drop-exec-requirements
Nov 4, 2025
Merged

tetromino merged 1 commit into
bazelbuild:mainfrom
fmeum:drop-exec-requirements

Conversation

@fmeum

@fmeum fmeum commented Mar 19, 2025 •

Copy link
Copy Markdown
Contributor
  • ctx.actions.symlink can be used on all platforms and falls back to a copy if a symlink is unsupported. It is also heavily optimized, avoiding the need to hash the output file again.
  • Bazel never guarantees that an input to an action is staged as a non-symlink, so whether an output is a symlink or a hard copy only matters for top-level outputs consumed outside Bazel and handled by tools that don't follow symlinks by default, which should be extremely rare and could be worked around by explicitly setting allow_symlink to False in user-controlled code.
  • For file copy actions that do not go through ctx.actions.symlink, caching is extremely cheap since the CAS entry of the input will be reused as the CAS entry of the output. Allowing remote execution and caching enables BwoB for the copy, which can avoid downloads of both the input and the output file.

The same changes are not applied to copy_directory as source directories are not officially supported by Bazel before 9 and any kind of change could cause subtle incorrectness.

Fixes #562

@fmeum

fmeum commented Mar 19, 2025

Copy link
Copy Markdown
Contributor Author

@ulfjack @tjgq Could you review this?

@fmeum
fmeum force-pushed the drop-exec-requirements branch 3 times, most recently from 8e6d9e9 to 3127c3f Compare March 19, 2025 11:29
@fmeum

fmeum commented Mar 19, 2025

Copy link
Copy Markdown
Contributor Author

FYI @kormide, I think that most of the exec requirements are actually harmful for copy_file. Do you see any downside to dropping them as long as they are kept on copy_directory?

@fmeum
fmeum force-pushed the drop-exec-requirements branch from 3127c3f to 0ebea4d Compare March 19, 2025 11:35
@ulfjack

ulfjack commented Mar 20, 2025

Copy link
Copy Markdown

I wonder if it would be helpful to have a mechanism to pass in execution_requirements manually?

@fmeum

fmeum commented Mar 20, 2025

Copy link
Copy Markdown
Contributor Author

I wonder if it would be helpful to have a mechanism to pass in execution_requirements manually?

You mean on a per-target level (per-mnemonic level is possible with --modify_execution_info)? I think that exec_properties can be used for this (with empty values).

@fmeum
fmeum force-pushed the drop-exec-requirements branch from 4c70b00 to e5875fd Compare November 4, 2025 10:50
* `ctx.actions.symlink` can be used on all platforms and falls back to a copy if a symlink is unsupported. It is also heavily optimized, avoiding the need to hash the output file again.
* Bazel never guarantees that an input to an action is staged as a non-symlink, so whether an output is a symlink or a hard copy only matters for top-level outputs consumed outside Bazel and handled by tools that don't follow symlinks by default, which should be extremely rare and could be worked around by explicitly setting `allow_symlink` to `False` in user-controlled code.
* For file copy actions that do not go through `ctx.actions.symlink`, caching is extremely cheap since the CAS entry of the input will be reused as the CAS entry of the output. Allowing remote execution and caching enables BwoB for the copy, which can avoid downloads of both the input and the output file.

The same changes are not applied to `copy_directory` as source directories are not officially supported by Bazel and any kind of change could cause subtle incorrectness.
@fmeum
fmeum force-pushed the drop-exec-requirements branch from e5875fd to c1fcab0 Compare November 4, 2025 10:51

@tjgq tjgq left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Rationale: this change is beneficial to many Google-owned (and other) Bazel projects that use skylib. It's also widely used in the Google monorepo.

@tetromino

Copy link
Copy Markdown
Collaborator

So the idea of this PR is to

Seems reasonable.

@tetromino
tetromino merged commit 392fdb1 into bazelbuild:main Nov 4, 2025
gzm0 added a commit to gzm0/bazel-skylib that referenced this pull request Dec 19, 2025
tetromino pushed a commit that referenced this pull request Dec 20, 2025
AleDema pushed a commit to AleDema/ic that referenced this pull request Nov 18, 2026
Upgrades `bazel_skylib` from 1.9.0 to
[1.9.2](https://github.com/bazelbuild/bazel-skylib/releases/tag/1.9.2),
its latest release. 1.9.1 was tagged but its release CI failed, so it
was never released or published to the BCR (see the
[CHANGELOG](https://github.com/bazelbuild/bazel-skylib/blob/1.9.2/CHANGELOG.md)).

The unstamped release artifacts are byte-identical to master.

## Changes

- **`MODULE.bazel`**: version bump. BCR's `bazel_skylib/1.9.2` has no
patches or overlays, and its `MODULE.bazel` declares the same
dependencies as 1.9.0 (`platforms` 0.0.10, `rules_license` 1.0.0). Apart
from skylib's own version, the full `bazel mod graph` is identical to
master.
- **`publish/canisters/BUILD.bazel`**: sets `allow_symlink = True` on
its two `copy_file` calls, which copy the canister `.wasm.gz` files and
their `.did` files. This keeps those copies as symlinks (see below).

dfinity#12159 (rules_foreign_cc 0.17.0) also bumps `bazel_skylib` to 1.9.2,
because rules_foreign_cc 0.17.0 requires it. It carries the same two
`allow_symlink = True` lines, which are needed whichever of the two PRs
lands first.

## Upstream changes that touch our setup

Between the 1.9.0 and 1.9.2 release archives, only `copy_file`,
`selects`, the `common_settings` rules and the version strings change.
`write_file`, `run_binary`, `unittest`, `paths` and the rest of what we
use are unchanged.

- **`copy_file` copies by default again.** 1.9.0 made `allow_symlink`
default to `True` for non-executable files
([bazelbuild/bazel-skylib#565](bazelbuild/bazel-skylib#565)).
1.9.2 reverts that default to `False`
([bazelbuild/bazel-skylib#605](bazelbuild/bazel-skylib#605),
fixing
[bazelbuild/bazel-skylib#604](bazelbuild/bazel-skylib#604)),
so such a `copy_file` now runs `cp` instead of creating a symlink.
- The `copy_file`s in `ic-os/defs.bzl` and `ic-os/setupos/defs.bzl`
already set `allow_symlink = True`, so they don't change.
- The two in `publish/canisters/BUILD.bazel` relied on the 1.9.0
default. dfinity#10679 removed the `.did` copies' `allow_symlink = False` so
they would be symlinked, after dfinity#10672 made `ci/src/artifacts/upload.sh`
upload symlinked bundle entries under their own names. With
`allow_symlink = True` set explicitly, those 87 copies (44 `.wasm.gz`,
43 `.did`) stay symlinks; otherwise they would turn into `cp` actions.
- Outside the release artifacts' graph, the revert turns 18 external
`copy_file`s in the `//...` graph from symlinks into copies, because
they use the default: rules_python's namespace-package `__init__.py`
files for the numpy (11), pulp (4) and protobuf (2) wheels, and gawk's
`config.h` (gawk comes in through rules_distroless). Their content
doesn't change, and the tests that import those packages pass (see
Verification).
- **`selects.config_setting_group` with `//conditions:default`** now
aliases a new `@bazel_skylib//lib:always_true` target instead of
generating two `config_setting`s on `--stamp`
([bazelbuild/bazel-skylib#611](bazelbuild/bazel-skylib#611),
[bazelbuild/bazel-skylib#629](bazelbuild/bazel-skylib#629)).
We don't load `selects`, and no target in the configured `//...` graph
depends on a `@bazel_skylib//lib` target.
- **The `scope` attribute of the `common_settings` rules defaults to
`"target"` instead of `"universal"`**
([bazelbuild/bazel-skylib#608](bazelbuild/bazel-skylib#608)).
Our flags are defined with these rules (`//bazel:enable_fuzzing_code`,
`//bazel:enable_malicious_code`, `//bazel:hermetic_cc`,
`//:pocket-ic-server-variant`, ...), and none of them sets `scope`.
- Bazel 9.3.2 only reads a flag's `scope` when it is set explicitly.
That applies both to flags set on the command line
([`StarlarkOptionsParser`](https://github.com/bazelbuild/bazel/blob/9.3.2/src/main/java/com/google/devtools/build/lib/runtime/StarlarkOptionsParser.java#L256-L260))
and to flags set by transitions
([`BuildOptionsScopeFunction`](https://github.com/bazelbuild/bazel/blob/9.3.2/src/main/java/com/google/devtools/build/lib/skyframe/BuildOptionsScopeFunction.java#L181-L189)).
- `--incompatible_exclude_starlark_flags_from_exec_config` is off by
default in 9.3.2, and we don't set it. With it off, a flag without an
explicit scope still propagates to the exec configuration
([`FunctionTransitionUtil`](https://github.com/bazelbuild/bazel/blob/9.3.2/src/main/java/com/google/devtools/build/lib/analysis/starlark/FunctionTransitionUtil.java#L239-L251)).
- Checked with both versions: `--//bazel:enable_fuzzing_code=True` on
the command line and `//bazel:enable_malicious_code` set by the
`malicious_binary` transition both reach the exec configurations. The
configuration hashes are the same as on master.

## Verification

All in the dev container:

- **Module graph:** `bazel mod graph` is identical to master apart from
skylib's own version. `bazel mod explain bazel_skylib` shows every
dependent resolving 1.9.2. There are no new warnings; the only one is
the existing `rules_qemu` `use_repo()` warning.
- **Analysis:** `bazel build //... --nobuild` analyzes all 3387 targets.
- **Lint:** the CI Linter checks (`shfmt-check`, `ruff-check`,
`buf-check`, `DFINITY-capitalization-check`, `do-not-merge-check`,
`buildifier-check`, `gazelle-check`, `buf-breaking-check`) pass.
`buildifier-fix`, `shfmt-fix`, `ruff-fix` and `gazelle` change nothing.
- **`copy_file` actions:** in the release artifacts' graph there are 227
(215 symlinks, 12 copies). `bazel aquery` prints identical output for
them with 1.9.0 and 1.9.2, action keys included. In the `//...` graph,
the only differences are the 18 external symlink-to-copy switches listed
above.
- **Release artifacts are byte-identical to master** (133 files): the
unstamped IC-OS bundles that `ci/container/build-ic.sh --icos` builds
(GuestOS prod disk and update, GuestOS recovery update, HostOS update,
SetupOS), `//publish/binaries:bundle` and `//publish/canisters:bundle`.
No sandboxed actions ran: everything was a disk-cache hit apart from 26
uncacheable local actions.
- **Tests:** these 33 pass:
- `//bazel/mainnet_artifact_refs_tests:mainnet_artifact_refs_test`: 10
analysis tests built on skylib's `unittest`.
- The 20 `file_size_check` tests in `//publish/canisters`, which read
the symlinked canisters.
- `//rs/recovery/subnet_splitting/split_finder:split_finder_test`,
`//ci/githubstats:query_test` and `//ci/githubstats:query_help_test`.
They import pandas (and with it numpy), pulp and protobuf, whose
namespace `__init__.py` files are now copies.
- **Builds:**
- `//rs/rosetta-api/icp:version` and `//rs/rosetta-api/icrc1:version`
(`write_file`) contain `2.1.10` and `1.2.10`.
  - `@@gawk+//:gawk` compiles with its `config.h` now a copied file.
- **Not run locally:** the full build and test suite, the system tests
and the macOS builds. CI covers those.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

copy_file should *never* set no-remote

4 participants