typedoc icon indicating copy to clipboard operation
typedoc copied to clipboard

Decide a ReferenceType's package name by looking for a package.json file

Open bodil opened this issue 1 year ago • 8 comments

The current mechanism by which a ReferenceType figures out which package the type came from is by parsing the path and taking the first path element following node_modules. This breaks when using pnpm which builds a symlink tree into a .pnpm folder inside node_modules, so every ReferenceType comes out .pnpm. It also breaks if you're linking into a different project inside a monorepo, where there's no node_modules on the source path.

The most immediate consequence of this is that plugins which resolve external links such as typedoc-plugin-mdn-links silently fail to do anything when using pnpm.

This patch walks up the directory tree looking for a package.json file and grabbing the package name from its name key rather than trying to guess it from the file path, which solves the issues mentioned above.

bodil avatar Jul 26 '22 12:07 bodil

Looking at https://pnpm.io/symlinked-node-modules-structure... would an alternative, much less IO expensive, fix be to switch the indexOf("node_modules") to lastIndexOf("node_modules")?

Gerrit0 avatar Jul 27 '22 12:07 Gerrit0

That was my first attempt at a fix, which solves the problem of linking into the symlink farm, but not cross project links which won't have node_modules in their path at all. Looking at it again with performance in mind, it would be better to try lastIndexOf first, and look for a package.json only if that gives no result. It's probably worth caching the package.json lookups too.

bodil avatar Jul 27 '22 13:07 bodil

I've implemented the above, how does this look?

bodil avatar Jul 27 '22 14:07 bodil

I missed the cross project links mention in your original post - I'm probably missing something... not familiar with pnpm, but why would those not have node_modules? Are they not actually normal dependencies? package isn't meant to be set for references which aren't from a dependency.

Gerrit0 avatar Jul 30 '22 14:07 Gerrit0

In pnpm monorepo setups, you can list a package as a dependency of another package inside the monorepo workspace, and you'll get a symlink in node_modules to its actual location in the workspace, so assuming symlinks are being resolved you won't have node_modules in the dependency's path.

bodil avatar Jul 30 '22 14:07 bodil

Hmmm.... that's a thorny problem then. The test failures are real failures because the patch is inappropriately setting the package property on all reference types. I'm going to try setting up a pnpm setup and see if there's a better way to do this...

Gerrit0 avatar Jul 31 '22 15:07 Gerrit0

... I might need to change the definition of ReferenceType.package, not happy about it, but... going to think about this some more.

Gerrit0 avatar Jul 31 '22 16:07 Gerrit0

Yeah, I'm convinced ReferenceType.package should (almost) always be set. This is a pretty major breaking change, so it's going to have to be a part of 0.24 though.

Gerrit0 avatar Aug 06 '22 13:08 Gerrit0