bazelbuild / rules_pkg

Bazel rules for creating packages of many types (zip, tar, deb, rpm, ...)
Apache License 2.0
216 stars 171 forks source link

pkg_rpm: Specify _topdir for install stanza #763

Closed kellyma2 closed 8 months ago

kellyma2 commented 10 months ago

If we don't specify the _topdir as part of the install-from path we can't seem to find the correct files to install when using a set of rules like:

pkg_filegroup(
    name = "my_filegroup",
    srcs = [
      "//some/label:foo",
    ],
    prefixs = "/usr/bin",
)

pkg_rpm(
    name = "rpm",
    ...
    srcs = [
        ":my_filegroup",
    ],
)

Adding %{{_topdir}/BUILD/ as a prefix to the source path seems to solve this problem.

aiuto commented 10 months ago

Can you add a test for this and explain more clearly what is failing? The RPM builder has been working for a long time, and some test input is from pkg_filegroup, so it is surprising that the code is so for off.

kellyma2 commented 10 months ago

Can you add a test for this and explain more clearly what is failing? The RPM builder has been working for a long time, and some test input is from pkg_filegroup, so it is surprising that the code is so for off.

I'll try to rig up a test for this tomorrow and also provide more details. The tl;dr was that it couldn't find the expected inputs in the relative path, but putting the prefix in place means it becomes an absolute path and can be resolved.

kellyma2 commented 10 months ago

Can you add a test for this and explain more clearly what is failing? The RPM builder has been working for a long time, and some test input is from pkg_filegroup, so it is surprising that the code is so for off.

Still working through adding a test for this, but figured I'd provide a bit more detail as to what the failure (and my rules) look like. I'm using https://github.com/bazelbuild/examples/tree/main/cpp-tutorial/stage1 as a simple scaffold for verifying the failure. My rules are as follows:

pkg_files( name = "exe", srcs = [ ":hello-world", ], visibility = ["//visibility:public"], attributes = pkg_attributes( mode = "0755", user = "root", group = "root", ), )


Root BUILD:

load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup") load("@rules_pkg//pkg:rpm.bzl", "pkg_rpm")

pkg_filegroup( name = "rpm_filegroup", srcs = [ "//main:exe", ], prefix = "/usr/bin", )

pkg_rpm( name = "rpm", package_name = "example", license = "bar", summary = "baz", version = "1.0.0", release = "foo", description = "bam", architecture = "x86_64", srcs = [ ":rpm_filegroup", ], )

in WORKSPACE I'm configuring things with:

load("@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild")

find_system_rpmbuild(name="rule_pkg_rpmbuild")


Where @rules_pkg is just pointing at a fresh clone of rules_pkg in my local worktree.

When I run `bazel build rpm` the output I get is:

bazel build rpm Loading: Loading: Loading: 0 packages loaded Analyzing: target //:rpm (1 packages loaded, 0 targets configured) INFO: Analyzed target //:rpm (2 packages loaded, 5 targets configured). INFO: Found 1 target... bazel: Entering directory `/tmp/mkelly/bazel/execroot/main/' [0 / 8] [Prepa] BazelWorkspaceStatusAction stable-status.txt INFO: From MakeRpm example-1.0.0-foo.x86_64.rpm: Error calling rpmbuild: Executing(%install): /bin/sh -e /tmp/tmp2s0xmxiy/TMP/rpm-tmp.u26xbD

  • umask 022
  • cd /tmp/tmp2s0xmxiy/bld-x86_64/example
  • '[' /tmp/tmp2s0xmxiy/BUILDROOT '!=' / ']'
  • rm -rf /tmp/tmp2s0xmxiy/BUILDROOT ++ dirname /tmp/tmp2s0xmxiy/BUILDROOT
  • mkdir -p /tmp/tmp2s0xmxiy
  • mkdir /tmp/tmp2s0xmxiy/BUILDROOT ++ dirname /usr/bin/hello-world
  • install -d /tmp/tmp2s0xmxiy/BUILDROOT//usr/bin
  • cp bazel-out/k8-fastbuild/bin/main/hello-world /tmp/tmp2s0xmxiy/BUILDROOT//usr/bin/hello-world cp: cannot stat 'bazel-out/k8-fastbuild/bin/main/hello-world': No such file or directory error: Bad exit status from /tmp/tmp2s0xmxiy/TMP/rpm-tmp.u26xbD (%install)

RPM build errors: Bad exit status from /tmp/tmp2s0xmxiy/TMP/rpm-tmp.u26xbD (%install)

No RPM file created. ERROR: /tmp/test/examples/cpp-tutorial/stage1/BUILD:12:8: output 'example-1.0.0-foo.x86_64.rpm' was not created ERROR: /tmp/test/examples/cpp-tutorial/stage1/BUILD:12:8: MakeRpm example-1.0.0-foo.x86_64.rpm failed: not all outputs were created or valid bazel: Leaving directory `/tmp/mkelly/bazel/execroot/main/' Target //:rpm failed to build Use --verbose_failures to see the command lines of failed build steps. INFO: Elapsed time: 0.255s, Critical Path: 0.17s INFO: 2 processes: 1 internal, 1 linux-sandbox. FAILED: Build did NOT complete successfully

aiuto commented 10 months ago
  • cp bazel-out/k8-fastbuild/bin/main/hello-world /tmp/tmp2s0xmxiy/BUILDROOT//usr/bin/hello-world cp: cannot stat 'bazel-out/k8-fastbuild/bin/main/hello-world': No such file or directory I'm scratching my head a bit on this. That should be the path to hello-world.
kellyma2 commented 10 months ago
  • cp bazel-out/k8-fastbuild/bin/main/hello-world /tmp/tmp2s0xmxiy/BUILDROOT//usr/bin/hello-world cp: cannot stat 'bazel-out/k8-fastbuild/bin/main/hello-world': No such file or directory I'm scratching my head a bit on this. That should be the path to hello-world.

That would be my expectation and with this PR it actually correctly points to it because the path ceases to be relative. When you take the example repo and apply my BUILD + WORKSPACE setup what do you get?

Also, missed part of my WORKSPACE the correct variant also includes:

local_repository(
    name = "rules_pkg",
    path = "/home/mkelly/repos/bazel/rules_pkg",
)

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")

rules_pkg_dependencies()
aiuto commented 10 months ago

Take a look at https://github.com/bazelbuild/rules_pkg/pull/770

I tried to prove your change works, but it failed in new and unexpected ways. Perhaps you do something like that as a test case or example.

kellyma2 commented 9 months ago

After a bit of thrashing around with the Fedora docker image it seems like I got this working and it's using rpmbuild 4.18.1 - I'll see if I can upgrade rpmbuild in my build environment to determine if this also happens there.

Secondary to this, should there be a minimum version check for the version of rpmbuild being used?