Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tsconfigRootDir relative to the .eslintrc file #251

Closed
wibblymat opened this issue Feb 11, 2019 · 6 comments
Closed

Make tsconfigRootDir relative to the .eslintrc file #251

wibblymat opened this issue Feb 11, 2019 · 6 comments
Labels
enhancement New feature or request package: typescript-estree Issues related to @typescript-eslint/typescript-estree

Comments

@wibblymat
Copy link

I am linting my project from two places. Firstly, I run eslint in a shell as part of the CI process. This will have a current working directory that is the location of the .eslintrc file.

I also run the ESLint plugin in VS Code. The current working directory for this is the project root, which is not the same location.

If I set the project setting to be tsconfig.json, and tsconfigRootDir to be . then I can run eslint from the shell. And if I set tsconfigRootDir to be path/to/my/subproject then I can use the VS code plugin. But there is no possible combination of options that let's both work.

Furthermore, tsconfigRootDir seems to be entirely redundant anyway, because there is no combination of project and tsconfigRootDir that can't be just concatenated into the project field, like { "project": "path/to/my/subproject/tsconfig.json" }

@wibblymat wibblymat added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Feb 11, 2019
@bradzacher bradzacher added enhancement New feature or request package: parser Issues related to @typescript-eslint/parser package: typescript-estree Issues related to @typescript-eslint/typescript-estree and removed package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look package: parser Issues related to @typescript-eslint/parser labels Feb 15, 2019
@ypresto
Copy link
Contributor

ypresto commented Feb 15, 2019

FYI: To workaround, rename your eslintrc into .eslintrc.js then "tsconfigRootDir": __dirname.

@prokopsimek
Copy link

@ypresto Does not work for me.

.eslintrc.js

module.exports = {
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint'],
  env: {
    es6: true,
    node: true,
  },
  extends: ['plugin:@typescript-eslint/recommended', 'prettier/@typescript-eslint', 'plugin:prettier/recommended'],
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  },
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module',
    project: './tsconfig.json',
    tsconfigRootDir: __dirname,
  },
  rules: {
    quotes: ['off'],
    'no-process-env': ['warn'],
    'no-var': ['warn'],
    'sort-imports': ['warn'],
    'func-style': ['error', 'expression'],
    'prefer-arrow-callback': ['error'],
    '@typescript-eslint/explicit-function-return-type': ['warn'],
    '@typescript-eslint/await-thenable': ['warn'],
    '@typescript-eslint/no-require-imports': ['warn'],
    '@typescript-eslint/no-unnecessary-type-assertion': ['warn'],
    '@typescript-eslint/prefer-string-starts-ends-with': ['warn'],
    '@typescript-eslint/interface-name-prefix': ['off'],
    '@typescript-eslint/explicit-member-accessibility': [
      'warn',
      {
        accessibility: 'no-public',
      },
    ],
    'prettier/prettier': [
      'warn',
      {
        semi: false,
        trailingComma: 'es5',
        singleQuote: true,
        printWidth: 140,
        tabWidth: 2,
        arrowParens: 'always',
      },
      {
        usePrettierrc: false,
      },
    ],
  },
}

error:

5/1/2019, 3:09:36 PM:
---------------------
Error while loading rule '@typescript-eslint/await-thenable'/Users/prokop/Sites/_other/typescript-tutorial-adel/src/index.ts:: You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.
Occurred while linting /Users/prokop/Sites/_other/typescript-tutorial-adel/src/index.ts

@bradzacher
Copy link
Member

bradzacher commented May 2, 2019

the easy workaround for this is:

module.exports = {
  parserOptions: {
    project: './tsconfig.json',
    tsconfigRootDir: __dirname,
  },
};

@GitTom
Copy link

GitTom commented May 16, 2019

Here's my solution for a Google Cloud Functions project in VSCode in case it helps someone.

My project has the usual structure for a functions project: the functions code and related configuration files are in a 'functions' subfolder of the project/workspace's root.

The beginning of my .eslint.json...

  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "tsconfig.json",
    "tsconfigRootDir": "functions" 
  },
  "plugins": ["@typescript-eslint"],

This got it working properly in the IDE but broke command line invocations, so now my package.json includes this:

  "scripts": {
    "lint": "eslint **/*.ts --parser-options={tsconfigRootDir:null}",

Which reverses the setting of 'tsconfigRootDir' in my eslintrc.json for the cmd line.

[edit: though this solves the problem of this issue (for me, at least) it doesn't help with the location of eslintignore.]

@ValeryVS
Copy link

If I understand the question, this can be resolved at VSCode's ESLint plugin side

  "eslint.workingDirectories": [
    {"directory": "./client", "changeProcessCWD": true}
  ],

kaicataldo pushed a commit to kaicataldo/typescript-eslint that referenced this issue Aug 27, 2019
@bradzacher
Copy link
Member

Revisiting this - there's nothing we can do here.
ESLint does not provide us with the path to the current .eslintrc

I believe this is due to a few reasons:

  • There may not be a config file at all
    • Config can be provided entirely via CLI flags, or via their JS API.
  • Similarly, CLI flags may override options in your .eslintrc
    • If your .eslintrc contains no parser/parserOptions, but you use --parser @typescript-eslint/parser --parser-options "{ project: './tsconfig.json' }", what should the config path be?
  • There may be one config file for any given file being linted.
    • Config files can be cascaded up until a config with root: true is found.
    • If you have a config file in the root which defines parserOptions.project, and a config file deep in your folder tree that does not, then eslint will combine the two configs together when linting a file in that deep folder. What should the config path be in that case?

For the above reasons, I doubt that ESLint will ever pass the config path for the above reasons.

The simple workaround for this is to rename your config to .eslintrc.js, and then do the following:

module.exports = {
  parserOptions: {
    project: './tsconfig.json',
    tsconfigRootDir: __dirname,
  },
};

Unfortunately, if you want to use yaml or json format files, then you will have to ensure you run ESLint from the correct directory.

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request package: typescript-estree Issues related to @typescript-eslint/typescript-estree
Projects
None yet
Development

No branches or pull requests

6 participants