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

Move ctor param info from global map to metadata. Fixes #161 #204

Closed
wants to merge 1 commit into from

Conversation

florian-g2
Copy link

@florian-g2 florian-g2 commented Jun 28, 2022

Hi there.

This PR moves the paramInfos acquired by the injectable() decorator from the global typeInfo map to constructor/class metadata.

That fixes a TypeInfo not known for ... bug occurring in an environment where a library with injectable dependencies is linked using symlinks (npm-link).

In this special environment, globals are not shared between the library and the main project, therefore the typeInfos populated by the injectable() decorator weren't accessible before to the main project.

Due to this unfavorable side-effect of the npm-link, the registry() and autoInjectable() decorators will still be broken because their functionalities require direct access to the global container instance.

Minimal repo reproducing the issue in the initial commit: https://github.com/florian-g2/npm-link-missing-globals

@ghost
Copy link

ghost commented Jun 28, 2022

CLA assistant check
All CLA requirements met.

@florian-g2
Copy link
Author

Another important note to take here is that no reflection polyfill is allowed to be imported inside the linked library.
This would lead to the same fact that the polyfills also use a global map to store the metadata.

If reflection methods are required by the library itself, the types can be declared like in https://github.com/rbuckton/reflect-metadata/blob/master/Reflect.d.ts

@florian-g2
Copy link
Author

Hey @Xapphire13 @MeltingMosaic, any chances this could get merged?

@endurance
Copy link

Definitely would like to get this merged in.

@risen228
Copy link

Added this to tsyringe-neo

@kapitanluffy
Copy link

Any news on this?

@florian-g2
Copy link
Author

Well, actually, two years in I now know the true root cause for this issue.
With a npm link, node ends up using two copies of the tsyringe library.
In my case, one in ./shared-logic/node_modules/tsyringe and one in ./backend/node_modules/tsyringe.
At runtime, booth resolved modules have completely independent (global) variables.

My original PR here removed the global from the tsyringe library, and implicitly used the global of the reflection polyfill.
This is a wrong approach and in turn only moved the same problem over to the reflection polyfill.

Possibly this could be resolved with the globalThis variable, maybe this is shared across all modules at runtime?
I might look into getting another PR ready to fix this, but then in tsyringe-neo.
This repo is no more maintained for quite a while now.

In the mean time, you could workaround this by linking the tsyringe dependency from the main project into the library.
In my case:

shared-logic$ npm link ../backend/node_modules/tsyringe

Now booth projects point to the same tsyringe dependency, the one in ./backend/node_modules/tsryinge.

Hope this helps! 😊

@kapitanluffy
Copy link

Thank you for keeping it alive @risen228 @florian-g2

Just an fyi,I opened an issue (#248) regarding the state of the repo and was fortunate enough to receive a response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants