import-js / eslint-plugin-import

ESLint plugin with rules that help validate proper imports.
MIT License
5.45k stars 1.56k forks source link

Invalid eslint error for "import/no-extraneous-dependencies" when it's reference self #2430

Open lygstate opened 2 years ago

lygstate commented 2 years ago
{
  "name": "@ts/deferred",
  "version": "1.0.0",
  "description": "The deferred library implemented in Typescript",
  "license": "MIT",
  "scripts": {
    "build": "yarn run build:esm && yarn run build:cjs && yarn run build:types",
    "build:esm": "tsc -p tsconfig.mjs.json",
    "build:cjs": "tsc -p tsconfig.cjs.json",
    "build:types": "tsc -p tsconfig.types.json",
    "test": "jest --config ../../jest.config.ts"
  },
  "module": "./dist/mjs/index.js",
  "main": "./dist/cjs/index.js",
  "types": "./dist/types/index.d.ts"
}

Error:

[{
    "resource": "/c:/work/tslibs/packages/deferred/test/deferred-in-js.test.js",
    "owner": "eslint",
    "code": {
        "value": "import/no-extraneous-dependencies",
        "target": {
            "$mid": 1,
            "external": "https://github.com/import-js/eslint-plugin-import/blob/v2.26.0/docs/rules/no-extraneous-dependencies.md",
            "path": "/import-js/eslint-plugin-import/blob/v2.26.0/docs/rules/no-extraneous-dependencies.md",
            "scheme": "https",
            "authority": "github.com"
        }
    },
    "severity": 8,
    "message": "'@ts/deferred' should be listed in the project's dependencies. Run 'npm i -S @ts/deferred' to add it",
    "source": "eslint",
    "startLineNumber": 1,
    "startColumn": 22,
    "endLineNumber": 1,
    "endColumn": 45
}]
ljharb commented 2 years ago

Self-reference only works with the "exports" or "imports" field, which this plugin doesn't yet support.

You shouldn't be able to self-require the package otherwise.

jacobrask commented 2 years ago

Yarn workspaces, and I assume npm and pnpm workspaces too, installs all local packages to node_modules/package-name.

In the tests of a package I want to reference the built package. The file

packages/my-package/__tests__/my-package.test.js

imports my-package (resolving to packages/my-package/dist/index.js), which triggers import/no-extraneous-dependencies.

ljharb commented 2 years ago

@jacobrask yes, in that case it should Just Work. If it doesn't, and you can run the node repl and require.resolve(specifier) works, then please file a separate issue.

ljharb commented 2 years ago

@rhangai bundleDependencies is for publishing packages that vendor in node_modules - i'd suggest avoiding it.

ljharb commented 2 years ago

Self-referencing only works in node via the "exports" or "imports" fields, which this package (via resolve) does not yet support - but will hopefully, soon.

It should not be expected to work otherwise.

rhangai commented 2 years ago

In my case, since it is a custom rollup project, it works as expected, but I get your point, deleting my comments to prevent bad practices/habits. :+1:

rmarkins-godaddy commented 1 year ago

Self-referencing only works in node via the "exports" or "imports" fields, which this package (via resolve) does not yet support - but will hopefully, soon.

It should not be expected to work otherwise.

@ljharb Do we have an idea on when this will be added to this plugin? or waiting on someone to contribute this?

ljharb commented 1 year ago

@rmarkins-godaddy it needs to be added to resolve, and i'm working on the reverse algorithm for list-exports first to serve as test fixtures.

Since it's a best practice to make packages backwards-compatible to pre-exports node, i'd hope this feature gap is minimally impactful in the meantime.

lxsmnsyc commented 9 months ago

What's the status on this? It's already been a year and it seems to be still unsolved. I would have to guess that resolve still doesn't support it?

ljharb commented 9 months ago

@lxsmnsyc it's been 4 years since node shipped "exports", and yes, resolve still doesn't support it.

lxsmnsyc commented 9 months ago

@ljharb how is resolve different from, say, resolve.exports package? or is it possible to transition to it?

ljharb commented 9 months ago

There's a lot of functionality we'd need - in particular, we'd need the ability to lint against a particular version of node's support, and nothing in the ecosystem provides that (resolve, however, will).

No matter how much longer it takes, I'm unlikely to switch from resolve.