rules_pkg icon indicating copy to clipboard operation
rules_pkg copied to clipboard

pkg_zip: overlapping TreeArtifact and file inputs result in duplicated directory entries

Open rbeasley-avgo opened this issue 10 months ago • 0 comments

Looks like more fallout from #640 .

Suppose a pkg_zip with two inputs, a TreeArtifact and a file where their destinations overlap and are expected to be merged in the resulting .zip file. The directories implicitly created by #640 may duplicate those already provided by the TreeArtifact, resulting in warnings to stderr and undefined downstream behavior.

# MODULE.bazel
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "rules_pkg", version = "1.0.1")
# defs.bzl
def _directory_impl(ctx):
    out = ctx.actions.declare_directory(ctx.attr.out)
    args = ctx.actions.args()
    args.add(out.path)
    args.add_all(ctx.attr.contents)
    ctx.actions.run_shell(
        outputs = [out],
        command = """
outdir=$1; shift
mkdir -p $outdir
for f in $@; do
    mkdir -p ${outdir}/$(dirname $f)
    touch ${outdir}/$f
done
""",
        arguments = [args],
    )
    return DefaultInfo(files = depset([out]))

directory = rule(
    _directory_impl,
    attrs = {
        "contents": attr.string_list(),
        "out": attr.string(),
    },
)
# BUILD
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("@rules_pkg//pkg:zip.bzl", "pkg_zip")
load(":defs.bzl", "directory")

directory(
    name = "metainf",
    out = "META-INF",
    contents = [
        "MANIFEST.MF",
        "com/example/foo/pom.xml",
    ],
)

write_file(
    name = "pom_properties",
    out = "pom.properties",
    content = [],
)

pkg_files(
    name = "metainf_pkg",
    srcs = ["metainf"],
)

pkg_files(
    name = "pom_pkg",
    srcs = [":pom_properties"],
    prefix = "META-INF/com/example/foo",
)

pkg_zip(
    name = "foo",
    out = "foo.jar",
    srcs = [
        ":metainf_pkg",
        ":pom_pkg",
    ],
)
$ bazelisk build :foo
INFO: Invocation ID: 1dcc898b-e2fe-4543-95b0-4b45f2f59cb8
INFO: Analyzed target //:foo (77 packages loaded, 2836 targets configured).
INFO: From PackageZip foo.jar:
.../execroot/_main/external/rules_python++python+python_3_11_x86_64-unknown-linux-gnu/lib/python3.11/zipfile.py:1567: UserWarning: Duplicate name: 'META-INF/com/'
  return self._open_to_write(zinfo, force_zip64=force_zip64)
.../execroot/_main/external/rules_python++python+python_3_11_x86_64-unknown-linux-gnu/lib/python3.11/zipfile.py:1567: UserWarning: Duplicate name: 'META-INF/com/example/'
  return self._open_to_write(zinfo, force_zip64=force_zip64)
.../execroot/_main/external/rules_python++python+python_3_11_x86_64-unknown-linux-gnu/lib/python3.11/zipfile.py:1567: UserWarning: Duplicate name: 'META-INF/com/example/foo/'
  return self._open_to_write(zinfo, force_zip64=force_zip64)
INFO: Found 1 target...
Target //:foo up-to-date:
  bazel-bin/foo.jar
INFO: Elapsed time: 0.912s, Critical Path: 0.30s
INFO: 3 processes: 7 action cache hit, 2 internal, 1 linux-sandbox.
INFO: Build completed successfully, 3 total actions
$ zipinfo  -1 bazel-bin/foo.jar
META-INF/
META-INF/MANIFEST.MF
META-INF/com/
META-INF/com/example/
META-INF/com/example/foo/
META-INF/com/example/foo/pom.xml
META-INF/com/
META-INF/com/example/
META-INF/com/example/foo/
META-INF/com/example/foo/pom.properties
Exception in thread "main" java.lang.RuntimeException: java.util.zip.ZipException: duplicate entry: META-INF/com/
        at org.eclipse.virgo.bundlor.support.manifestwriter.JarFileManifestWriter.write(JarFileManifestWriter.java:83)
        at org.eclipse.virgo.bundlor.commandline.internal.CommandLineBundlorExecutor.execute(CommandLineBundlorExecutor.java:64)
        at org.eclipse.virgo.bundlor.commandline.Bundlor.run(Bundlor.java:51)
        at org.eclipse.virgo.bundlor.commandline.Bundlor.main(Bundlor.java:37)

rbeasley-avgo avatar Feb 05 '25 20:02 rbeasley-avgo