From 2c3ea16a5925fc8206e3aced97bb6ffa74acb86f Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Wed, 30 Aug 2023 00:36:04 +0800 Subject: [PATCH 1/4] fix: add arch to cached path --- __tests__/cache-restore.test.ts | 6 +++++- dist/setup/index.js | 4 +++- src/cache-restore.ts | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 90153a403..770497a70 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -11,7 +11,11 @@ describe('cache-restore', () => { if (!process.env.RUNNER_OS) { process.env.RUNNER_OS = 'Linux'; } + if (!process.env.RUNNER_ARCH) { + process.env.RUNNER_ARCH = 'X64'; + } const platform = process.env.RUNNER_OS; + const arch = process.env.RUNNER_ARCH; const commonPath = '/some/random/path'; const npmCachePath = `${commonPath}/npm`; const pnpmCachePath = `${commonPath}/pnpm`; @@ -135,7 +139,7 @@ describe('cache-restore', () => { await restoreCache(packageManager, ''); expect(hashFilesSpy).toHaveBeenCalled(); expect(infoSpy).toHaveBeenCalledWith( - `Cache restored from key: node-cache-${platform}-${packageManager}-${fileHash}` + `Cache restored from key: node-cache-${platform}-${arch}-${packageManager}-${fileHash}` ); expect(infoSpy).not.toHaveBeenCalledWith( `${packageManager} cache is not found` diff --git a/dist/setup/index.js b/dist/setup/index.js index c4b448b1d..4fc551a6b 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -92559,6 +92559,7 @@ const core = __importStar(__nccwpck_require__(2186)); const glob = __importStar(__nccwpck_require__(8090)); const path_1 = __importDefault(__nccwpck_require__(1017)); const fs_1 = __importDefault(__nccwpck_require__(7147)); +const os_1 = __importDefault(__nccwpck_require__(2037)); const constants_1 = __nccwpck_require__(9042); const cache_utils_1 = __nccwpck_require__(1678); const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () { @@ -92567,6 +92568,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, throw new Error(`Caching for '${packageManager}' is not supported`); } const platform = process.env.RUNNER_OS; + const arch = os_1.default.arch(); const cachePaths = yield (0, cache_utils_1.getCacheDirectories)(packageManagerInfo, cacheDependencyPath); core.saveState(constants_1.State.CachePaths, cachePaths); const lockFilePath = cacheDependencyPath @@ -92576,7 +92578,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, if (!fileHash) { throw new Error('Some specified paths were not resolved, unable to cache dependencies.'); } - const keyPrefix = `node-cache-${platform}-${packageManager}`; + const keyPrefix = `node-cache-${platform}-${arch}-${packageManager}`; const primaryKey = `${keyPrefix}-${fileHash}`; core.debug(`primary key is ${primaryKey}`); core.saveState(constants_1.State.CachePrimaryKey, primaryKey); diff --git a/src/cache-restore.ts b/src/cache-restore.ts index 3b230970e..af12ad83c 100644 --- a/src/cache-restore.ts +++ b/src/cache-restore.ts @@ -3,6 +3,7 @@ import * as core from '@actions/core'; import * as glob from '@actions/glob'; import path from 'path'; import fs from 'fs'; +import os from 'os'; import {State} from './constants'; import { @@ -21,6 +22,7 @@ export const restoreCache = async ( throw new Error(`Caching for '${packageManager}' is not supported`); } const platform = process.env.RUNNER_OS; + const arch = os.arch(); const cachePaths = await getCacheDirectories( packageManagerInfo, @@ -38,7 +40,7 @@ export const restoreCache = async ( ); } - const keyPrefix = `node-cache-${platform}-${packageManager}`; + const keyPrefix = `node-cache-${platform}-${arch}-${packageManager}`; const primaryKey = `${keyPrefix}-${fileHash}`; core.debug(`primary key is ${primaryKey}`); From 8454a14c98aea86afafe1a0a5cbcffd3e5244805 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Tue, 26 Dec 2023 17:11:46 +0800 Subject: [PATCH 2/4] fix: change from using env to os module --- __tests__/cache-restore.test.ts | 20 ++++++++++++-------- dist/setup/index.js | 2 +- src/cache-restore.ts | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 770497a70..8ff99b07d 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -2,20 +2,15 @@ import * as core from '@actions/core'; import * as cache from '@actions/cache'; import * as path from 'path'; import * as glob from '@actions/glob'; +import osm from 'os'; import * as utils from '../src/cache-utils'; import {restoreCache} from '../src/cache-restore'; describe('cache-restore', () => { process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); - if (!process.env.RUNNER_OS) { - process.env.RUNNER_OS = 'Linux'; - } - if (!process.env.RUNNER_ARCH) { - process.env.RUNNER_ARCH = 'X64'; - } - const platform = process.env.RUNNER_OS; - const arch = process.env.RUNNER_ARCH; + const platform = 'Linux'; + const arch = 'arm64'; const commonPath = '/some/random/path'; const npmCachePath = `${commonPath}/npm`; const pnpmCachePath = `${commonPath}/pnpm`; @@ -56,6 +51,8 @@ describe('cache-restore', () => { let getCommandOutputSpy: jest.SpyInstance; let restoreCacheSpy: jest.SpyInstance; let hashFilesSpy: jest.SpyInstance; + let archSpy: jest.SpyInstance; + let platformSpy: jest.SpyInstance; beforeEach(() => { // core @@ -106,6 +103,13 @@ describe('cache-restore', () => { // cache-utils getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput'); + + // os + archSpy = jest.spyOn(osm, 'arch'); + archSpy.mockImplementation(() => arch); + + platformSpy = jest.spyOn(osm, 'platform'); + platformSpy.mockImplementation(() => platform); }); describe('Validate provided package manager', () => { diff --git a/dist/setup/index.js b/dist/setup/index.js index 4fc551a6b..4fb187cff 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -92567,7 +92567,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, if (!packageManagerInfo) { throw new Error(`Caching for '${packageManager}' is not supported`); } - const platform = process.env.RUNNER_OS; + const platform = os_1.default.platform(); const arch = os_1.default.arch(); const cachePaths = yield (0, cache_utils_1.getCacheDirectories)(packageManagerInfo, cacheDependencyPath); core.saveState(constants_1.State.CachePaths, cachePaths); diff --git a/src/cache-restore.ts b/src/cache-restore.ts index af12ad83c..f5ad58ffa 100644 --- a/src/cache-restore.ts +++ b/src/cache-restore.ts @@ -21,7 +21,7 @@ export const restoreCache = async ( if (!packageManagerInfo) { throw new Error(`Caching for '${packageManager}' is not supported`); } - const platform = process.env.RUNNER_OS; + const platform = os.platform(); const arch = os.arch(); const cachePaths = await getCacheDirectories( From 2db60599ddbb406d2397a20092cc135c8e63746c Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Tue, 24 Sep 2024 18:23:13 +0800 Subject: [PATCH 3/4] fix: use process.env.RUNNER_OS instead of os.platform() --- __tests__/cache-restore.test.ts | 8 ++++---- dist/setup/index.js | 2 +- src/cache-restore.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 8ff99b07d..94c6aba6b 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -9,7 +9,10 @@ import {restoreCache} from '../src/cache-restore'; describe('cache-restore', () => { process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); - const platform = 'Linux'; + if (!process.env.RUNNER_OS) { + process.env.RUNNER_OS = 'Linux'; + } + const platform = process.env.RUNNER_OS; const arch = 'arm64'; const commonPath = '/some/random/path'; const npmCachePath = `${commonPath}/npm`; @@ -107,9 +110,6 @@ describe('cache-restore', () => { // os archSpy = jest.spyOn(osm, 'arch'); archSpy.mockImplementation(() => arch); - - platformSpy = jest.spyOn(osm, 'platform'); - platformSpy.mockImplementation(() => platform); }); describe('Validate provided package manager', () => { diff --git a/dist/setup/index.js b/dist/setup/index.js index d9e700111..5465f259a 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -92567,7 +92567,7 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, if (!packageManagerInfo) { throw new Error(`Caching for '${packageManager}' is not supported`); } - const platform = os_1.default.platform(); + const platform = process.env.RUNNER_OS; const arch = os_1.default.arch(); const cachePaths = yield (0, cache_utils_1.getCacheDirectories)(packageManagerInfo, cacheDependencyPath); core.saveState(constants_1.State.CachePaths, cachePaths); diff --git a/src/cache-restore.ts b/src/cache-restore.ts index f5ad58ffa..af12ad83c 100644 --- a/src/cache-restore.ts +++ b/src/cache-restore.ts @@ -21,7 +21,7 @@ export const restoreCache = async ( if (!packageManagerInfo) { throw new Error(`Caching for '${packageManager}' is not supported`); } - const platform = os.platform(); + const platform = process.env.RUNNER_OS; const arch = os.arch(); const cachePaths = await getCacheDirectories( From 408bb570d1f7df1190020443473df010214cb9ad Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Tue, 24 Sep 2024 18:26:22 +0800 Subject: [PATCH 4/4] fix: remove unused var --- __tests__/cache-restore.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 94c6aba6b..0bbf28126 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -55,7 +55,6 @@ describe('cache-restore', () => { let restoreCacheSpy: jest.SpyInstance; let hashFilesSpy: jest.SpyInstance; let archSpy: jest.SpyInstance; - let platformSpy: jest.SpyInstance; beforeEach(() => { // core