From 75256790ddaffca50316a4785857202945ebbc13 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Thu, 11 Mar 2021 08:00:10 +0900 Subject: [PATCH] Make the path be handled politely during path resolution Signed-off-by: Sora Morimoto --- packages/cache/__tests__/tar.test.ts | 43 ++++++++++------------- packages/cache/src/internal/cacheUtils.ts | 8 ++--- packages/cache/src/internal/tar.ts | 18 ++++------ packages/tool-cache/src/tool-cache.ts | 12 ++----- 4 files changed, 30 insertions(+), 51 deletions(-) diff --git a/packages/cache/__tests__/tar.test.ts b/packages/cache/__tests__/tar.test.ts index 3f7a1b3b91..9ab09ddf9a 100644 --- a/packages/cache/__tests__/tar.test.ts +++ b/packages/cache/__tests__/tar.test.ts @@ -23,12 +23,10 @@ beforeAll(async () => { return tool }) - process.env['GITHUB_WORKSPACE'] = process.cwd() await jest.requireActual('@actions/io').rmRF(getTempDir()) }) afterAll(async () => { - delete process.env['GITHUB_WORKSPACE'] await jest.requireActual('@actions/io').rmRF(getTempDir()) }) @@ -39,7 +37,7 @@ test('zstd extract tar', async () => { const archivePath = IS_WINDOWS ? `${process.env['windir']}\\fakepath\\cache.tar` : 'cache.tar' - const workspace = process.env['GITHUB_WORKSPACE'] + const workspace = process.cwd() await tar.extractTar(archivePath, CompressionMethod.Zstd) @@ -51,10 +49,10 @@ test('zstd extract tar', async () => { '--use-compress-program', 'zstd -d --long=30', '-xf', - IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath, + path.normalize(archivePath), '-P', '-C', - IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace + path.normalize(workspace) ].concat(IS_WINDOWS ? ['--force-local'] : []), {cwd: undefined} ) @@ -66,7 +64,7 @@ test('gzip extract tar', async () => { const archivePath = IS_WINDOWS ? `${process.env['windir']}\\fakepath\\cache.tar` : 'cache.tar' - const workspace = process.env['GITHUB_WORKSPACE'] + const workspace = process.cwd() await tar.extractTar(archivePath, CompressionMethod.Gzip) @@ -80,10 +78,10 @@ test('gzip extract tar', async () => { [ '-z', '-xf', - IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath, + path.normalize(archivePath), '-P', '-C', - IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace + path.normalize(workspace) ], {cwd: undefined} ) @@ -98,7 +96,7 @@ test('gzip extract GNU tar on windows', async () => { .mockReturnValue(Promise.resolve(true)) const execMock = jest.spyOn(exec, 'exec') const archivePath = `${process.env['windir']}\\fakepath\\cache.tar` - const workspace = process.env['GITHUB_WORKSPACE'] + const workspace = process.cwd() await tar.extractTar(archivePath, CompressionMethod.Gzip) @@ -109,10 +107,10 @@ test('gzip extract GNU tar on windows', async () => { [ '-z', '-xf', - archivePath.replace(/\\/g, '/'), + path.normalize(archivePath), '-P', '-C', - workspace?.replace(/\\/g, '/'), + path.normalize(workspace), '--force-local' ], {cwd: undefined} @@ -124,7 +122,7 @@ test('zstd create tar', async () => { const execMock = jest.spyOn(exec, 'exec') const archiveFolder = getTempDir() - const workspace = process.env['GITHUB_WORKSPACE'] + const workspace = process.cwd() const sourceDirectories = ['~/.npm/cache', `${workspace}/dist`] await fs.promises.mkdir(archiveFolder, {recursive: true}) @@ -139,10 +137,10 @@ test('zstd create tar', async () => { '--use-compress-program', 'zstd -T0 --long=30', '-cf', - IS_WINDOWS ? CacheFilename.Zstd.replace(/\\/g, '/') : CacheFilename.Zstd, + path.normalize(CacheFilename.Zstd), '-P', '-C', - IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace, + path.normalize(workspace), '--files-from', 'manifest.txt' ].concat(IS_WINDOWS ? ['--force-local'] : []), @@ -156,7 +154,7 @@ test('gzip create tar', async () => { const execMock = jest.spyOn(exec, 'exec') const archiveFolder = getTempDir() - const workspace = process.env['GITHUB_WORKSPACE'] + const workspace = process.cwd() const sourceDirectories = ['~/.npm/cache', `${workspace}/dist`] await fs.promises.mkdir(archiveFolder, {recursive: true}) @@ -174,10 +172,10 @@ test('gzip create tar', async () => { '--posix', '-z', '-cf', - IS_WINDOWS ? CacheFilename.Gzip.replace(/\\/g, '/') : CacheFilename.Gzip, + path.normalize(CacheFilename.Gzip), '-P', '-C', - IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace, + path.normalize(workspace), '--files-from', 'manifest.txt' ], @@ -203,7 +201,7 @@ test('zstd list tar', async () => { '--use-compress-program', 'zstd -d --long=30', '-tf', - IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath, + path.normalize(archivePath), '-P' ].concat(IS_WINDOWS ? ['--force-local'] : []), {cwd: undefined} @@ -226,7 +224,7 @@ test('zstdWithoutLong list tar', async () => { '--use-compress-program', 'zstd -d', '-tf', - IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath, + path.normalize(archivePath), '-P' ].concat(IS_WINDOWS ? ['--force-local'] : []), {cwd: undefined} @@ -247,12 +245,7 @@ test('gzip list tar', async () => { expect(execMock).toHaveBeenCalledTimes(1) expect(execMock).toHaveBeenCalledWith( `"${tarPath}"`, - [ - '-z', - '-tf', - IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath, - '-P' - ], + ['-z', '-tf', path.normalize(archivePath), '-P'], {cwd: undefined} ) }) diff --git a/packages/cache/src/internal/cacheUtils.ts b/packages/cache/src/internal/cacheUtils.ts index 36044d48e5..201ab0aa33 100644 --- a/packages/cache/src/internal/cacheUtils.ts +++ b/packages/cache/src/internal/cacheUtils.ts @@ -41,18 +41,16 @@ export function getArchiveFileSizeIsBytes(filePath: string): number { export async function resolvePaths(patterns: string[]): Promise { const paths: string[] = [] - const workspace = process.env['GITHUB_WORKSPACE'] ?? process.cwd() const globber = await glob.create(patterns.join('\n'), { implicitDescendants: false }) for await (const file of globber.globGenerator()) { - const relativeFile = path - .relative(workspace, file) - .replace(new RegExp(`\\${path.sep}`, 'g'), '/') + const cwd = process.cwd() + const relativeFile = path.normalize(path.relative(cwd, file)) core.debug(`Matched: ${relativeFile}`) // Paths are made relative so the tar entries are all relative to the root of the workspace. - paths.push(`${relativeFile}`) + paths.push(relativeFile) } return paths diff --git a/packages/cache/src/internal/tar.ts b/packages/cache/src/internal/tar.ts index 1b70dac09f..9ad42dcee2 100644 --- a/packages/cache/src/internal/tar.ts +++ b/packages/cache/src/internal/tar.ts @@ -48,16 +48,12 @@ async function execTar( } } -function getWorkingDirectory(): string { - return process.env['GITHUB_WORKSPACE'] ?? process.cwd() -} - export async function extractTar( archivePath: string, compressionMethod: CompressionMethod ): Promise { // Create directory to extract tar into - const workingDirectory = getWorkingDirectory() + const workingDirectory = process.cwd() await io.mkdirP(workingDirectory) // --d: Decompress. // --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit. @@ -75,10 +71,10 @@ export async function extractTar( const args = [ ...getCompressionProgram(), '-xf', - archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), + path.normalize(archivePath), '-P', '-C', - workingDirectory.replace(new RegExp(`\\${path.sep}`, 'g'), '/') + path.normalize(workingDirectory) ] await execTar(args, compressionMethod) } @@ -95,7 +91,7 @@ export async function createTar( path.join(archiveFolder, manifestFilename), sourceDirectories.join('\n') ) - const workingDirectory = getWorkingDirectory() + const workingDirectory = process.cwd() // -T#: Compress using # working thread. If # is 0, attempt to detect and use the number of physical CPU cores. // --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit. @@ -115,10 +111,10 @@ export async function createTar( '--posix', ...getCompressionProgram(), '-cf', - cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), + path.normalize(cacheFileName), '-P', '-C', - workingDirectory.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), + path.normalize(workingDirectory), '--files-from', manifestFilename ] @@ -146,7 +142,7 @@ export async function listTar( const args = [ ...getCompressionProgram(), '-tf', - archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), + path.normalize(archivePath), '-P' ] await execTar(args, compressionMethod) diff --git a/packages/tool-cache/src/tool-cache.ts b/packages/tool-cache/src/tool-cache.ts index 3e9ecab409..085d5f42d4 100644 --- a/packages/tool-cache/src/tool-cache.ts +++ b/packages/tool-cache/src/tool-cache.ts @@ -255,16 +255,8 @@ export async function extractTar( args.push('-v') } - let destArg = dest - let fileArg = file - if (IS_WINDOWS && isGnuTar) { - args.push('--force-local') - destArg = dest.replace(/\\/g, '/') - - // Technically only the dest needs to have `/` but for aesthetic consistency - // convert slashes in the file arg too. - fileArg = file.replace(/\\/g, '/') - } + const destArg = path.normalize(dest) + const fileArg = path.normalize(file) if (isGnuTar) { // Suppress warnings when using GNU tar to extract archives created by BSD tar