Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clean node_modules only after all iteration of installation were completed #7369

Merged
merged 4 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export type InstallOptions = {
packageManagerConfigRootDir?: string;
resolveVersionsFromDependenciesOnly?: boolean;
linkedDependencies?: Record<string, Record<string, string>>;
pruneNodeModules?: boolean;
};

export type GetComponentManifestsOptions = {
Expand Down Expand Up @@ -180,7 +179,6 @@ export class DependencyInstaller {
engineStrict: this.engineStrict,
packageManagerConfigRootDir: options.packageManagerConfigRootDir,
peerDependencyRules: this.peerDependencyRules,
pruneNodeModules: options.pruneNodeModules,
hidePackageManagerOutput,
...packageManagerOptions,
};
Expand Down Expand Up @@ -233,6 +231,13 @@ export class DependencyInstaller {
return installResult;
}

public async pruneModules(rootDir: string): Promise<void> {
if (!this.packageManager.pruneModules) {
return;
}
await this.packageManager.pruneModules(rootDir);
}

/**
* Compute all the component manifests (a.k.a. package.json files) that should be passed to the package manager
* in order to install the dependencies.
Expand Down
4 changes: 2 additions & 2 deletions scopes/dependencies/dependency-resolver/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export type PackageManagerInstallOptions = {
updateAll?: boolean;

hidePackageManagerOutput?: boolean;

pruneNodeModules?: boolean;
};

export type PackageManagerGetPeerDependencyIssuesOptions = PackageManagerInstallOptions;
Expand Down Expand Up @@ -95,6 +93,8 @@ export interface PackageManager {
options: PackageManagerInstallOptions
): Promise<{ dependenciesChanged: boolean }>;

pruneModules?(rootDir: string): Promise<void>;

resolveRemoteVersion(
packageName: string,
options: PackageManagerResolveRemoteVersionOptions
Expand Down
3 changes: 1 addition & 2 deletions scopes/dependencies/pnpm/lynx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export async function install(
updateAll?: boolean;
nodeLinker?: 'hoisted' | 'isolated';
overrides?: Record<string, string>;
pruneNodeModules?: boolean;
rootComponents?: boolean;
rootComponentsForCapsules?: boolean;
includeOptionalDeps?: boolean;
Expand Down Expand Up @@ -226,7 +225,7 @@ export async function install(
workspacePackages,
preferFrozenLockfile: true,
pruneLockfileImporters: true,
modulesCacheMaxAge: options.pruneNodeModules ? 0 : undefined,
modulesCacheMaxAge: Infinity, // pnpm should never prune the virtual store. Bit does it on its own.
neverBuiltDependencies: ['core-js'],
registries: registriesMap,
resolutionMode: 'highest',
Expand Down
23 changes: 23 additions & 0 deletions scopes/dependencies/pnpm/pnpm-prune-modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs from 'fs-extra';
import path from 'path';
import { difference } from 'lodash';
import { readCurrentLockfile } from '@pnpm/lockfile-file';
import { depPathToFilename } from '@pnpm/dependency-path';

/**
* Reads the private lockfile at node_modules/.pnpm/lock.yaml
* and removes any directories from node_modules/.pnpm that are not listed in the lockfile.
*/
export async function pnpmPruneModules(rootDir: string): Promise<void> {
const virtualStoreDir = path.join(rootDir, 'node_modules/.pnpm');
const pkgDirs = await readPackageDirsFromVirtualStore(virtualStoreDir);
if (pkgDirs.length === 0) return;
const lockfile = await readCurrentLockfile(virtualStoreDir, { ignoreIncompatible: false });
const dirsShouldBePresent = Object.keys(lockfile?.packages ?? {}).map(depPathToFilename);
await Promise.all(difference(pkgDirs, dirsShouldBePresent).map((dir) => fs.remove(path.join(virtualStoreDir, dir))));
}

async function readPackageDirsFromVirtualStore(virtualStoreDir: string): Promise<string[]> {
const allDirs = await fs.readdir(virtualStoreDir);
return allDirs.filter((dir) => dir !== 'lock.yaml' && dir !== 'node_modules');
}
6 changes: 5 additions & 1 deletion scopes/dependencies/pnpm/pnpm.package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { readModulesManifest } from '@pnpm/modules-yaml';
import { ProjectManifest } from '@pnpm/types';
import { join } from 'path';
import { readConfig } from './read-config';
import { pnpmPruneModules } from './pnpm-prune-modules';

export class PnpmPackageManager implements PackageManager {
readonly name = 'pnpm';
Expand Down Expand Up @@ -74,7 +75,6 @@ export class PnpmPackageManager implements PackageManager {
sideEffectsCacheRead: installOptions.sideEffectsCache ?? true,
sideEffectsCacheWrite: installOptions.sideEffectsCache ?? true,
pnpmHomeDir: config.pnpmHomeDir,
pruneNodeModules: installOptions.pruneNodeModules,
updateAll: installOptions.updateAll,
hidePackageManagerOutput: installOptions.hidePackageManagerOutput,
},
Expand Down Expand Up @@ -198,4 +198,8 @@ export class PnpmPackageManager implements PackageManager {
getWorkspaceDepsOfBitRoots(manifests: ProjectManifest[]): Record<string, string> {
return Object.fromEntries(manifests.map((manifest) => [manifest.name, 'workspace:*']));
}

async pruneModules(rootDir: string): Promise<void> {
return pnpmPruneModules(rootDir);
}
}
6 changes: 3 additions & 3 deletions scopes/workspace/install/install.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,6 @@ export class InstallMain {
current.componentDirectoryMap,
{
installTeambitBit: false,
// We clean node_modules only on the first install.
// Otherwise, we might load an env from a location that we later remove.
pruneNodeModules: installCycle === 0,
},
pmInstallOptions
);
Expand All @@ -277,6 +274,9 @@ export class InstallMain {
current = await this._getComponentsManifests(installer, mergedRootPolicy, pmInstallOptions);
installCycle += 1;
} while ((!prevManifests.has(manifestsHash(current.manifests)) || hasMissingLocalComponents) && installCycle < 5);
// We clean node_modules only after the last install.
// Otherwise, we might load an env from a location that we later remove.
await installer.pruneModules(this.workspace.path);
await this.workspace.consumer.componentFsCache.deleteAllDependenciesDataCache();
/* eslint-enable no-await-in-loop */
return current.componentDirectoryMap;
Expand Down