Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Make the path be handled politely during path resolution #733

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions packages/cache/__tests__/tar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand All @@ -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)

Expand All @@ -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}
)
Expand All @@ -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)

Expand All @@ -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}
)
Expand All @@ -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)

Expand All @@ -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}
Expand All @@ -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})
Expand All @@ -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'] : []),
Expand All @@ -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})
Expand All @@ -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'
],
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
)
})
8 changes: 3 additions & 5 deletions packages/cache/src/internal/cacheUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ export function getArchiveFileSizeIsBytes(filePath: string): number {

export async function resolvePaths(patterns: string[]): Promise<string[]> {
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
Expand Down
18 changes: 7 additions & 11 deletions packages/cache/src/internal/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
// 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.
Expand All @@ -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)
}
Expand All @@ -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.
Expand All @@ -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
]
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 2 additions & 10 deletions packages/tool-cache/src/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down