#Stop code auto-format on save (solved)

1 messages · Page 1 of 1 (latest)

opal plinth
#

I'm using VSCode and I'm working on an external project - when I make the smallest change, the whole file gets modified in subtle ways, which makes a tonne of changes to the git stage that I have to revert - not only is this time consuming, but I run the risk of accidentally reverting my real changes.

I've spent 1.5 hours on this doing web searches, convos with ChatGPT, I've tried everything I've been given, and VSCode still insists on reformatting the file. I've made settings changes to my user settings.json, and my ./vscode/settings.json in the project folder, I've made setting changes related to OmniSharp, Razor, Roslyn... settings for formatting, whitespace changes, tabs/spaces formatting, newline normalising... nothing works. My files keep getting reformatted on saving in VSCode.

marble viper
opal plinth
#

I forgot to mention - I tried that too...

#

I think the formatting happens when the file is open, which is why that wouldn't make a difference

smoky compass
#

VS Code by default doesn't format on save, and it doesn't even know how to format files, only a formatter knows how (which when you do format, it calls the formatter of your choice).

#

Check:

  • If you have format on save enabled in your VS Code.
  • If you have another extension that tries to format your file.
opal plinth
# smoky compass VS Code by default doesn't format on save, and it doesn't even know how to forma...

Correct - every setting I was told to change, was already set to false...

As for the extensions - I just have the basic Unity workflow, which seems to entail extensions C# and C# Dev Kit, I scrolled through their custom settings, all of them, to see if there was a relevant setting, I couldn't find one, none that were enabled. I tried turning off various ones, I can't remember exactly.

For example, here's the settings for the C# extension - the Omnisharp one was On, which should be what I want, but I tried disabling it (which is why it's currently off) I'm desperately trying anything

smoky compass
#

Are you changing the settings globally, or in the workspace? The also modified in workspace seems to suggest that you are changing these options globally, but your workspace value might be different.

opal plinth
#

I've been doing both, I was initially restricting it to workspace, which I believe is the ./vscode/settings.json, but when nothing was working, I expanded out to user settings or default settings

#

Relevant area I modified (mostly from ChatGPT's ideas)

"csharp.format.enable": false,
"files.trimTrailingWhitespaceInRegexAndStrings": false,
"razor.format.enable": false,
"omnisharp.useEditorFormattingSettings": false,
"editor.formatOnSave": false,
"[csharp]": {
    "editor.formatOnSave": false
},
"files.trimTrailingWhitespace": false,
"editor.detectIndentation": false,
"dotnet.defaultSolution": "unity-ready-attention-go.sln",

// settings.json
"files.eol": "\r\n",                // or "\r\n" if you want Windows-style
"files.autoGuessEncoding": false  // don't let VS Code override based on content
#

false, false, false, false, false... nothing makes a difference

smoky compass
#

My best guess is that you have some extension that is formatting your file.

opal plinth
#

It's crazy that a program (especially VSCode which was supposed to be a simpler alternative to Visual Studio) can have so many "falses" thrown at it based off of instructions that should work, and still do the thing it's not supposed to do

smoky compass
#

Find out which one is doing it, you can do an extension bisect.

#

I use VS Code, never had this issue.

opal plinth
#

This is my full extension list;

#

And I added "Bookmarks" myself one time.

smoky compass
#

Well just do an extension bisect, with 6 extensions it should be quick to find out which one is causing the issue.

opal plinth
#

Ok. If I disable C#, it chain reactions and says all the others are dependant on it, so I'll work the other way starting with Unity

smoky compass
#

By extension bisect I mean that's a command.

#

But you can also trial and error one by one I guess, since you only have 6 anyways.

opal plinth
#

Don't know if you can tell, but the extensions are "dull" which means they're disabled

#

Would've thought there'd be an easier way to tell, like "disabled" next to the name...

smoky compass
#

That looks like either mixing CR/LF, or spaces/tabs.

opal plinth
#

That diff shows all the changes that happend after simply adding a newline at the bottom of the file and hitting CTRL+S

opal plinth
#

Which means having one ruling for the whole file would still cause changes, which I do not want because this is not my project and I need to respect the existing file and just commit my changes.

#

Seems like a simple enough request - stop doing things to my file...

smoky compass
#

Okay then your original file just has mixed line endings.

#

VS Code does normalization of your line endings automatically, and IIRC you can't turn it off.

opal plinth
#

According to the repository, the author used VS Code, but there were multiple team members, so they may have had different OS's

#

You'd think if they all used VS, then the whole file would be one or the other

opal plinth
smoky compass
#

Idk how much control you have over the repo, but files having mixed line endings is not a good sign and you want to fix that/prevent it from happening in the future anyways.

opal plinth
#

Yeah TBH there is some crappy code in this project, so it wouldn't surprise me they were letting this slip through

#

Thanks for your insight, I'll make some whitespace commits

#

The original author is not around anymore so the client is not going to care

smoky compass
#

If you have control, throw something in your CI/git pre commit hook to enforce line ending/formatting.

opal plinth
#

Will that avoid the changes showing up as differences?

#

line ending changes I mean

smoky compass
#

No, that's for preventing this from happening again in the future.

opal plinth
#

Ok cool

smoky compass
#

What's already happened, still needs to be fixed.

opal plinth
#

No worries, thanks for this