0x80 / isolate-package

Isolate a monorepo package with its internal dependencies to form a self-contained directory with a pruned lockfile
MIT License
121 stars 13 forks source link

Package names with periods don't work (e.g. "@mylib/a.b") #16

Closed bmcbarron closed 1 year ago

bmcbarron commented 1 year ago

In create-packages-registry.ts, the registry is constructed which indexes package manifests by their name. However, the registry object is built using lodash.set, which parses the key as a recursive path when it includes periods, so that package name "@mylib/a.b" results in { "@mylib/a": { "b": { ... }}}. Later, when "@mylib/a.b" is looked up in the registry, it's not found resulting in the error error TypeError: Cannot read properties of undefined (reading 'rootRelativeDir')

It appears that the extra behavior of lodash.set is not needed. If so, the following change fixes this issue:

.reduce<PackagesRegistry>(
    (acc, info) => {
      if (info) {
        acc[info.manifest.name] = info;
      }
      return acc;
    },
    {}
  );
0x80 commented 1 year ago

It wouldn't be difficult to fix this but I don't remember ever having seen dots being used in Node.js package names. Could you point me to some documentation which describes this as being supported?

bmcbarron commented 1 year ago

Here's the docs on name. Here's an excerpt about URL-safe characters. Bottom line: dot, dash, underscore, tilde, and alphanumeric characters are all legal. Additional characters may work or they may not, depending on the particular tool and context.

I'm personally a fan of the Robustness principle which suggests that tools should be as relaxed as possible in what they accept.

0x80 commented 1 year ago

Fair enough! The NPM name rules are clearly less strict than I had assumed from experience. I have published v1.3.1 which contains this fix.