Skip to content

no-extraneous-dependencies should not report the package itself #305

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

Open
dmail opened this issue Apr 16, 2025 · 12 comments · May be fixed by #309
Open

no-extraneous-dependencies should not report the package itself #305

dmail opened this issue Apr 16, 2025 · 12 comments · May be fixed by #309
Labels
question Further information is requested

Comments

@dmail
Copy link

dmail commented Apr 16, 2025

The following package.json

{
  "name": "package-name"
}

And index.js:

import { foo } from "package-name";

Actual

import-x/no-extraneous-dependencies rule reports an error with the following message.

'package-name' should be listed in the project's dependencies. Run 'npm i -S package-name' to add it

Expected

import-x/no-extraneous-dependencies remains silent because the package can import itself.

@JounQin
Copy link
Member

JounQin commented Apr 16, 2025

Is importing the package itself a good practice? Isn't it a circle dependency?

@JounQin JounQin added the question Further information is requested label Apr 16, 2025
@dmail
Copy link
Author

dmail commented Apr 16, 2025

Yep indeed.

But it happens in my test files: they are importing the package to test it.

So the example is more like this:

index.js:

export const answer = 42;

tests/index.test.js:

import { answer } from "package-name";

if (answer !== 42) {
  throw new Error("answer should be 42");
}

@JounQin
Copy link
Member

JounQin commented Apr 16, 2025

I believe you should disable no-extraneous-dependencies rule for test files? The source and test should have different rules, and this plugin cannot determine that automatically.

@dmail
Copy link
Author

dmail commented Apr 16, 2025

My goal with this rule is mainly to ensure my dependencies are properly listed in the package.json. So I could disable it entirely for test files.

But ideally I could also use it to ensure my devDependencies are listed. So I would like an option like allowSelfImport that I would set to true only in test files. Is that something that make sense?

@JounQin
Copy link
Member

JounQin commented Apr 16, 2025

But ideally I could also use it to ensure my devDependencies are listed. So I would like an option like allowSelfImport that I would set to true only in test files. Is that something that make sense?

That could make sense, but I need to think about it more and discuss with others.

cc @Shinigami92 @SukkaW @thepassle

@Shinigami92
Copy link
Member

Can't you add it as internal devDependency?

Like I did in https://github.com/salsita/node-pg-migrate/blob/main/package.json#L114

@dmail
Copy link
Author

dmail commented Apr 16, 2025

Can't you add it as internal devDependency?

That would likely fix my issue indeed 🤔 I'll try this

(By the way I think file:. dependencies can be declared with ./, at least that works for me)

@JounQin
Copy link
Member

JounQin commented Apr 16, 2025

(By the way I think file:. dependencies can be declared with ./, at least that works for me)

By the way, I'd prefer link:. for yarn. 😁

@dmail
Copy link
Author

dmail commented Apr 16, 2025

In the end I've added the package name into my devDependencies as follow:

{
  "name": "package-name",
  "devDependencies": {
    "package-name": "./"
  }
}

For me it's a good enough way to be able to reference the package from test files while keeping import-x/no-extraneous-dependencies rule enabled.

Thanks to the quick responses and feedback.

@JounQin
Copy link
Member

JounQin commented Apr 16, 2025

Can this issue be closed then?

@dmail dmail closed this as completed Apr 16, 2025
@remcohaszing
Copy link

Can this be reopened? I like this rule, but it’s also the one I have to disable very often because of this issue. It looks easy to fix too. I’ll gladly provide a PR if this is accepted.

Is importing the package itself a good practice? Isn't it a circle dependency?

This was already brought up, but I think it’s a great practice to import a package using its name in test files. This tests whether the package exports are correct.

I believe you should disable no-extraneous-dependencies rule for test files? The source and test should have different rules, and this plugin cannot determine that automatically.

I agree they should have different rules. For tests I allow devDependencies in addition to the other kinds of dependencies. This rule is still useful for tests.

Can't you add it as internal devDependency?

That might work, but it would only be a workaround for this issue.

@JounQin
Copy link
Member

JounQin commented Apr 18, 2025

Hi, @remcohaszing.

Great to see you here!

A PR resolving this issue would be appreciated for sure! ❤️

remcohaszing added a commit to remcohaszing/eslint-plugin-import-x that referenced this issue Apr 18, 2025
Allow packages to import themselves via the `package.json` exports
field.

If a package tries this, but the exports field is not defined, a new
message is reported.

Closes un-ts#305
@JounQin JounQin reopened this Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants