dimaMachina / graphql-eslint

ESLint parser, plugin, and rule set for GraphQL (for schema and operations). Easily customizable with custom rules. Integrates with IDEs and modern GraphQL tools.
https://the-guild.dev/graphql/eslint
MIT License
800 stars 104 forks source link

graphql-eslint does not work with VSCode ESLint extension #1091

Closed TimoWilhelm closed 1 year ago

TimoWilhelm commented 2 years ago

Issue workflow progress

Progress of the issue based on the Contributor Workflow


Describe the bug

Rules that try to access the schema via url-loader are broken when invoked by the VSCode eslint extension.

image

It seems like this line in sync-fetch expects to be called by Node.js, but in my case it's the VSCode executable when invoked by the VSCode eslint extension (dbaeumer.vscode-eslint).

To Reproduce Steps to reproduce the behavior:

This is my settings.json (I have fixAll onSave enabled):

  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "typescriptreact",
    "graphql"
  ],
  "eslint.options": {
    "extensions": [
      ".js",
      ".jsx",
      ".ts",
      ".tsx",
      ".graphql"
    ]
  },
  "eslint.workingDirectories": [
    {
      "mode": "auto"
    }
  ],
  "editor.codeActionsOnSave": {
    "source.fixAll": true
  },

The section from my .eslintrc.json

 {
      "files": [
        "*.graphql"
      ],
      "extends": "plugin:@graphql-eslint/schema-recommended",
      "parserOptions": {
        "operations": "./**/*.graphql"
      },
      "rules": {
        "@graphql-eslint/description-style": [
          "off"
        ],
        "@graphql-eslint/known-directives": [
          "off"
        ],
        "@graphql-eslint/naming-convention": [
          "off"
        ],
        "@graphql-eslint/strict-id-in-types": [
          "off"
        ]
      }
    }

Expected behavior

I would expect it to work with the VSCode extension.

Environment:

Additional context

If I invoke it with eslint . in the command line it works without any issues.

dimaMachina commented 2 years ago

Hi @TimoWilhelm, can you provide a reproduction repo of your problem? This will be helpful 🙏

TimoWilhelm commented 2 years ago

Hi @B2o5T, I created an MRE at https://github.com/TimoWilhelm/mre-graphql-eslint-vscode.

To reproduce:

  1. Clone the repo and open it with VSCode.
  2. Run npm install.
  3. Make sure the dbaeumer.vscode-eslint extension is installed.
  4. Open the sample.graphql file.
  5. Edit anything and there should be a pop-up in the notification corner.

Otherwise you can check the ESLint output window for errors.

Please let me you know if you were able to reproduce the issue or if you need anything else.

Thanks in advance!

dimaMachina commented 2 years ago
  1. You should use parserOptions.operations or graphql-config file but not a both in same time, remove parserOptions.operations from eslint config and add documents: ./**/*.graphql in your graphql-config.
  2. Why do you extend plugin:@graphql-eslint/schema-recommended if your schema is remote? Actually for your project is make sense to lint operations but not schema types
  3. I added some public remote schema in your graphql-config - https://countries.trevorblades.com. And replaced your eslint config with
    {
    "root": true,
    "overrides": [
    {
      "files": "*.graphql",
      "extends": "plugin:@graphql-eslint/operations-recommended"
    }
    ]
    }

    and received expected output

    
    /Users/dimitri/Desktop/mre-graphql-eslint-vscode/sample.graphql
    2:3  error  Cannot query field "sample_item" on type "Query"  @graphql-eslint/fields-on-correct-type

✖ 1 problem (1 error, 0 warnings)



## ❗️So everything works as expected in terminal but not in VSCode-ESLint extension and you pointed a right line
Because `process.execPath` equals `/Users/dimitri/.nvm/versions/node/v16.14.2/bin/node` via terminal but via VSCode - `/Applications/Visual Studio Code - Insiders.app/Contents/Frameworks/Code - Insiders Helper.app/Contents/MacOS/Code - Insiders Helper`
And when I replaced `process.execPath` with `/Users/dimitri/.nvm/versions/node/v16.14.2/bin/node` (for debugging), I got errors in my VSCode also
<img width="764" alt="image" src="https://user-images.githubusercontent.com/7361780/174688705-fbee99a8-adfa-4601-9ab3-1050ce7213cc.png">
TimoWilhelm commented 2 years ago

In my repo I actually have a mix between a remote schema and client-only schema, so the linter setup in the example doesn't really make sense, I agree! And I also didn't really check if the eslint command works in the MRE, which I should have. In my main repo, invoking ESLint with the CLI works just fine.

There seems to be some activity about this issue @ https://github.com/larsgw/sync-fetch/issues/23.

dimaMachina commented 1 year ago

to fix the incorrect process.execPath, add .vscode/settings.json file with the following content (node path you can get by which node in terminal)

{
  "eslint.runtime": "/Users/dmytro/.nvm/versions/node/v18.16.1/bin/node"
}

that I described in https://github.com/B2o5T/graphql-eslint/issues/1721#issuecomment-1616574376