Skip to content

Commit

Permalink
fix(core): support import detection of packages installed from git re…
Browse files Browse the repository at this point in the history
…mote URL (#27569)
  • Loading branch information
jaysoo authored Aug 21, 2024
1 parent ecbd2cb commit 16b2e02
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ describe('TargetProjectLocator', () => {
expect(proj5).toEqual('proj5');
});

it('should be able to resolve packages alises', () => {
it('should be able to resolve packages aliases', () => {
const lodash = targetProjectLocator.findProjectFromImport(
'lodash',
'libs/proj/index.ts'
Expand Down Expand Up @@ -899,6 +899,85 @@ describe('TargetProjectLocator', () => {
expect(result).toEqual('npm:@json2csv/plainjs');
});
});

describe('findNpmProjectFromImport', () => {
it('should resolve external node when the version does not match its own package.json (i.e. git remote) ', () => {
const projects = {
proj: {
name: 'proj',
type: 'lib' as const,
data: {
root: 'proj',
},
},
};
const npmProjects = {
'npm:foo': {
name: 'npm:foo' as const,
type: 'npm' as const,
data: {
version:
'git+ssh://[email protected]/example/foo.git#6f4b450fc642abba540535f0755c990b42a16026',
packageName: 'foo',
},
},
};

const targetProjectLocator = new TargetProjectLocator(
projects,
npmProjects,
new Map()
);
targetProjectLocator['readPackageJson'] = () => ({
name: 'foo',
version: '0.0.1',
});
const result = targetProjectLocator.findNpmProjectFromImport(
'lodash',
'proj/index.ts'
);

expect(result).toEqual('npm:foo');
});

it('should resolve a specific version of external node', () => {
const projects = {
proj: {
name: 'proj',
type: 'lib' as const,
data: {
root: 'proj',
},
},
};
const npmProjects = {
'npm:[email protected]': {
name: 'npm:[email protected]' as const,
type: 'npm' as const,
data: {
version: '0.0.1',
packageName: 'foo',
},
},
};

const targetProjectLocator = new TargetProjectLocator(
projects,
npmProjects,
new Map()
);
targetProjectLocator['readPackageJson'] = () => ({
name: 'foo',
version: '0.0.1',
});
const result = targetProjectLocator.findNpmProjectFromImport(
'lodash',
'proj/index.ts'
);

expect(result).toEqual('npm:[email protected]');
});
});
});

describe('isBuiltinModuleImport()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
resolveModuleByImport,
} from '../../utils/typescript';
import { getPackageNameFromImportPath } from '../../../../utils/get-package-name-from-import-path';

/**
* The key is a combination of the package name and the workspace relative directory
* containing the file importing it e.g. `lodash__packages/my-lib`, the value is the
Expand Down Expand Up @@ -187,15 +188,24 @@ export class TargetProjectLocator {
}

const version = clean(externalPackageJson.version);
const npmProjectKey = `npm:${externalPackageJson.name}@${version}`;
let matchingExternalNode = this.npmProjects[npmProjectKey];
let matchingExternalNode =
this.npmProjects[`npm:${externalPackageJson.name}@${version}`];

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

if (!matchingExternalNode) {
// Fallback to package name as key. This can happen if the version in project graph is not the same as in the resolved package.json.
// e.g. Version in project graph is a git remote, but the resolved version is semver.
matchingExternalNode =
this.npmProjects[`npm:${externalPackageJson.name}`];
}

if (!matchingExternalNode) {
return null;
}

this.npmResolutionCache.set(
Expand Down

0 comments on commit 16b2e02

Please sign in to comment.