editorconfig-vscode: trim_trailing_whitespace = false not working

  • I have a question that is specific to this extension; thus, inappropriate for the main EditorConfig issue tracker.
  • I tried running code --disable-extensions and the issue did NOT present itself.

Delete the following condition if it doesn’t apply to your case:

If the extension is not picking up the expected configuration for a file:

  • I tried npm install editorconfig -g and ran editorconfig [file-in-question] and the configuration was what I expected. If not, please file on the editorconfig-core-js issue tracker.

Issue

Visual Studio Code editorconfig-vscode
Version 1.12.2 0.9.3

Root .editorconfig File

# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false

Are there any other relevant .editorconfig files in your project? No

Visual Studio Code Setting Default User Workspace
editor.insertSpaces true ____ ____
editor.tabSize 4 2 _
editor.trimAutoWhitespace true ____ ____
files.autoSave "off" "___" "___"
files.insertFinalNewline false true _____
files.trimTrailingWhitespace false true _____

File opened

./README.md

Expected behavior

trim_trailing_whitespace = false

Actual behavior

trim_trailing_whitespace = true

Additional comments or steps to reproduce

When I add 2 spaces to the end of a line to create a markdown line break and then save the file, the trailing spaces are removed. OS: Ubuntu 17.04

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 54
  • Comments: 39 (13 by maintainers)

Commits related to this issue

Most upvoted comments

i like my editor to trim whitespace. the only problem i have is with markdown files, where whitespace at the end is used for a newline. to workaround the problem i added this in vscode settings.json

    "files.trimTrailingWhitespace": true,
    "[markdown]": {
        "files.trimTrailingWhitespace": false
    }

I 100% agree, but the tests don’t cover this scenario, so it’s not exactly a supported feature. The {} braces are for multiple matches, so there’s really no reason to use them unless you’re trying to target multiple files or extensions like:

[{*.md,*.yml}]

☝️ that works. You know what also works?

[{*.md,}]

So you can either add this scenario to the tests or just use [{*.md,}] or [*.md] for now. Up to you!

Found the workaround of removing `“files.trimTrailingWhitespace” from user/workspace settings. This way the extension seem to work properly.

This is not an issue with this extension, but with the core and the tests written against it.

If the code was programmed correctly, it should not matter if the singular or plural version was used, as that’s a feature of editorconfig….

@pjsvis that’s a great idea. You should submit that to vscode issues.

@S2- your solution sounds like a good fix for this, considering the user settings overrides the EditorConfig setting.

Everyone else, I think a good fix here would be to add the following to your workspace settings to undo the user setting:

    "files.trimTrailingWhitespace": false,

You’re basically telling the editor, “Don’t worry about trimming trailing whitespace on this project, because I have an EditorConfig configuration that takes care of that.”

For all other projects w/o an EditorConfig configuration, your user setting will still apply!

What do you think?

files.trimTrailingWhitespace could do with a debounce setting to allow a bit of thinking time when typing. Often I find the trailing whitespace is trimmed before I finish typing a line.

After disabling and deselecting as many extensions & settings as concerned the issue of visual studio code removing my spaces, but with no positive results, I found out that the issue was with this one simple selection Auto Save deselecting that is what worked for me. (Files -> Auto Save)

Same here, OS: macOS Sierra

For me, just disable files.trimTrailingWhitespace in vscode settings, and then give control to .editorconfig.

now markdown doesn’t trim trailing whitespace, but other files will

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

@clystian there is nothing actionable on this issue until https://github.com/microsoft/vscode/issues/65663 is addressed. Please follow that issue for now.

@jedmao Thank you for your cooperation. Hmm, the API of Code is too limited…

Do you really belive this issue is not due to this extension? If so, why don’t you send an issue to VSCode itself? If you have already sent one, paste the URL of it.

https://github.com/Microsoft/vscode/issues/65663

I don’t believe vscode is doing this on purpose. There are three EditorConfig settings that execute on the workspace.onWillSaveTextDocument event.

  • SetEndOfLine()
  • TrimTrailingWhitespace()
  • InsertFinalNewline()

They do their respective jobs correctly, but it doesn’t matter, because vscode then executes the built-in save operation, which takes into account the workspace and user settings. In this case, that means they trim trailing whitespace after EditorConfig is done not trimming the whitespace.

I don’t think it matters if this is a pre-save operation or a post-save operation, because once that whitespace is gone, it’s gone. EditorConfig isn’t going to add it back. So the issue that remains is that we need a way to short-circuit the built-in save operation to defer to the EditorConfig settings instead of only consulting workspace / user settings.

This is another reason why EditorConfig being built-in to the editor (as it is in other editors) makes a lot of sense.

Unless there are some new APIs that allow us to do this, by all means, point me in the right direction. As for the last time I looked into this, there wasn’t really a solution. I don’t think it’s a vscode bug per se, but it could be a good new feature request to ask for a way to short-circuit the built-in save operation somehow.

Do you really belive this issue is not due to this extension? If so, why don’t you send an issue to VSCode itself? If you have already sent one, paste the URL of it.

@S2- nope, it’s not:

  • editorconfig plugin enabled
  • editorconfig file:
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[{*.html,*.hbs,*.json,*.yml}]
indent_size = 2

[{*.md}]
trim_trailing_whitespace = false
  • workspace settings:
{
    "html.format.wrapAttributes": "force-aligned",
}
  • user settings:
{

}

VSCode removes trailing whitespaces from .md files

Even with these workspace settings:

{
    "files.trimTrailingWhitespace": false,
    "html.format.wrapAttributes": "force-aligned",
}

And with these workspace settings:

{
  "files.trimTrailingWhitespace": true,
    "[markdown]": {
        "files.trimTrailingWhitespace": false
    },
  "html.format.wrapAttributes": "force-aligned",
}

THE only solution right now is to not use this editorconfig plugin and add all the settings from it to the workspace settings:

{
  // Settings to mimic `.editorconfig` as the plugin has bugs on trim trailing whitespace, 
  // see https://github.com/editorconfig/editorconfig-vscode/issues/153
  "files.encoding": "utf8",
  "editor.insertSpaces": true,
  "editor.tabSize": 4,
  "editor.detectIndentation": false,
  "files.eol": "\n",
  "files.insertFinalNewline": true,
  "files.trimTrailingWhitespace": true,
  "[javascript]": {
    "editor.tabSize": 2,    
  },
  "[handlebars]": {
    "editor.tabSize": 2,    
  },
  "[json]": {
    "editor.tabSize": 2,    
  },
  "[yaml]": {
    "editor.tabSize": 2,    
  },
  "[markdown]": {
    "editor.tabSize": 2,
    "files.trimTrailingWhitespace": false
  },

  // Other project settings
  "html.format.wrapAttributes": "force-aligned",
}

^^^ +1 this. It is so effing annoying