Skip to content

global module code that is required is executed twice #430

Open
@luxzeitlos

Description

@luxzeitlos

This is a rather complex problem, and I've spend hours debugging it. But basically the problem is, that if a dependency is requireing a file in the app folder, this file will get executed twice, with a different context, and so this will also break equality.

I've encountered the problem while trying to integrate TypeORM. Basically you specify a glob pattern for TypeORM to find the entities, and then it will call require on them. This will now execute a decorator on each entity (because the decorator is executed when the class is declared) which writes metadata to TypeORM and saves it on the global object.
Now when importing (not requireing) this entity from within a denali action, this will execute the same file again. This means that a === check done by TypeORM on the class saved to the global object and the class now avaliable in my denali action will fail. This is actually logically because we now have two identical classes, but they are not the same!

I'm also not sure if one of the classes is from the splash-server.bundle.js file and one not.

I'm also not 100% sure if this can be fixed by denali. It would mean that mean that the loader may never call the native require, but instead shim it over all dependencies, and if it ever encounters a path inside the app module inject its non-standard behaviour. The only alternative I see is not to use a custom loader at all.

I think for my TypeORM integration I can tweak around this, but if this behaviour remains, it should be a big red warning somewhere in the docs. Also it could be possible, for debug purpose only, to inject some kind of check into every file. Something like this allows to detect a double-load:

{
  const fileSymbol = Symbol.for('/app/models/bar');
  if(global[fileSymbol]) {
    throw 'double loading file';
  }
  global[fileSymbol] = true;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions