dependency-cruiser
dependency-cruiser copied to clipboard
Monorepo: Unexpected Error: Unusal baseDir passed to package reading function:
When setting combinedDependencies
to true
for my projects in a monorepo, and I run npm run deps:check --workspace apps/users
from my monorepo root (which runs deps:check for just the one workspace) I get an error. When I remove the setting, I get no-non-package-json
errors as expected since the child package.json file has no deps specified.
Expected Behavior
dep cruiser properly resolves the packages from my monorepo root.
Current Behavior
I get this error:
> [email protected] deps:check
> depcruise --validate .dependency-cruiser.json src
ERROR: Extracting dependencies ran afoul of...
Unexpected Error: Unusal baseDir passed to package reading function: '/home/jubairsaidi/projects/openbay/openbay-stack/apps/users'
Please file a bug: https://github.com/sverweij/dependency-cruiser/issues/new?template=bug-report.md&title=Unexpected Error: Unusal baseDir passed to package reading function: '/abs/path/to/apps/users'
... in ../../node_modules/express/index.js
Possible Solution
Steps to Reproduce (for bugs)
create a project with the following structure:
- monodash/
- package.json
- tscofnig.json
- apps/
- users/
- package.json (with no deps)
- .dependency-cruiser.json
- tsconfig.build.ts
- src/
- index.ts
- users/
in your monodash/apps/users/src/index.ts
file, add an import that will cause dependency cruiser to fail if run with combinedDependencies
to false
and pass if it's set to true
.
contents of monodash/tsconfig.json
:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"noUnusedLocals": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2019",
"esnext.asynciterable"
],
"target": "es2019",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
},
"exclude": [
"node_modules",
"dist"
]
}
contents of monodash/apps/users/tsconfig.build.json
:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"declaration": false,
"outDir": "../../dist/apps/users"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist",
"test",
"**/*spec.ts"
]
}
add the following to your monodash/package.json
file:
"workspaces": [
"apps/*"
],
add the following options to your monodash/apps/users/.dependency-cruiser.json
file:
"options": {
"combinedDependencies": true,
"doNotFollow": {
"dependencyTypes": [
"npm",
"npm-dev",
"npm-optional",
"npm-peer",
"npm-bundled",
"npm-no-pkg"
]
},
"tsPreCompilationDeps": true,
"tsConfig": {
"fileName": "./tsconfig.build.json"
}
}
add the following command to your monodash/apps/users/package.json
file:
"deps:check": "depcruise --validate .dependency-cruiser.json src",
then from your project root run:
npm run deps:check --workspace apps/users
Context
Your Environment
- Version used: ^11.4.1
- Node version: v16.14.1
- Operating System and version: Ubuntu 20
- Link to your project: private
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
hi @jubairsaidi thanks for raising this bug.
My guess is that this is because the dependency is found 'above' the folder dependency-cruiser is run from. I'll have to ponder a solution for that.
@sverweij are dependencies that are part of the workspace supposed to be detected and not trigger no-non-package-json
?
@danielo515 (completely missed your question for some reason - hope the answer is still relevant for you)
The answer is yes - no-non-package-json
is supposed to take declarations in package.jsons 'up' from modules in the mono repo into account - using the same resolution mechanism nodejs has.
Two caveats:
- ~at the moment dependency-cruiser does not look higher than its working directory. This should not be an issue when you run it from the root of the mono-repo. (as said above I'm looking for a way to remove even that restriction)~ (this now works).
- the combinedDependencies option should be set on
true
(if you set up dependency-cruiser withdepcruise --init
this should already be the case).
Thanks for the clarification
@sverweij I'm trying to use the combinedDependencies
option, but I can't run the depcruise
from monorepo root as all paths in tsconfig.json
files are relative. So I'm running it from each project in the monorepo instead.
There are two issues still (I'm running 12.6.0
now):
- even with the
combinedDependencies: true
, the rootpackage.json
is not being considered: I getno-non-package-json
for dependencies listed there. - the
--ts-config
option does not read the"extends": "@my-workspace/tsconfig/tsconfig.base.json"
, the file installed into the rootnode_modules
, which contains"paths"
. So I get thenot-to-unresolvable
for imports resolved bytsc
I've tracked the second problem to https://github.com/dividab/tsconfig-paths/issues/236
What I'd propose is to add a config option to mark the monorepo root for the purpose of finding package.json
files. And probably to set it by default to the dirname
of the root .dependency-cruiser.js
(in case of chaining via extends
).
This way a depcruise
process started at monorepo/packages/some-package
would resolve relative files using local tsconfig.json
, but would use dependencies described in both monorepo/packages/some-package/package.json
up to the monorepo/package.json