Skip to content

Commit eaa95a2

Browse files
committed
Distinguish patched versions
1 parent 2c343b0 commit eaa95a2

File tree

9 files changed

+81
-46
lines changed

9 files changed

+81
-46
lines changed

src/test/py/bazel/bzlmod/bazel_lockfile_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ def setUp(self):
4444
[
4545
# In ipv6 only network, this has to be enabled.
4646
# 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true',
47-
'build --experimental_isolated_extension_usages',
48-
'build --registry=' + self.main_registry.getURL(),
47+
'common --experimental_isolated_extension_usages',
48+
'common --registry=' + self.main_registry.getURL(),
4949
# We need to have BCR here to make sure built-in modules like
5050
# bazel_tools can work.
51-
'build --registry=https://bcr.bazel.build',
52-
'build --verbose_failures',
51+
'common --registry=https://bcr.bazel.build',
52+
'common --verbose_failures',
5353
# Set an explicit Java language version
54-
'build --java_language_version=8',
55-
'build --tool_java_language_version=8',
56-
'build --lockfile_mode=update',
54+
'common --java_language_version=8',
55+
'common --tool_java_language_version=8',
56+
'common --lockfile_mode=update',
5757
],
5858
)
5959
# TODO(pcloudy): investigate why this is needed, MODULE.bazel.lock is not

src/test/py/bazel/bzlmod/bazel_module_test.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from src.test.py.bazel import test_base
2727
from src.test.py.bazel.bzlmod.test_utils import BazelRegistry
2828
from src.test.py.bazel.bzlmod.test_utils import integrity
29+
from src.test.py.bazel.bzlmod.test_utils import queryTargetAttr
2930
from src.test.py.bazel.bzlmod.test_utils import read
3031
from src.test.py.bazel.bzlmod.test_utils import scratchFile
3132

@@ -114,13 +115,6 @@ def writeMainProjectFiles(self):
114115
executable=True,
115116
)
116117

117-
def getTargetAttr(self, target, attr_name):
118-
_, stdout, _ = self.RunBazel(['query', '--output=streamed_jsonproto', target])
119-
query_json = json.loads('\n'.join(stdout))
120-
attrs = [attr for attr in query_json["rule"]["attribute"] if attr["name"] == attr_name]
121-
self.assertEqual(len(attrs), 1)
122-
return attrs[0]
123-
124118
def testSimple(self):
125119
self.ScratchFile('MODULE.bazel', [
126120
'bazel_dep(name = "aaa", version = "1.0")',
@@ -150,10 +144,10 @@ def testSimple(self):
150144
self.assertIn('main function => aaa@1.0', stdout)
151145

152146
# Verify the default package metadata.
153-
package_metadata = self.getTargetAttr('@aaa//:lib_aaa', 'package_metadata')['stringListValue']
147+
package_metadata = queryTargetAttr(self, '@aaa//:lib_aaa', 'package_metadata')['stringListValue']
154148
self.assertEqual(len(package_metadata), 1)
155149
package_metadata_target = package_metadata[0]
156-
purl = self.getTargetAttr(package_metadata_target, 'purl')['stringValue']
150+
purl = queryTargetAttr(self, package_metadata_target, 'purl')['stringValue']
157151
self.assertEqual(purl, 'pkg:bazel/aaa@1.0')
158152

159153
def testSimpleTransitive(self):

src/test/py/bazel/bzlmod/bazel_overrides_test.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from absl.testing import absltest
2222
from src.test.py.bazel import test_base
2323
from src.test.py.bazel.bzlmod.test_utils import BazelRegistry
24+
from src.test.py.bazel.bzlmod.test_utils import queryTargetAttr
2425

2526

2627
class BazelOverridesTest(test_base.TestBase):
@@ -46,15 +47,15 @@ def setUp(self):
4647
[
4748
# In ipv6 only network, this has to be enabled.
4849
# 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true',
49-
'build --registry=' + self.main_registry.getURL(),
50+
'common --registry=' + self.main_registry.getURL(),
5051
# We need to have BCR here to make sure built-in modules like
5152
# bazel_tools can work.
52-
'build --registry=https://bcr.bazel.build',
53-
'build --verbose_failures',
53+
'common --registry=https://bcr.bazel.build',
54+
'common --verbose_failures',
5455
# Set an explicit Java language version
55-
'build --java_language_version=8',
56-
'build --tool_java_language_version=8',
57-
'build --lockfile_mode=update',
56+
'common --java_language_version=8',
57+
'common --tool_java_language_version=8',
58+
'common --lockfile_mode=update',
5859
],
5960
)
6061

@@ -125,6 +126,13 @@ def testSingleVersionOverrideWithPatch(self):
125126
self.assertIn('main function => bbb@1.1', stdout)
126127
self.assertIn('bbb@1.1 => aaa@1.0 (locally patched)', stdout)
127128

129+
# Verify the default package metadata.
130+
package_metadata = queryTargetAttr(self, '@aaa//:lib_aaa', 'package_metadata')['stringListValue']
131+
self.assertEqual(len(package_metadata), 1)
132+
package_metadata_target = package_metadata[0]
133+
purl = queryTargetAttr(self, package_metadata_target, 'purl')['stringValue']
134+
self.assertEqual(purl, 'pkg:bazel/aaa@1.0.patched.2e17a4fe')
135+
128136
def testSingleVersionOverrideWithPatchCmds(self):
129137
self.writeMainProjectFiles()
130138
self.ScratchFile(
@@ -153,6 +161,13 @@ def testSingleVersionOverrideWithPatchCmds(self):
153161
'bbb@1.1 => aaa@1.0 (locally patched with patch_cmds)', stdout
154162
)
155163

164+
# Verify the default package metadata.
165+
package_metadata = queryTargetAttr(self, '@aaa//:lib_aaa', 'package_metadata')['stringListValue']
166+
self.assertEqual(len(package_metadata), 1)
167+
package_metadata_target = package_metadata[0]
168+
purl = queryTargetAttr(self, package_metadata_target, 'purl')['stringValue']
169+
self.assertEqual(purl, 'pkg:bazel/aaa@1.0.patched.1a1372d')
170+
156171
def testSingleVersionOverrideWithPatchAndPatchCmds(self):
157172
self.writeMainProjectFiles()
158173
self.ScratchFile(
@@ -186,6 +201,14 @@ def testSingleVersionOverrideWithPatchAndPatchCmds(self):
186201
stdout,
187202
)
188203

204+
205+
# Verify the default package metadata.
206+
package_metadata = queryTargetAttr(self, '@aaa//:lib_aaa', 'package_metadata')['stringListValue']
207+
self.assertEqual(len(package_metadata), 1)
208+
package_metadata_target = package_metadata[0]
209+
purl = queryTargetAttr(self, package_metadata_target, 'purl')['stringValue']
210+
self.assertEqual(purl, 'pkg:bazel/aaa@1.0.patched.1efd6af1')
211+
189212
def testSingleVersionOverrideVersionTooLow(self):
190213
self.writeMainProjectFiles()
191214
self.ScratchFile(

src/test/py/bazel/bzlmod/bazel_repo_mapping_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ def setUp(self):
4343
[
4444
# In ipv6 only network, this has to be enabled.
4545
# 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true',
46-
'build --registry=' + self.main_registry.getURL(),
46+
'common --registry=' + self.main_registry.getURL(),
4747
# We need to have BCR here to make sure built-in modules like
4848
# bazel_tools can work.
49-
'build --registry=https://bcr.bazel.build',
50-
'build --verbose_failures',
49+
'common --registry=https://bcr.bazel.build',
50+
'common --verbose_failures',
5151
# Set an explicit Java language version
52-
'build --java_language_version=8',
53-
'build --tool_java_language_version=8',
54-
'build --lockfile_mode=update',
52+
'common --java_language_version=8',
53+
'common --tool_java_language_version=8',
54+
'common --lockfile_mode=update',
5555
( # fmt: skip pylint: disable=line-too-long
56-
'build'
56+
'common'
5757
' --extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain'
5858
),
5959
],

src/test/py/bazel/bzlmod/bazel_yanked_versions_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ def writeBazelrcFile(self, allow_yanked_versions=True):
6464
[
6565
# In ipv6 only network, this has to be enabled.
6666
# 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true',
67-
'build --registry=' + self.main_registry.getURL(),
67+
'common --registry=' + self.main_registry.getURL(),
6868
# We need to have BCR here to make sure built-in modules like
6969
# bazel_tools can work.
70-
'build --registry=https://bcr.bazel.build',
71-
'build --verbose_failures',
70+
'common --registry=https://bcr.bazel.build',
71+
'common --verbose_failures',
7272
# Set an explicit Java language version
73-
'build --java_language_version=8',
74-
'build --tool_java_language_version=8',
75-
'build --lockfile_mode=update',
73+
'common --java_language_version=8',
74+
'common --tool_java_language_version=8',
75+
'common --lockfile_mode=update',
7676
]
7777
+ (
7878
[
7979
# Disable yanked version check so we are not affected BCR
8080
# changes.
81-
'build --allow_yanked_versions=all',
81+
'common --allow_yanked_versions=all',
8282
]
8383
if allow_yanked_versions
8484
else []

src/test/py/bazel/bzlmod/mod_command_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ def setUp(self):
4343
[
4444
# In ipv6 only network, this has to be enabled.
4545
# 'startup --host_jvm_args=-Djava.net.preferIPv6Addresses=true',
46-
'mod --registry=' + self.main_registry.getURL(),
46+
'common --registry=' + self.main_registry.getURL(),
4747
# We need to have BCR here to make sure built-in modules like
4848
# bazel_tools can work.
49-
'mod --registry=https://bcr.bazel.build',
49+
'common --registry=https://bcr.bazel.build',
5050
# Disable yanked version check so we are not affected BCR changes.
51-
'mod --allow_yanked_versions=all',
51+
'common --allow_yanked_versions=all',
5252
# Make sure Bazel CI tests pass in all environments
53-
'mod --charset=ascii',
53+
'common --charset=ascii',
5454
],
5555
)
5656

src/test/py/bazel/bzlmod/repo_contents_cache_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def setUp(self):
3232
self.ScratchFile(
3333
'.bazelrc',
3434
[
35-
'build --verbose_failures',
35+
'common --verbose_failures',
3636
'common --repo_contents_cache=%s' % self.repo_contents_cache,
3737
],
3838
)

src/test/py/bazel/bzlmod/test_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ def scratchFile(path, lines=None):
5555
f.write(l)
5656
f.write('\n')
5757

58+
def queryTargetAttr(test_base, target, attr_name):
59+
"""Returns the attribute info returned by a query for a particular target and attribute."""
60+
_, stdout, _ = test_base.RunBazel(['query', '--output=streamed_jsonproto', target])
61+
test_base.assertEqual(len(stdout), 1)
62+
query_json = json.loads(stdout[0])
63+
attrs = [attr for attr in query_json["rule"]["attribute"] if attr["name"] == attr_name]
64+
test_base.assertEqual(len(attrs), 1)
65+
return attrs[0]
66+
5867

5968
class Module:
6069
"""A class to represent information of a Bazel module."""

tools/build_defs/repo/utils.bzl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,20 @@ def patch(ctx, patches = None, patch_cmds = None, patch_cmds_win = None, patch_t
280280
# Ensure that local patches (applied via single_version_override) result
281281
# in a different version number than unmodified registry modules.
282282
if patches or patch_cmds:
283-
hash = hash((patch_cmds, patch_args))
284-
for patch in patches:
285-
hash = ctx.read(ctx.path(patch))
283+
hashes = [hash(ctx.read(ctx.path(patch))) for patch in patches]
284+
hashes += [hash(cmd) for cmd in patch_cmds]
285+
hashes += [hash(arg) for arg in patch_args]
286+
combined_hash = abs(hash(" ".join([str(h) for h in hashes])))
287+
purl_fragments = dict(purl_fragments)
288+
# Additional segments can be appended to both the release and
289+
# prerelease part. Modules in Bazel registries never have build
290+
# metadata (the + part of semver) as Bazel drops this before
291+
# requesting a module version.
292+
purl_fragments["version"] += ".patched.%x" % combined_hash
286293
ctx.file(
287294
".bazel_package_metadata/BUILD.bazel",
288-
"""# Generated by Bazel. Add a custom REPO.bazel file to disable generation.
295+
"""# Generated by Bazel.
296+
# Add a custom REPO.bazel file to disable generation.
289297
290298
package(default_visibility = ["//:__subpackages__"])
291299
@@ -302,7 +310,8 @@ package_metadata(
302310
)
303311
ctx.file(
304312
"REPO.bazel",
305-
"""# Generated by Bazel. Add a custom REPO.bazel file to disable generation.
313+
"""# Generated by Bazel.
314+
# Add a custom REPO.bazel file to disable generation.
306315
307316
repo(default_package_metadata = ["//.bazel_package_metadata:package_metadata"])
308317
""",

0 commit comments

Comments
 (0)