Skip to content

Commit 2cd4f2d

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.
1 parent 5fb3af0 commit 2cd4f2d

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

@@ -311,10 +312,32 @@ export default class AddCommandModule
311312
context: AddCommandTaskContext,
312313
task: AddCommandTaskWrapper,
313314
): Promise<void> {
315+
let tempDirectory: string | undefined;
316+
const tempOptions = ['node_modules'];
317+
318+
const cacheConfig = getCacheConfig(this.context.workspace);
319+
if (cacheConfig.enabled) {
320+
const cachePath = resolve(this.context.root, cacheConfig.path);
321+
if (!relative(this.context.root, cachePath).startsWith('..')) {
322+
tempOptions.push(cachePath);
323+
}
324+
}
325+
326+
for (const tempOption of tempOptions) {
327+
try {
328+
const directory = resolve(this.context.root, tempOption);
329+
if ((await fs.stat(directory)).isDirectory()) {
330+
tempDirectory = directory;
331+
break;
332+
}
333+
} catch {}
334+
}
335+
314336
context.packageManager = await createPackageManager({
315337
cwd: this.context.root,
316338
logger: this.context.logger,
317339
dryRun: context.dryRun,
340+
tempDirectory,
318341
});
319342
task.output = `Using package manager: ${color.dim(context.packageManager.name)}`;
320343
}

0 commit comments

Comments
 (0)