microsoft / tsyringe

Lightweight dependency injection container for JavaScript/TypeScript
MIT License
5.16k stars 173 forks source link

TypeInfo not known for "" when use symlinks (i.e. with npm link) #161

Open bromzh opened 3 years ago

bromzh commented 3 years ago

Describe the bug

When I use injectable services (via @injectable decorator) in lib and try to link that lib to my app, I got an error. But when I use same lib directly in app (via npm install, or just by copying builded lib in app's node_modules for example), error not appears.

To Reproduce

I made a repo with lib and app that uses lib. NOTE! Please use bash or zsh while reproducing. I not so good in using windows cmd or powershell, so some commands wouldn't works in windows as planned.

Prepearing

git clone https://github.com/bromzh/tsyringe-npm-link-issue.git
  1. Install lib dependencies and build library
    cd library
    npm i
    npm run build
  2. Link library
    npm link
  3. Install app dependencies and link library to app
    cd ../simple-app
    npm i
    npm link @my/library

A symlink "@my/library" must appears in app's node_modules after this steps.

Building and running app Then we'll trying to build our app:

npm run build

Build must be successful if you did all right. After build, we'll trying to run our app:

npm run start

And we got error with these reason:

TypeInfo not known for "ApiService"

BUT! If we publish our lib to repo and install it in app or just copy builded lib to app, all things will be fine.

Use library without symlinks

Copy library directly in app

# work in simple_app directory
rm -rf node_modules/@my
mkdir node_modules/@my
mkdir node_modules/@my/library
cp ../library/package.json node_modules/@my/library
cp -r ../library/build node_modules/@my/library

And then repeat build and run:

npm run build
npm run start

Now all works fine!

Expected behavior

An error TypeInfo not known for ... shouldn't appears when I use npm link. Both methods - npm link lib and npm install lib - must works well.

Version: ^4.5.0

monarchwadia commented 3 years ago

@bromzh do you have a workaround here? I've tried downgrading tsyringe to 4.4.0, 4.0.0 and 3.4.0, but to no avail.

I just ran across this issue myself. I'm using yarn workspaces, svelte and tsdx in a monorepo. Not sure why this is happening.

I've made my repo public so you folks can triage. https://github.com/monarchwadia/bootcamp-tycoon

Repro steps:

  1. git checkout f738e6f85cb0842341e4043bd0f162860574430f to go to the broken hash
  2. yarn
  3. cd packages/core && test It should pass all tests
  4. cd packages/web && yarn dev And then visit localhost:3000

Expected behaviour: It should resolve all dependencies Actual behaviour: Getting TypeInfo not known for...

monarchwadia commented 3 years ago

Linked repo seems to be working in Inversify. Leaving it here in case you guys could make use of it, you can see it working in commit 0b0e2afc162e42ccbccad053f2a40de340f3f4f1 on my repo.

florian-g2 commented 1 month ago

See: https://github.com/microsoft/tsyringe/pull/204#issuecomment-2401656829