-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
Application crash when using barrel files (multi index.ts) #1181
Comments
I would suggest updating all modules, there are no breaking changes between 5.0.1 and latest version. |
@kamilmysliwiec I think, the problem is not there. It is something really weird. Probably it is not directly related to the NestJS. I'll post it on the TS github.. But maybe somebody here can suggest where to look at... What I've noticed is that if I debug my code, some ts imports are And what is even more interesting, that in some cases they are undefined, in others - the same imports are defined. 😄 let's say I have this: // accounts/index.ts
export * from './interfaces';
export * from './bindings';
export * from './schemas';
export * from './dto';
export { AccountsModule } from './accounts.module';
export { AccountsService } from './accounts.service'; import { Module } from '@nestjs/common';
import { DatabaseModule } from '../database';
import { MailModule } from '../mail';
import { invitationModelProvider } from './providers';
import { AccountsModule } from '../accounts';
console.log(AccountsModule, MailModule);
@Module({
imports: [
DatabaseModule.forRoot([invitationModelProvider]),
AccountsModule,
MailModule,
]
})
export class InvitationsModule {} it logs
but if I change it to it logs but have similar issues in some And I can't see any pattern in the bahaviour. E.g. a test for some module A can be green, but when testing a module B that depends on A, the imported A is undefined. I'm confused. Not sure where to look at. We didn't change the ts configuration or anything like that. But several days ago it started failing. Maybe some package dependencies resolved to the later versions and that caused all problems. Trying to figure out... |
You need to wrap the import { Module, forwardRef } from '@nestjs/common';
import { DatabaseModule } from '../database';
import { MailModule } from '../mail';
import { invitationModelProvider } from './providers';
import { AccountsModule } from '../accounts';
@Module({
imports: [
DatabaseModule.forRoot([invitationModelProvider]),
forwardRef(() => AccountsModule), // <-- here
MailModule,
]
})
export class InvitationsModule {} Circular dependencies are the reason why the imports are |
No, there're no circular dependencies. I know about Please check the prev. comment. The undefined is the ts import, not the NestJS import. |
@marcus-sa look, import { Module } from '@nestjs/common';
import { DatabaseModule } from '../database';
import { MailModule } from '../mail';
import { invitationModelProvider } from './providers';
import { AccountsModule } from '../accounts';
console.log(AccountsModule, MailModule);
@Module({
imports: [
DatabaseModule.forRoot([invitationModelProvider]),
AccountsModule,
MailModule,
]
})
export class InvitationsModule {} See, where the it has nothing to do with It is undefined. Then it goes as undefined to the nest's Then, eventually, Where am I wrong? Why did you downvote my prev comment? |
@marcus-sa
Because the function that was passed to Understand? |
Maybe you should provide a repository to that we can try to reproduce the error. |
tried to update. No difference |
Yep, sure. If I don't find the issue otherwise. |
It's the weirdest thing I've ever seen. I have a common test utils module // some-path/utils/index.ts
export { createRandomId } from './create-random-id';
export { createUser } from './create-user';
export { createUsers } from './create-users';
export { createAccessToken } from './create-access-token';
export { createDefaultAccessToken } from './create-default-access-token';
export { findAllUsers } from './find-all-users';
export { findOneUser } from './find-one-user';
export { createAccount } from './create-account';
export { inviteUser } from './invite-user';
export { findOneRole } from './find-one-role'; apparently, one of such functions caused this issue. all async function findOneInvitation(
fixturesService: MongooseFixturesService,
filters: { _id: string } | { email: string; account: string }
): Promise<Invitation> {
return await fixturesService.findOne(InvitationsBindings.MODEL_NAME, filters);
} And all works fine until I've added another one. How is that possible... |
Even more interesting, when I just moved the function from the separate file in |
So in the end, nothing has changed... I didn't remove/change the code. I've just moved 2 functions from separate files to the test file body. That's it! And of course, the imports in the test file changed: /* <...> */
import {
createAccessToken,
createAccount,
createUser,
// findOneInvitation, <-- was here when I had an exception
// findOneAccount, <-- was here when I had an exception
E2ETestingContainer,
E2ETestingModulesFactory,
findOneRole,
findOneUser,
FixtureLoader,
FixtureLoaderConfig,
JEST_E2E_TIMEOUT,
MongooseFixturesService,
} from '../../testing';
/* <...> */ I think we can close the issue if you want. |
Why do you think it might be an issue? I remember in the Angular community it was a common pattern. They called it "barrels". |
Seems like a dublicate of #825. I can confirm @alexandr2110pro behavior. I am absolutely clueless why this happens. It only occuree to me inside a Nest application context, which is super strange. I think we should track this issue somewhere and do not close it again. Maybe it is a bug of TypeScript or maybe of Nest. Nonetheless we should track it |
As far as I know, barrels are no longer recommended due to this issue. It is nothing related to nest itself |
But now it's closed 😃 |
I have changed the issue title to make this thread more descriptive and easier to find by other people. Anyway, I don't think that we have to track this, this error is not related to Nest itself. I would suggest omitting barrel files when it comes to module's providers/module class OR using them very carefully (for example, even if you create a barrel, you shouldn't use it from within the same module itself [only from outside] and when circular dependency may potentially appear). |
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. |
I'm submitting a...
Current behavior
Having
where
ExternalAuthModule
andMailModule
have no module imports,I have this:
which leads to
Expected behavior
It should work
Minimal reproduction of the problem with instructions
Not sure. We've got nearly 50 modules and all are working fine.
And most of them actually use
AuthModule
as you might guess.Only this one, that I've created recently can't load. Any ideas on what am I doing wrong? :)
What is the motivation / use case for changing the behavior?
Environment
The text was updated successfully, but these errors were encountered: