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

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

Open
ziazon opened this issue Mar 20, 2022 · 8 comments
Labels
bug something is not working as expected

Comments

@ziazon
Copy link

ziazon commented Mar 20, 2022

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:

> users@0.0.1 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
@github-actions
Copy link

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 github-actions bot added the stale label Mar 28, 2022
@sverweij sverweij removed the stale label Mar 29, 2022
@sverweij
Copy link
Owner

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 sverweij added the bug something is not working as expected label Mar 29, 2022
@danielo515
Copy link
Contributor

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

@sverweij
Copy link
Owner

sverweij commented Dec 25, 2022

@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).

@danielo515
Copy link
Contributor

Thanks for the clarification

@allista
Copy link

allista commented Jan 23, 2023

@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
Copy link

allista commented Jan 23, 2023

I've tracked the second problem to dividab/tsconfig-paths#236

@allista
Copy link

allista commented Jan 23, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is not working as expected
Projects
None yet
Development

No branches or pull requests

4 participants