Skip to content

createTwoFilesPatch and newlines - is this working as expected? #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aliak00 opened this issue Mar 31, 2025 · 5 comments
Closed

createTwoFilesPatch and newlines - is this working as expected? #598

aliak00 opened this issue Mar 31, 2025 · 5 comments

Comments

@aliak00
Copy link

aliak00 commented Mar 31, 2025

Not sure if bug or using the api wrong, but trying to get the diff from these two:

Old text

One line
Two line
Other lines

New text with new line

One line
One.Five line
Two line
Other lines

Where new text has a new line at the end of the file, I get this patch diff:

Index: /Users/aliak/dev/quillia/apps/backend/test-app-data.local/README.md
===================================================================
--- /Users/aliak/dev/quillia/apps/backend/test-app-data.local/README.md original
+++ /Users/aliak/dev/quillia/apps/backend/test-app-data.local/README.md modified
@@ -1,3 +1,4 @@
 One line
+One.Five line
 Two line
\ No newline at end of file
-Other lines
+Other lines

The \ No newline at the end of file. Is that in the right place? How do I tell that a new line was added. If I do it the other way around - i.e. new line is in old text, and I remove it in new text then I get

--- /Users/aliak/dev/quillia/apps/backend/test-app-data.local/README.md original
+++ /Users/aliak/dev/quillia/apps/backend/test-app-data.local/README.md modified
@@ -1,3 +1,4 @@
 One line
+One.Five line
 Two line
-Other lines
+Other lines
\ No newline at end of file

Was \ No newline at end of file added or removed?

@ExplodingCabbage
Copy link
Collaborator

Hmmm... this looks wrong to me. In the first case (new version has a trailing newline, old version doesn't), GNU diff -u returns this diff:

--- old.txt	2025-04-28 10:39:39.716869201 +0100
+++ new.txt	2025-04-28 10:42:06.787201215 +0100
@@ -1,3 +1,4 @@
 One line
+One.Five line
 Two line
-Other lines
\ No newline at end of file
+Other lines

I would think we ought to be outputting the same patch and the output you exhibit is surely a bug, quite possibly introduced by me since I took over maintaining jsdiff. Yuck. Will investigate.

@ExplodingCabbage
Copy link
Collaborator

Oh - but I can't reproduce. This is what I see using the version of jsdiff on master, which agrees with GNU diff:

> old
'One line\nTwo line\nOther lines'
> noo
'One line\nOne.Five line\nTwo line\nOther lines\n'
> diff.createTwoFilesPatch('old.txt', 'new.txt', old, noo)
'===================================================================\n' +
  '--- old.txt\n' +
  '+++ new.txt\n' +
  '@@ -1,3 +1,4 @@\n' +
  ' One line\n' +
  '+One.Five line\n' +
  ' Two line\n' +
  '-Other lines\n' +
  '\\ No newline at end of file\n' +
  '+Other lines\n'

What version were you using that produced the output you quote in the issue?

@ExplodingCabbage
Copy link
Collaborator

Aha! I can reproduce the output quoted in the issue if I use [email protected]:

> diff.createTwoFilesPatch('old.txt', 'new.txt', old, noo)
'===================================================================\n' +
  '--- old.txt\n' +
  '+++ new.txt\n' +
  '@@ -1,3 +1,4 @@\n' +
  ' One line\n' +
  '+One.Five line\n' +
  ' Two line\n' +
  '\\ No newline at end of file\n' +
  '-Other lines\n' +
  '+Other lines\n'

But not with [email protected]:

> old = 'One line\nTwo line\nOther lines'
'One line\nTwo line\nOther lines'
> noo = 'One line\nOne.Five line\nTwo line\nOther lines\n'
'One line\nOne.Five line\nTwo line\nOther lines\n'
> diff.createTwoFilesPatch('old.txt', 'new.txt', old, noo)
'===================================================================\n' +
  '--- old.txt\n' +
  '+++ new.txt\n' +
  '@@ -1,3 +1,4 @@\n' +
  ' One line\n' +
  '+One.Five line\n' +
  ' Two line\n' +
  '-Other lines\n' +
  '\\ No newline at end of file\n' +
  '+Other lines\n'

@ExplodingCabbage
Copy link
Collaborator

Seems like I am the hero of this story and not the villain as I first assumed! I believe the issue you're hitting is the one I describe fixing in this entry from the release notes for 6.0.0:

  • #535 A bug in patch generation functions is now fixed that would sometimes previously cause \ No newline at end of file to appear in the wrong place in the generated patch, resulting in the patch being invalid. These invalid patches can also no longer be applied successfully with applyPatch. (It was already the case that tools other than jsdiff, like GNU patch, would consider them malformed and refuse to apply them; versions of jsdiff with this fix now do the same thing if you ask them to apply a malformed patch emitted by jsdiff v5.)

6.0.0 introduced a new (unrelated) bug and shouldn't be used, but upgrading to 7.0.0 (which is just 6.0.0 without the new bug) should fix everything for you.

@aliak00
Copy link
Author

aliak00 commented Apr 28, 2025

oooh! nice! I will give it a try as soon as I can! but sounds like it should be fine - so ill just ping here again if there is a problem still!

Else, thank you very much :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants