From d6a0cfb119e7f45057eba44442949a508559692c Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Wed, 21 Aug 2024 18:04:22 -0400 Subject: [PATCH] fix(core): fix compat for old remote caches (#27574) ## Current Behavior The old remote caches are broken with the new db cache. ## Expected Behavior The old remote caches are working properly with the new db cache. ## Related Issue(s) Fixes # --- packages/nx/src/tasks-runner/cache.ts | 2 +- .../src/tasks-runner/default-tasks-runner.ts | 27 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/nx/src/tasks-runner/cache.ts b/packages/nx/src/tasks-runner/cache.ts index 05e8e7cfaa021..dc05a3a9fee84 100644 --- a/packages/nx/src/tasks-runner/cache.ts +++ b/packages/nx/src/tasks-runner/cache.ts @@ -123,7 +123,7 @@ export class DbCache { return nxCloudClient.remoteCache; } else { // old nx cloud instance - return RemoteCacheV2.fromCacheV1(this.options.nxCloudRemoteCache); + return await RemoteCacheV2.fromCacheV1(this.options.nxCloudRemoteCache); } } else { return null; diff --git a/packages/nx/src/tasks-runner/default-tasks-runner.ts b/packages/nx/src/tasks-runner/default-tasks-runner.ts index b7ea0673dd117..fc015689d46ce 100644 --- a/packages/nx/src/tasks-runner/default-tasks-runner.ts +++ b/packages/nx/src/tasks-runner/default-tasks-runner.ts @@ -7,7 +7,8 @@ import { NxJsonConfiguration } from '../config/nx-json'; import { Task, TaskGraph } from '../config/task-graph'; import { NxArgs } from '../utils/command-line-utils'; import { DaemonClient } from '../daemon/client/client'; -import { readFile, writeFile } from 'fs/promises'; +import { cacheDir } from '../utils/cache-directory'; +import { readFile, writeFile, mkdir } from 'fs/promises'; import { join } from 'path'; import { CachedResult } from '../native'; @@ -17,18 +18,28 @@ export interface RemoteCache { } export abstract class RemoteCacheV2 { - static fromCacheV1(cache: RemoteCache): RemoteCacheV2 { + static async fromCacheV1(cache: RemoteCache): Promise { + await mkdir(join(cacheDir, 'terminalOutputs'), { recursive: true }); + return { retrieve: async (hash, cacheDirectory) => { - const res = cache.retrieve(hash, cacheDirectory); - const [terminalOutput, code] = await Promise.all([ - readFile(join(cacheDirectory, hash, 'terminalOutputs'), 'utf-8'), - readFile(join(cacheDirectory, hash, 'code'), 'utf-8').then((s) => +s), - ]); + const res = await cache.retrieve(hash, cacheDirectory); if (res) { + const [terminalOutput, oldTerminalOutput, code] = await Promise.all([ + readFile( + join(cacheDirectory, hash, 'terminalOutputs'), + 'utf-8' + ).catch(() => null), + readFile(join(cacheDir, 'terminalOutputs', hash), 'utf-8').catch( + () => null + ), + readFile(join(cacheDirectory, hash, 'code'), 'utf-8').then( + (s) => +s + ), + ]); return { outputsPath: cacheDirectory, - terminalOutput, + terminalOutput: terminalOutput ?? oldTerminalOutput, code, }; } else {