diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/PatchUtil.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/PatchUtil.java index d756ea1dc95a8b..6793078dd7a44b 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/PatchUtil.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/PatchUtil.java @@ -447,6 +447,8 @@ private static void applyInternal( throw new PatchFailedException("Cannot find patch file: " + patchFile.getPathString()); } + String singleFileStr = + singleFile != null ? singleFile.relativeTo(outputDirectory).getPathString() : null; boolean isGitDiff = false; boolean hasRenameFrom = false; boolean hasRenameTo = false; @@ -594,6 +596,16 @@ private static void applyInternal( oldFile, newFile, oldFileStr, newFileStr, patchStartLocation); } } + if (singleFileStr != null + && strip == 0 + && ("a/" + singleFileStr).equals(oldFileStr) + && ("b/" + singleFileStr).equals(newFileStr)) { + throw new PatchFailedException( + String.format( + "error at line %d: the patch file contains a/b prefixes, did you forget to" + + " set patch_strip = 1?", + patchStartLocation)); + } if (singleFile == null || (singleFile.equals(newFile) && singleFile.equals(oldFile))) { Patch patch = UnifiedDiffUtils.parseUnifiedDiff(patchContent); diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java index a9862288d75467..88962dbb819a84 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java @@ -1875,6 +1875,47 @@ public void testSingleVersionOverridePatches() throws Exception { "ccc", InterimModule.DepSpec.fromModuleKey(new ModuleKey("ccc", Version.parse("2.0")))); } + @Test + public void testSingleVersionOverridePatches_defaultPatchStripForGitPatch() throws Exception { + FakeRegistry registry = registryFactory.newFakeRegistry("/foo"); + ModuleFileFunction.REGISTRIES.set(differencer, ImmutableSet.of(registry.getUrl())); + ModuleKey bbb = createModuleKey("bbb", "1.0"); + registry.addModule(bbb, "module(name='bbb',version='1.0')"); + + scratch.file("BUILD"); + scratch.file( + "patch.diff", + """ + diff --git a/MODULE.bazel b/MODULE.bazel + --- a/MODULE.bazel + +++ b/MODULE.bazel + @@ -1,1 +1,1 @@ + -module(name='bbb',version='1.0') + +module(name='bbb',version='1.0',bazel_compatibility=[">=7.0.0"]) + """); + scratch.overwriteFile( + rootDirectory.getRelative("MODULE.bazel").getPathString(), + """ + single_version_override( + module_name="bbb", + patches = [ + "//:patch.diff", + ], + ) + """); + + var moduleFileKey = ModuleFileValue.key(bbb); + EvaluationResult result = + evaluator.evaluate(ImmutableList.of(moduleFileKey), evaluationContext); + assertThat(result.hasError()).isTrue(); + assertThat(result.getError().getException()) + .hasMessageThat() + .isEqualTo( + "error applying single_version_override patch /workspace/patch.diff to module file:" + + " error at line 2: the patch file contains a/b prefixes, did you forget to set" + + " patch_strip = 1?"); + } + @Test public void testSingleVersionOverridePatches_failsOnRename() throws Exception { FakeRegistry registry = registryFactory.newFakeRegistry("/foo");