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(js): locate npm nodes correctly for aliased packages #27124

Merged
merged 1 commit into from
Aug 7, 2024
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 @@ -96,6 +96,14 @@ describe('TargetProjectLocator', () => {
name: '@proj/proj123-base',
version: '1.0.0',
}),
'./node_modules/lodash/package.json': JSON.stringify({
name: 'lodash',
version: '3.0.0',
}),
'./node_modules/lodash-4/package.json': JSON.stringify({
name: 'lodash',
version: '4.0.0',
}),
};
vol.fromJSON(fsJson, '/root');
projects = {
Expand Down Expand Up @@ -263,6 +271,22 @@ describe('TargetProjectLocator', () => {
packageName: '@proj/proj123-base',
},
},
'npm:lodash': {
name: 'npm:lodash',
type: 'npm',
data: {
version: '3.0.0',
packageName: 'lodash',
},
},
'npm:lodash-4': {
name: 'npm:lodash-4',
type: 'npm',
data: {
packageName: 'lodash-4',
version: 'npm:[email protected]',
},
},
};

targetProjectLocator = new TargetProjectLocator(projects, npmProjects);
Expand Down Expand Up @@ -454,6 +478,20 @@ describe('TargetProjectLocator', () => {
);
expect(proj5).toEqual('proj5');
});

it('should be able to resolve packages alises', () => {
const lodash = targetProjectLocator.findProjectFromImport(
'lodash',
'libs/proj/index.ts'
);
expect(lodash).toEqual('npm:lodash');

const lodash4 = targetProjectLocator.findProjectFromImport(
'lodash-4',
'libs/proj/index.ts'
);
expect(lodash4).toEqual('npm:lodash-4');
});
});

describe('findTargetProjectWithImport (without tsconfig.json)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,14 @@ export class TargetProjectLocator {

const version = clean(externalPackageJson.version);
const npmProjectKey = `npm:${externalPackageJson.name}@${version}`;
const matchingExternalNode = this.npmProjects[npmProjectKey];
let matchingExternalNode = this.npmProjects[npmProjectKey];
if (!matchingExternalNode) {
return null;
// check if it's a package alias, where the resolved package key is used as the version
const aliasNpmProjectKey = `npm:${packageName}@${npmProjectKey}`;
matchingExternalNode = this.npmProjects[aliasNpmProjectKey];
if (!matchingExternalNode) {
return null;
}
}

this.npmResolutionCache.set(
Expand Down