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

[file-extension-in-import] Dir imports resolve to package/dir.js instead of package/dir/index.js #136

Closed
1 task done
Sarabadu opened this issue Nov 8, 2023 · 4 comments

Comments

@Sarabadu
Copy link

Sarabadu commented Nov 8, 2023

Environment

Node version: 20
npm version: 10.1.0
ESLint version: 8.51.0
eslint-plugin-n version: 16.2.0
Operating System: macos

What rule do you want to report?

file-extension-in-import

Link to Minimal Reproducible Example

https://eslint-online-playground.netlify.app/#eNqtkktP4zAUhf/KlTcpUus8ZjTDdMRokFggsWCDYEFAcpNLMfglP0pL6X/HeYBIRSQWrJL4npzz+ehuibNVimsmjUD64MiccGm09bCF8+BN8LCDO6slJP9NEEHytH/odpr8hTQFf88duHsdRA0LhDu+ZguBwNzIXzEoKdVbkODOX+Hi2JhT7fwZblyXvB/MnoPFmWKerzB9wkW6KrKiyP5k+Zcpxi1Srmpcd2ClGkOaJHWQcpMcNKJKK+eBwREofOrbmsRJc65jm0IvJ+yATAlFJ7jytor2WsWKt6UCKAmuParalWQO1/GrFc0tVlrKeI51SaZRZURYcjVX6WByM+08DLMO7bnxPKY2Tq13615JdonWxUFzXhLBPDofTd8UTgdb4cXGYCeQug4ivjfzXal2Ed2w6pEtm80YgKtIhMM4peve5jb/SfPfNIOXF/h3lP+iGc160x66xtUJmuYmquJ7Pl0NI8TdcNZVMhu713Dneqgf9PCQ5p+oPu5Ery1oXgyYY8+Ido/5Hfk789red6/+GzDA

What did you expect to happen?

I was expecting to auto fix this issues with file-extension-in-import plugin instead of being flagged as error from n/no-missing-import

import { Output } from '@pulumi/pulumi/output'; // this should be fixable as '@pulumi/pulumi/output.js'
import {listWebAppHostKeysOutput} from '@pulumi/azure-native/web/v20220901'; // this should be fixable as '@pulumi/azure-native/web/v20220901/index.js'

migrating to node 20 those imports are failling with:

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/.../node_modules/@pulumi/azure-native/web/v20220901' is not supported resolving ES modules imported from /.../someFile.js
    Did you mean to import @pulumi/azure-native/web/v20220901/index.js?

Would be nice to use file-extension-in-import (maybe with some additional configs) to auto fix those

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

@Sarabadu Sarabadu added the bug label Nov 8, 2023
@scagood scagood added the rule label Nov 8, 2023
@scagood
Copy link

scagood commented Nov 8, 2023

Thank you for the report!

This looks to be because the package does not have an "exports" in it's package.json

I suppose that we need to detect if the package.json has exports defined or not, then change the behaviour accordingly 🤔

I am not sure if that should be in file-extension-in-import or in no-missing-import though. This is because the second example does not just add an extension, it adds a whole file to the directory.

@Sarabadu
Copy link
Author

Sarabadu commented Nov 8, 2023

yes my first though was on adding "/index.js" in the tryExtensions config, but you are right, maybe we need a no-dir-imports separated rule, with auto fix?

@scagood
Copy link

scagood commented Nov 8, 2023

I think that that is a good rule idea, but I don't think it'd work in this case as things get a lot more complicated when you go into modules.

It'd work well for relative / absolute imports, but in the case of modules you need to check for:

  • exports in the package json
  • symlinked packages (eg pnpm) and compressed packages with a loader (eg yarn v3 pnp)
  • additional file types (eg .d.ts, .node, and .js)

In my company we created a new rule called custom/require-resolve which (as the name implies) called require.resolve(import) then resolved the path relative to the file.
But we explicitly removed all modules from that.

@scagood
Copy link

scagood commented Feb 19, 2024

This looks to have been resolved in #139

For now, you can use the beta version (npm i eslint-plugin-n@next) if you don't want to wait for the next release (#158)

@scagood scagood closed this as completed Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants