eslint-plugin-import: import/no-relative-packages fix breaks in monorepo

After updating my eslint config and running it on a yarn workspaces monorepo, I found that a new rule was added: import/no-relative-packages. I like the rule as it prevents someone from relatively importing something from another package.

But in my case, I have some test utils in a folder at the root of the monorepo which is just a folder and not a namespaced package. I’m currently importing them (relatively) only from some Jest tests. I’m not sure how the fix works, but in this case it seems to be simply assigning the name of the root folder for the project as the absolute path base like:

../../../testConfig/utils (import path before)

npm-library/testConfig/utils (after the fix)

The problem is it doesn’t work since Node can’t resolve ‘npm-library’. Perhaps it does work when fixing a relative import from a namespaced package but I haven’t tried.

If there’s not way to resolve this automatically, maybe it be a good idea to add an exception to the configuration (like a regex/glob pattern to disable for certain files/directories).

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Thanks, I’ll keep it open, this seems like a pretty important bug to fix.

Thanks for the quick reply! npm-library (not the most creative name I admit) is actually the name of the monorepo, so that’s the top parent. It’s also the name the fix used as the parent folder for the absolute pathname.

So npm-library doesn’t live in the monorepo, it is the monorepo. The packages/apps are in a packages directory, and the tests are within the individual packages. Right under the parent (npm-library) and as a sibling to the package directory is a folder that contains the test configs, which I want to link to from the tests.

This could perhaps be a WSL2 issue because it doesn’t always handle symlinks correctly. But a lot of people are on WSL2.

If this isn’t enough info, it’ll be at least a month from now, but at that point I’ll have time to post a repro. So feel free to keep open or closed as you think is best 👍