From 00058d0ae5cc9e5a6d7f02708ba9b0fcefd2c898 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 2 Dec 2024 01:20:44 +0100 Subject: [PATCH] repo-tools: make generate-patch only create a single resolutions entry Signed-off-by: Patrik Oldsberg --- .changeset/tiny-mice-run.md | 5 ++ .../commands/generate-patch/generate-patch.ts | 61 +------------------ 2 files changed, 7 insertions(+), 59 deletions(-) create mode 100644 .changeset/tiny-mice-run.md diff --git a/.changeset/tiny-mice-run.md b/.changeset/tiny-mice-run.md new file mode 100644 index 0000000000..4829ef7390 --- /dev/null +++ b/.changeset/tiny-mice-run.md @@ -0,0 +1,5 @@ +--- +'@backstage/repo-tools': patch +--- + +The `generate-patch` command will now add a single resolution entry for all versions of the patched package, rather than separate entries for each version query. diff --git a/packages/repo-tools/src/commands/generate-patch/generate-patch.ts b/packages/repo-tools/src/commands/generate-patch/generate-patch.ts index e817abd74b..224b7ae4e6 100644 --- a/packages/repo-tools/src/commands/generate-patch/generate-patch.ts +++ b/packages/repo-tools/src/commands/generate-patch/generate-patch.ts @@ -182,8 +182,6 @@ async function loadTrimmedRootPkg(ctx: PatchContext, query?: string) { }; } - const resolutionMap = await readResolutionMap(ctx); - // Any existing resolution entries for the package are removed const entriesToRemove = Object.entries(resolutionsObj).filter(([key]) => key.startsWith(`${ctx.packageName}@`), @@ -201,29 +199,9 @@ async function loadTrimmedRootPkg(ctx: PatchContext, query?: string) { } } - // This collects the list of version descriptors that we want to apply the patch to - const descriptors = new Array(); - for (const [descriptor, locator] of resolutionMap) { - // Skip virtual and other non-npm entries - if (!locator.includes('@npm:')) { - // We know that virtual entries are fine to skip, but want to warn if we skip others - if (!locator.includes('@virtual:')) { - console.warn(`Skipping resolution for ${descriptor}, no version found`); - } - continue; - } - - descriptors.push(descriptor); - } - return async (patchEntry?: string) => { - if (descriptors.length > 0) { - for (const descriptor of descriptors) { - resolutionsObj[descriptor] = patchEntry; - } - } else { - resolutionsObj[ctx.packageName] = patchEntry; - } + resolutionsObj[ctx.packageName] = patchEntry; + // We use the same patch for all versions of the package, if they don't // apply it might require manual intervention using the --query option await fs.writeJson( @@ -297,41 +275,6 @@ function tryParsePatchResolution(value?: string): string | undefined { return patchFilePath; } -// Read out the descriptors for all entries of the package in the target repo -async function readResolutionMap( - ctx: PatchContext, -): Promise> { - const { stdout: whyOutput } = await exec( - 'yarn', - ['why', '--json', ctx.packageName], - { - cwd: ctx.targetRoot, - maxBuffer: 64 * 1024 * 1024, - }, - ); - - const resolutionMap = new Map(); - - for (const line of whyOutput.toString('utf8').trim().split(/\r?\n/)) { - for (const { locator, descriptor } of Object.values( - JSON.parse(line).children, - ) as Array<{ locator: string; descriptor: string }>) { - const existing = resolutionMap.get(descriptor); - if (existing) { - if (existing !== locator) { - throw new Error( - `Conflicting resolutions in target package for ${descriptor}: ${existing} vs ${locator}`, - ); - } - } else { - resolutionMap.set(descriptor, locator); - } - } - } - - return resolutionMap; -} - // Build and pack the source package to an archive in the work directory async function buildTargetArchive(ctx: PatchContext): Promise { await exec('yarn', ['build'], {