Vscode: Format document inserting 4 spaces instead of 2

Created on 10 Nov 2016  路  27Comments  路  Source: microsoft/vscode

  • VSCode Version: Code 1.7.1 (02611b40b24c9df2726ad8b33f5ef5f67ac30b44, 2016-11-03T13:46:00.226Z)
  • OS Version: Darwin x64 16.1.0
  • Extensions:

|Extension|Author|Version|
|---|---|---|
|prettify-json|mohsen1|0.0.3|
|dust|nemesarial|0.0.1|


Steps to Reproduce:

  1. open user settings
  2. set "editor.tabSize": 2
  3. "editor.detectIndentation": false
  4. ensure nothing set in workSpace Settings
  5. write some JS code
  6. right-click "Format Document" or alt + shift + F
formatting javascript needs more info

Most helpful comment

This might be related to the JS-CSS-HTML-formatter extension. Removing that extension seems to have solved the problem for me.

See https://github.com/Lonefy/vscode-JS-CSS-HTML-formatter/issues/31

All 27 comments

@frankorama What is shown on the VSCode status line?
Formatting which language?

UTF-8 LF JavaScript. The file I'm working in has a .js extension.

Some strange behaviour: It seems to be back to 2 spaces now, but when I select all, shift+tab so all the indentation is removed, then right-click "format code", nothing happens.

I thought I had a syntax problem or forgot a semicolon somewhere, but everything looks good. I removed all my comments as well. Same behaviour.

I am having this same problem with my .js files. The status line shows "Spaces: 2" but when I Format Document, it changes all my tabs to 4 spaces. I'm using v1.7.1 on OSX.

The problem with the automatic formatting is only happening in VS Code for OSX. I also work on a laptop with Windows 10 and the same .js file formats normally.

This might be related to the JS-CSS-HTML-formatter extension. Removing that extension seems to have solved the problem for me.

See https://github.com/Lonefy/vscode-JS-CSS-HTML-formatter/issues/31

I only had DUST language support and JSON prettify extensions installed. Removed them and the formatting is working normally now. Not sure which one was causing the problem though. Should have tested bette.

I am seeing the same issue with Windows 10. It strangely only happens the second time I open a file.

For example, if I open up this:

image

then hit alt + shift + f, the file stays the same with 2-space indentation. If I then close the file, open it up again, and hit alt + shift + f, I get this:

image

The same occurs for .js and .jsx files, but does not happen for .json which I am also formatting with 2-space indentation. It also appears to happen for .ts and .tsx as well鈥擨'm guessing they use the same formatter now?

I have disabled all the extensions I have installed to see if it was any of them causing this, but it does not appear to be the case as disabling them made no difference.

@joaomoreno isn't this what you have seen with TS/JS and reported to them?

It could be Microsoft/TypeScript#12175

I'm having exactly the same issue as @dylanparry . OSX 10.11.6 VSCode 1.7.1.

I don't know if this helps, but it really is a random problem. I just installed ESLint, JavaScript (ES6) code snippits, and jshint extensions. The formatting function stopped working. I disabled and reenabled the 3 extensions and formatting started working again. Difficult to pinoint exactly which extension is causing the problem because the problem doesn't occur until after a couple of hours of coding sometimes.

The issue intermittently happens and very often, but only on files that contains ES6 functions notations
() => { ... }
and not affecting
function() { ... }

I am observing this issue as well. Although, I identified no reproducable issue yet, I recognized, that the formatter will internally crash after some time of work. After that it will just use default settings rather than user settings and eventually does not format code at all anymore.

Is there any log, where suppressed issues might be logged to?

After restart it works again for some time.

@maybeec if this is a TS / JS issue then it could be showing up in that log. You can see the log in the output window after adding this setting to your user setting:

"typescript.tsserver.trace": "verbose",

If you (and others) could post the output of the log when you see the issue here that would be helpful.

Also if anyone has a workspace that reproduces this issue please post to help us reproduce.

I enabled the setting and will have a look when the error occurred. So where can I find the log file?

If you open the output window the log information is there... I don't know where the log file is actually created... @dbaeumer do you know the location of the TS log file that gets sent to the output window? Maybe the data is not persisted.

Ok basically, I was able to have a view at the output during observation of the error.
However, nothing of interest from tsserver as far as I can see. I save a file and the response comes from format (2566) which indicates to insert whitespaces in front of multiple lines. But there is nothing about format itself, e.g., which config is used.

So what I was observing now is, that this behavior comes up, when having multiple instances of vscode open for some time. After some time, the formatter begins to behave odd

To get the server log you can use an environment variable to control the location. Something like TSS_LOG="-level verbose -file c:\tmp\server.log"

This seems to be fixed in the latest release, which bundles a new version of TypeScript. Can anyone else confirm this?

Yes, I think that TS fixed a number of issues around formatting in 2.1.4 and 2.1.5. Please open a new issue if you still see this problem in VSCode 1.9 using TS 2.1.5+

Happening on vscode 1.13.1 for me on mac os latest, plugins: debugger for chrome 3.1.4, ESLint 1.2.11, React native Tools 0.3.2. Language: JavaScript, UTF-8

Not fixed, I still have this issue on VSCode 1.13.1 and TS 2.3.0.

UPDATE: In my case the issue was caused by a third party formatting plugin overriding the defaults.

Not fixed, still have this issue.
macOS 10.12.6
VS CodeVersion 1.14.2 (1.14.2)

Works here with .html files, set Workspace Settings to:

{
    "editor.tabSize": 2,
    "editor.detectIndentation": false
}

Default without either of these were 4 spaces.
1.15.1 on Win10.

edit: solved. (See 2 comments below)

Still have this issue:

  • Windows 10
  • VS Code 1.17.1
  • html file: indent using spaces (4)
  • Workspace Settings:
{
    "editor.tabSize": 4,
    "editor.detectIndentation": false
}

But pressing ALT + SHIFT + F still indented with 2 spaces

@colorpump Any extensions enabled related to HTML formatter? Please try without extensions.

To force tabs, use "editor.insertSpaces": false

It was my fault.
I had a .editorconfig file in my project as well which overrode my .vscode/settings.json

I'm sorry for confusion

Was this page helpful?
0 / 5 - 0 ratings

Related issues

curtw picture curtw  路  3Comments

trstringer picture trstringer  路  3Comments

chrisdias picture chrisdias  路  3Comments

DovydasNavickas picture DovydasNavickas  路  3Comments

villiv picture villiv  路  3Comments