Skip to content

Commit 77761a4

Browse files
Aiqiao YanAiqiao Yan
authored andcommitted
Fix issue with using zstd long range mode on ubuntu-16.04
1 parent a67b91e commit 77761a4

File tree

6 files changed

+58
-15
lines changed

6 files changed

+58
-15
lines changed

packages/cache/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cache/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141
"@actions/glob": "^0.1.0",
4242
"@actions/http-client": "^1.0.8",
4343
"@actions/io": "^1.0.1",
44+
"semver": "^6.1.0",
4445
"uuid": "^3.3.3"
4546
},
4647
"devDependencies": {
4748
"typescript": "^3.8.3",
49+
"@types/semver": "^6.0.0",
4850
"@types/uuid": "^3.4.5"
4951
}
5052
}

packages/cache/src/internal/cacheHttpClient.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ export function getCacheVersion(
9696
compressionMethod?: CompressionMethod
9797
): string {
9898
const components = paths.concat(
99-
compressionMethod === CompressionMethod.Zstd ? [compressionMethod] : []
99+
!compressionMethod || compressionMethod === CompressionMethod.Gzip
100+
? []
101+
: [compressionMethod]
100102
)
101103

102104
// Add salt to cache version to support breaking changes in cache entry

packages/cache/src/internal/cacheUtils.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as glob from '@actions/glob'
44
import * as io from '@actions/io'
55
import * as fs from 'fs'
66
import * as path from 'path'
7+
import * as semver from 'semver'
78
import * as util from 'util'
89
import {v4 as uuidV4} from 'uuid'
910
import {CacheFilename, CompressionMethod} from './constants'
@@ -82,16 +83,24 @@ async function getVersion(app: string): Promise<string> {
8283

8384
// Use zstandard if possible to maximize cache performance
8485
export async function getCompressionMethod(): Promise<CompressionMethod> {
85-
const versionOutput = await getVersion('zstd')
86-
return versionOutput.toLowerCase().includes('zstd command line interface')
87-
? CompressionMethod.Zstd
88-
: CompressionMethod.Gzip
86+
if (process.platform === 'win32') {
87+
// Disable zstd on windows due to bug https://github.com/actions/cache/issues/301
88+
return CompressionMethod.Gzip
89+
} else {
90+
const versionOutput = await getVersion('zstd')
91+
const version = semver.clean(versionOutput)
92+
return !versionOutput.toLowerCase().includes('zstd command line interface')
93+
? CompressionMethod.Gzip
94+
: !version || semver.lt(version, 'v1.3.2')
95+
? CompressionMethod.ZstdOld
96+
: CompressionMethod.Zstd
97+
}
8998
}
9099

91100
export function getCacheFileName(compressionMethod: CompressionMethod): string {
92-
return compressionMethod === CompressionMethod.Zstd
93-
? CacheFilename.Zstd
94-
: CacheFilename.Gzip
101+
return compressionMethod === CompressionMethod.Gzip
102+
? CacheFilename.Gzip
103+
: CacheFilename.Zstd
95104
}
96105

97106
export async function useGnuTar(): Promise<boolean> {

packages/cache/src/internal/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export enum CacheFilename {
55

66
export enum CompressionMethod {
77
Gzip = 'gzip',
8+
ZstdOld = 'zstd-old',
89
Zstd = 'zstd'
910
}
1011

packages/cache/src/internal/tar.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ export async function extractTar(
4141
// --d: Decompress.
4242
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
4343
// Using 30 here because we also support 32-bit self-hosted runners.
44+
function getProg(): string[] {
45+
switch (compressionMethod) {
46+
case CompressionMethod.Zstd:
47+
return ['--use-compress-program', 'zstd -d --long=30']
48+
case CompressionMethod.ZstdOld:
49+
return ['--use-compress-program', 'zstd -d']
50+
default:
51+
return ['-z']
52+
}
53+
}
4454
const args = [
45-
...(compressionMethod === CompressionMethod.Zstd
46-
? ['--use-compress-program', 'zstd -d --long=30']
47-
: ['-z']),
55+
...getProg(),
4856
'-xf',
4957
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
5058
'-P',
@@ -66,14 +74,24 @@ export async function createTar(
6674
path.join(archiveFolder, manifestFilename),
6775
sourceDirectories.join('\n')
6876
)
77+
const workingDirectory = getWorkingDirectory()
78+
6979
// -T#: Compress using # working thread. If # is 0, attempt to detect and use the number of physical CPU cores.
7080
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
7181
// Using 30 here because we also support 32-bit self-hosted runners.
72-
const workingDirectory = getWorkingDirectory()
82+
// Long range mode is added to zstd in v1.3.2 release, so we will not use --long in older version of zstd.
83+
function getProg(): string[] {
84+
switch (compressionMethod) {
85+
case CompressionMethod.Zstd:
86+
return ['--use-compress-program', 'zstd -T0 --long=30']
87+
case CompressionMethod.ZstdOld:
88+
return ['--use-compress-program', 'zstd -T0']
89+
default:
90+
return ['-z']
91+
}
92+
}
7393
const args = [
74-
...(compressionMethod === CompressionMethod.Zstd
75-
? ['--use-compress-program', 'zstd -T0 --long=30']
76-
: ['-z']),
94+
...getProg(),
7795
'-cf',
7896
cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
7997
'-P',

0 commit comments

Comments
 (0)