Quramy / typescript-eslint-language-service

TypeScript language service plugin for ESLint
MIT License
246 stars 10 forks source link

Can't load plugins when used in Visual Studio #29

Open andyrooger opened 4 years ago

andyrooger commented 4 years ago

If any plugins are specified in the .eslintrc.js then typescript-eslint-language-service fails to load.

module.exports = {
    root: true,
    parser: '@typescript-eslint/parser',
    parserOptions: {
        tsconfigRootDir: __dirname,
        project: ['./tsconfig.json'],
        sourceType: 'module'
    },
    plugins: [
        '@typescript-eslint',
    ],
    extends: [],
    rules: {
        '@typescript-eslint/no-unused-vars': ['error'],
    }
};

In the TSS logs I see the following output:

Info 4136 [18:25:57.648] [typescript-eslint-language-service] Failed to load plugin '@typescript-eslint' declared in '....\src\myproject.eslintrc.js': Cannot find module '@typescript-eslint/eslint-plugin' Require stack: - C:\WINDOWS\system32\__placeholder__.js

andyrooger commented 4 years ago

I've been experimenting - seems to be the problem is that on Visual Studio the cwd is set to C:\WINDOWS\system32 when this gets run.

CascadingConfigArrayFactory and Linter both have options object that can be passed with a cwd to use. If I set that to something sensible and temporarily process.chdir(...) for the duration of getESLintResult then I can get this working.

CLIEngine also has a cwd you can pass but it seems to make things worse...

I think this should be testable outside of VS by just doing a process.chdir to something silly when the plugin is starting.

andyrooger commented 4 years ago

For now I'm working around this with a plugin that looks like

function init(modules) {
    process.chdir('<project root>');

    function create(info) {
        return info.languageService;
    }
    return { create };
}

module.exports = init;

If I load it right before typescript-eslint-language-service then I can get the plugin loaded and working successfully. It's obviously not a great permanent solution though.

laduke commented 1 year ago

I seeing something similar on macos. I don't use visual studio code, but I wanted to make sure repo/set up worked for people that do. It's 3 years later, so probably a different issue. How did no one else run into this? Are we doing something weird in our eslintrc?

Info 26   [09:16:07.651] Failed to load module 'typescript-eslint-language-service' from /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/node_modules: Error: Could not resolve JS module 'typescript-eslint-language-service' starting at '/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/node_modules'
....
/Applications/node_modules/
...
/node_modules/typescript-eslint-language-service/index.jsx

Why is it trying to load stuff from /Applications/Visual Studio ? and from /node_modules?

https://github.com/typescript-eslint/typescript-eslint/issues/6883

andyrooger commented 1 year ago

If it is the same problem you can try https://www.npmjs.com/package/vs-compat-ts-plugin - it's how I've been solving the initial problem for a few years.

Contains the code above with a few extra pieces. Though I don't recognise your errors.

laduke commented 1 year ago

Thanks for responding. Sadly, now it's trying to load vs-compat-ts-plugin from the same wrong places. Was worth a try.

Oops. I'm testing in Visual Studio CODE, wrong issue.