From cd705bb67f41ce0e18b78e0a8e8c50e0bbd65888 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Thu, 17 Sep 2026 16:55:25 +0200 Subject: [PATCH] refactor(cache): scope tar scratch directories to reads Keep execCommands as a plain executor that honors its cwd argument. Move scratch-directory allocation and cleanup into a shared list/extract helper, leaving archive creation on the caller-supplied directory. --- src/internal/tar.ts | 50 ++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/internal/tar.ts b/src/internal/tar.ts index e3334df..7b8d81a 100644 --- a/src/internal/tar.ts +++ b/src/internal/tar.ts @@ -251,29 +251,39 @@ async function getCompressionProgram( } // Executes all commands as separate processes -async function execCommands( - {commands, requiresTempDirectory}: TarCommands, - cwd?: string +async function execCommands(commands: string[], cwd?: string): Promise { + for (const command of commands) { + try { + await exec(command, undefined, { + cwd, + env: {...(process.env as object), MSYS: 'winsymlinks:nativestrict'} + }) + } catch (error) { + throw new Error( + `${command.split(' ')[0]} failed with error: ${error?.message}` + ) + } + } +} + +async function execReadCommands( + archivePath: string, + compressionMethod: CompressionMethod, + type: 'list' | 'extract' ): Promise { + const {commands, requiresTempDirectory} = await getCommands( + compressionMethod, + type, + archivePath + ) // The Windows BSD-tar fallback decompresses into cache.tar before reading // it. Isolate that intermediate for each list/extract operation, including - // debug listings during saves. Creation already has its own archive folder. + // debug listings during saves. const tempDirectory = requiresTempDirectory ? await utils.createTempDirectory() : undefined try { - for (const command of commands) { - try { - await exec(command, undefined, { - cwd: tempDirectory ?? cwd, - env: {...(process.env as object), MSYS: 'winsymlinks:nativestrict'} - }) - } catch (error) { - throw new Error( - `${command.split(' ')[0]} failed with error: ${error?.message}` - ) - } - } + await execCommands(commands, tempDirectory) } finally { if (tempDirectory) { await io.rmRF(tempDirectory) @@ -286,8 +296,7 @@ export async function listTar( archivePath: string, compressionMethod: CompressionMethod ): Promise { - const commands = await getCommands(compressionMethod, 'list', archivePath) - await execCommands(commands) + await execReadCommands(archivePath, compressionMethod, 'list') } // Extract a tar @@ -298,8 +307,7 @@ export async function extractTar( // Create directory to extract tar into const workingDirectory = getWorkingDirectory() await io.mkdirP(workingDirectory) - const commands = await getCommands(compressionMethod, 'extract', archivePath) - await execCommands(commands) + await execReadCommands(archivePath, compressionMethod, 'extract') } // Create a tar @@ -313,6 +321,6 @@ export async function createTar( path.join(archiveFolder, ManifestFilename), sourceDirectories.join('\n') ) - const commands = await getCommands(compressionMethod, 'create') + const {commands} = await getCommands(compressionMethod, 'create') await execCommands(commands, archiveFolder) }