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

[code-infra] Refactor eslint import/no-cycle rule #42705

Merged
merged 8 commits into from
Jun 26, 2024

Conversation

LukasTy
Copy link
Member

@LukasTy LukasTy commented Jun 21, 2024

Discussed here: https://mui-org.slack.com/archives/C042YB5RB3N/p1718977920175249

  • Invert the ignoreExternal flag as it seems to improve performance
  • change rule files path to also target internal packages (i.e. api-docs-builder)
  • refactor the api-docs-builder package typing structure to avoid cyclic imports
    • this introduces a BC: './ApiBuilders/ComponentApiBuilder' and './ApiBuilders/HookApiBuilder' no longer export ReactApi that needs to be aliased on the user side to make them unique

@LukasTy LukasTy added the scope: code-infra Specific to the core-infra product label Jun 21, 2024
@LukasTy LukasTy self-assigned this Jun 21, 2024
@mui-bot
Copy link

mui-bot commented Jun 21, 2024

Netlify deploy preview

https://deploy-preview-42705--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against a3971ca

@LukasTy LukasTy requested a review from a team June 21, 2024 18:13
@Janpot
Copy link
Member

Janpot commented Jun 24, 2024

  • Invert the ignoreExternal flag as it seems to improve performance

How is that possible? The docs specifically say:

Its value is false by default, but can be set to true for reducing total project lint time, if needed.


In case it's just types that create cyclic dependencies: As far as I know import/no-cycle ignores type imports. The following should work:

import { foo } from './cyclic-dep'
// error

import type { Foo } from './cyclic-dep'
// no error

This could make a refactor simpler and avoid introducing BCs

@LukasTy
Copy link
Member Author

LukasTy commented Jun 26, 2024

How is that possible? The docs specifically say:

There is more information in the linked Slack message and the following discussion.
TLDR: There seems to be this disconnect between the intent and actual behavior as noted by other users.

In case it's just types that create cyclic dependencies: As far as I know import/no-cycle ignores type imports. The following should work:

Yes, you are correct on that, but do you think that the BC is difficult and should be avoided?
IMHO, it doesn't seem that scary, and, IMHO, the current solution of using the same name type and having the need to alias it seems a bit inefficient. 🙈 🤷

@Janpot
Copy link
Member

Janpot commented Jun 26, 2024

but do you think that the BC is difficult and should be avoided?

Not at all, I'm just mentioning as I've seen devs jump through hoops to avoid cyclic dependencies, and there really isn't much benefit to it when it's about types. If the change results in higher quality code and the refactors in dependent projects are low effort then I'm all for it.

@@ -436,7 +436,7 @@ module.exports = {
],
},
],
'import/no-cycle': ['error', { ignoreExternal: true }],
'import/no-cycle': ['error', { ignoreExternal: false }],
Copy link
Member

@Janpot Janpot Jun 26, 2024

Choose a reason for hiding this comment

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

nit: If the default is false, maybe it makes sense to just remove the options altogether?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a todo comment regarding this: 5e32a4c
Ideally, it should be true and hopefully it will be fixed at some point. 🤔
WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

I think it depends on the impact of import-js/eslint-plugin-import#2998
If after this change the difference between true and false is marginal, it could stay on false.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a mention of this PR in the comment for more context.

Copy link
Member

@Janpot Janpot left a comment

Choose a reason for hiding this comment

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

Looking good 👍

@@ -436,7 +436,9 @@ module.exports = {
],
},
],
'import/no-cycle': ['error', { ignoreExternal: true }],
// TODO: Consider setting back to `ignoreExternal: true` when the expected behavior is fixed:
Copy link
Member

Choose a reason for hiding this comment

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

But do we want it to be true? Or was it only at true because we thought back then it would improve performance?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, as you mentioned, and they specify in the readme:

Its value is false by default, but can be set to true for reducing total project lint time, if needed.

@LukasTy
Copy link
Member Author

LukasTy commented Jun 26, 2024

I've also reverted the path change to no avoid targetting internal packages: ed2ea49
based on https://mui-org.slack.com/archives/C042YB5RB3N/p1719043409683339?thread_ts=1718977920.175249&cid=C042YB5RB3N.
@Janpot do you agree with it?

@Janpot
Copy link
Member

Janpot commented Jun 26, 2024

@Janpot do you agree with it?

If the rule passes, and it's not adding significant overhead them I don't see why we wouldn't enable it on internal packages as well. I'm also fine to keep the main focus on improving the performance in this effort.

@LukasTy
Copy link
Member Author

LukasTy commented Jun 26, 2024

If the rule passes, and it's not adding significant overhead them I don't see why we wouldn't enable it on internal packages as well. I'm also fine to keep the main focus on improving the performance in this effort.

There is a performance difference. It's not a huge one, but it's significant enough to keep the existing rule path. 👌

.eslintrc.js Show resolved Hide resolved
Signed-off-by: Lukas <llukas.tyla@gmail.com>
.eslintrc.js Outdated Show resolved Hide resolved
Signed-off-by: Lukas <llukas.tyla@gmail.com>
@LukasTy LukasTy enabled auto-merge (squash) June 26, 2024 10:35
@LukasTy LukasTy merged commit 786c82f into mui:next Jun 26, 2024
19 checks passed
@LukasTy LukasTy deleted the refactor-import-no-cycle branch June 26, 2024 10:53
joserodolfofreitas pushed a commit to joserodolfofreitas/material-ui that referenced this pull request Jul 29, 2024
Signed-off-by: Lukas <llukas.tyla@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: code-infra Specific to the core-infra product
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants