dependents / node-dependency-tree

Get the dependency tree of a module
MIT License
707 stars 84 forks source link

Unable to resolve directory dependencies #119

Closed clayrisser closed 3 years ago

clayrisser commented 4 years ago

I am unable to resolve the dependency if I'm importing a directory with an index.ts file.

For example . . . src/index.ts

export default class Hello {}

tests/index.ts

import Hello from '../src'

Instead, what I have to do to make it work is explicitly import the index, which is less ideal. src/index.ts

export default class Hello {}

tests/index.ts

import Hello from '../src/index'

Here is the debug log for running the following command.

DEBUG=* dependency-tree --directory="." --list-form tests/index.ts
  tree given filename: tests/index.ts +0ms
  tree resolved filename: /home/codejamninja/Projects/generator-node-module-typescript/demo/tests/index.ts +1ms
  tree visited:  {} +0ms
  tree traversing /home/codejamninja/Projects/generator-node-module-typescript/demo/tests/index.ts +0ms
  precinct paperwork: stripping the dot from the extension to serve as the type +0ms
  precinct paperwork: setting the module type +0ms
  precinct paperwork: invoking precinct +0ms
  precinct options given:  { includeCore: false, type: 'ts' } +0ms
  precinct module type:  ts +0ms
  typescript-eslint:typescript-estree:createSourceFile Getting AST without type information in TS mode for: /home/codejamninja/Projects/generator-node-module-typescript/demo/estree.ts +0ms
  tree extracted 1 dependencies:  [ '../src/index' ] +9ms
  cabinet Given filename: /home/codejamninja/Projects/generator-node-module-typescript/demo/tests/index.ts +0ms
  cabinet which has the extension: .ts +0ms
  cabinet found a resolver for .ts +0ms
  cabinet performing a typescript lookup +0ms
  cabinet given typescript config:  undefined +0ms
  cabinet no tsconfig given, defaulting +0ms
  cabinet processed typescript config:  undefined +0ms
  cabinet processed typescript config type:  undefined +0ms
  cabinet with options:  { module: 2 } +0ms
  cabinet result: /home/codejamninja/Projects/generator-node-module-typescript/demo/src/index.ts +1ms
  cabinet resolved path for ../src/index: /home/codejamninja/Projects/generator-node-module-typescript/demo/src/index.ts +0ms
  tree cabinet-resolved all dependencies:  [
  '/home/codejamninja/Projects/generator-node-module-typescript/demo/src/index.ts'
] +2ms
  tree given filename: /home/codejamninja/Projects/generator-node-module-typescript/demo/tests/index.ts +11ms
  tree resolved filename: /home/codejamninja/Projects/generator-node-module-typescript/demo/tests/index.ts +0ms
  tree visited:  {
  '/home/codejamninja/Projects/generator-node-module-typescript/demo/tests/index.ts': []
} +0ms
  tree traversing /home/codejamninja/Projects/generator-node-module-typescript/demo/src/index.ts +0ms
  precinct paperwork: stripping the dot from the extension to serve as the type +11ms
  precinct paperwork: setting the module type +0ms
  precinct paperwork: invoking precinct +0ms
  precinct options given:  { includeCore: false, type: 'ts' } +0ms
  precinct module type:  ts +0ms
  typescript-eslint:typescript-estree:createSourceFile Getting AST without type information in TS mode for: /home/codejamninja/Projects/generator-node-module-typescript/demo/estree.ts +10ms
  tree extracted 0 dependencies:  [] +1ms
  tree cabinet-resolved all dependencies:  [] +1ms
  tree traversal complete Set {
  '/home/codejamninja/Projects/generator-node-module-typescript/demo/src/index.ts',
  '/home/codejamninja/Projects/generator-node-module-typescript/demo/tests/index.ts'
} +0ms
  tree deduped list of nonExistent partials:  [] +0ms
  tree list form of results requested +0ms
  tree final tree [
  '/home/codejamninja/Projects/generator-node-module-typescript/demo/src/index.ts',
  '/home/codejamninja/Projects/generator-node-module-typescript/demo/tests/index.ts'
] +0ms
/home/codejamninja/Projects/generator-node-module-typescript/demo/src/index.ts
/home/codejamninja/Projects/generator-node-module-typescript/demo/tests/index.ts
clayrisser commented 4 years ago

Maybe this is related to issue #118

mrjoelkemp commented 4 years ago

hey! I notice in the output that you don't have a TSconfig defined. Is there a TS config option that would get this to work; if so, then you could supply the path to the TS config file and see if that works. If the file is pure commonjs, then it relies on the commonjs resolution algorithm (which handles the case you're mentioning). Since the "../src" module is going through TS proper: https://github.com/dependents/node-filing-cabinet/blob/master/index.js#L221, there might be a TS thing you need to set in a TS config. Let me know if that works at all.

jeanlescure commented 3 years ago

The following worked for me:

const tree = dependencyTree.toList(
  {
    filename,
    tsConfig: {
      compilerOptions: {
        allowSyntheticDefaultImports: true,
        module: 'commonjs',
      }
    },
    directory: './src',
    nonExistent: non,
  },
);
mrjoelkemp commented 3 years ago

Thanks for closing the loop @jeanlescure.

If anyone wants to add docs to the readme to clarify this or help someone with a similar issue in the future, then I'd gladly review a PR. Cheers!