dependency-cruiser icon indicating copy to clipboard operation
dependency-cruiser copied to clipboard

Monorepo: Unexpected Error: Unusal baseDir passed to package reading function:

Open ziazon opened this issue 2 years ago • 8 comments

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

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

ziazon avatar Mar 20 '22 20:03 ziazon

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

github-actions[bot] avatar Mar 28 '22 03:03 github-actions[bot]

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 avatar Mar 29 '22 17:03 sverweij

@sverweij are dependencies that are part of the workspace supposed to be detected and not trigger no-non-package-json ?

danielo515 avatar Dec 14 '22 11:12 danielo515

@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 with depcruise --init this should already be the case).

sverweij avatar Dec 25 '22 17:12 sverweij

Thanks for the clarification

danielo515 avatar Dec 25 '22 19:12 danielo515

@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 root package.json is not being considered: I get no-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 root node_modules, which contains "paths". So I get the not-to-unresolvable for imports resolved by tsc

allista avatar Jan 23 '23 14:01 allista

I've tracked the second problem to https://github.com/dividab/tsconfig-paths/issues/236

allista avatar Jan 23 '23 16:01 allista

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

allista avatar Jan 23 '23 16:01 allista