bazel-contrib / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.21k stars 381 forks source link

Gazelle diff mode patch generation creates invalid patches #1916

Closed alandonham closed 2 months ago

alandonham commented 2 months ago

What version of gazelle are you using?

1.22.5

What version of rules_go are you using?

0.46.0

What version of Bazel are you using?

7.2.1

Does this issue reproduce with the latest releases of all the above?

Yes.

What operating system and processor architecture are you using?

MAC OS ARM64

What did you do?

How to Reproduce: Edit a BUILD.bazel file that has already had Gazelle run on it (I edited one in this repository to reproduce) and add new lines to the end of the file. Next, run the command bazel run //:gazelle -- -mode=diff -patch=temp.patch and generate a patch file. Next, run the command git apply -p0 temp.patch.

What did you expect to see?

Patch is successfully applied. File modifications that added the newlines are reverted.

What did you see instead?

error: patch failed: BUILD.bazel:132 error: BUILD.bazel: patch does not apply

It's also worth noting that the system patch utility does apply the patches successfully, however it prints a warning regarding the validity of the patch. No such line 134 in input file, ignoring

*The lines numbers in the error messages will be subject to change based on the file you are editing.

alandonham commented 2 months ago

It appears that the diff library used does not correctly ignore trailing new lines, thus resulting in invalid patches.

The diff library used is no longer being maintained, so long term this should likely migrate to a different diff library that is actively maintained, however in the short term I think we could look at simply trimming the trailing new lines after the diff object is created.

The PR I have linked contains the required changes for the new line trimming.

I wanted to gather feedback from the maintainers on the desired approach and possible paths forward.