Skip to content

Commit

Permalink
Merge pull request #4697 from L-Qun/main
Browse files Browse the repository at this point in the history
[lockfile-explorer] Fix some issues when parsing certain lockfile syntaxes
  • Loading branch information
octogonz committed May 15, 2024
2 parents 0ed5899 + 5afbebc commit 624dc0c
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 27 deletions.
20 changes: 9 additions & 11 deletions apps/lockfile-explorer-web/src/parsing/readLockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,17 @@ export function generateLockfileGraph(
for (const [dependencyKey, dependencyValue] of Object.entries(lockfile.packages)) {
// const normalizedPath = new Path(dependencyKey).makeAbsolute('/').toString();

let packageDepKey = dependencyKey;
if (pnpmLockfileVersion === PnpmLockfileVersion.V6) {
packageDepKey = dependencyKey.replace('@', '/');
}

const currEntry = new LockfileEntry({
// entryId: normalizedPath,
rawEntryId: packageDepKey,
rawEntryId: dependencyKey,
kind: LockfileEntryFilter.Package,
rawYamlData: dependencyValue
rawYamlData: dependencyValue,
subspaceName
});

allPackages.push(currEntry);
allEntries.push(currEntry);
allEntriesById[packageDepKey] = currEntry;
allEntriesById[dependencyKey] = currEntry;
}
}

Expand All @@ -171,9 +167,11 @@ export function generateLockfileGraph(
dependency.resolvedEntry = matchedEntry;
matchedEntry.referrers.push(entry);
} else {
// Local package
// eslint-disable-next-line no-console
console.error('Could not resolve dependency entryId: ', dependency.entryId, dependency);
if (dependency.entryId.startsWith('/')) {
// Local package
// eslint-disable-next-line no-console
console.error('Could not resolve dependency entryId: ', dependency.entryId, dependency);
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions apps/lockfile-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,24 @@
}
},
"devDependencies": {
"@microsoft/rush-lib": "workspace:*",
"@rushstack/heft": "workspace:*",
"@rushstack/lockfile-explorer-web": "workspace:*",
"@types/cors": "~2.8.12",
"@types/express": "4.17.21",
"@types/js-yaml": "3.12.1",
"@types/update-notifier": "~6.0.1",
"local-node-rig": "workspace:*"
"local-node-rig": "workspace:*",
"@pnpm/lockfile-types": "~6.0.0"
},
"dependencies": {
"@microsoft/rush-lib": "workspace:*",
"@rushstack/node-core-library": "workspace:*",
"@rushstack/terminal": "workspace:*",
"cors": "~2.8.5",
"express": "4.19.2",
"js-yaml": "~3.13.1",
"open": "~8.4.0",
"update-notifier": "~5.1.0"
"update-notifier": "~5.1.0",
"@pnpm/dependency-path": "~2.1.2"
}
}
31 changes: 30 additions & 1 deletion apps/lockfile-explorer/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { AlreadyReportedError } from '@rushstack/node-core-library';
import { FileSystem, type IPackageJson, JsonFile, PackageJsonLookup } from '@rushstack/node-core-library';
import type { IAppContext } from '@rushstack/lockfile-explorer-web/lib/AppContext';
import { Colorize } from '@rushstack/terminal';
import type { Lockfile } from '@pnpm/lockfile-types';

import { convertLockfileV6DepPathToV5DepPath } from './utils';
import { init } from './init';
import type { IAppState } from './state';
import { type ICommandLine, parseCommandLine } from './commandLine';
Expand Down Expand Up @@ -103,7 +105,34 @@ function startApp(debugMode: boolean): void {

app.get('/api/lockfile', async (req: express.Request, res: express.Response) => {
const pnpmLockfileText: string = await FileSystem.readFileAsync(appState.pnpmLockfileLocation);
const doc = yaml.load(pnpmLockfileText);
const doc = yaml.load(pnpmLockfileText) as Lockfile;
const { packages, lockfileVersion } = doc;

let shrinkwrapFileMajorVersion: number;
if (typeof lockfileVersion === 'string') {
const isDotIncluded: boolean = lockfileVersion.includes('.');
shrinkwrapFileMajorVersion = parseInt(
lockfileVersion.substring(0, isDotIncluded ? lockfileVersion.indexOf('.') : undefined),
10
);
} else if (typeof lockfileVersion === 'number') {
shrinkwrapFileMajorVersion = Math.floor(lockfileVersion);
} else {
shrinkwrapFileMajorVersion = 0;
}

if (shrinkwrapFileMajorVersion < 5 || shrinkwrapFileMajorVersion > 6) {
throw new Error('The current lockfile version is not supported.');
}

if (packages && shrinkwrapFileMajorVersion === 6) {
const updatedPackages: Lockfile['packages'] = {};
for (const [dependencyPath, dependency] of Object.entries(packages)) {
updatedPackages[convertLockfileV6DepPathToV5DepPath(dependencyPath)] = dependency;
}
doc.packages = updatedPackages;
}

res.send({
doc,
subspaceName
Expand Down
11 changes: 11 additions & 0 deletions apps/lockfile-explorer/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import * as dp from '@pnpm/dependency-path';

export function convertLockfileV6DepPathToV5DepPath(newDepPath: string): string {
if (!newDepPath.includes('@', 2) || newDepPath.startsWith('file:')) return newDepPath;
const index = newDepPath.indexOf('@', newDepPath.indexOf('/@') + 2);
if (newDepPath.includes('(') && index > dp.indexOfPeersSuffix(newDepPath)) return newDepPath;
return `${newDepPath.substring(0, index)}/${newDepPath.substring(index + 1)}`;
}
10 changes: 10 additions & 0 deletions common/changes/@microsoft/rush/main_2024-05-14-23-45.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Optimize the judgment of shrinkwrap file major version.",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/lockfile-explorer",
"comment": "Fix some issues when parsing certain lockfile syntaxes",
"type": "patch"
}
],
"packageName": "@rushstack/lockfile-explorer"
}
4 changes: 4 additions & 0 deletions common/config/rush/nonbrowser-approved-packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
"name": "@pnpm/link-bins",
"allowedCategories": [ "libraries" ]
},
{
"name": "@pnpm/lockfile-types",
"allowedCategories": [ "libraries" ]
},
{
"name": "@pnpm/logger",
"allowedCategories": [ "libraries" ]
Expand Down
35 changes: 26 additions & 9 deletions common/config/subspaces/default/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/config/subspaces/default/repo-state.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
{
"pnpmShrinkwrapHash": "4b7734088f9537c4644d58706e7a1e2cbb41b5d3",
"pnpmShrinkwrapHash": "fdb1015454ffa0bac21eeff77ab0314887083c7e",
"preferredVersionsHash": "ce857ea0536b894ec8f346aaea08cfd85a5af648"
}
5 changes: 3 additions & 2 deletions libraries/rush-lib/src/logic/pnpm/PnpmShrinkwrapFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,13 @@ export class PnpmShrinkwrapFile extends BaseShrinkwrapFile {
// Normalize the data
const lockfileVersion: string | number | undefined = shrinkwrapJson.lockfileVersion;
if (typeof lockfileVersion === 'string') {
const isDotIncluded: boolean = lockfileVersion.includes('.');
this.shrinkwrapFileMajorVersion = parseInt(
lockfileVersion.substring(0, lockfileVersion.indexOf('.')),
lockfileVersion.substring(0, isDotIncluded ? lockfileVersion.indexOf('.') : undefined),
10
);
} else if (typeof lockfileVersion === 'number') {
this.shrinkwrapFileMajorVersion = lockfileVersion;
this.shrinkwrapFileMajorVersion = Math.floor(lockfileVersion);
} else {
this.shrinkwrapFileMajorVersion = 0;
}
Expand Down

0 comments on commit 624dc0c

Please sign in to comment.