bazel-lib/lib/tests
Peter Lobsinger bca34bd17c
perf: report unused inputs for the tar rule (#951)
* perf: report unused inputs for the tar rule

The `mtree` spec passed to the `tar` rule very often selects a subset of the
inputs made available through the `srcs` attribute. In many cases, these
subsets do not break down cleanly along dependency-tree lines and there
is no simple way just pass less content to the `tar` rule.

One prominent example where this occurs is when constructing the tars
for OCI image layers. For instance when [building a Python-based
container image](https://github.com/bazel-contrib/rules_oci/blob/main/docs/python.md),
we might want to split the Python interpreter, third-party dependencies, and
application code into their own layers. This is done by [filtering the
`mtree_spec`](85cb2aaf8c/oci_python_image/py_layer.bzl (L39)).

However, in the operation to construct a `tar` from a subsetted mtree,
it is usually still an unsubsetted tree of `srcs` that gets passed. As
a result, the subset tarball is considered dependent upon a larger set
of sources than is strictly necessary.

This over-scoping runs counter to a very common objective associated with
breaking up an image into layers - isolating churn to a smaller slice of
the application. Because of the spurious relationships established in
Bazel's dependency graph, all tars get rebuilt anytime any content in
the application gets changed. Tar rebuilds can even be triggered by
changes to files that are completely filtered-out from all layers of the container.

Redundent creation of archive content is usually not too computationally
intensive, but the archives can be quite large in some cases, and
avoiding a rebuild might free up gigabytes of disk and/or network
bandwidth for
better use. In addition, eliminating the spurious dependency edges
removes erroneous constraints applied to the build action schedule;
these tend to push all Tar-building operations towards the end of a
build, even when some archive construction could be scheduled much earlier.

## Risk assessment and mitigation

The `unused_inputs_list` mechanism used to report spurious dependency
relationships is a bit difficult to use. Reporting an actually-used
input as unused can create difficult to diagnose problems down the line.

However, the behaviour of the `mtree`-based `tar` rule is sufficiently
simple and self-contained that I am fairly confident that this rule's
used/unused set can be determined accurately in a maintainable fashion.

Out of an abundance of caution I have gated this feature behind a
default-off flag. The `tar` rule will continue to operate as it had
before - typically over-reporting dependencies - unless the
`--@aspect_bazel_lib//lib:tar_compute_unused_inputs` flag is passed.

### Filter accuracy

The `vis` encoding used by the `mtree` format to resiliently handle path
names has a small amount of "play" to it - it is reversable but the
encoded representation of a string is not
unique. Two unequal encoded strings might decode to the same value; this
can happen when at least one of the encoded strings contains unnecessary
escapes that are nevertheless honoured by the decoder.

The unused-inputs set is determined using a filter that compares
`vis`-encoded strings. In the presence of non-canonically-encoded
paths, false-mismatches can lead to falsely reporting that an input is
unused.

The only `vis`-encoded path content that is under the control of callers
is the `mtree` content itself; all other `vis`-encoded strings are
constructed internally to this package, not exposed publicly, and are
all derived using the `lib/private/tar.bzl%_vis_encode` function; all of
these paths are expected to compare exactly. Additionally, it is expected that
many/most users will use this package's helpers (e.g. `mtree_spec`) when
crafting their mtree content; such content is also safe. It is only when
the user crafts their own mtree, or modifies an mtree spec's `content=`
fields' encoding in some way, that a risk of inaccurate reporting
arises. The chances for this are expected to be minor since this seems
like an inconvenient and not-particularly-useful thing for a user to go
out of their way to do.

* Also include other bsdtar toolchain files in keep set

* Add tri-state attribute to control unused-inputs behaviour

This control surface provides for granular control of the feature. The
interface is selected to mirror the common behaviour of `stamp` attributes.

* Add bzl_library level dep

* Update docs

* pre-commit

* Add reminder to change flag default on major-version bump

* Add note about how to make unused input computation exactly correct

* Add a test for unused_inputs listing

* Support alternate contents= form

This is accepted by bsdtar/libarchive. In fact `contents=` is the only of
the pair documented in `mtree(5)`; `content=` is an undocumented
alternate form supported by libarchive.

* Don't try to prune the unprunable

Bazel's interpretation of unused_inputs_list cannot accomodate certain
things in filenames. These are also likely to mess up our own
line-oriented protocol in the shellscript that produces this file.

Co-authored-by: Sahin Yort <thesayyn@gmail.com>

* Rerun docs update

---------

Co-authored-by: Sahin Yort <thesayyn@gmail.com>
2024-10-13 09:58:56 -07:00
..
assert_archive_contains chore: also accept expected as a list of entries 2023-01-20 16:16:42 -08:00
assert_contains fix: support special chars in assert_contains expected expression (#426) 2023-05-10 16:07:40 -07:00
assert_directory_contains feat: add assert_directory_contains test to testing.bzl (#560) 2023-09-29 09:53:00 -07:00
bats chore: enable go, shell, yaml formatters and bazel run //:format (#917) 2024-08-20 11:56:40 -07:00
bazelrc_presets chore: update git urls (#926) 2024-09-17 17:05:35 -07:00
copy_directory chore: update pre-commit buildifier (#563) 2023-09-29 14:42:33 -07:00
copy_directory_bin_action chore: add windows binaries (#610) 2023-10-10 14:13:17 -07:00
copy_file chore: update pre-commit buildifier (#563) 2023-09-29 14:42:33 -07:00
copy_to_bin chore: set test timeouts to short 2022-08-20 13:58:43 -07:00
copy_to_directory feat: add an option to not include copy_to_directory output in runfiles (#886) 2024-09-19 01:16:14 +00:00
copy_to_directory_bin_action chore: update git urls (#926) 2024-09-17 17:05:35 -07:00
coreutils chore: add windows binaries (#610) 2023-10-10 14:13:17 -07:00
expand_template feat: don't require 'out' on expand_template (#798) 2024-03-26 16:57:16 -07:00
external_test_repo refactor: remove directory functionality from copy_file (#64) 2022-04-05 13:38:54 -07:00
jq jq: add data file and location expansion support (#757) 2024-02-26 17:43:56 -08:00
run_binary test: add test / example of using root path on a run_binary directory output (#862) 2024-06-05 13:48:07 -07:00
run_binary_expansions chore: cleanup before bazel-contrib handoff (#918) 2024-09-02 09:32:38 -07:00
stamping chore: cleanup before bazel-contrib handoff (#918) 2024-09-02 09:32:38 -07:00
tar perf: report unused inputs for the tar rule (#951) 2024-10-13 09:58:56 -07:00
transitions chore: update git urls (#926) 2024-09-17 17:05:35 -07:00
write_source_files fix: Set size to a default value as well as timeout. (#839) 2024-07-19 12:50:50 -07:00
yq chore: update pre-commit buildifier (#563) 2023-09-29 14:42:33 -07:00
zstd feat: introduce zstd toolchain (#831) 2024-05-03 16:12:56 -07:00
BUILD.bazel feat: allow write_source_file(s) to write source files to bazel packages outside of the target's package (#717) 2024-01-10 15:07:03 -08:00
a.js feat: allow write_source_file(s) to write source files to bazel packages outside of the target's package (#717) 2024-01-10 15:07:03 -08:00
base64_tests.bzl chore: fixup test sizes to resolve warnings (#911) 2024-08-19 15:33:54 -04:00
expand_make_vars_test.bzl chore: set test timeouts to short 2022-08-20 13:58:43 -07:00
generate_outputs.bzl feat: add executable attribute to write_source_files 2022-12-03 10:46:08 -08:00
glob_match_test.bzl fix: rewrite glob implementation to follow standard glob specs 2023-05-08 07:46:19 -07:00
lists_test.bzl chore: fixup test sizes to resolve warnings (#911) 2024-08-19 15:33:54 -04:00
paths_test.bzl fix(ci): fix bzlmod issues and enable on ci (#658) 2023-11-15 15:07:03 -08:00
strings_tests.bzl fix: correctly split quoted args (#909) 2024-08-19 16:36:41 -04:00
utils_test.bzl fix(ci): fix bzlmod issues and enable on ci (#658) 2023-11-15 15:07:03 -08:00