Skip to content

Prohibit cross-package relative imports#8238

Merged
mcmire merged 8 commits intomainfrom
prohibit-cross-package-imports
Mar 19, 2026
Merged

Prohibit cross-package relative imports#8238
mcmire merged 8 commits intomainfrom
prohibit-cross-package-imports

Conversation

@mcmire
Copy link
Contributor

@mcmire mcmire commented Mar 18, 2026

Explanation

There are some packages which import files from other packages using relative imports. This won't work in production code, and TypeScript will type the imports as any, which will damage type-safety upstream.

This commit adds an ESLint rule which checks to ensure that cross-package relative imports are not being used and suggests that the engineer directly import the package instead. There are some existing lint violations of the new rule in this repo, so this commit also instructs ESLint to suppress them (we will address them in other PRs).

References

(These problems were discovered while working on the UI messenger integration prototype)

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Repo-wide ESLint rule change that can introduce new lint failures in production packages/*/src/**/*.ts as code is touched or when suppressions drift. Functionality/runtime behavior is unchanged, but developer workflows and CI linting may be impacted.

Overview
Adds an ESLint check to forbid cross-package relative imports in non-test TypeScript sources under packages/*/src/**/*.ts via import-x/no-relative-packages.

Updates eslint-suppressions.json to temporarily suppress existing violations in a handful of files so the new rule can be enabled without immediately failing lint.

Written by Cursor Bugbot for commit c1a516e. This will update automatically on new commits. Configure here.

There are some packages which import files from other packages using
relative imports. This won't work in production code, and TypeScript
will type the imports as `any`, which will damage type-safety upstream.

This commit adds an ESLint rule which checks to ensure that
cross-package relative imports are not being used and suggests that
the engineer directly import the package instead. There are some
existing lint violations, so this commit also instructs ESLint to
suppress them.
"count": 6
}
},
"packages/assets-controller/src/__fixtures__/MockAssetControllerMessenger.ts": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these files should be in src/, they should probably be in tests/ instead, but we can address that in another PR.

{
// Prohibit relative imports that cross package boundaries in non-test files.
// This is like the `no-relative-packages` rule in the `import-x` plugin,
// but does not suggest that engineers use subpath imports that may or may
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error from the import-x plugin comes from here. For the following code:

import type { MultichainAccountServiceWalletStatusChangeEvent } from '../../multichain-account-service/src/types';

the error would be:

Relative import from another package is not allowed. Use `@metamask/multichain-account-service/src/types` instead of `../../multichain-account-service/src/types`

However, @metamask/multichain-account-service/src/types is not a valid export path, so this won't be useful for engineers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That error message isn't bad IMO. It would still lead contributors in the right direction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I guess I was a bit worried as well because if you ran yarn lint:fix and you didn't know better, no-relative-packages would apply an autofix that caused a build failure. Should something that passes lint cause a different error somewhere else? That feels strange to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, but I guess the bad path would get flagged in your IDE by a different lint rule and/or tsserver. Hmm... Maybe it would be okay to just use no-relative-packages after all.

@mcmire mcmire marked this pull request as ready for review March 18, 2026 19:41
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mcmire mcmire enabled auto-merge March 19, 2026 14:27
@mcmire mcmire added this pull request to the merge queue Mar 19, 2026
Merged via the queue into main with commit ca13ccc Mar 19, 2026
326 checks passed
@mcmire mcmire deleted the prohibit-cross-package-imports branch March 19, 2026 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants