nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Require database dependency not working with yarn pnp + workspaces

lucasheim opened this issue · comments

I'm submitting a...

[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When running a nest application using @nestjs/typeorm, it doesn't find the driver dependency if the application is inside a yarn workspace.

Expected behavior

No difference between running the app standalone or inside a yarn workspace

Minimal reproduction of the problem with instructions

I created this repo: https://github.com/lucasheim/nest_typeorm_yarn_V2/

In the main branch you can find a simple app which I generated using nest new. The only thing I changed it was adding database packages:

yarn add typeorm @nestjs/typeorm hdb-pool @sap/hana-client

If you run the application you'll get a timeout problem, which is ok, it means it tried to reach the database and could not find it.

In the yarn-pnp branch, I converted the app to yarn pnp behavior, in which dependencies are hosted at .yarn/cache instead of node_modules, by doing

yarn set version berry && yarn install

Here if you try to run the app we already have a bug, but it's not a typeorm one, so I'll just report to the other repository. It's missing one dependency.

In the yarn-pnp-source-map branch, I added the missing dependency.

yarn add source-map-support --dev

If you run the app, it works properly, you should get the same timeout error because there's no database running.

In the yarn-pnp-source-map-workspaces, I moved my app so it's part of a yarn workspace. So I just moved the whole app to packages/pkgA and created a root package that declares the workspaces:

"workspaces": {
    "packages": [
      "packages/*"
    ]
  }

After that I ran yarn install again to update .pnp.cjs and the lock file, but now when running the app, it fails, not finding the drivers (hdb-pool for HANA), even though they are already installed.

DriverPackageNotInstalledError: SAP Hana package has not been found installed. Try to install it: npm install hdb-pool --save
    at DriverPackageNotInstalledError.TypeORMError [as constructor] (<folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/__virtual__/typeorm-virtual-834c3f6e26/0/cache/typeorm-npm-0.2.37-50e2579456-881839ff3a.zip/src/error/TypeORMError.ts:7:9)
    at new DriverPackageNotInstalledError (<folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/__virtual__/typeorm-virtual-834c3f6e26/0/cache/typeorm-npm-0.2.37-50e2579456-881839ff3a.zip/src/error/DriverPackageNotInstalledError.ts:8:9)
    at SapDriver.loadDependencies (<folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/__virtual__/typeorm-virtual-834c3f6e26/0/cache/typeorm-npm-0.2.37-50e2579456-881839ff3a.zip/src/driver/sap/SapDriver.ts:727:19)
    at new SapDriver (<folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/__virtual__/typeorm-virtual-834c3f6e26/0/cache/typeorm-npm-0.2.37-50e2579456-881839ff3a.zip/src/driver/sap/SapDriver.ts:211:14)
    at DriverFactory.create (<folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/__virtual__/typeorm-virtual-834c3f6e26/0/cache/typeorm-npm-0.2.37-50e2579456-881839ff3a.zip/src/driver/DriverFactory.ts:40:24)
    at new Connection (<folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/__virtual__/typeorm-virtual-834c3f6e26/0/cache/typeorm-npm-0.2.37-50e2579456-881839ff3a.zip/src/connection/Connection.ts:122:43)
    at ConnectionManager.create (<folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/__virtual__/typeorm-virtual-834c3f6e26/0/cache/typeorm-npm-0.2.37-50e2579456-881839ff3a.zip/src/connection/ConnectionManager.ts:61:28)
    at <folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/__virtual__/typeorm-virtual-834c3f6e26/0/cache/typeorm-npm-0.2.37-50e2579456-881839ff3a.zip/src/globals.ts:77:35
    at step (<folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/cache/tslib-npm-2.3.1-0e21e18015-de17a98d46.zip/node_modules/tslib/tslib.js:143:27)
    at Object.next (<folder>/typeorm_yarn_bug/typeorm-yarn-bug/.yarn/cache/tslib-npm-2.3.1-0e21e18015-de17a98d46.zip/node_modules/tslib/tslib.js:124:57)

It seems like if the package is running inside a workspace, the driver dependency specifically cannot be find. You can check that other dependencies are resolved correctly because yarn build works and if I remove the database, the app also works properly.

What is the motivation / use case for changing the behavior?

If that's not fixed, it's not possible to use nestjs applications inside yarn pnp with workspaces.

Environment


Nest version: 8.0.6

 
For Tooling issues:
- Node version: 16.8.0  
- Platform:  Mac

Others:

yarn version 3.0.2

Pinging @arcanis as the yarn documentation says to do it in pnp related issues. I know this may not be directly unlisted dependencies, but maybe it's related (?)

Reading your post, I have the suspicion that the no-workspace version works because of our legacy fallback for missing dependencies.

Most likely, typeorm or something else requires optional package to see if it should enable an optional integration, and didn't list them as proper deps. As a result, Yarn rejects the resolution once the fallback doesn't apply.

The fix is probably to list those optional packages as optional peer deps, using peerDependenciesMeta.

You are absolutely correct @arcanis, when setting pnpFallbackMode: none in branch "yarn-pnp-source-map", the error already happens, even without workspaces. But also typeorm already sets these as peerDependencies

Did some digging and at the end the problem is that package hdb-pool requires @sap/hana-client but it does not disclose it as a dependency, just a devDependency, so everything is ok with typeorm itself. Creating an issue in hdb-pool.