message: cli: work around race condition while packing dependencies of the CLI
diff: @@ -24,8 +24,16 @@ import { tmpdir } from 'os';
  import tar, { CreateOptions } from 'tar';
  import { paths } from '../paths';
  import { run } from '../run';
  +import { packageVersions } from '../version';
  import { ParallelOption } from '../parallel';
  +// These packages aren't safe to pack in parallel since the CLI depends on them
  +const UNSAFE_PACKAGES = [
  + ...Object.keys(packageVersions),
  + '@backstage/cli-common',
  + '@backstage/config-loader',
  +];
  +
  type LernaPackage = {
  name: string;
  private: boolean;
  @@ -135,10 +143,8 @@ async function moveToDistWorkspace(
  workspaceDir: string,
  localPackages: LernaPackage[],
  ): Promise<void> {
  - await Promise.all(
  - localPackages.map(async (target, index) => {
  + async function pack(target: LernaPackage, archive: string) {
  console.log(`Repacking ${target.name} into dist workspace`);
  - const archive = `temp-package-${index}.tgz`;
  const archivePath = resolvePath(workspaceDir, archive);
  await run('yarn', ['pack', '--filename', archivePath], {
  @@ -179,7 +185,26 @@ async function moveToDistWorkspace(
  },
  );
  }
  - }),
  + }
  +
  + const unsafePackages = localPackages.filter(p =>
  + UNSAFE_PACKAGES.includes(p.name),
  + );
  + const safePackages = localPackages.filter(
  + p => !UNSAFE_PACKAGES.includes(p.name),
  + );
  +
  + // The unsafe package are packed first one by one in order to avoid race conditions
  + // where the CLI is being executed with broken dependencies.
  + for (const target of unsafePackages) {
  + await pack(target, `temp-package.tgz`);
  + }
  +
  + // Repacking in parallel is much faster and safe for all packages outside of the Backstage repo
  + await Promise.all(
  + safePackages.map(async (target, index) =>
  + pack(target, `temp-package-${index}.tgz`),
  + ),
  );
  }
  