Skip to content

Commit 85c452c

Browse files
bazel-iofmeum
andauthored
[9.0.1] Error on single_version_override patch with wrong patch_strip (#28566) (#28622)
Since diffs that don't apply to the MODULE.bazel file are silently skipped (see c413192), this is needed to make users aware that their patch doesn't actually apply to the file. Fixes #28560 Closes #28566. PiperOrigin-RevId: 868458465 Change-Id: I78d4027c5e8e01c2f5297e679c271bd4dac7b724 Commit 55312cc Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
1 parent 51c8fc7 commit 85c452c

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/PatchUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ private static void applyInternal(
447447
throw new PatchFailedException("Cannot find patch file: " + patchFile.getPathString());
448448
}
449449

450+
String singleFileStr =
451+
singleFile != null ? singleFile.relativeTo(outputDirectory).getPathString() : null;
450452
boolean isGitDiff = false;
451453
boolean hasRenameFrom = false;
452454
boolean hasRenameTo = false;
@@ -594,6 +596,16 @@ private static void applyInternal(
594596
oldFile, newFile, oldFileStr, newFileStr, patchStartLocation);
595597
}
596598
}
599+
if (singleFileStr != null
600+
&& strip == 0
601+
&& ("a/" + singleFileStr).equals(oldFileStr)
602+
&& ("b/" + singleFileStr).equals(newFileStr)) {
603+
throw new PatchFailedException(
604+
String.format(
605+
"error at line %d: the patch file contains a/b prefixes, did you forget to"
606+
+ " set patch_strip = 1?",
607+
patchStartLocation));
608+
}
597609

598610
if (singleFile == null || (singleFile.equals(newFile) && singleFile.equals(oldFile))) {
599611
Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patchContent);

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,47 @@ public void testSingleVersionOverridePatches() throws Exception {
18751875
"ccc", InterimModule.DepSpec.fromModuleKey(new ModuleKey("ccc", Version.parse("2.0"))));
18761876
}
18771877

1878+
@Test
1879+
public void testSingleVersionOverridePatches_defaultPatchStripForGitPatch() throws Exception {
1880+
FakeRegistry registry = registryFactory.newFakeRegistry("/foo");
1881+
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableSet.of(registry.getUrl()));
1882+
ModuleKey bbb = createModuleKey("bbb", "1.0");
1883+
registry.addModule(bbb, "module(name='bbb',version='1.0')");
1884+
1885+
scratch.file("BUILD");
1886+
scratch.file(
1887+
"patch.diff",
1888+
"""
1889+
diff --git a/MODULE.bazel b/MODULE.bazel
1890+
--- a/MODULE.bazel
1891+
+++ b/MODULE.bazel
1892+
@@ -1,1 +1,1 @@
1893+
-module(name='bbb',version='1.0')
1894+
+module(name='bbb',version='1.0',bazel_compatibility=[">=7.0.0"])
1895+
""");
1896+
scratch.overwriteFile(
1897+
rootDirectory.getRelative("MODULE.bazel").getPathString(),
1898+
"""
1899+
single_version_override(
1900+
module_name="bbb",
1901+
patches = [
1902+
"//:patch.diff",
1903+
],
1904+
)
1905+
""");
1906+
1907+
var moduleFileKey = ModuleFileValue.key(bbb);
1908+
EvaluationResult<ModuleFileValue> result =
1909+
evaluator.evaluate(ImmutableList.of(moduleFileKey), evaluationContext);
1910+
assertThat(result.hasError()).isTrue();
1911+
assertThat(result.getError().getException())
1912+
.hasMessageThat()
1913+
.isEqualTo(
1914+
"error applying single_version_override patch /workspace/patch.diff to module file:"
1915+
+ " error at line 2: the patch file contains a/b prefixes, did you forget to set"
1916+
+ " patch_strip = 1?");
1917+
}
1918+
18781919
@Test
18791920
public void testSingleVersionOverridePatches_failsOnRename() throws Exception {
18801921
FakeRegistry registry = registryFactory.newFakeRegistry("/foo");

0 commit comments

Comments
 (0)