Skip to content

Bug: 6.0.0 - cannot use parser with absolute path anymore #7226

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

Closed
4 tasks done
kkmuffme opened this issue Jul 14, 2023 · 6 comments
Closed
4 tasks done

Bug: 6.0.0 - cannot use parser with absolute path anymore #7226

kkmuffme opened this issue Jul 14, 2023 · 6 comments
Labels
bug Something isn't working fix: user error issue was fixed by correcting the configuration / correcting the code working as intended Issues that are closed as they are working as intended

Comments

@kkmuffme
Copy link

kkmuffme commented Jul 14, 2023

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Relevant Package

parser

Playground Link

No response

Repro Code

eslint --resolve-plugins-relative-to /my/path/to/ --no-eslintrc --config /config/path/to/eslint-config.json example.js

ESLint Config

{
    "plugins": [
        "@typescript-eslint"
    ],
    "parser": "/my/path/to/@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": 13,
        "sourceType": "script",
        "ecmaFeatures": {
            "globalReturn": false,
            "jsx": false
        },
        "project": [
            "/config/path/to/tsconfig.tmp.json"
        ]
    },
    "extends": [
        "plugin:@typescript-eslint/recommended-type-checked"
    ]
}

tsconfig

No response

Expected Result

Should work

Actual Result

Error: Failed to load parser '/my/path/to/@typescript-eslint/parser' declared in '--config': Cannot find module '/my/path/to/@typescript-eslint/parser'

Additional Info

With 5.62.0 (and before) it works perfectly fine.
However with 6.0.0 it fails.

The package @typescript-eslint/parser is there, is accessible (correct permissions) and also shows perfectly fine when checking with npm list.

I spent hours today thinking it was a bug in eslint, but it turns out it's not, and I am stuck at a dead-end.

I'd really appreciate if you could fix this!

Versions

package version
@typescript-eslint/eslint-plugin 6.0.0
@typescript-eslint/parser 6.0.0
TypeScript 5.1.4
ESLint 8.45.0
node 16.19.1
@kkmuffme kkmuffme added bug Something isn't working triage Waiting for team members to take a look labels Jul 14, 2023
@bradzacher
Copy link
Member

This is just a quirk of how package.json exports work in conjunction with require.resolve (which ESLint itself uses under the hood):

> require('module').createRequire(path.join(process.cwd(), '_placeholder_.js')).resolve('@typescript-eslint/parser')
'/Users/bjz/temp/playground/node_modules/@typescript-eslint/parser/dist/index.js'

> require('module').createRequire(path.join(process.cwd(), '_placeholder_.js')).resolve(path.join(process.cwd(), 'node_modules', '@typescript-eslint/parser'))
Uncaught:
Error: Cannot find module '/Users/bjz/temp/playground/node_modules/@typescript-eslint/parser'
Require stack:
- /Users/bjz/temp/playground/_placeholder_.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Function.resolve (node:internal/modules/cjs/helpers:116:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/bjz/temp/playground/_placeholder_.js' ]
}

You will want to either use require.resolve('@typescript-eslint/parser') or you will want to just use '@typescript-eslint/parser' and rely on your --resolve-plugins-relative-to flag which will tell ESLint where to look.

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Jul 15, 2023
@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended fix: user error issue was fixed by correcting the configuration / correcting the code and removed triage Waiting for team members to take a look labels Jul 15, 2023
@bradzacher bradzacher added this to the v6.0.0 release bugs milestone Jul 15, 2023
@kkmuffme
Copy link
Author

you will want to just use '@typescript-eslint/parser' and rely on your --resolve-plugins-relative-to

I already use --resolve-plugins-relative-to - but this applies to "plugin" in the e.g. "extends" only. This does NOT apply to the parser.

You will want to either use require.resolve('@typescript-eslint/parser')

This doesn't work with a .json config obviously

This worked fine before 6.0.0, not sure how this is a user error?

@JoshuaKGoldberg
Copy link
Member

This doesn't work with a .json config obviously

True! ESLint is moving away from .json configs: https://eslint.org/blog/2022/08/new-config-system-part-2/ -> https://eslint.org/docs/latest/use/configure/configuration-files-new. The new flat config will be particularly emphasized in ESLint 9: https://eslint.org/blog/2023/07/eslint-v8.45.0-released/#new-configuration-migration-guide. We're much less motivated to support them now. So hey, maybe this is a good excuse to switch over sooner? 😄

This worked fine before 6.0.0, not sure how this is a user error?

Before 6, you're right again - it would have been a bug. As of the changes described by https://typescript-eslint.io/blog/announcing-typescript-eslint-v6#package-exports, it's now intentional. We don't want to work against native Node/ESM import/require behaviors.

@kkmuffme
Copy link
Author

We're much less motivated to support them now. So hey, maybe this is a good excuse to switch over sooner? 😄

That's what I wanted to do, but it turns out @typescript-eslint isn't compatible with the new flat config yet.
While it's possible to make it work by manually importing all the rules (since the "extends" isn't supported anymore), it's kind of a waste of time to now change a .json to a .js and then eventually to the flat config.

But alas, that's what I'm doing now.

@JoshuaKGoldberg
Copy link
Member

Got it - I meant to suggest just switching over to a .cjs/.js config now. But yeah, we're hoping to support flat configs soon (#6836).

Thanks for the conversation, by the way! This was very helpful in understanding a way we didn't previously know about how folks use us.

@bradzacher
Copy link
Member

it's kind of a waste of time to now change a .json to a .js and then eventually to the flat config.

Note that the migration from .json to .js is this:

  1. rename extension from .json to .js
  2. update first line from { to module.exports = {

And that's it! JSON is valid JS so there are no further updates required to your config file (unless you have prettier or something which will want to format it...).

There's no difference in resolution or anything between the two config file types - so you can fearlessly make the switch now to improve your experience :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working fix: user error issue was fixed by correcting the configuration / correcting the code working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

3 participants