Inconsistent line endigs
mradenovic opened this issue · 9 comments
When a new project is created with npx create-react-app my-app --template redux
, almost all of the files have CRLF
line endings. package.json
and yarn.lock
have LF
line endings.
This is also the opposite of the default create-react-app
template where all files have LF
endings.
Hmm. This may be because I'm publishing from a Windows machine, and Git is auto-checking out files with native line endings.
Can you file a PR to force Git to always treat the template files as LF endings?
I found this instructions, but I will have to figure out how to do this.
When I clone this repo everything is fine. When I run npx create-react-app my-app --template redux
, I get a lot of CRLF
.
I guess when git is checking files, it depends on core.autocrlf
, but cra-template
is just copied and doesn't get the same benifit.
It might be necessary to both push correct files to repo and set .gitattributes
.
Yeah, I expect that the line endings on disk at the time of publish (ie, on my computer) are what end up in the published NPM package, so we need to convince Git to always check them out as LF.
I found this in git book:
Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the core.autocrlf setting. If you’re on a Windows machine, set it to true — this converts LF endings into CRLF when you check out code:
$ git config --global core.autocrlf true
That's for converting files to and from the local file system. The problem is that setting gives all checked-out files on a Windows machine CRLF endings. What we want is to force some set of files to always have LF endings, no matter what machine they get checked out on.
I believe we can achieve this via .gitattributes
, either by forcing a specific autocrlf
setting on the whole repository or just the template
directory. https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings#per-repository-settings
Just noticed that Prettier 2.0 includes this (breaking) config change: Change default value for endOfLine
to lf
Could this fix the issue? I'd be happy to start a PR if you want to test a publish on Windows.
The best thing to do to get control over the LF/CRLF situation is to ensure git is not doing any magic for you upon commit and checkout, so what you have on your working dir on your computer (no matter what OS) is the same that is in the repo and coworkers and/or CI servers working directory.
This is done by putting the following in .gitattributes
:
* -text
It basically says: For all files [the asterisk], don't [the minus] treat it as text file. It's when git is treating files as text it can do magic line ending tricks for you. You don't want that.
In addition to this, now that the files (in the repo) is stored with CRLF, you need to do one commit (after the .gitattributes
is commited) that converts them all to LF.
@markerikson wrote
What we want is to force some set of files to always have LF endings, no matter what machine they get checked out on.
You can achieve EOL control by the above feature. You can apply it to a subset of the files if you want.