Skip to content
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

Behavior of strip_prefix.files_only() and strip_prefix.from_pkg() swapped between pkg_files and pkg_tar #805

Open
sitaktif opened this issue Jan 5, 2024 · 0 comments
Labels
P2 An issue that should be worked on when time is available

Comments

@sitaktif
Copy link
Contributor

sitaktif commented Jan 5, 2024

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)

@aiuto aiuto added the P2 An issue that should be worked on when time is available label Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 An issue that should be worked on when time is available
Projects
None yet
Development

No branches or pull requests

2 participants