Skip to content

Conversation

NMinhNguyen
Copy link
Contributor

@NMinhNguyen NMinhNguyen commented Oct 20, 2018

I noticed that if a patch file was checked out on Windows and it contained CRLF linebreaks, then patch-package would fail on file creation because this condition would be false due to startPath containing a trailing \r (carriage return) character: https://github.com/ds300/patch-package/blob/54a168b/src/patch/parse.ts#L297

After some investigation, it turned out that this was happening because https://github.com/ds300/patch-package/blob/54a168b/src/patch/parse.ts#L387 was splitting patch file contents on \n and so every line contained a trailing \r

Interestingly, I couldn't get yarn test --runInBand to pass locally even on a fresh checkout (without my changes) 🙁 Thanks to CI, I've now managed to ensure all tests pass.

@NMinhNguyen NMinhNguyen changed the title Handle line breaks such as CRLF in the patch parser Handle CRLF line breaks in the patch parser Oct 20, 2018
This fixes an issue where file creation wasn't occurring
on Windows due to `startPath !== "/dev/null"`
because `startPath` had a trailing `\r`.
it("is OK when blank lines are accidentally created", () => {
expect(parsePatch(accidentalBlankLine)).toEqual(parsePatch(patch))
})
it(`can handle files with CRLF line breaks`, () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, I could've written a more focused test that does

expect(parsePatch(crlfLineBreaks)[0].type).toBe("file creation")

Let me know if you'd prefer that instead of a snapshot test


private parseFileModification() {
const startPath = this.currentLine.slice("--- ".length)
const startPath = this.currentLine.trim().slice("--- ".length)
Copy link
Contributor Author

@NMinhNguyen NMinhNguyen Oct 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there's any implications from using .trim() as opposed to trimming from the end e.g.

const trimEnd = str => str.replace(/\s+$/, '')

@ds300
Copy link
Owner

ds300 commented Oct 24, 2018

This looks great, thanks! Sorry it took me a while to get around to it.

@ds300 ds300 merged commit aea8f4d into ds300:master Oct 24, 2018
@ds300
Copy link
Owner

ds300 commented Oct 24, 2018

published in patch-package@6.0.0-11 Thanks again! 🎉

@NMinhNguyen
Copy link
Contributor Author

No problem, thank you for reviewing and releasing! 🙂

@NMinhNguyen NMinhNguyen deleted the line-breaks branch October 25, 2018 01:46
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

Successfully merging this pull request may close these issues.

2 participants