diff --git a/src/cache-distributions/cache-distributor.ts b/src/cache-distributions/cache-distributor.ts index 211946fbb..efc9eee4e 100644 --- a/src/cache-distributions/cache-distributor.ts +++ b/src/cache-distributions/cache-distributor.ts @@ -1,6 +1,5 @@ import * as cache from '@actions/cache'; import * as core from '@actions/core'; -import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants'; export enum State { STATE_CACHE_PRIMARY_KEY = 'cache-primary-key', @@ -10,27 +9,20 @@ export enum State { abstract class CacheDistributor { protected CACHE_KEY_PREFIX = 'setup-python'; - constructor( - protected packageManager: string, - protected cacheDependencyPath: string - ) {} + protected abstract readonly packageManager: string; protected abstract getCacheGlobalDirectories(): Promise; protected abstract computeKeys(): Promise<{ primaryKey: string; restoreKey: string[] | undefined; + cacheDependencyPath: string; }>; protected async handleLoadedCache() {} public async restoreCache() { - const {primaryKey, restoreKey} = await this.computeKeys(); + const {primaryKey, restoreKey, cacheDependencyPath} = await this.computeKeys(); if (primaryKey.endsWith('-')) { - const file = - this.packageManager === 'pip' - ? `${this.cacheDependencyPath - .split('\n') - .join(',')} or ${CACHE_DEPENDENCY_BACKUP_PATH}` - : this.cacheDependencyPath.split('\n').join(','); + const file = cacheDependencyPath.split('\n').join(','); throw new Error( `No file in ${process.cwd()} matched to [${file}], make sure you have checked out the target repository` ); diff --git a/src/cache-distributions/constants.ts b/src/cache-distributions/constants.ts deleted file mode 100644 index 08dc74507..000000000 --- a/src/cache-distributions/constants.ts +++ /dev/null @@ -1 +0,0 @@ -export const CACHE_DEPENDENCY_BACKUP_PATH = '**/pyproject.toml'; diff --git a/src/cache-distributions/pip-cache.ts b/src/cache-distributions/pip-cache.ts index d64ae931d..16450c671 100644 --- a/src/cache-distributions/pip-cache.ts +++ b/src/cache-distributions/pip-cache.ts @@ -8,16 +8,16 @@ import os from 'os'; import CacheDistributor from './cache-distributor'; import {getLinuxInfo, IS_LINUX, IS_WINDOWS} from '../utils'; -import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants'; class PipCache extends CacheDistributor { - private cacheDependencyBackupPath: string = CACHE_DEPENDENCY_BACKUP_PATH; + private cacheDependencyBackupPath = '**/pyproject.toml'; + protected readonly packageManager = 'pip'; constructor( private pythonVersion: string, - cacheDependencyPath = '**/requirements.txt' + protected readonly cacheDependencyPath = '**/requirements.txt' ) { - super('pip', cacheDependencyPath); + super(); } protected async getCacheGlobalDirectories() { @@ -59,9 +59,12 @@ class PipCache extends CacheDistributor { } protected async computeKeys() { - const hash = - (await glob.hashFiles(this.cacheDependencyPath)) || - (await glob.hashFiles(this.cacheDependencyBackupPath)); + let cacheDependencyPath = this.cacheDependencyPath; + let hash = await glob.hashFiles(this.cacheDependencyPath); + if(!hash) { + hash = await glob.hashFiles(this.cacheDependencyBackupPath); + cacheDependencyPath = this.cacheDependencyBackupPath; + } let primaryKey = ''; let restoreKey = ''; @@ -76,7 +79,8 @@ class PipCache extends CacheDistributor { return { primaryKey, - restoreKey: [restoreKey] + restoreKey: [restoreKey], + cacheDependencyPath, }; } } diff --git a/src/cache-distributions/pipenv-cache.ts b/src/cache-distributions/pipenv-cache.ts index 8ddcbed86..66627caf5 100644 --- a/src/cache-distributions/pipenv-cache.ts +++ b/src/cache-distributions/pipenv-cache.ts @@ -6,11 +6,13 @@ import * as core from '@actions/core'; import CacheDistributor from './cache-distributor'; class PipenvCache extends CacheDistributor { + protected readonly packageManager = 'pipenv'; + constructor( private pythonVersion: string, - protected patterns: string = '**/Pipfile.lock' + protected readonly cacheDependencyPath: string = '**/Pipfile.lock' ) { - super('pipenv', patterns); + super(); } protected async getCacheGlobalDirectories() { @@ -31,12 +33,13 @@ class PipenvCache extends CacheDistributor { } protected async computeKeys() { - const hash = await glob.hashFiles(this.patterns); + const hash = await glob.hashFiles(this.cacheDependencyPath); const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${process.arch}-python-${this.pythonVersion}-${this.packageManager}-${hash}`; const restoreKey = undefined; return { primaryKey, - restoreKey + restoreKey, + cacheDependencyPath: this.cacheDependencyPath, }; } } diff --git a/src/cache-distributions/poetry-cache.ts b/src/cache-distributions/poetry-cache.ts index cd3a189b2..b8b83a254 100644 --- a/src/cache-distributions/poetry-cache.ts +++ b/src/cache-distributions/poetry-cache.ts @@ -8,18 +8,21 @@ import CacheDistributor from './cache-distributor'; import {logWarning} from '../utils'; class PoetryCache extends CacheDistributor { + protected readonly packageManager = 'poetry'; + + constructor( private pythonVersion: string, - protected patterns: string = '**/poetry.lock', + protected readonly cacheDependencyPath: string = '**/poetry.lock', protected poetryProjects: Set = new Set() ) { - super('poetry', patterns); + super(); } protected async getCacheGlobalDirectories() { // Same virtualenvs path may appear for different projects, hence we use a Set const paths = new Set(); - const globber = await glob.create(this.patterns); + const globber = await glob.create(this.cacheDependencyPath); for await (const file of globber.globGenerator()) { const basedir = path.dirname(file); @@ -45,13 +48,14 @@ class PoetryCache extends CacheDistributor { } protected async computeKeys() { - const hash = await glob.hashFiles(this.patterns); + const hash = await glob.hashFiles(this.cacheDependencyPath); // "v2" is here to invalidate old caches of this cache distributor, which were created broken: const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${process.arch}-python-${this.pythonVersion}-${this.packageManager}-v2-${hash}`; const restoreKey = undefined; return { primaryKey, - restoreKey + restoreKey, + cacheDependencyPath: this.cacheDependencyPath, }; }