From 54eb9b8055b5027dd2907ce2e8ec17b60222c190 Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Mon, 21 Nov 2022 12:05:03 +0000 Subject: [PATCH] Address some comments and correct compression commands --- packages/cache/__tests__/tar.test.ts | 12 +++--- packages/cache/src/internal/cacheUtils.ts | 5 --- packages/cache/src/internal/tar.ts | 47 +++++++++++++---------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/packages/cache/__tests__/tar.test.ts b/packages/cache/__tests__/tar.test.ts index 3166ad68..bdb79cb8 100644 --- a/packages/cache/__tests__/tar.test.ts +++ b/packages/cache/__tests__/tar.test.ts @@ -149,6 +149,8 @@ test('zstd create tar', async () => { `"${tarPath}"`, [ '--posix', + '-cf', + IS_WINDOWS ? CacheFilename.Zstd.replace(/\\/g, '/') : CacheFilename.Zstd, '--exclude', IS_WINDOWS ? CacheFilename.Zstd.replace(/\\/g, '/') : CacheFilename.Zstd, '-P', @@ -157,9 +159,7 @@ test('zstd create tar', async () => { '--files-from', 'manifest.txt', '--use-compress-program', - IS_WINDOWS ? 'zstd -T0 --long=30' : 'zstdmt --long=30', - '-cf', - IS_WINDOWS ? CacheFilename.Zstd.replace(/\\/g, '/') : CacheFilename.Zstd + IS_WINDOWS ? 'zstd -T0 --long=30' : 'zstdmt --long=30' ] .concat(IS_WINDOWS ? ['--force-local'] : []) .concat(IS_MAC ? ['--delay-directory-restore'] : []), @@ -187,6 +187,8 @@ test('gzip create tar', async () => { `"${tarPath}"`, [ '--posix', + '-cf', + IS_WINDOWS ? CacheFilename.Gzip.replace(/\\/g, '/') : CacheFilename.Gzip, '--exclude', IS_WINDOWS ? CacheFilename.Gzip.replace(/\\/g, '/') : CacheFilename.Gzip, '-P', @@ -194,9 +196,7 @@ test('gzip create tar', async () => { IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace, '--files-from', 'manifest.txt', - '-z', - '-cf', - IS_WINDOWS ? CacheFilename.Gzip.replace(/\\/g, '/') : CacheFilename.Gzip + '-z' ] .concat(IS_WINDOWS ? ['--force-local'] : []) .concat(IS_MAC ? ['--delay-directory-restore'] : []), diff --git a/packages/cache/src/internal/cacheUtils.ts b/packages/cache/src/internal/cacheUtils.ts index 03c52561..ea1e7de6 100644 --- a/packages/cache/src/internal/cacheUtils.ts +++ b/packages/cache/src/internal/cacheUtils.ts @@ -94,11 +94,6 @@ async function getVersion(app: string): Promise { // Use zstandard if possible to maximize cache performance export async function getCompressionMethod(): Promise { - // if (process.platform === 'win32' && !(await getGnuTarPathOnWindows())) { - // // Disable zstd due to bug https://github.com/actions/cache/issues/301 - // return CompressionMethod.Gzip - // } - const versionOutput = await getVersion('zstd') const version = semver.clean(versionOutput) diff --git a/packages/cache/src/internal/tar.ts b/packages/cache/src/internal/tar.ts index 182bc7bb..99c638ec 100644 --- a/packages/cache/src/internal/tar.ts +++ b/packages/cache/src/internal/tar.ts @@ -62,10 +62,10 @@ async function getCompressionProgram( // --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit. // Using 30 here because we also support 32-bit self-hosted runners. const tarPath = await getTarPath([]) - const BSD_TAR_ZSTD = IS_WINDOWS && tarPath === SystemTarPathOnWindows + const BSD_TAR_WINDOWS = IS_WINDOWS && tarPath === SystemTarPathOnWindows switch (compressionMethod) { case CompressionMethod.Zstd: - if (BSD_TAR_ZSTD) { + if (BSD_TAR_WINDOWS) { return ['-a'] // auto-detect compression } return [ @@ -73,7 +73,7 @@ async function getCompressionProgram( IS_WINDOWS ? 'zstd -d --long=30' : 'unzstd --long=30' ] case CompressionMethod.ZstdWithoutLong: - if (BSD_TAR_ZSTD) { + if (BSD_TAR_WINDOWS) { return ['a'] // auto-detect compression } return ['--use-compress-program', IS_WINDOWS ? 'zstd -d' : 'unzstd'] @@ -121,6 +121,9 @@ export async function createTar( // Write source directories to manifest.txt to avoid command length limits const manifestFilename = 'manifest.txt' const cacheFileName = utils.getCacheFileName(compressionMethod) + const tarFile = 'cache.tar' + const tarPath = await getTarPath([]) + const BSD_TAR_WINDOWS = IS_WINDOWS && tarPath === SystemTarPathOnWindows writeFileSync( path.join(archiveFolder, manifestFilename), sourceDirectories.join('\n') @@ -133,42 +136,44 @@ export async function createTar( // Using 30 here because we also support 32-bit self-hosted runners. // Long range mode is added to zstd in v1.3.2 release, so we will not use --long in older version of zstd. async function getCompressionProgram(): Promise { - const tarPath = await getTarPath([]) - const BSD_TAR_ZSTD = IS_WINDOWS && tarPath === SystemTarPathOnWindows switch (compressionMethod) { case CompressionMethod.Zstd: - if (BSD_TAR_ZSTD) { - return ['-O', '|', 'zstd -T0 --long=30 -o'] + if (BSD_TAR_WINDOWS) { + return ['&&', 'zstd -T0 --long=30 -o'] } return [ '--use-compress-program', - IS_WINDOWS ? 'zstd -T0 --long=30' : 'zstdmt --long=30', - '-cf' + IS_WINDOWS ? 'zstd -T0 --long=30' : 'zstdmt --long=30' ] case CompressionMethod.ZstdWithoutLong: - if (BSD_TAR_ZSTD) { - return ['-O', '|', 'zstd -T0 -o'] + if (BSD_TAR_WINDOWS) { + return ['&&', 'zstd -T0 -o'] } - return [ - '--use-compress-program', - IS_WINDOWS ? 'zstd -T0' : 'zstdmt', - '-cf' - ] + return ['--use-compress-program', IS_WINDOWS ? 'zstd -T0' : 'zstdmt'] default: - return ['-z', '-cf'] + return ['-z'] } } const args = [ '--posix', + '-cf', + BSD_TAR_WINDOWS + ? tarFile + : cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), '--exclude', - cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), + BSD_TAR_WINDOWS + ? tarFile + : cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), '-P', '-C', workingDirectory.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), '--files-from', manifestFilename, - ...(await getCompressionProgram()), - cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/') - ] + ...(await getCompressionProgram()) + ].concat( + BSD_TAR_WINDOWS + ? [cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/')] + : [] + ) await execTar(args, archiveFolder) }