|
| 1 | +# npm diff ignore CR at EOL |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +An option to ignore Carriage Return (CR) at the end of lines when performing npm diff. |
| 6 | + |
| 7 | +## Motivation |
| 8 | + |
| 9 | +To improve `npm diff` on packages developed on different platforms. Currently |
| 10 | +there are scenarios where every line is shown as changed if you are comparing |
| 11 | +to a package that was developed on a different platform than yours. |
| 12 | + |
| 13 | +## Detailed Explanation |
| 14 | + |
| 15 | +If you build a compiled package, e.g. with typescript `tsc` on windows, the |
| 16 | +output is produced with CRLF (Carriage Return, Line Feed) line endings. |
| 17 | +If you then build the same application on UNIX, the output is produced with |
| 18 | +LF line endings, as expected. |
| 19 | + |
| 20 | +The problem arises when you want to see if your newly produced package has |
| 21 | +any changes compared to another package, if that package was produced |
| 22 | +on another platform. |
| 23 | + |
| 24 | +If the package you are comparing to is built on Windows, while you are |
| 25 | +working in UNIX, every produced file will be different because of the different |
| 26 | +file endings. |
| 27 | + |
| 28 | +This problem is avoided in some cases when the files you are comparing are |
| 29 | +tracked by git, depending on your git `autocrlf` configuration. The |
| 30 | +scenarios where I've noticed this is when the files we are comparing are built |
| 31 | +locally and `.gitignore`d. |
| 32 | + |
| 33 | +I'm suggesting the flag `--diff-ignore-cr-at-eol`. This is quite a mouthful, |
| 34 | +but would be consistent with |
| 35 | +[`git diff --ignore-cr-at-eol`](https://github.com/git/git/commit/e9282f02b2f21118f3383608718e38efc3d967e1) |
| 36 | +introduced in |
| 37 | +[`git v2.16`](https://git-scm.com/docs/git-diff/2.16.6). |
| 38 | + |
| 39 | +## Rationale and Alternatives |
| 40 | + |
| 41 | +* Packages have their lineendings normalized when downloaded/installed from |
| 42 | + the registry. |
| 43 | + |
| 44 | + Similiar to `git config core.autocrlf`, npm could also normalize line |
| 45 | + endings when installing on a Windows machine. By doing this, |
| 46 | + `npm diff` would always be comparing files that are normalized for windows. |
| 47 | + |
| 48 | + However this would be a big change with plenty of potential pitfall. It'd |
| 49 | + also become problematic if a Windows user had configured git to keep files |
| 50 | + with LF line endings and tried to compare to a package that npm would then |
| 51 | + normalize to CRLF, again causing diffs... |
| 52 | + |
| 53 | +## Implementation |
| 54 | + |
| 55 | +A change to `libnpmdiff` to add a new option `diffIgnoreCRAtEOL` similiarly |
| 56 | +to the |
| 57 | +[`diffIgnoreAllSpace` option](https://github.com/npm/cli/blob/892b66eba9f21dbfbc250572d437141e39a6de24/workspaces/libnpmdiff/lib/format-diff.js#L76). |
| 58 | +However, as far as I can tell, |
| 59 | +[npmjs.com/diff](https://npmjs.com/diff)/[kpdecker/jsdiff](https://github.com/kpdecker/jsdiff/) |
| 60 | +does not have any option to do something like this. One option is to submit a |
| 61 | +PR to add this. |
| 62 | + |
| 63 | +Add the `--diff-ignore-cr-at-eol` configuration support to the npm cli and |
| 64 | +make sure `./lib/commands/diff.js` handles it as expected to `libnpmdiff`. |
| 65 | + |
| 66 | +## Prior Art |
| 67 | + |
| 68 | +* As mentioned previously |
| 69 | + [`git diff --ignore-cr-at-eol`](https://github.com/git/git/commit/e9282f02b2f21118f3383608718e38efc3d967e1) |
| 70 | + |
| 71 | +* Gnu diffutils |
| 72 | + [`diff --strip-trailing-cr`](https://www.gnu.org/software/diffutils/manual/html_node/diff-Options.html#diff-Options) |
0 commit comments