standard / vscode-standard

VS Code extension for JavaScript Standard Style (`standard`) with automatic fixing
https://marketplace.visualstudio.com/items?itemName=standard.vscode-standard
MIT License
121 stars 24 forks source link

validation only triggers if `package.json` is in workspace root #536

Open missinglink opened 10 months ago

missinglink commented 10 months ago

Here's what I did

settings.json
{
    "standard.enable": true,
    "standard.usePackageJson": true,
    "standard.autoFixOnSave": true,
    "standard.enableGlobally": false,
    "standard.validate": [
        "javascript",
        "javascriptreact"
    ]
}
package.json
{
  "devDependencies": {
    "standard": "^17.1.0"
  }
}

Create a two-level directory structure: parent -> example

Screenshot 2023-10-25 at 14 02 11

What I expected to happen

I expected validation to be enabled when my package.json was not in the root of the Workspace.

What seems to have happened

It works if I remove the parent directory such that the package.json is now in the Workspace root. It doesn't work when the package.json is nested below the root.

ie. this directory layout works:

Screenshot 2023-10-25 at 14 01 33

Explanation

This is the coded behaviour here:

https://github.com/standard/vscode-standard/blob/773679471b5f9bfc0209af87c58683888d42b9c3/client/src/extension.ts#L226-L232

Possible Workaround

Enabling "standard.enableGlobally": true allows the two-level directory structure to be validated.

This isn't acceptable in my situation as, even though I have only one top-level directory, only some of my second-level directories follow this style guide and I can't enable it for all those directories.

Suggested Fix

Instead of assuming that the package.json file is in the root of the Workspace it might be better to 'walk down' the textDocument.path until a directory with a package.json file is found (in the same way npm walks down until it finds the module root).

Closing notes

There is some code which mandates that workspace only has one top-level directory (Workspace.workspaceFolders?.length === 1), this is actually unrelated to this issue, since I do only have one top-level directory in my example.

missinglink commented 10 months ago

related https://github.com/standard/vscode-standard/pull/263 cc/ @theoludwig