Skip to content

Commit 7613be7

Browse files
committed
fix(@angular/cli): copy project config files to temp directory during ng update
Fixes #27163
1 parent a4666ad commit 7613be7

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

packages/angular/cli/src/package-managers/package-manager-descriptor.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export const SUPPORTED_PACKAGE_MANAGERS = {
155155
ignoreScriptsFlag: '--ignore-scripts',
156156
ignorePeerDependenciesFlag: '--force',
157157
configFiles: ['.npmrc'],
158+
copyConfigFromProject: true,
158159
getRegistryOptions: (registry: string) => ({ args: ['--registry', registry] }),
159160
versionCommand: ['--version'],
160161
listDependenciesCommand: ['list', '--depth=0', '--json=true', '--all=true'],
@@ -180,6 +181,7 @@ export const SUPPORTED_PACKAGE_MANAGERS = {
180181
noLockfileFlag: '',
181182
ignoreScriptsFlag: '--mode=skip-build',
182183
configFiles: ['.yarnrc.yml', '.yarnrc.yaml'],
184+
copyConfigFromProject: true,
183185
getRegistryOptions: (registry: string) => ({ env: { YARN_NPM_REGISTRY_SERVER: registry } }),
184186
versionCommand: ['--version'],
185187
listDependenciesCommand: ['info', '--name-only', '--json'],
@@ -208,6 +210,7 @@ export const SUPPORTED_PACKAGE_MANAGERS = {
208210
noLockfileFlag: '--no-lockfile',
209211
ignoreScriptsFlag: '--ignore-scripts',
210212
configFiles: ['.yarnrc', '.npmrc'],
213+
copyConfigFromProject: true,
211214
getRegistryOptions: (registry: string) => ({ args: ['--registry', registry] }),
212215
versionCommand: ['--version'],
213216
listDependenciesCommand: ['list', '--depth=0', '--json'],
@@ -233,7 +236,10 @@ export const SUPPORTED_PACKAGE_MANAGERS = {
233236
noLockfileFlag: '--no-lockfile',
234237
ignoreScriptsFlag: '--ignore-scripts',
235238
ignorePeerDependenciesFlag: '--strict-peer-dependencies=false',
236-
configFiles: ['.npmrc', 'pnpm-workspace.yaml'],
239+
// Note: pnpm-workspace.yaml is intentionally excluded. Copying it to the
240+
// temporary directory could cause pnpm to treat it as a workspace root.
241+
configFiles: ['.npmrc'],
242+
copyConfigFromProject: true,
237243
getRegistryOptions: (registry: string) => ({ args: ['--registry', registry] }),
238244
versionCommand: ['--version'],
239245
listDependenciesCommand: ['list', '--depth=0', '--json'],

packages/angular/cli/src/package-managers/package-manager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,8 @@ export class PackageManager {
621621
// Writing an empty package.json file beforehand prevents this.
622622
await this.host.writeFile(join(workingDirectory, 'package.json'), '{}');
623623

624-
// Copy configuration files if the package manager requires it (e.g., bun).
624+
// Copy configuration files from the project to the temp directory so that
625+
// registry settings and other package manager config are respected.
625626
if (this.descriptor.copyConfigFromProject) {
626627
for (const configFile of this.descriptor.configFiles) {
627628
try {

packages/angular/cli/src/package-managers/package-manager_spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,51 @@ describe('PackageManager', () => {
113113
expect(manifest).toEqual({ name: 'foo', version: '1.0.0' });
114114
});
115115
});
116+
117+
describe('acquireTempPackage', () => {
118+
it('should copy config files from the project when copyConfigFromProject is true', async () => {
119+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
120+
const copyFileSpy = spyOn(host as any, 'copyFile').and.resolveTo();
121+
spyOn(host, 'createTempDirectory').and.resolveTo('/tmp/angular-cli-packages-test');
122+
spyOn(host, 'writeFile').and.resolveTo();
123+
spyOn(host, 'deleteDirectory').and.resolveTo();
124+
125+
const pm = new PackageManager(host, '/project', descriptor);
126+
127+
await pm.acquireTempPackage('foo');
128+
129+
expect(copyFileSpy).toHaveBeenCalledWith(
130+
'/project/.npmrc',
131+
'/tmp/angular-cli-packages-test/.npmrc',
132+
);
133+
});
134+
135+
it('should not copy config files when copyConfigFromProject is not set', async () => {
136+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
137+
const copyFileSpy = spyOn(host as any, 'copyFile').and.resolveTo();
138+
spyOn(host, 'createTempDirectory').and.resolveTo('/tmp/angular-cli-packages-test');
139+
spyOn(host, 'writeFile').and.resolveTo();
140+
spyOn(host, 'deleteDirectory').and.resolveTo();
141+
142+
const noCopyDescriptor = { ...descriptor, copyConfigFromProject: undefined };
143+
const pm = new PackageManager(host, '/project', noCopyDescriptor);
144+
145+
await pm.acquireTempPackage('foo');
146+
147+
expect(copyFileSpy).not.toHaveBeenCalled();
148+
});
149+
150+
it('should ignore missing config files gracefully', async () => {
151+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
152+
const copyFileSpy = spyOn(host as any, 'copyFile').and.rejectWith(new Error('ENOENT'));
153+
spyOn(host, 'createTempDirectory').and.resolveTo('/tmp/angular-cli-packages-test');
154+
spyOn(host, 'writeFile').and.resolveTo();
155+
spyOn(host, 'deleteDirectory').and.resolveTo();
156+
157+
const pm = new PackageManager(host, '/project', descriptor);
158+
159+
await expectAsync(pm.acquireTempPackage('foo')).toBeResolved();
160+
expect(copyFileSpy).toHaveBeenCalled();
161+
});
162+
});
116163
});

0 commit comments

Comments
 (0)