2

Hi I've a problem commiting a file in VSCode (1.30.1) using git source control (2.19.1.windows.1). The commit changes a file, I did not change and want not to be changed. The setup is the following: I am on Windows 10, having git configured with "checkout windows style, commit unix style" git config --global core.autocrlf true. But when I commit the following happens.

It is a .json file with the following key:

{
  "description":
    "xxxxxxx"
},

but my commit turns this line into

{
  "description": "xxxxxxx"
},

Tested core.autocrlf input and false. But that does not fix that issue. Didn't face that behaviour before. Any other suggestions or ideas? Thanks and cheers.


Update: Another example

Creating a test.json file containing

{
  "test1": {},
  "test2": {}
}

works perfectly, but adding a new line like

{
  "test1": {
  },
  "test2": {}
}

will look like the first one after commiting. It seems like a styling issue in some way, not depending on editor or system, because other repos are working as expected.

Felix Lemke
  • 6,189
  • 3
  • 40
  • 67
  • Where did you see the second version of your JSON? Did you view it on the repo (e.g. on GitHub or Bitbucket) _after_ you committed it? – Tim Biegeleisen Dec 29 '18 at 15:45
  • Both locally and on github – Felix Lemke Dec 29 '18 at 15:47
  • Can you post your git configuration (or at least whatever isn't sensitive) with `git config -l`? – Silas Coker Dec 29 '18 at 15:53
  • What linebreak style is your editor set to? On Windows you have `\r\n`, on UNIX just `\n`. Perhaps the line break vanishes because your editor just sets the `\n` and git expects the `\r` as well? – Martin Ueding Dec 29 '18 at 16:06
  • How can I get that info? As I said its windows 10, latest VSCode and "checkout windows, commit unix"-style, but tested the other styles as well. – Felix Lemke Dec 29 '18 at 16:10
  • what is the difference of the files when openning them in a hex-editor? (I doubt that line break handling is the issue since the braces are sill on their own lines.) – Timothy Truckle Dec 29 '18 at 16:25
  • Not allowed to install additional editors on my machine besides VSCode and VS. – Felix Lemke Dec 29 '18 at 16:30
  • @TimothyTruckle In an online hex editor the original file contains `0D 0A 20 20 ...`. When I create a new line manually on my machine its only `0A 20 20 ...`, but both commits are removing the line-breaks, ending in a single `20` between `"description":` and `"xx..."`. – Felix Lemke Dec 29 '18 at 16:56
  • what about the linebreaks before and after the braces? – Timothy Truckle Dec 29 '18 at 17:03
  • If I add a `"test": {}` the line-break will be removed after the commit. – Felix Lemke Dec 29 '18 at 17:08
  • 1
    Could it be a VSC configuration issue, what line ending is VSC detecting/using for this file? (see https://stackoverflow.com/questions/39525417/visual-studio-code-how-to-show-line-endings) – joran Dec 29 '18 at 17:10
  • It was `CRLF`. But using `LF` adding a new line, the line-break will be removed. Is git aware of `$schema`s? It is contains a `$schema` property containing "http://json-schema.org/schema". – Felix Lemke Dec 29 '18 at 17:15
  • Removing it doesn't work either. But it behaves like a formatting thing. E.g. `"test": {},"test2": {}` works perfectly. – Felix Lemke Dec 29 '18 at 17:19
  • 3
    I'd bet your editor imposes auto formatting – erik258 Dec 29 '18 at 17:55
  • 1
    Forget about your git configuration for a minute, what does your VS Code configuration look like? – Edward Thomson Dec 29 '18 at 18:04

2 Answers2

0

It should work with git config --global core.autocrlf false

Or you can try with

.gitattributes file. You can use it as a template for your repositories:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto
IMParasharG
  • 1,869
  • 1
  • 15
  • 26
0

The problem was related to the lint-staged library in combination with a precommit hook in package.json

{
  "scripts": {
    "precommit": "lint-staged"
  }
}

It formats the code due to given linting rules before commiting. If you like to get more details, have a look at the @ngrx/platform library which is using it.

Felix Lemke
  • 6,189
  • 3
  • 40
  • 67