sverweij / dependency-cruiser

Validate and visualize dependencies. Your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.
https://npmjs.com/dependency-cruiser
MIT License
5.15k stars 249 forks source link

Question: dependency-cruiser API gives a different result when `baseDir` is changed #936

Closed harrisonzhu-db closed 3 months ago

harrisonzhu-db commented 3 months ago

Summary

I'm migrating my package that uses dependency-cruiser from one location to another. Using the exact same code, my cruise output gives the following in its summary

old package location (works):

{
  violations: [],
  error: 0,
  warn: 0,
  info: 0,
  ignore: 0,
  totalCruised: 580,
  totalDependenciesCruised: 2045,
  optionsUsed: {
    baseDir: '/Users/harrison.zhu/universe2/experimental/harrisonzhu/safex-lint',
    combinedDependencies: false,
    exclude: { path: '.*node_modules' },
    exoticRequireStrings: [],
    externalModuleResolutionStrategy: 'node_modules',
    metrics: false,
    moduleSystems: [ 'es6', 'cjs', 'tsd', 'amd' ],
    preserveSymlinks: false,
    tsPreCompilationDeps: false,
    args: '../../../redash/managed_redash/packages/app/src/open-marketplace/OpenMarketplace.tsx'
  }
}

new package location (does not work):

{
  violations: [],
  error: 0,
  warn: 0,
  info: 0,
  ignore: 0,
  totalCruised: 3,
  totalDependenciesCruised: 2,
  optionsUsed: {
    baseDir: '/Users/harrison.zhu/universe2/js/scripts',
    combinedDependencies: false,
    exclude: { path: '.*node_modules' },
    exoticRequireStrings: [],
    externalModuleResolutionStrategy: 'node_modules',
    metrics: false,
    moduleSystems: [ 'es6', 'cjs', 'tsd', 'amd' ],
    preserveSymlinks: false,
    tsPreCompilationDeps: false,
    args: '../../redash/managed_redash/packages/app/src/open-marketplace/OpenMarketplace.tsx'
  }
}

everything is the same here except the baseDir and args. But resolving baseDir/args resolves to the same absolute path, so I'm not sure why there would be a difference in outputs, since I would hope that the operation of cruise is agnostic of where the script is located.

Context

Moving from a traditional yarn setup to a yarn pnp directory.

Environment

sverweij commented 3 months ago

Hi @harrisonzhu-db thanks for raising this issue. Does this also occur on the latest version of dependency-cruiser? 12.2 is already ~1.5 years old and I recall the baseDir not working as expected in earlier versions.

harrisonzhu-db commented 3 months ago

Hi - thanks for the response. Was there a workaround to the baseDir issue? I'm using 12.2 specifically since later versions are ESM only. Not sure if I can test w/ later versions.

sverweij commented 3 months ago

Hi @harrisonzhu-db baseDir used to not work at all. It was fixed as a drive-by in a PR for a new feature, but don't recall which PR or when it was made. Regardless - between the 44 releases between v12.2.x and the current version many a bug was fixed & many a feature was added. This is why I encourage anyone to upgrade when possible.

Seeing the code from your other issue it is likely 'converting' to ESM won't a large a hurdle, it looks like it's there already for 90%, although typescript might need a bit of convincing (a tsconfig.json with "target": "ES2022", "module": "NodeNext" and "moduleResolution": "NodeNext" seems to work in my other projects - YMMV).

If the issue persists in the latest version I'd love to see a minimal reproduction example so we're sure we're looking at the same thing and it's possible to debug (+ come up with a fix if necessary).

harrisonzhu-db commented 3 months ago

Sounds good - I'll see if converting to ESM is an option for me. However, just to be clear, when i say baseDir it's not an argument I was passing in - it was just something dependency-cruiser was outputting based on where i ran my project from.

harrisonzhu-db commented 3 months ago

@sverweij another hypothesis - I mentioned that these issues arise simply from running cruise(<path>) from different directories on the same path. However, the directory where cruise is malfunctioning itself is set up w/ yarn PnP, and running cruise in fresh yarn projects (w/ a traditional node_modules directory) in other locations work as intended - is there any chance that dependency-cruiser@12.12.2 is reading some config from the directory in which its script is located rather than the directory that it should target?

github-actions[bot] commented 3 months ago

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