Skip to content

Commit d33b2c1

Browse files
committed
Change getDefaultCacheConfig to be a const by turning paths into a function
Changing `paths` to be a function is necessary to allow `getTemporaryDirectory` to be called
1 parent 8ea9aef commit d33b2c1

File tree

3 files changed

+144
-124
lines changed

3 files changed

+144
-124
lines changed

lib/analyze-action.js

Lines changed: 45 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 40 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dependency-caching.ts

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import { getRequiredEnvParam } from "./util";
1818
* Caching configuration for a particular language.
1919
*/
2020
interface CacheConfig {
21-
/** The paths of directories on the runner that should be included in the cache. */
22-
paths: string[];
21+
/** Gets the paths of directories on the runner that should be included in the cache. */
22+
getDependencyPaths: () => string[];
2323
/**
2424
* Patterns for the paths of files whose contents affect which dependencies are used
2525
* by a project. We find all files which match these patterns, calculate a hash for
@@ -41,47 +41,57 @@ export function getJavaTempDependencyDir(): string {
4141
}
4242

4343
/**
44-
* Default caching configurations per language.
44+
* Returns an array of paths of directories on the runner that should be included in a dependency cache
45+
* for a Java analysis. It is important that this is a function, because we call `getTemporaryDirectory`
46+
* which would otherwise fail in tests if we haven't had a chance to initialise `RUNNER_TEMP`.
47+
*
48+
* @returns The paths of directories on the runner that should be included in a dependency cache
49+
* for a Java analysis.
4550
*/
46-
function getDefaultCacheConfig(): { [language: string]: CacheConfig } {
47-
return {
48-
java: {
49-
paths: [
50-
// Maven
51-
join(os.homedir(), ".m2", "repository"),
52-
// Gradle
53-
join(os.homedir(), ".gradle", "caches"),
54-
// CodeQL Java build-mode: none
55-
getJavaTempDependencyDir(),
56-
],
57-
hash: [
58-
// Maven
59-
"**/pom.xml",
60-
// Gradle
61-
"**/*.gradle*",
62-
"**/gradle-wrapper.properties",
63-
"buildSrc/**/Versions.kt",
64-
"buildSrc/**/Dependencies.kt",
65-
"gradle/*.versions.toml",
66-
"**/versions.properties",
67-
],
68-
},
69-
csharp: {
70-
paths: [join(os.homedir(), ".nuget", "packages")],
71-
hash: [
72-
// NuGet
73-
"**/packages.lock.json",
74-
// Paket
75-
"**/paket.lock",
76-
],
77-
},
78-
go: {
79-
paths: [join(os.homedir(), "go", "pkg", "mod")],
80-
hash: ["**/go.sum"],
81-
},
82-
};
51+
export function getJavaDependencyDirs(): string[] {
52+
return [
53+
// Maven
54+
join(os.homedir(), ".m2", "repository"),
55+
// Gradle
56+
join(os.homedir(), ".gradle", "caches"),
57+
// CodeQL Java build-mode: none
58+
getJavaTempDependencyDir(),
59+
];
8360
}
8461

62+
/**
63+
* Default caching configurations per language.
64+
*/
65+
const defaultCacheConfigs: { [language: string]: CacheConfig } = {
66+
java: {
67+
getDependencyPaths: getJavaDependencyDirs,
68+
hash: [
69+
// Maven
70+
"**/pom.xml",
71+
// Gradle
72+
"**/*.gradle*",
73+
"**/gradle-wrapper.properties",
74+
"buildSrc/**/Versions.kt",
75+
"buildSrc/**/Dependencies.kt",
76+
"gradle/*.versions.toml",
77+
"**/versions.properties",
78+
],
79+
},
80+
csharp: {
81+
getDependencyPaths: () => [join(os.homedir(), ".nuget", "packages")],
82+
hash: [
83+
// NuGet
84+
"**/packages.lock.json",
85+
// Paket
86+
"**/paket.lock",
87+
],
88+
},
89+
go: {
90+
getDependencyPaths: () => [join(os.homedir(), "go", "pkg", "mod")],
91+
hash: ["**/go.sum"],
92+
},
93+
};
94+
8595
async function makeGlobber(patterns: string[]): Promise<glob.Globber> {
8696
return glob.create(patterns.join("\n"));
8797
}
@@ -104,7 +114,7 @@ export async function downloadDependencyCaches(
104114
const restoredCaches: Language[] = [];
105115

106116
for (const language of languages) {
107-
const cacheConfig = getDefaultCacheConfig()[language];
117+
const cacheConfig = defaultCacheConfigs[language];
108118

109119
if (cacheConfig === undefined) {
110120
logger.info(
@@ -136,7 +146,7 @@ export async function downloadDependencyCaches(
136146
);
137147

138148
const hitKey = await actionsCache.restoreCache(
139-
cacheConfig.paths,
149+
cacheConfig.getDependencyPaths(),
140150
primaryKey,
141151
restoreKeys,
142152
);
@@ -167,7 +177,7 @@ export async function uploadDependencyCaches(
167177
logger: Logger,
168178
): Promise<void> {
169179
for (const language of config.languages) {
170-
const cacheConfig = getDefaultCacheConfig()[language];
180+
const cacheConfig = defaultCacheConfigs[language];
171181

172182
if (cacheConfig === undefined) {
173183
logger.info(
@@ -197,7 +207,11 @@ export async function uploadDependencyCaches(
197207
// use the cache quota that we compete with. In that case, we do not wish to use up all of the quota
198208
// with the dependency caches. For this, we could use the Cache API to check whether other workflows
199209
// are using the quota and how full it is.
200-
const size = await getTotalCacheSize(cacheConfig.paths, logger, true);
210+
const size = await getTotalCacheSize(
211+
cacheConfig.getDependencyPaths(),
212+
logger,
213+
true,
214+
);
201215

202216
// Skip uploading an empty cache.
203217
if (size === 0) {
@@ -214,7 +228,7 @@ export async function uploadDependencyCaches(
214228
);
215229

216230
try {
217-
await actionsCache.saveCache(cacheConfig.paths, key);
231+
await actionsCache.saveCache(cacheConfig.getDependencyPaths(), key);
218232
} catch (error) {
219233
// `ReserveCacheError` indicates that the cache key is already in use, which means that a
220234
// cache with that key already exists or is in the process of being uploaded by another

0 commit comments

Comments
 (0)