editorconfig-vscode: Editorconfig should not apply styles to files located outside the root directory

I’ve found that editorconfig changes to tabs as specified in the project’s .editorconfig when I edit my settings.json file which is located away from the root open folder in ~/.config. It should instead not apply the editorconfig settings in this case.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Verified fixed 👍 thanks guys

@Tyriar You are right!

@jedmao It looks like the code wasn’t compiled before releasing a new version.

Source: DocumentWatcher.ts

private _onConfigChanged() {
    const workspaceConfig = workspace.getConfiguration('editor');
    const detectIndentation = workspaceConfig.get<boolean>('detectIndentation');

    this._defaults = (detectIndentation) ? {} : {
        tabSize: workspaceConfig.get<string | number>('tabSize'),
        insertSpaces: workspaceConfig.get<string | boolean>('insertSpaces')
    };
}

Code in Extension: DocumentWatcher.js

_onConfigChanged() {
    this._defaults = {
        tabSize: vscode_1.workspace.getConfiguration('editor')
            .get('tabSize'),
        insertSpaces: vscode_1.workspace.getConfiguration('editor')
            .get('insertSpaces')
    };
}

TL;DR: I have a solution, will create a PR.

Sorry for this long explanation.

Alright, I started debugging this. Please note that this is on my computer with my setup. I don’t have any .editorconfig files defined in home directories or whatsoever. This is important as EditorConfig will automatically traverse all the way up to the root untill it finds a .editorconfig file.

Let’s get started!

When I open the global settings, the DocumentWatcher._onDidOpenTextDocument kicks in. Because the config was not yet loaded, it will ask EditorConfig to parse the path. This is expected as stated before, it could be possible that you have a .editorconfig in your home directory. In my case, that promises just returns an empty object as no .editorconfig file was found.

Then it calls the applyEditorConfigToTextEditor method. The settings for the document are empty, e.g. {}.


sidetrack

The main question we have to ask first, is that behaviour correct? I already opened an issue on the editorconfig-core-js.

The thing is, if editorconfig.parse() would’ve returned undefined, it would exit early and we’re all happy.

This made me wonder. We already had Paul Irish creating issue #44 for this behaviour and I proposed fix #38 which was rejected in favour of #43. The thing is that #38 would’ve fixed this issue while #43 only made sure the message didn’t get displayed.


So let’s continue where I left. After that, the Utils.fromEditorConfig are called and create an options object based on the .editorconfig file (which is {}) and the editor settings.

And this is what causes the issue. It will read your workspace settings BUT it doesn’t take the editor.detectIndentation into account resulting in the hard fixed settings defined in settings.json.

Let me create a PR that solves this issue.

Are you sure this is because of the EditorConfig plugin? I scanned the code real quick and it shouldn’t do that, so if it is the case, it’s definitely a bug.

I recorded a screen capture and tried editing and saving it. As you can see, I have trim_trailing_whitespace set to true and insert_final_newline as well. Neither both of those transformations are being applied.

editorconfig

I know it might be a stupid question, but I have to ask 😃. You aren’t editing your workspace defined settings, right? Probably not because key bindings are always global, but you never know.

Care to screen capture the behaviour? Also, what OS are you using?