Skip to content

Commit dff982e

Browse files
committed
fix(test): make packaging_test work with Bazel 9
Bazel 9 flipped `--incompatible_strict_action_env` to `true` by default (bazelbuild/bazel#27670), which means tests no longer inherit `PATH` from the host environment. This breaks subprocess calls that rely on `PATH` lookup: ``` ==================== Test output for //distro:packaging_test: E. ====================================================================== ERROR: testBuild (__main__.PackagingTest.testBuild) ---------------------------------------------------------------------- Traceback (most recent call last): [...] build_result = subprocess.check_output(['bazel', 'build', '--enable_workspace', ':dummy_tar']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [...] FileNotFoundError: [Errno 2] No such file or directory: 'bazel' ``` This change therefore adds `env_inherit = ["PATH"]` to that very test, which matches the behavior of earlier Bazel versions and is consistent with the presence of a "noci" tag. Additionally, Bazel 9 removed `WORKSPACE` support entirely (bazelbuild/bazel#26131), requiring `bzlmod` with `MODULE.bazel`: ``` ==================== Test output for //distro:packaging_test: [...] WARNING: --enable_bzlmod is set, but no MODULE.bazel file was found at the workspace root. Bazel will create an empty MODULE.bazel file. Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. For more details, please refer to bazelbuild/bazel#18958. [...] ERROR: error loading package '': Unable to find package for @@[unknown repo 'not_named_rules_pkg' requested from @@]//pkg:tar.bzl: The repository '@@[unknown repo 'not_named_rules_pkg' requested from @@]' could not be resolved: No repository visible as '@not_named_rules_pkg' from main repository. [...] ERROR: Build did NOT complete successfully E. ====================================================================== ERROR: testBuild (__main__.PackagingTest.testBuild) ---------------------------------------------------------------------- [...] build_result = subprocess.check_output(['bazel', 'build'] + bazel_flags + [':dummy_tar']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [...] subprocess.CalledProcessError: Command '['bazel', 'build', '--enable_workspace', ':dummy_tar']' returned non-zero exit status 1. ``` The test now detects the Bazel version at runtime and generates the appropriate setup: - Bazel 9+: `MODULE.bazel`, with `bazel_dep` and `archive_override`, - earlier versions, as before: `WORKSPACE`, with `http_archive` and `--enable_workspace` flag. For good measure, the change also addresses a leftover TODO by replacing the `tar` subprocess invocation with pure Python code.
1 parent e93d65a commit dff982e

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

distro/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ py_test(
9898
":small_module",
9999
"//:standard_package",
100100
],
101+
env_inherit = ["PATH"],
101102
imports = [".."],
102103
local = True,
103104
python_version = "PY3",

distro/packaging_test.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import re
1919
import subprocess
20+
import tarfile
2021
import unittest
2122

2223
from python.runfiles import runfiles
@@ -57,26 +58,20 @@ def testBuild(self):
5758
tempdir = os.path.join(os.environ['TEST_TMPDIR'], 'build')
5859
if not os.path.exists(tempdir):
5960
os.makedirs(tempdir)
60-
with open(os.path.join(tempdir, 'WORKSPACE'), 'w') as workspace:
61+
filename, setup_lines, bazel_flags = self._select_bazel_supported_setup()
62+
with open(os.path.join(tempdir, filename), 'w') as setup:
6163
file_name = release_tools.package_basename(self.source_repo, self.version)
6264
# The code looks wrong, but here is why it is correct.
6365
# - Rlocation requires '/' as path separators, not os.path.sep.
6466
# - When we read the file, the path must use os.path.sep
6567
local_path = self.data_files.Rlocation(
6668
'rules_pkg/distro/' + file_name).replace('/', os.path.sep)
6769
sha256 = release_tools.get_package_sha256(local_path)
68-
workspace_content = '\n'.join((
69-
'workspace(name = "test_rules_pkg_packaging")',
70-
release_tools.workspace_content(
71-
'file://%s' % local_path, self.source_repo, sha256,
72-
rename_repo=self.dest_repo,
73-
deps_method='rules_pkg_dependencies'
74-
)
75-
))
76-
workspace.write(workspace_content)
70+
setup_content = '\n'.join(setup_lines(local_path, sha256))
71+
setup.write(setup_content)
7772
if _VERBOSE:
78-
print('=== WORKSPACE ===')
79-
print(workspace_content)
73+
print(f'=== {filename} ===')
74+
print(setup_content)
8075

8176
# We do a little dance of renaming *.tpl to *, mostly so that we do not
8277
# have a BUILD file in testdata, which would create a package boundary.
@@ -91,15 +86,39 @@ def CopyTestFile(source_name, dest_name):
9186
CopyTestFile('BUILD.tpl', 'BUILD')
9287

9388
os.chdir(tempdir)
94-
build_result = subprocess.check_output(['bazel', 'build', '--enable_workspace', ':dummy_tar'])
89+
build_result = subprocess.check_output(['bazel', 'build'] + bazel_flags + [':dummy_tar'])
9590
if _VERBOSE:
9691
print('=== Build Result ===')
9792
print(build_result)
9893

99-
# TODO(aiuto): Find tar in a disciplined way
100-
content = subprocess.check_output(
101-
['tar', 'tzf', 'bazel-bin/dummy_tar.tar.gz'])
102-
self.assertEqual(b'etc/\netc/BUILD\n', content)
94+
with tarfile.open('bazel-bin/dummy_tar.tar.gz', 'r') as tar:
95+
self.assertEqual(['etc', 'etc/BUILD'], tar.getnames())
96+
97+
def _select_bazel_supported_setup(self):
98+
output = subprocess.check_output(['bazel', 'version'], text=True)
99+
major_version = re.search(r'Build label:\s+(\d+)', output)
100+
if major_version and int(major_version.group(1)) >= 9:
101+
return 'MODULE.bazel', self._module_bazel_lines, []
102+
return 'WORKSPACE', self._workspace_lines, ['--enable_workspace']
103+
104+
def _module_bazel_lines(self, local_path, sha256):
105+
return (
106+
'module(name = "test_rules_pkg_packaging")',
107+
f'bazel_dep(name = "{self.source_repo}", version = "{self.version}", repo_name = "{self.dest_repo}")',
108+
f'archive_override(module_name = "{self.source_repo}", sha256 = "{sha256}", url = "file://{local_path}")',
109+
)
110+
111+
def _workspace_lines(self, local_path, sha256):
112+
return (
113+
'workspace(name = "test_rules_pkg_packaging")',
114+
release_tools.workspace_content(
115+
url=f'file://{local_path}',
116+
repo=self.source_repo,
117+
sha256=sha256,
118+
rename_repo=self.dest_repo,
119+
deps_method='rules_pkg_dependencies',
120+
),
121+
)
103122

104123

105124
if __name__ == '__main__':

0 commit comments

Comments
 (0)