mirror of
				https://github.com/actions/setup-node.git
				synced 2025-10-31 16:14:00 +08:00 
			
		
		
		
	Do not ivalidate the cache entirely on lock file change (#744)
* Do not ivalidate the cache entirely on yarn3 lock file change * Use cache prefix if all sub-projects are yarn managed * Rename functions & add e2e tests
This commit is contained in:
		| @@ -8,6 +8,7 @@ import {State} from './constants'; | ||||
| import { | ||||
|   getCacheDirectories, | ||||
|   getPackageManagerInfo, | ||||
|   repoHasYarnBerryManagedDependencies, | ||||
|   PackageManagerInfo | ||||
| } from './cache-utils'; | ||||
|  | ||||
| @@ -37,12 +38,26 @@ export const restoreCache = async ( | ||||
|     ); | ||||
|   } | ||||
|  | ||||
|   const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`; | ||||
|   const keyPrefix = `node-cache-${platform}-${packageManager}`; | ||||
|   const primaryKey = `${keyPrefix}-${fileHash}`; | ||||
|   core.debug(`primary key is ${primaryKey}`); | ||||
|  | ||||
|   core.saveState(State.CachePrimaryKey, primaryKey); | ||||
|  | ||||
|   const cacheKey = await cache.restoreCache(cachePaths, primaryKey); | ||||
|   const isManagedByYarnBerry = await repoHasYarnBerryManagedDependencies( | ||||
|     packageManagerInfo, | ||||
|     cacheDependencyPath | ||||
|   ); | ||||
|   let cacheKey: string | undefined; | ||||
|   if (isManagedByYarnBerry) { | ||||
|     core.info( | ||||
|       'All dependencies are managed locally by yarn3, the previous cache can be used' | ||||
|     ); | ||||
|     cacheKey = await cache.restoreCache(cachePaths, primaryKey, [keyPrefix]); | ||||
|   } else { | ||||
|     cacheKey = await cache.restoreCache(cachePaths, primaryKey); | ||||
|   } | ||||
|  | ||||
|   core.setOutput('cache-hit', Boolean(cacheKey)); | ||||
|  | ||||
|   if (!cacheKey) { | ||||
|   | ||||
| @@ -110,6 +110,20 @@ export const getPackageManagerInfo = async (packageManager: string) => { | ||||
|   } | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * getProjectDirectoriesFromCacheDependencyPath is called twice during `restoreCache` | ||||
|  *  - first through `getCacheDirectories` | ||||
|  *  - second from `repoHasYarn3ManagedCache` | ||||
|  * | ||||
|  *  it contains expensive IO operation and thus should be memoized | ||||
|  */ | ||||
|  | ||||
| let projectDirectoriesMemoized: string[] | null = null; | ||||
| /** | ||||
|  * unit test must reset memoized variables | ||||
|  */ | ||||
| export const resetProjectDirectoriesMemoized = () => | ||||
|   (projectDirectoriesMemoized = null); | ||||
| /** | ||||
|  * Expands (converts) the string input `cache-dependency-path` to list of directories that | ||||
|  * may be project roots | ||||
| @@ -120,6 +134,10 @@ export const getPackageManagerInfo = async (packageManager: string) => { | ||||
| const getProjectDirectoriesFromCacheDependencyPath = async ( | ||||
|   cacheDependencyPath: string | ||||
| ): Promise<string[]> => { | ||||
|   if (projectDirectoriesMemoized !== null) { | ||||
|     return projectDirectoriesMemoized; | ||||
|   } | ||||
|  | ||||
|   const globber = await glob.create(cacheDependencyPath); | ||||
|   const cacheDependenciesPaths = await globber.glob(); | ||||
|  | ||||
| @@ -133,6 +151,7 @@ const getProjectDirectoriesFromCacheDependencyPath = async ( | ||||
|       `No existing directories found containing cache-dependency-path="${cacheDependencyPath}"` | ||||
|     ); | ||||
|  | ||||
|   projectDirectoriesMemoized = existingDirectories; | ||||
|   return existingDirectories; | ||||
| }; | ||||
|  | ||||
| @@ -152,8 +171,9 @@ const getCacheDirectoriesFromCacheDependencyPath = async ( | ||||
|   ); | ||||
|   const cacheFoldersPaths = await Promise.all( | ||||
|     projectDirectories.map(async projectDirectory => { | ||||
|       const cacheFolderPath = | ||||
|         packageManagerInfo.getCacheFolderPath(projectDirectory); | ||||
|       const cacheFolderPath = await packageManagerInfo.getCacheFolderPath( | ||||
|         projectDirectory | ||||
|       ); | ||||
|       core.debug( | ||||
|         `${packageManagerInfo.name}'s cache folder "${cacheFolderPath}" configured for the directory "${projectDirectory}"` | ||||
|       ); | ||||
| @@ -202,6 +222,74 @@ export const getCacheDirectories = async ( | ||||
|   return getCacheDirectoriesForRootProject(packageManagerInfo); | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * A function to check if the directory is a yarn project configured to manage | ||||
|  * obsolete dependencies in the local cache | ||||
|  * @param directory - a path to the folder | ||||
|  * @return - true if the directory's project is yarn managed | ||||
|  *  - if there's .yarn/cache folder do not mess with the dependencies kept in the repo, return false | ||||
|  *  - global cache is not managed by yarn @see https://yarnpkg.com/features/offline-cache, return false | ||||
|  *  - if local cache is not explicitly enabled (not yarn3), return false | ||||
|  *  - return true otherwise | ||||
|  */ | ||||
| const projectHasYarnBerryManagedDependencies = async ( | ||||
|   directory: string | ||||
| ): Promise<boolean> => { | ||||
|   const workDir = directory || process.env.GITHUB_WORKSPACE || '.'; | ||||
|   core.debug(`check if "${workDir}" has locally managed yarn3 dependencies`); | ||||
|  | ||||
|   // if .yarn/cache directory exists the cache is managed by version control system | ||||
|   const yarnCacheFile = path.join(workDir, '.yarn', 'cache'); | ||||
|   if ( | ||||
|     fs.existsSync(yarnCacheFile) && | ||||
|     fs.lstatSync(yarnCacheFile).isDirectory() | ||||
|   ) { | ||||
|     core.debug( | ||||
|       `"${workDir}" has .yarn/cache - dependencies are kept in the repository` | ||||
|     ); | ||||
|     return Promise.resolve(false); | ||||
|   } | ||||
|  | ||||
|   // NOTE: yarn1 returns 'undefined' with return code = 0 | ||||
|   const enableGlobalCache = await getCommandOutput( | ||||
|     'yarn config get enableGlobalCache', | ||||
|     workDir | ||||
|   ); | ||||
|   // only local cache is not managed by yarn | ||||
|   const managed = enableGlobalCache.includes('false'); | ||||
|   if (managed) { | ||||
|     core.debug(`"${workDir}" dependencies are managed by yarn 3 locally`); | ||||
|     return true; | ||||
|   } else { | ||||
|     core.debug(`"${workDir}" dependencies are not managed by yarn 3 locally`); | ||||
|     return false; | ||||
|   } | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * A function to report the repo contains Yarn managed projects | ||||
|  * @param packageManagerInfo - used to make sure current package manager is yarn | ||||
|  * @param cacheDependencyPath - either a single string or multiline string with possible glob patterns | ||||
|  *                              expected to be the result of `core.getInput('cache-dependency-path')` | ||||
|  * @return - true if all project directories configured to be Yarn managed | ||||
|  */ | ||||
| export const repoHasYarnBerryManagedDependencies = async ( | ||||
|   packageManagerInfo: PackageManagerInfo, | ||||
|   cacheDependencyPath: string | ||||
| ): Promise<boolean> => { | ||||
|   if (packageManagerInfo.name !== 'yarn') return false; | ||||
|  | ||||
|   const yarnDirs = cacheDependencyPath | ||||
|     ? await getProjectDirectoriesFromCacheDependencyPath(cacheDependencyPath) | ||||
|     : ['']; | ||||
|  | ||||
|   const isManagedList = await Promise.all( | ||||
|     yarnDirs.map(projectHasYarnBerryManagedDependencies) | ||||
|   ); | ||||
|  | ||||
|   return isManagedList.every(Boolean); | ||||
| }; | ||||
|  | ||||
| export function isGhes(): boolean { | ||||
|   const ghUrl = new URL( | ||||
|     process.env['GITHUB_SERVER_URL'] || 'https://github.com' | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Sergey Dolin
					Sergey Dolin