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

Add external imports whitelist logic to nx-enforce-module-boundaries (i.e. allowedExternalImports option) #12870

Closed
1 of 3 tasks
yjaaidi opened this issue Oct 28, 2022 · 9 comments
Assignees
Labels
outdated scope: linter Issues related to Eslint support in Nx type: feature

Comments

@yjaaidi
Copy link
Contributor

yjaaidi commented Oct 28, 2022

Description

nx-enforce-module-boundaries should allow a deny-all + whitelist logic in addition to the blacklist logic.

Motivation

There are use cases where users might need more restrictive rules and only whitelist some external imports instead of explicitly banning them.

Here is one, when implementing something similar hexagonal, onion, or clean architecture, it is safer to use whitelists.
For instance, a library tagged as "type:core" will not be able to import anything using something like this allowedExternalImports: [], while a "platform:backend" & "type:data-access" will be allowed to use libraries that can communicate with databases or remote services: allowedExternalImports: ['mongo', '@google-cloud/*', ...] or something like this.

Suggested Implementation

We could implement this using an allowedExternalImports or whitelistedExternalImports option.

  • allowedExternalImports is exclusive with bannedExternalImports. We can't use both on the same rule.
  • Nice to have: It would be nice to figure out a way of combining rules. If the project matches multiple rules, then the allowed imports should be merged together

Alternate Implementations

Meanwhile, here is the workaround:

"bannedExternalImports": [
    "((?!(@lib-a/*|@lib-b/something|lib-c)).)+"
 ]

Special thanks @meeroslav for the hint 😉

cf. #6727 (comment)
cc. @mlebarron

@yjaaidi yjaaidi changed the title Add allowedExternalImports option to nx-enforce-module-boundaries Add external imports whitelist logic to nx-enforce-module-boundaries (i.e. allowedExternalImports option) Oct 28, 2022
@meeroslav
Copy link
Contributor

Thanks @yjaaidi,

After thinking more about this, I think it makes sense. Despite having the possibility to use regex, I think that would be too complex for most folks. We need something that is simple and easy to read, and allowedExternalImports would be exactly that.

Please note that bannedExternalImports has pairing banTransitiveDependencies that toggles the check on transitive dependencies. We could reuse the same for allowedExternalImports to perform checks on nested projects.

@meeroslav
Copy link
Contributor

Feel free to implement this feature and ping me if you need any help.

@meeroslav meeroslav added the scope: linter Issues related to Eslint support in Nx label Oct 28, 2022
@yjaaidi
Copy link
Contributor Author

yjaaidi commented Oct 28, 2022

Cool! I'll submit a PR as soon as I can... expect some questions 😊

First one is about your remark:

Please note that bannedExternalImports has pairing banTransitiveDependencies that toggles the check on transitive dependencies. We could reuse the same for allowedExternalImports to perform checks on nested projects.

Is this related to checkNestedExternalImports?
I don't really get why we would go through nested projects as we are just whitelisting direct external imports. I think that I am missing something.

@meeroslav
Copy link
Contributor

I don't really get why we would go through nested projects as we are just whitelisting direct external imports. I think that I am missing something.

True, it doesn't matter in the opposite direction. Sorry for the confusion

@meeroslav
Copy link
Contributor

Hey @yjaaidi, do you plan to work on this?

@yjaaidi
Copy link
Contributor Author

yjaaidi commented Dec 9, 2022

Hey @meeroslav! Yes ! Sorry that I couldn't take care of it before. I'll take care of by next week.

yjaaidi added a commit to yjaaidi/nx that referenced this issue Dec 17, 2022
eslint's enforce-module-boundaries'
allowedExternalImports option will only allow
the project to import npm packages
matching the string or wildcard

nrwl#12870
yjaaidi added a commit to yjaaidi/nx that referenced this issue Dec 17, 2022
eslint's enforce-module-boundaries'
allowedExternalImports option will only allow
the project to import npm packages
matching the string or wildcard

Closes nrwl#12870
yjaaidi added a commit to yjaaidi/nx that referenced this issue Dec 17, 2022
@yjaaidi
Copy link
Contributor Author

yjaaidi commented Dec 17, 2022

Hey @meeroslav! I finally managed to take some time and send a PR #13891 🎉

I was about to send a distinct PR for the docs as I thought that this would require a major rewrite of the "Ban External Imports" recipe but I managed to just add a chapter at the end with some examples.

Let me know what you think of this and if you want me to squash everything.

Good job for the allSourceTags btw 😉

@meeroslav
Copy link
Contributor

Implemented with #13891

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: linter Issues related to Eslint support in Nx type: feature
Projects
None yet
Development

No branches or pull requests

2 participants