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

Having trouble with import/no-extraneous-dependencies rule with monorepo using yarn v1 workspaces #2709

Closed
kepeterson opened this issue Feb 7, 2023 · 6 comments

Comments

@kepeterson
Copy link

Hello eslint-plugin-import team,

I'm working on a monorepo using yarn v1 with a package structure like the diagram below. The root index.js file imports a single export from packages/module-a/index.js. This import generates a import/no-extraneous-dependencies error with my current config.

From reading issues here #1174 and here #1650 I see there is a packageDir option to handle monorepos but as far as I can tell it is not the right choice for my case where a package.json that defines a workspaces property uses one of the modules in workspaces itself.

When I do add a file:./packages/module-a dependency to my package.json, the eslint error is removed but my app behaves differently because yarn has now cached the current version of module-a in node_modules as of yarn install instead of having a simple symlink. This means my changes to packages/module-a/index.js changing the exported value are not reflected when I run the node index.js command from root. This is not the behavior I want, so adding a file:.. dependency to make this rule happy is not a good option.

Is it possible to get import/no-extraneous-dependencies working with this case? Since the module is coming from a folder in workspaces, the dependency is not truly extraneous ie it is defined in the package.json. It just isn't in dependencies et al. I see that later versions of yarn have a workspace: type dependency which might help out here, do I need to upgrade to a later version of yarn perhaps?

root
+- package.json
+- index.js
+- packages
  +- module-a
  |  +- package.json
  |  +- index.js

I have put together a minimal repro of the problem here in a standalone repo https://github.com/kepeterson/no-extraneous-with-workspaces

image

.eslintrc.js

'use strict';

module.exports = {
  extends: [
    'eslint:recommended',
    'plugin:import/recommended',
  ],
  env: {
    node: true
  },
  plugins: ['import'],
  root: true,
  rules: {
    'import/no-extraneous-dependencies': 'error',
  }
};
@Stefanama
Copy link

@kepeterson 👋 same folder structure and I had the same issue happening after upgrading to anything above 2.22.1, didn't have the error before. I'm sure I'm missing something but for now I'll stay on 2.22.1.
Another small note, in the rule I had to specify the packageDir to "./" otherwise it wouldn't work 🙂

@zhl0791

This comment was marked as spam.

@marcospgp
Copy link

Is there an update on this? I am seeing the same issue even though #1174 has been closed.

@nazarhussain
Copy link

Is there an update on this? I am seeing the same issue even though #1174 has been closed.

Yes it's the same for me.

@nazarhussain
Copy link

To me it was resolved with following package eslint-import-resolver-typescript with following settings.

settings: {
    "import/resolver": {
      typescript: {
        project: "packages/*/tsconfig.json",
      },
    }
}

@ljharb
Copy link
Member

ljharb commented Jul 12, 2023

That seems to answer it.

@kepeterson if not, i'm happy to reopen.

@ljharb ljharb closed this as completed Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants