dependents / node-filing-cabinet

Get the file location associated with a dependency/partial's path
MIT License
79 stars 44 forks source link

When `noTypeDefinitions` is enabled and the extension `.d.ts` is found, a crash can occur. #115

Closed TotalTechGeek closed 1 year ago

TotalTechGeek commented 1 year ago

There are likely two bugs here:

Given that I am performing a TypeScript import, with noTypeDefinitions as a parameter, if I have a package like @types/ramda installed, resolveJSModule will not resolve to ramda.

Instead of gracefully ignoring it, what will instead occur is it will check for the .js file in the wrong directory, and resolveJSModule will throw an exception, rather than nothing (which the || result implies the code expects)

if (namedModule.resolvedModule) {
    result = namedModule.resolvedModule.resolvedFileName;
    if (namedModule.resolvedModule.extension === '.d.ts' && noTypeDefinitions) {
      const resolvedFileNameWithoutExtension = result.replace(namedModule.resolvedModule.extension, '');
      result = ts.resolveJSModule(resolvedFileNameWithoutExtension, path.dirname(filename), host) || result;
    }
  }

For now, I will work around it by enabling type definitions, but I recommend wrapping the logic with a try catch instead of using the or statement, and potentially reworking the logic to check another directory.

XhmikosR commented 1 year ago

I hit this while updating the deps and I actually that's what I did too. Your issue should be fixed in v4.0.0