Skip to content

Commit 471dc97

Browse files
authored
fix(ng-dev): check pnpm lock for latest ng-dev version when running release tool (#2659)
Currently we only check `yarn.lock`, but in e.g. CLI we already removed the yarn lock file.
1 parent e38c95e commit 471dc97

File tree

6 files changed

+110
-23
lines changed

6 files changed

+110
-23
lines changed

ng-dev/utils/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ ts_library(
5353
"@npm//@octokit/request-error",
5454
"@npm//@octokit/rest",
5555
"@npm//@octokit/types",
56+
"@npm//@pnpm/dependency-path",
5657
"@npm//@types/node",
5758
"@npm//@types/semver",
5859
"@npm//@types/supports-color",

ng-dev/utils/constants.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,3 @@ export const ngDevNpmPackageName = '@angular/ng-dev';
1111

1212
/** Workspace-relative path for the "package.json" file. */
1313
export const workspaceRelativePackageJsonPath = 'package.json';
14-
15-
/** Workspace-relative path for the "yarn.lock" file. */
16-
export const workspaceRelativeYarnLockFilePath = 'yarn.lock';

ng-dev/utils/version-check.ts

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,53 @@ import * as path from 'path';
1010
import * as fs from 'fs';
1111
import lockfile from '@yarnpkg/lockfile';
1212
import {parse as parseYaml} from 'yaml';
13-
import {
14-
ngDevNpmPackageName,
15-
workspaceRelativePackageJsonPath,
16-
workspaceRelativeYarnLockFilePath,
17-
} from './constants.js';
13+
import {ngDevNpmPackageName, workspaceRelativePackageJsonPath} from './constants.js';
1814
import {Log} from './logging.js';
15+
import {tryGetPackageId} from '@pnpm/dependency-path';
1916

2017
/**
2118
* Verifies that the `ng-dev` tool is up-to-date in the workspace. The check will compare
2219
* the local version of the tool against the requested version in the workspace lock file.
2320
*
2421
* This check is helpful ensuring that the caretaker does not accidentally run with an older
25-
* local version of `ng-dev` due to not running `yarn` after checking out new revisions.
22+
* local version of `ng-dev` due to not running `yarn`/`pnpm` after checking out new revisions.
2623
*
2724
* @returns a boolean indicating success or failure.
2825
*/
2926
export async function verifyNgDevToolIsUpToDate(workspacePath: string): Promise<boolean> {
3027
// The placeholder will be replaced by the `pkg_npm` substitutions.
3128
const localVersion = `0.0.0-{SCM_HEAD_SHA}`;
3229
const workspacePackageJsonFile = path.join(workspacePath, workspaceRelativePackageJsonPath);
33-
const workspaceDirLockFile = path.join(workspacePath, workspaceRelativeYarnLockFilePath);
30+
const pnpmLockFile = path.join(workspacePath, 'pnpm-lock.yaml');
31+
const yarnLockFile = path.join(workspacePath, 'yarn.lock');
3432

33+
// TODO: Clean up this logic when fully dropping Yarn
34+
const isPnpmMigrated = fs.existsSync(pnpmLockFile) && !fs.existsSync(yarnLockFile);
35+
const expectedVersion = isPnpmMigrated
36+
? getExpectedVersionFromPnpmLock(workspacePackageJsonFile, pnpmLockFile)
37+
: getExpectedVersionFromYarnLock(workspacePackageJsonFile, yarnLockFile);
38+
39+
Log.debug(`Expecting the following ng-dev version: ${expectedVersion}`);
40+
41+
if (localVersion !== expectedVersion) {
42+
Log.error(' ✘ Your locally installed version of the `ng-dev` tool is outdated and not');
43+
Log.error(' matching with the version in the `package.json` file.');
44+
Log.error(' Re-install the dependencies to ensure you are using the correct version.');
45+
return false;
46+
}
47+
48+
return true;
49+
}
50+
51+
function getExpectedVersionFromYarnLock(workspacePackageJsonFile: string, lockFilePath: string) {
3552
try {
3653
const packageJson = JSON.parse(fs.readFileSync(workspacePackageJsonFile, 'utf8')) as any;
3754
// If we are operating in the actual dev-infra repo, always return `true`.
3855
if (packageJson.name === ngDevNpmPackageName) {
3956
return true;
4057
}
4158

42-
const lockFileContent = fs.readFileSync(workspaceDirLockFile, 'utf8');
59+
const lockFileContent = fs.readFileSync(lockFilePath, 'utf8');
4360

4461
let lockFileObject: Record<string, {version: string}>;
4562
try {
@@ -57,17 +74,33 @@ export async function verifyNgDevToolIsUpToDate(workspacePath: string): Promise<
5774
packageJson?.dependencies?.[ngDevNpmPackageName] ??
5875
packageJson?.devDependencies?.[ngDevNpmPackageName] ??
5976
packageJson?.optionalDependencies?.[ngDevNpmPackageName];
60-
const expectedVersion = lockFileObject[`${ngDevNpmPackageName}@${devInfraPkgVersion}`].version;
77+
return lockFileObject[`${ngDevNpmPackageName}@${devInfraPkgVersion}`].version;
78+
} catch (e) {
79+
Log.debug('Could not find expected ng-dev version from `yarn.lock` file:', e);
80+
return null;
81+
}
82+
}
6183

62-
if (localVersion !== expectedVersion) {
63-
Log.error(' ✘ Your locally installed version of the `ng-dev` tool is outdated and not');
64-
Log.error(' matching with the version in the `package.json` file.');
65-
Log.error(' Re-install the dependencies to ensure you are using the correct version.');
66-
return false;
84+
function getExpectedVersionFromPnpmLock(workspacePackageJsonFile: string, lockFilePath: string) {
85+
try {
86+
const packageJson = JSON.parse(fs.readFileSync(workspacePackageJsonFile, 'utf8')) as any;
87+
// If we are operating in the actual dev-infra repo, always return `true`.
88+
if (packageJson.name === ngDevNpmPackageName) {
89+
return true;
6790
}
68-
return true;
91+
92+
const lockFileContent = fs.readFileSync(lockFilePath, 'utf8');
93+
const lockFile = parseYaml(lockFileContent);
94+
const importers = lockFile['importers']['.'];
95+
const depEntry =
96+
importers.dependencies?.['@angular/ng-dev'] ??
97+
importers.devDependencies?.['@angular/ng-dev'] ??
98+
importers.optionalDependencies?.['@angular/ng-dev'];
99+
const packageId = tryGetPackageId(depEntry.version);
100+
101+
return lockFile['packages'][`@angular/ng-dev@${packageId}`].version;
69102
} catch (e) {
70-
Log.error(e);
71-
return false;
103+
Log.debug('Could not find expected ng-dev version from `pnpm-lock.yaml` file:', e);
104+
return null;
72105
}
73106
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"@octokit/webhooks-definitions": "3.67.3",
8989
"@octokit/webhooks-types": "7.6.1",
9090
"@openid/appauth": "^1.3.1",
91+
"@pnpm/dependency-path": "^1000.0.5",
9192
"@rollup/plugin-commonjs": "^28.0.0",
9293
"@rollup/plugin-node-resolve": "^16.0.0",
9394
"@types/babel__core": "^7.1.19",

tools/local-dev.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ export TS_NODE_PROJECT=${PWD}/.ng-dev/tsconfig.json
2222

2323
# Execute the built ng-dev command in the current working directory
2424
# and pass-through arguments unmodified.
25-
node node_modules/.bin/tsx ${ngDevBinFile} ${@}
25+
node ${devInfraProjectDir}/node_modules/.bin/tsx ${ngDevBinFile} ${@}

yarn.lock

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ __metadata:
271271
"@octokit/webhooks-definitions": "npm:3.67.3"
272272
"@octokit/webhooks-types": "npm:7.6.1"
273273
"@openid/appauth": "npm:^1.3.1"
274+
"@pnpm/dependency-path": "npm:^1000.0.5"
274275
"@rollup/plugin-commonjs": "npm:^28.0.0"
275276
"@rollup/plugin-node-resolve": "npm:^16.0.0"
276277
"@types/babel__core": "npm:^7.1.19"
@@ -3878,6 +3879,44 @@ __metadata:
38783879
languageName: node
38793880
linkType: hard
38803881

3882+
"@pnpm/crypto.hash@npm:1000.1.1":
3883+
version: 1000.1.1
3884+
resolution: "@pnpm/crypto.hash@npm:1000.1.1"
3885+
dependencies:
3886+
"@pnpm/crypto.polyfill": "npm:1000.1.0"
3887+
"@pnpm/graceful-fs": "npm:1000.0.0"
3888+
ssri: "npm:10.0.5"
3889+
checksum: 10c0/1ff392d1434abf81c9dcefa21d3ef844c1d371cd971b037f2425c67990b71302b390117cb353b4ea9ce2158fd31eb1c78de9b385647e9b795129854ed35da7e1
3890+
languageName: node
3891+
linkType: hard
3892+
3893+
"@pnpm/crypto.polyfill@npm:1000.1.0":
3894+
version: 1000.1.0
3895+
resolution: "@pnpm/crypto.polyfill@npm:1000.1.0"
3896+
checksum: 10c0/894d7e7579c0c26d3e55ff4d52db9a65c01742728c8e341f04d676803d44e37c22b8d075517100268e513fc44d97d5250d4c5b9fdf57df96c046710fd4cd25ad
3897+
languageName: node
3898+
linkType: hard
3899+
3900+
"@pnpm/dependency-path@npm:^1000.0.5":
3901+
version: 1000.0.5
3902+
resolution: "@pnpm/dependency-path@npm:1000.0.5"
3903+
dependencies:
3904+
"@pnpm/crypto.hash": "npm:1000.1.1"
3905+
"@pnpm/types": "npm:1000.2.1"
3906+
semver: "npm:^7.7.1"
3907+
checksum: 10c0/a2605fd321284e5f81b355843fe6eb48983cab3d94364a3f2547b0cb6f6ef19cb7efba6df0851420fbb78242cf42c1b8117ec5a527778f98da76cde65d27d0d2
3908+
languageName: node
3909+
linkType: hard
3910+
3911+
"@pnpm/graceful-fs@npm:1000.0.0":
3912+
version: 1000.0.0
3913+
resolution: "@pnpm/graceful-fs@npm:1000.0.0"
3914+
dependencies:
3915+
graceful-fs: "npm:^4.2.11"
3916+
checksum: 10c0/544e00660c901260417b0b5086c5589491b8c33fc79bb3206173e8e4572c9e800dc80153e07f7f22ee28fda267b112d97e6e390edaa22428bbde275568438698
3917+
languageName: node
3918+
linkType: hard
3919+
38813920
"@pnpm/network.ca-file@npm:^1.0.1":
38823921
version: 1.0.2
38833922
resolution: "@pnpm/network.ca-file@npm:1.0.2"
@@ -3898,6 +3937,13 @@ __metadata:
38983937
languageName: node
38993938
linkType: hard
39003939

3940+
"@pnpm/types@npm:1000.2.1":
3941+
version: 1000.2.1
3942+
resolution: "@pnpm/types@npm:1000.2.1"
3943+
checksum: 10c0/e5df4dd9c63f242b478e26957d4603d6c0aaa460441afc7461d4ca4aa3ae05bdb572f65873dbe24e1f8257776d413be88cea63f3bf1377aa600f181231a2ee01
3944+
languageName: node
3945+
linkType: hard
3946+
39013947
"@protobufjs/aspromise@npm:^1.1.1, @protobufjs/aspromise@npm:^1.1.2":
39023948
version: 1.1.2
39033949
resolution: "@protobufjs/aspromise@npm:1.1.2"
@@ -8691,7 +8737,7 @@ __metadata:
86918737
languageName: node
86928738
linkType: hard
86938739

8694-
"graceful-fs@npm:^4.1.2, graceful-fs@npm:^4.1.6, graceful-fs@npm:^4.2.0, graceful-fs@npm:^4.2.10, graceful-fs@npm:^4.2.6":
8740+
"graceful-fs@npm:^4.1.2, graceful-fs@npm:^4.1.6, graceful-fs@npm:^4.2.0, graceful-fs@npm:^4.2.10, graceful-fs@npm:^4.2.11, graceful-fs@npm:^4.2.6":
86958741
version: 4.2.11
86968742
resolution: "graceful-fs@npm:4.2.11"
86978743
checksum: 10c0/386d011a553e02bc594ac2ca0bd6d9e4c22d7fa8cfbfc448a6d148c59ea881b092db9dbe3547ae4b88e55f1b01f7c4a2ecc53b310c042793e63aa44cf6c257f2
@@ -13218,7 +13264,7 @@ __metadata:
1321813264
languageName: node
1321913265
linkType: hard
1322013266

13221-
"semver@npm:7.7.1, semver@npm:^7.0.0, semver@npm:^7.1.1, semver@npm:^7.3.5, semver@npm:^7.3.7, semver@npm:^7.3.8, semver@npm:^7.5.2, semver@npm:^7.5.3, semver@npm:^7.5.4":
13267+
"semver@npm:7.7.1, semver@npm:^7.0.0, semver@npm:^7.1.1, semver@npm:^7.3.5, semver@npm:^7.3.7, semver@npm:^7.3.8, semver@npm:^7.5.2, semver@npm:^7.5.3, semver@npm:^7.5.4, semver@npm:^7.7.1":
1322213268
version: 7.7.1
1322313269
resolution: "semver@npm:7.7.1"
1322413270
bin:
@@ -13790,6 +13836,15 @@ __metadata:
1379013836
languageName: node
1379113837
linkType: hard
1379213838

13839+
"ssri@npm:10.0.5":
13840+
version: 10.0.5
13841+
resolution: "ssri@npm:10.0.5"
13842+
dependencies:
13843+
minipass: "npm:^7.0.3"
13844+
checksum: 10c0/b091f2ae92474183c7ac5ed3f9811457e1df23df7a7e70c9476eaa9a0c4a0c8fc190fb45acefbf023ca9ee864dd6754237a697dc52a0fb182afe65d8e77443d8
13845+
languageName: node
13846+
linkType: hard
13847+
1379313848
"ssri@npm:^10.0.0":
1379413849
version: 10.0.6
1379513850
resolution: "ssri@npm:10.0.6"

0 commit comments

Comments
 (0)