diff --git a/src/plugins/installed-plugin-index-store.ts b/src/plugins/installed-plugin-index-store.ts index d41e73d6f2d..0c97f808797 100644 --- a/src/plugins/installed-plugin-index-store.ts +++ b/src/plugins/installed-plugin-index-store.ts @@ -56,8 +56,12 @@ const InstalledPluginIndexRecordSchema = z packageInstall: z.unknown().optional(), manifestPath: z.string(), manifestHash: z.string(), - packageJsonPath: z.string().optional(), - packageJsonHash: z.string().optional(), + packageJson: z + .object({ + path: z.string(), + hash: z.string(), + }) + .optional(), rootDir: z.string(), origin: z.string(), enabled: z.boolean(), diff --git a/src/plugins/installed-plugin-index.test.ts b/src/plugins/installed-plugin-index.test.ts index 71078aba5a9..678b27d16b2 100644 --- a/src/plugins/installed-plugin-index.test.ts +++ b/src/plugins/installed-plugin-index.test.ts @@ -196,8 +196,10 @@ describe("installed plugin index", () => { ], }); expect(index.plugins[0]?.manifestHash).toMatch(/^[a-f0-9]{64}$/u); - expect(index.plugins[0]?.packageJsonHash).toMatch(/^[a-f0-9]{64}$/u); - expect(index.plugins[0]?.packageJsonPath).toBe(path.join(fixture.rootDir, "package.json")); + expect(index.plugins[0]?.packageJson).toMatchObject({ + path: "package.json", + hash: expect.stringMatching(/^[a-f0-9]{64}$/u), + }); expect(index.plugins[0]?.installRecord).toBeUndefined(); expect(index.plugins[0]?.installRecordHash).toBeUndefined(); diff --git a/src/plugins/installed-plugin-index.ts b/src/plugins/installed-plugin-index.ts index cc90f7b62d3..87d028a419a 100644 --- a/src/plugins/installed-plugin-index.ts +++ b/src/plugins/installed-plugin-index.ts @@ -87,8 +87,10 @@ export type InstalledPluginIndexRecord = { packageInstall?: PluginInstallSourceInfo; manifestPath: string; manifestHash: string; - packageJsonPath?: string; - packageJsonHash?: string; + packageJson?: { + path: string; + hash: string; + }; rootDir: string; origin: PluginManifestRecord["origin"]; enabled: boolean; @@ -244,6 +246,30 @@ function resolvePackageJsonPath(candidate: PluginCandidate | undefined): string return fs.existsSync(packageJsonPath) ? packageJsonPath : undefined; } +function resolvePackageJsonRecord(params: { + candidate: PluginCandidate | undefined; + packageJsonPath: string | undefined; + diagnostics: PluginDiagnostic[]; + pluginId: string; +}): InstalledPluginIndexRecord["packageJson"] | undefined { + if (!params.candidate?.packageDir || !params.packageJsonPath) { + return undefined; + } + const hash = safeHashFile({ + filePath: params.packageJsonPath, + pluginId: params.pluginId, + diagnostics: params.diagnostics, + required: false, + }); + if (!hash) { + return undefined; + } + return { + path: path.relative(params.candidate.rootDir, params.packageJsonPath) || "package.json", + hash, + }; +} + function describePackageInstallSource( candidate: PluginCandidate | undefined, ): PluginInstallSourceInfo | undefined { @@ -416,14 +442,12 @@ function buildInstalledPluginIndex( diagnostics, required: true, }) ?? ""; - const packageJsonHash = packageJsonPath - ? safeHashFile({ - filePath: packageJsonPath, - pluginId: record.id, - diagnostics, - required: false, - }) - : undefined; + const packageJson = resolvePackageJsonRecord({ + candidate, + packageJsonPath, + diagnostics, + pluginId: record.id, + }); const enabled = resolveEffectiveEnableState({ id: record.id, origin: record.origin, @@ -457,11 +481,8 @@ function buildInstalledPluginIndex( if (packageInstall) { indexRecord.packageInstall = packageInstall; } - if (packageJsonPath) { - indexRecord.packageJsonPath = packageJsonPath; - } - if (packageJsonHash) { - indexRecord.packageJsonHash = packageJsonHash; + if (packageJson) { + indexRecord.packageJson = packageJson; } return indexRecord; }); @@ -698,7 +719,8 @@ export function diffInstalledPluginIndexInvalidationReasons( } if ( previousPlugin.packageVersion !== currentPlugin.packageVersion || - previousPlugin.packageJsonHash !== currentPlugin.packageJsonHash + previousPlugin.packageJson?.path !== currentPlugin.packageJson?.path || + previousPlugin.packageJson?.hash !== currentPlugin.packageJson?.hash ) { reasons.add("stale-package"); }