Skip to content

Commit

Permalink
fix(core): clean versions read from package.json when resolving depen… (
Browse files Browse the repository at this point in the history
#26457)

…dencies

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

When versions in published `package.json` files such as
https://unpkg.com/@trpc/[email protected]/package.json are depended
on, the version is not cleaned of extra parts of the version, and the
dependency is resolved properly.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

When versions in published `package.json` files such as
https://unpkg.com/@trpc/[email protected]/package.json are depended
on, the version is cleaned and resolves properly as a dependency.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #26410
  • Loading branch information
FrozenPandaz authored Jun 7, 2024
1 parent 6de382c commit 08a0e5c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ describe('TargetProjectLocator', () => {
'./tsconfig.base.json': JSON.stringify(tsConfig),
'./node_modules/@ng/core/package.json': JSON.stringify({
name: '@ng/core',
version: '1',
version: '1.0.0',
}),
'./node_modules/npm-package/package.json': JSON.stringify({
name: 'npm-package',
version: '1',
version: '1.0.0',
}),
'./node_modules/@proj/proj123-base/package.json': JSON.stringify({
name: '@proj/proj123-base',
version: '1',
version: '1.0.0',
}),
};
vol.fromJSON(fsJson, '/root');
Expand Down Expand Up @@ -200,63 +200,63 @@ describe('TargetProjectLocator', () => {
name: 'npm:@ng/core',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@ng/core',
},
},
'npm:@ng/common': {
name: 'npm:@ng/common',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@ng/common',
},
},
'npm:npm-package': {
name: 'npm:npm-package',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: 'npm-package',
},
},
'npm:@proj/my-second-proj': {
name: 'npm:@proj/my-second-proj',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/my-second-proj',
},
},
'npm:@proj/proj5': {
name: 'npm:@proj/proj5',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/proj5',
},
},
'npm:@proj/proj6': {
name: 'npm:@proj/proj6',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/proj6',
},
},
'npm:@proj/proj7': {
name: 'npm:@proj/proj7',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/proj7',
},
},
'npm:@proj/proj123-base': {
name: 'npm:@proj/proj123-base',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/proj123-base',
},
},
Expand Down Expand Up @@ -487,15 +487,19 @@ describe('TargetProjectLocator', () => {
'./libs/proj1234-child/index.ts': 'export const a = 12345',
'./node_modules/@proj/proj123-base/package.json': JSON.stringify({
name: '@proj/proj123-base',
version: '1',
version: '1.0.0',
}),
'./node_modules/@ng/core/package.json': JSON.stringify({
name: '@ng/core',
version: '1',
version: '1.0.0',
}),
'./node_modules/npm-package/package.json': JSON.stringify({
name: 'npm-package',
version: '1',
version: '1.0.0',
}),
'./node_modules/@trpc/server/package.json': JSON.stringify({
name: '@trpc/server',
version: '1.0.0-rc.0+abc123',
}),
// minimatch is a real world example of a multi module format package with nested package.json files.
// node will resolve the dist/cjs/package.json file when using require.resolve('minimatch') in commonjs
Expand Down Expand Up @@ -595,63 +599,63 @@ describe('TargetProjectLocator', () => {
name: 'npm:@ng/core',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@ng/core',
},
},
'npm:@ng/common': {
name: 'npm:@ng/common',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@ng/common',
},
},
'npm:npm-package': {
name: 'npm:npm-package',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: 'npm-package',
},
},
'npm:@proj/my-second-proj': {
name: 'npm:@proj/my-second-proj',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/my-second-proj',
},
},
'npm:@proj/proj5': {
name: 'npm:@proj/proj5',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/proj5',
},
},
'npm:@proj/proj6': {
name: 'npm:@proj/proj6',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/proj6',
},
},
'npm:@proj/proj7': {
name: 'npm:@proj/proj7',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/proj7',
},
},
'npm:@proj/proj123-base': {
name: 'npm:@proj/proj123-base',
type: 'npm',
data: {
version: '1',
version: '1.0.0',
packageName: '@proj/proj123-base',
},
},
Expand Down Expand Up @@ -679,6 +683,15 @@ describe('TargetProjectLocator', () => {
hash: 'sha512-ggewenDQWc5azOEM/HI7AREuIHXSPO0STL+ehAG2PvoQPHglCdfLQy904D85ttm9wS7AKdK+d3wqMzSQaj7FsA==',
},
},
'npm:@trpc/server': {
type: 'npm',
name: 'npm:@trpc/server',
data: {
version: '1.0.0-rc.0',
packageName: '@trpc/server',
hash: 'sha512-ggewenDQWc5azOEM/HI7AREuIHXSPO0STL+ehAG2PvoQPHglCdfLQy904D85ttm9wS7AKdK+d3wqMzSQaj7FsA==',
},
},
/**
* We use minimatch as an example of a multiple module format package.
*/
Expand Down Expand Up @@ -774,6 +787,14 @@ describe('TargetProjectLocator', () => {
expect(similarDeepImportFromNpm).toEqual('npm:@proj/proj123-base');
});

it('should be able to resolve npm projects with unclean versions', () => {
const trpcProject = targetProjectLocator.findProjectFromImport(
'@trpc/server',
'libs/proj/index.ts'
);
expect(trpcProject).toEqual('npm:@trpc/server');
});

it('should return null for native modules', () => {
const result = targetProjectLocator.findProjectFromImport(
'path',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'node:fs';
import { builtinModules } from 'node:module';
import { dirname, join, parse, posix, relative } from 'node:path';
import { clean } from 'semver';
import {
ProjectGraphExternalNode,
ProjectGraphProjectNode,
Expand Down Expand Up @@ -186,7 +186,8 @@ export class TargetProjectLocator {
return externalNodeName;
}

const npmProjectKey = `npm:${externalPackageJson.name}@${externalPackageJson.version}`;
const version = clean(externalPackageJson.version);
const npmProjectKey = `npm:${externalPackageJson.name}@${version}`;
const matchingExternalNode = this.npmProjects[npmProjectKey];
if (!matchingExternalNode) {
return null;
Expand Down

0 comments on commit 08a0e5c

Please sign in to comment.