Skip to content

Commit 7483eb0

Browse files
authored
Do no move node_modules when generating NPM lockfile (#85)
1 parent 40f1ecf commit 7483eb0

File tree

2 files changed

+10
-49
lines changed

2 files changed

+10
-49
lines changed

README.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ integrated, check out [mono-ts](https://github.com/0x80/mono-ts)
6868

6969
Run `pnpm install isolate-package --dev` or the equivalent for `npm` or `yarn`.
7070

71-
It is recommended to use `pnpm` over `npm` or `yarn`. Apart from being fast and
72-
efficient, PNPM has better support for monorepos, and the lockfile isolation is
73-
solid and works in parallel for multiple packages, [unlike NPM](#npm)
71+
I recommended using `pnpm` over `npm` or `yarn`. Besides being fast and
72+
efficient, PNPM has better support for monorepos.
7473

7574
## Usage
7675

@@ -331,17 +330,8 @@ each package manager, with NPM currently being the least attractive.
331330
### NPM
332331

333332
For NPM we use a tool called Arborist, which is an integral part of the NPM
334-
codebase. It is executed in the isolate output directory and requires the
335-
adapted lockfile and the `node_modules` directory from the root of the
336-
repository. As this directory is typically quite large, copying it over as part
337-
of the isolate flow is not very desirable.
338-
339-
To work around this, we move it to the isolate output and then move it back
340-
after Arborist has finished doing its thing.
341-
342-
> !! Warning: This will not be compatible with setups that run multiple
343-
> isolation processes in parallel. Hopefully a future update to NPM Arborist
344-
> (the part the generates the lockfile) will solve this.
333+
codebase. It is executed in the isolate output directory with an adaptation
334+
adapted manifest file.
345335

346336
### PNPM
347337

@@ -355,11 +345,7 @@ dependencies of internally linked packages are not installed by PNPM.
355345
### Classic Yarn
356346

357347
For Yarn v1 we can simply copy the root lockfile to the isolate output, and run
358-
a `yarn install` to prune that lockfile. The command finds the installed node
359-
modules in the root of the monorepo so versions are preserved.
360-
361-
> Note: I expect this to break down if you configure the isolate output
362-
> directory to be located outside the monorepo tree.
348+
a `yarn install` to prune that lockfile.
363349

364350
### Modern Yarn
365351

src/lib/lockfile/helpers/generate-npm-lockfile.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Arborist from "@npmcli/arborist";
22
import fs from "fs-extra";
33
import path from "node:path";
44
import { useLogger } from "~/lib/logger";
5-
import { getErrorMessage, inspectValue } from "~/lib/utils";
5+
import { getErrorMessage } from "~/lib/utils";
66

77
/**
88
* Generate an isolated / pruned lockfile, based on the contents of installed
@@ -20,28 +20,15 @@ export async function generateNpmLockfile({
2020

2121
log.debug("Generating NPM lockfile...");
2222

23-
const origRootNodeModulesPath = path.join(workspaceRootDir, "node_modules");
24-
const tempRootNodeModulesPath = path.join(isolateDir, "node_modules");
25-
26-
let hasMovedNodeModules = false;
27-
28-
let hasError = false;
23+
const nodeModulesPath = path.join(workspaceRootDir, "node_modules");
2924

3025
try {
31-
if (!fs.existsSync(origRootNodeModulesPath)) {
32-
throw new Error(
33-
`Failed to find node_modules at ${origRootNodeModulesPath}`
34-
);
26+
if (!fs.existsSync(nodeModulesPath)) {
27+
throw new Error(`Failed to find node_modules at ${nodeModulesPath}`);
3528
}
3629

37-
log.debug(`Temporarily moving node_modules to the isolate output`);
38-
39-
await fs.move(origRootNodeModulesPath, tempRootNodeModulesPath);
40-
hasMovedNodeModules = true;
41-
4230
const arborist = new Arborist({ path: isolateDir });
4331

44-
log.debug(`Building tree...`);
4532
const { meta } = await arborist.buildIdealTree();
4633

4734
meta?.commit();
@@ -52,18 +39,6 @@ export async function generateNpmLockfile({
5239

5340
log.debug("Created lockfile at", lockfilePath);
5441
} catch (err) {
55-
console.error(inspectValue(err));
56-
log.error(`Failed to generate lockfile: ${getErrorMessage(err)}`);
57-
hasError = true;
58-
} finally {
59-
/** @todo We should be able to use the new "using" keyword for this I think. */
60-
if (hasMovedNodeModules) {
61-
log.debug(`Restoring node_modules to the workspace root`);
62-
await fs.move(tempRootNodeModulesPath, origRootNodeModulesPath);
63-
}
64-
}
65-
66-
if (hasError) {
67-
throw new Error("Failed to generate lockfile");
42+
throw new Error(`Failed to generate lockfile: ${getErrorMessage(err)}`);
6843
}
6944
}

0 commit comments

Comments
 (0)