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

Fix patch file creation using Gazelle in diff mode #1915

Closed alandonham closed 2 months ago

alandonham commented 2 months ago

What type of PR is this? Bug fix

What package or component does this PR mostly affect? cmd/gazelle

What does this PR do? Why is it needed? This PR adds a workaround to strip trailing newlines from the resulting diff when running Gazelle in mode=diff. This aligns with the expected behavior of modern diff tools, which will ignore the last newline in a file. This is implemented to resolve an issue where modifying the end of a file caused Gazelle to output a diff that was invalid, due to the newlines. Applying patches generated in this case could not be applied when using git apply and would apply, but with a warning identifying this issues when using the system patch utility.

Which issues(s) does this PR fix?

Fixes #1916

Other notes for review How to Reproduce: Edit a BUILD.bazel file in this repository, and then run bazel run //:gazelle -- -mode=diff -patch=temp.patch. Next, run git apply -p0 temp.patch. Without these changes, patch application will fail, due to the issue described above.

google-cla[bot] commented 2 months ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.