Skip to content

Commit b4fe1ba

Browse files
committed
fix(@angular/cli): resolve the package name inside npm workspace members
Inside an npm workspace member, `npm pkg get name` prints an object keyed by the workspace name instead of a JSON string. `getCurrentPackageName` returned that object, so the workspace lookup in `parseNpmLikeDependencies` never matched and `ng update` saw no dependencies for the member. The name is now only taken from the command output when it is a string. Otherwise it is read from the package.json of the current directory. Fixes #32787
1 parent 6ecbada commit b4fe1ba

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,12 @@ export class PackageManager {
397397
try {
398398
const { stdout } = await this.#run(this.descriptor.getPackageNameCommand);
399399
if (stdout) {
400-
return JSON.parse(stdout);
400+
const name = JSON.parse(stdout);
401+
402+
// In a workspace member npm prints an object keyed by workspace name instead of a string.
403+
if (typeof name === 'string') {
404+
return name;
405+
}
401406
}
402407
} catch {
403408
// Fall back to reading file if command fails

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,57 @@ describe('PackageManager', () => {
378378
});
379379
});
380380

381+
describe('getCurrentPackageName', () => {
382+
it('should return the name printed by the package manager', async () => {
383+
const pm = new PackageManager(host, '/repo/app', descriptor);
384+
runCommandSpy.and.resolveTo({ stdout: '"app"', stderr: '' });
385+
const readFileSpy = spyOn(host, 'readFile');
386+
387+
expect(await pm.getCurrentPackageName()).toBe('app');
388+
expect(readFileSpy).not.toHaveBeenCalled();
389+
});
390+
391+
it('should read package.json when npm prints an object keyed by workspace', async () => {
392+
const pm = new PackageManager(host, '/repo/apps/app', descriptor);
393+
// Inside a workspace member, `npm pkg get name` prints `{ "app": "app" }` instead of `"app"`.
394+
runCommandSpy.and.resolveTo({ stdout: '{\n "app": "app"\n}', stderr: '' });
395+
const readFileSpy = spyOn(host, 'readFile').and.resolveTo('{"name": "app"}');
396+
397+
expect(await pm.getCurrentPackageName()).toBe('app');
398+
expect(readFileSpy).toHaveBeenCalledWith(jasmine.stringMatching(/package\.json$/));
399+
});
400+
});
401+
402+
describe('getProjectDependencies', () => {
403+
it('should list the dependencies of an npm workspace member', async () => {
404+
const pm = new PackageManager(host, '/repo/apps/app', descriptor);
405+
runCommandSpy.and.callFake((_binary: string, args: readonly string[]) =>
406+
Promise.resolve({
407+
stdout:
408+
args[0] === 'pkg'
409+
? '{\n "app": "app"\n}'
410+
: JSON.stringify({
411+
name: 'monorepo',
412+
dependencies: {
413+
app: {
414+
version: '0.0.0',
415+
resolved: 'file:../apps/app',
416+
dependencies: { '@angular/core': { version: '22.0.0' } },
417+
},
418+
},
419+
}),
420+
stderr: '',
421+
}),
422+
);
423+
spyOn(host, 'readFile').and.resolveTo('{"name": "app"}');
424+
425+
const dependencies = await pm.getProjectDependencies();
426+
427+
expect([...dependencies.keys()]).toEqual(['@angular/core']);
428+
expect(dependencies.get('@angular/core')?.version).toBe('22.0.0');
429+
});
430+
});
431+
381432
describe('initializationError', () => {
382433
it('should throw initializationError when running commands', async () => {
383434
const error = new Error('Not installed');

0 commit comments

Comments
 (0)