Skip to content

Commit 122ed27

Browse files
committed
fix(@angular/cli): use project-local temporary directory in ng add
The 'ng add' command now attempts to use '.angular/cache' or 'node_modules' as the base for temporary directories when acquiring packages. This ensures that the package manager can inherit project-specific configuration (like '.npmrc' or '.yarnrc') during the installation of temporary packages. The implementation uses a fallback strategy, checking for candidate directories and defaulting to the system's temporary directory if none are found. (cherry picked from commit 2cd4f2d)
1 parent f48e7d6 commit 122ed27

File tree

1 file changed

+24
-1
lines changed
  • packages/angular/cli/src/commands/add

1 file changed

+24
-1
lines changed

packages/angular/cli/src/commands/add/cli.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Listr, ListrRenderer, ListrTaskWrapper, color, figures } from 'listr2';
1010
import assert from 'node:assert';
1111
import fs from 'node:fs/promises';
1212
import { createRequire } from 'node:module';
13-
import { dirname, join } from 'node:path';
13+
import { dirname, join, relative, resolve } from 'node:path';
1414
import npa from 'npm-package-arg';
1515
import semver, { Range, compare, intersects, prerelease, satisfies, valid } from 'semver';
1616
import { Argv } from 'yargs';
@@ -33,6 +33,7 @@ import {
3333
import { assertIsError } from '../../utilities/error';
3434
import { isTTY } from '../../utilities/tty';
3535
import { VERSION } from '../../utilities/version';
36+
import { getCacheConfig } from '../cache/utilities';
3637

3738
class CommandError extends Error {}
3839

@@ -298,10 +299,32 @@ export default class AddCommandModule
298299
context: AddCommandTaskContext,
299300
task: AddCommandTaskWrapper,
300301
): Promise<void> {
302+
let tempDirectory: string | undefined;
303+
const tempOptions = ['node_modules'];
304+
305+
const cacheConfig = getCacheConfig(this.context.workspace);
306+
if (cacheConfig.enabled) {
307+
const cachePath = resolve(this.context.root, cacheConfig.path);
308+
if (!relative(this.context.root, cachePath).startsWith('..')) {
309+
tempOptions.push(cachePath);
310+
}
311+
}
312+
313+
for (const tempOption of tempOptions) {
314+
try {
315+
const directory = resolve(this.context.root, tempOption);
316+
if ((await fs.stat(directory)).isDirectory()) {
317+
tempDirectory = directory;
318+
break;
319+
}
320+
} catch {}
321+
}
322+
301323
context.packageManager = await createPackageManager({
302324
cwd: this.context.root,
303325
logger: this.context.logger,
304326
dryRun: context.dryRun,
327+
tempDirectory,
305328
});
306329
task.output = `Using package manager: ${color.dim(context.packageManager.name)}`;
307330
}

0 commit comments

Comments
 (0)