-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
feat(@nestjs/core): execute middleware of dependent modules first. #1451 #2406
Conversation
Pull Request Test Coverage Report for Build 3231
💛 - Coveralls |
Pull Request Test Coverage Report for Build 3490
💛 - Coveralls |
packages/core/injector/module.ts
Outdated
|
||
set distance(distance: number){ | ||
this._distance = distance; | ||
this._imports.forEach((module) => module.distance = distance + 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the single module is imported by multiple modules that will override distance
value several times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public addRelatedModule(module: Module) {
this._imports.add(module);
module.distance = this._distance + 1 > module._distance ? this._distance + 1 : module._distance;
}
The single module distance will check distance when it is imported by multiple modules.If the distance less than the distance that imported module + 1, it will be updated.
I left a single comment. Could you look at it? |
Could you try running the integration tests? (more info here https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md) Lots of them are failing now (perhaps due to recursion - what if there is a circular dependency between modules?) |
Sorry, I get some errors when I run integration test, but it is no related with this, it is caused by mocha config.So I ignore it.The error with this, it is caused by scanner the test files in node_modules.
I already fix it by change glob path in mocha command.
If it is an issue, I can pull a pr fix it. |
The current code ignored circular dependency between modules. I am not an idea for this, could you give me some suggestion? |
9e95d89
to
5fd3547
Compare
Maybe we can ignore the middleware order with circular dependency between modules,only run middleware in order with normal dependency. |
5fd3547
to
fcf5042
Compare
That would make sense :) |
Because the order with circular dependency between modules is not clear, so I think ignore this sence, the order with this inited 0 and don |
Can I ask the logic behind this change?? Example: app.module b.module The execution logic said that MiddlewareA must be executed first than MiddlewareB because the app.module is loaded first than b.module. And, on the other side, app.module will have the most generic middlewares, so will be executed first, but now, we don't have an easy way to have a "generic and first" middleware. So, why is the logic behind invert the order? Or Am I losing something? |
@gelito I'm so sorry for this regression, you're totally right!! The middleware execution order should be inverted, and these modules closer to the root module (or in the root module, e.g. |
Thanks so much! I think the logic must be this in all modules; in our case, for example, we do: App.module.ts User.module.ts Cars.module.ts And so on... We think it's the more logical way, but we are always open to seeing alternatives ;-) |
So the inversion that was done was to revert it to the original order? as changing the execution of middleware is a breaking change in my mind. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: 1451
What is the new behavior?
Does this PR introduce a breaking change?
Other information