There is a bug that is confusing most users of pkg_tar (like it did with me): the effects of using strip_prefix.files_only() and strip_prefix.from_pkg() work as advertised when using it with pkg_files, but the behavior is unexpectedly swapped when using it with pkg_tar.
Below is a full repro:
#!/bin/bash
set -eu
echo '6.4.0' > .bazelversion
mkdir subdir
touch one.txt subdir/two.txt
cat >WORKSPACE <<'EOF'
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_pkg",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
],
sha256 = "8f9ee2dc10c1ae514ee599a8b42ed99fa262b757058f65ad3c384289ff70c4b8",
)
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
rules_pkg_dependencies()
EOF
cat >BUILD.bazel <<'EOF'
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
# Expected behavior
# `strip_prefix.from_pkg()` in `pkg_file` dependency produces expected non-flat layout:
# one.txt
# subdir/two.txt
pkg_files(
name = "pkg-files-strip-from-pkg",
srcs = [
"one.txt",
"subdir/two.txt",
],
strip_prefix = strip_prefix.from_pkg(),
)
pkg_tar(
name = "pkg-tar-nonflat-ok",
srcs = [":pkg-files-strip-from-pkg"],
)
# `strip_prefix.files_only()` in `pkg_file` dependency produces expected flat layout:
# one.txt
# two.txt
pkg_files(
name = "pkg-files-strip-files-only",
srcs = [
"one.txt",
"subdir/two.txt",
],
strip_prefix = strip_prefix.files_only(),
)
pkg_tar(
name = "pkg-tar-flat-ok",
srcs = [":pkg-files-strip-files-only"],
)
# Unexpected behaviors
# `strip_prefix.files_only()` in `pkg_tar` with inlined files in `srcs` produces unexpected non-flat layout:
# one.txt
# subdir/two.txt
pkg_tar(
name = "pkg-tar-flat-unexpected",
srcs = [
"one.txt",
"subdir/two.txt",
],
strip_prefix = strip_prefix.from_pkg(),
)
# `strip_prefix.from_pkg()` in `pkg_tar` with inlined files in `srcs` produces unexpected flat layout:
# one.txt
# two.txt
pkg_tar(
name = "pkg-tar-nonflat-unexpected",
srcs = [
"one.txt",
"subdir/two.txt",
],
strip_prefix = strip_prefix.files_only(),
)
EOF
for target in pkg-tar-nonflat-ok pkg-tar-flat-ok pkg-tar-flat-unexpected pkg-tar-nonflat-unexpected; do
echo "* Building target: $target"
bazel build "//:$target"
echo "Contents:"
tar tf "bazel-bin/$target.tar"
echo
done
(Side note: in both pkg_tar and pkg_files, the default behavior is to flatten the files when strip_prefix is unset, which is consistent)
There is a bug that is confusing most users of
pkg_tar
(like it did with me): the effects of usingstrip_prefix.files_only()
andstrip_prefix.from_pkg()
work as advertised when using it withpkg_files
, but the behavior is unexpectedly swapped when using it withpkg_tar
.Below is a full repro:
(Side note: in both
pkg_tar
andpkg_files
, the default behavior is to flatten the files whenstrip_prefix
is unset, which is consistent)