Skip to content

Commit a31f548

Browse files
authored
[8.5.0] Restore descriptive error on invalid patch labels (bazelbuild#27415)
This extra validation got lost in 3d60f1c. Closes bazelbuild#27260. PiperOrigin-RevId: 819594128 Change-Id: I09ecc6f4162a71c8833399f6ab2065e3846fd191 (cherry picked from commit 8c59d8f) Closes bazelbuild#27261
1 parent ef394f4 commit a31f548

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import net.starlark.java.eval.Sequence;
4848
import net.starlark.java.eval.Starlark;
4949
import net.starlark.java.eval.StarlarkInt;
50+
import net.starlark.java.eval.StarlarkList;
5051
import net.starlark.java.eval.StarlarkThread;
5152
import net.starlark.java.eval.StarlarkValue;
5253
import net.starlark.java.eval.Structure;
@@ -1089,6 +1090,12 @@ public void archiveOverride(String moduleName, Dict<String, Object> kwargs, Star
10891090
ModuleThreadContext context = ModuleThreadContext.fromOrFail(thread, "archive_override()");
10901091
context.setNonModuleCalled();
10911092
validateModuleName(moduleName);
1093+
var patches =
1094+
Sequence.cast(
1095+
kwargs.getOrDefault("patches", StarlarkList.empty()), String.class, "patches");
1096+
for (String patch : patches) {
1097+
var unused = convertAndValidatePatchLabel(context.getModuleBuilder(), patch);
1098+
}
10921099
context.addOverride(
10931100
moduleName,
10941101
new NonRegistryOverride(
@@ -1128,6 +1135,12 @@ public void gitOverride(String moduleName, Dict<String, Object> kwargs, Starlark
11281135
ModuleThreadContext context = ModuleThreadContext.fromOrFail(thread, "git_override()");
11291136
context.setNonModuleCalled();
11301137
validateModuleName(moduleName);
1138+
var patches =
1139+
Sequence.cast(
1140+
kwargs.getOrDefault("patches", StarlarkList.empty()), String.class, "patches");
1141+
for (String patch : patches) {
1142+
var unused = convertAndValidatePatchLabel(context.getModuleBuilder(), patch);
1143+
}
11311144
context.addOverride(
11321145
moduleName,
11331146
new NonRegistryOverride(

src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,7 @@ public void testNoUsages() throws Exception {
17151715
}
17161716

17171717
@Test
1718-
public void testInvalidRepoInPatches() throws Exception {
1718+
public void testInvalidRepoInSingleVersionOverridePatches() throws Exception {
17191719
scratch.overwriteFile(
17201720
rootDirectory.getRelative("MODULE.bazel").getPathString(),
17211721
"module(name='aaa')",
@@ -1736,6 +1736,51 @@ public void testInvalidRepoInPatches() throws Exception {
17361736
+ " repository can be applied, not from '@unknown_repo'");
17371737
}
17381738

1739+
@Test
1740+
public void testInvalidRepoInArchiveOverridePatches() throws Exception {
1741+
scratch.overwriteFile(
1742+
rootDirectory.getRelative("MODULE.bazel").getPathString(),
1743+
"module(name='aaa')",
1744+
"archive_override(",
1745+
" module_name = 'bbb',",
1746+
" urls = ['https://example.com'],",
1747+
" patches = ['@unknown_repo//:patch.bzl'],",
1748+
")");
1749+
1750+
reporter.removeHandler(failFastHandler);
1751+
EvaluationResult<RootModuleFileValue> result =
1752+
evaluator.evaluate(
1753+
ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext);
1754+
assertThat(result.hasError()).isTrue();
1755+
1756+
assertContainsEvent(
1757+
"Error in archive_override: invalid label in 'patches': only patches in the main"
1758+
+ " repository can be applied, not from '@unknown_repo'");
1759+
}
1760+
1761+
@Test
1762+
public void testInvalidRepoInGitOverridePatches() throws Exception {
1763+
scratch.overwriteFile(
1764+
rootDirectory.getRelative("MODULE.bazel").getPathString(),
1765+
"module(name='aaa')",
1766+
"git_override(",
1767+
" module_name = 'bbb',",
1768+
" remote = 'https://example.com',",
1769+
" commit = 'abcd1234',",
1770+
" patches = ['@unknown_repo//:patch.bzl'],",
1771+
")");
1772+
1773+
reporter.removeHandler(failFastHandler);
1774+
EvaluationResult<RootModuleFileValue> result =
1775+
evaluator.evaluate(
1776+
ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext);
1777+
assertThat(result.hasError()).isTrue();
1778+
1779+
assertContainsEvent(
1780+
"Error in git_override: invalid label in 'patches': only patches in the main"
1781+
+ " repository can be applied, not from '@unknown_repo'");
1782+
}
1783+
17391784
@Test
17401785
public void testInvalidUseExtensionLabel() throws Exception {
17411786
scratch.overwriteFile(

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ def testArchiveOverrideWithBadLabelPatch(self):
286286
)
287287
exit_code, _, stderr = self.RunBazel(['run', '//:main'], allow_failure=True)
288288
self.AssertNotExitCode(exit_code, 0, stderr)
289-
self.assertIn("@@[unknown repo 'bbb' requested from @@]", '\n'.join(stderr))
289+
self.assertIn(
290+
"Error in archive_override: invalid label in 'patches': only patches in"
291+
" the main repository can be applied, not from '@bbb'",
292+
'\n'.join(stderr),
293+
)
290294

291295
def testRepoNameForBazelDep(self):
292296
self.writeMainProjectFiles()

0 commit comments

Comments
 (0)