Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ attrs = {"rpm_filetag": ("config(missingok, noreplace)",)},

Is the equivalent to `%config(missingok, noreplace)` in the `%files` list.

The files of a TreeArtifact are listed in the `%files` list, but its
directories are not, so the package does not own them and they are left
behind on uninstall. Set `rpm_own_tree_dirs` to `True` in the attributes
of the packaging rule mapping the TreeArtifact to also add a `%dir` entry
for the TreeArtifact destination and every directory below it:

```
attributes = pkg_attributes(rpm_own_tree_dirs = True),
```

This rule produces 2 artifacts: an .rpm and a .changes file. The DefaultInfo will
include both. If you need downstream rule to specifically depend on only the .rpm or
.changes file then you can use `filegroup` to select distinct output groups.
Expand Down
15 changes: 15 additions & 0 deletions pkg/rpm/augment_rpm_files_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
{0} "{1}"
""".strip()

_INSTALL_DIR_STANZA_FMT = """
install -d '%{{buildroot}}/{0}'
""".strip()

_DIR_MODE_STANZA_FMT = """
%dir "{0}"
""".strip()

# Cheapo arg parsing. Currently this script is single-purpose.

# JSON file containing the TreeArtifact manifest info.
Expand All @@ -54,6 +62,7 @@
# Computed outputs to be combined with the originals
dir_install_script_segments = []
dir_files_segments = []
owned_dirs = set()

with open(dir_data_path, 'r') as fh:
dir_data = json.load(fh)
Expand All @@ -67,6 +76,12 @@
# destination.
path_relative_to_install_dest = os.path.relpath(root, start=d["src"])

dir_dest = os.path.normpath(os.path.join(d["dest"], path_relative_to_install_dest))
if d.get("own_dirs") and dir_dest not in owned_dirs:
owned_dirs.add(dir_dest)
dir_install_script_segments.append(_INSTALL_DIR_STANZA_FMT.format(dir_dest))
dir_files_segments.append(_DIR_MODE_STANZA_FMT.format(dir_dest))

for f in files:
full_dest = os.path.join(d["dest"], path_relative_to_install_dest, f)
dir_install_script_segments.append(_INSTALL_FILE_STANZA_FMT.format(
Expand Down
11 changes: 11 additions & 0 deletions pkg/rpm_pfg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def _process_files(pfi, origin_label, grouping_label, file_base, rpm_ctx):
# us from having to having to maintain tag processing
# code in multiple places.
"tags": file_base,
"own_dirs": pfi.attributes.get("rpm_own_tree_dirs", False),
})
else:
if dest in rpm_ctx.dest_check_map:
Expand Down Expand Up @@ -945,6 +946,16 @@ pkg_rpm_impl = rule(

Is the equivalent to `%config(missingok, noreplace)` in the `%files` list.

The files of a TreeArtifact are listed in the `%files` list, but its
directories are not, so the package does not own them and they are left
behind on uninstall. Set `rpm_own_tree_dirs` to `True` in the attributes
of the packaging rule mapping the TreeArtifact to also add a `%dir` entry
for the TreeArtifact destination and every directory below it:

```
attributes = pkg_attributes(rpm_own_tree_dirs = True),
```

This rule produces 2 artifacts: an .rpm and a .changes file. The DefaultInfo will
include both. If you need downstream rule to specifically depend on only the .rpm or
.changes file then you can use `filegroup` to select distinct output groups.
Expand Down
94 changes: 94 additions & 0 deletions tests/rpm/tree_artifacts/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,97 @@ pkg_files(
),
renames = {":test_dir1": REMOVE_BASE_DIRECTORY},
)

############################################################################
# Test that rpm_own_tree_dirs makes the RPM own the TreeArtifact directories
############################################################################
py_test(
name = "owned_tree_dirs",
srcs = ["rpm_owned_tree_dirs_test.py"],
data = [":owned_tree_dirs_rpm_test_data"],
env = {"TEST_RPM": "owned_tree_dirs_rpm-1.1.1-2222.noarch.rpm"},
main = "rpm_owned_tree_dirs_test.py",
tags = [
"no_windows", # Windows doesn't have rpm(8) or rpmbuild(8)
],
deps = [
"//tests/rpm:rpm_util",
"@rules_python//python/runfiles",
],
)

sh_library(
name = "owned_tree_dirs_rpm_test_data",
testonly = True,
srcs = [":owned_tree_dirs_rpm"],
)

pkg_rpm(
name = "owned_tree_dirs_rpm",
srcs = [
":not_owned_tree_dirs_pf",
":owned_tree_dirs_pf",
":owned_tree_dirs_same_dest_pf",
],
architecture = "noarch",
description = """pkg_rpm test rpm description""",
license = "Apache 2.0",
release = "2222",
spec_template = "//tests/rpm:template-test.spec.tpl",
summary = "pkg_rpm test rpm summary",
version = "1.1.1",
)

pkg_files(
name = "owned_tree_dirs_pf",
srcs = [
":test_dir1",
],
attributes = pkg_attributes(
group = "root",
mode = "0644",
rpm_own_tree_dirs = True,
user = "root",
),
prefix = "opt",
)

pkg_files(
name = "not_owned_tree_dirs_pf",
srcs = [
":test_dir1",
],
attributes = pkg_attributes(
group = "root",
mode = "0644",
rpm_own_tree_dirs = False,
user = "root",
),
prefix = "srv",
)

# Shares the destination of owned_tree_dirs_pf, so its directories must only be
# listed once.
pkg_files(
name = "owned_tree_dirs_same_dest_pf",
srcs = [
":test_dir2",
],
attributes = pkg_attributes(
group = "root",
mode = "0644",
rpm_own_tree_dirs = True,
user = "root",
),
prefix = "opt",
renames = {":test_dir2": "test_dir"},
)

directory(
name = "test_dir2",
contents = "test_dir1",
filenames = [
"g",
"subdir/h",
],
)
67 changes: 67 additions & 0 deletions tests/rpm/tree_artifacts/rpm_owned_tree_dirs_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

# Copyright 2026 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import csv
import io
import os
import unittest

from python.runfiles import runfiles
from tests.rpm import rpm_util

EXPECTED_RPM_MANIFEST_CSV = """
path,digest,user,group,mode,fflags,symlink
/opt/test_dir,,root,root,40755,,
/opt/test_dir/a,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
/opt/test_dir/b,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
/opt/test_dir/g,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
/opt/test_dir/subdir,,root,root,40755,,
/opt/test_dir/subdir/c,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
/opt/test_dir/subdir/d,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
/opt/test_dir/subdir/h,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
/srv/test_dir/a,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
/srv/test_dir/b,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
/srv/test_dir/subdir/c,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
/srv/test_dir/subdir/d,dc35e5df50b75e25610e1aaaa29edaa4,root,root,100644,,
""".strip()


class PkgRpmCompManifest(unittest.TestCase):
def setUp(self):
self.runfiles = runfiles.Create()
self.maxDiff = None
# Allow for parameterization of this test based on the desired RPM to
# test.
self.rpm_path = self.runfiles.Rlocation(os.path.join(
os.environ["TEST_WORKSPACE"],
"tests", "rpm", "tree_artifacts",
# The object behind os.environ is not a dict, and thus doesn't have
# the "getdefault()" we'd otherwise use here.
os.environ["TEST_RPM"] if "TEST_RPM" in os.environ else "owned_tree_dirs_rpm.rpm",
))

def test_contents_match(self):
sio = io.StringIO(EXPECTED_RPM_MANIFEST_CSV)
manifest_reader = csv.DictReader(sio)
manifest_specs = {r['path']: r for r in manifest_reader}

rpm_specs = rpm_util.read_rpm_filedata(self.rpm_path)

self.assertDictEqual(manifest_specs, rpm_specs)


if __name__ == "__main__":
unittest.main()
Loading