Skip to content

Commit 0324490

Browse files
committed
Use additional files for C# key hashes if Feature.CsharpNewCacheKey is enabled
1 parent 6b48207 commit 0324490

File tree

3 files changed

+91
-19
lines changed

3 files changed

+91
-19
lines changed

lib/analyze-action.js

Lines changed: 21 additions & 6 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: 21 additions & 6 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: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,54 @@ async function makePatternCheck(patterns: string[]): Promise<string[]> {
8888
return patterns;
8989
}
9090

91+
/**
92+
* Returns the list of glob patterns that should be used to calculate the cache key hash
93+
* for a C# dependency cache.
94+
*
95+
* @param codeql The CodeQL instance to use.
96+
* @param features Information about which FFs are enabled.
97+
* @returns A list of glob patterns to use for hashing.
98+
*/
99+
async function getCsharpHashPatterns(
100+
codeql: CodeQL,
101+
features: Features,
102+
): Promise<string[]> {
103+
// These files contain accurate information about dependencies, including the exact versions
104+
// that the relevant package manager has determined for the project. Using these gives us
105+
// stable hashes unless the dependencies change.
106+
const basePatterns = [
107+
// NuGet
108+
"**/packages.lock.json",
109+
// Paket
110+
"**/paket.lock",
111+
];
112+
const globber = await makeGlobber(basePatterns);
113+
114+
if ((await globber.glob()).length > 0) {
115+
return basePatterns;
116+
}
117+
118+
if (await features.getValue(Feature.CsharpNewCacheKey, codeql)) {
119+
// These are less accurate for use in cache key calculations, because they:
120+
//
121+
// - Don't contain the exact versions used. They may only contain version ranges or none at all.
122+
// - They contain information unrelated to dependencies, which we don't care about.
123+
//
124+
// As a result, the hash we compute from these files may change, even if
125+
// the dependencies haven't changed.
126+
return makePatternCheck([
127+
"**/*.csproj",
128+
"**/packages.config",
129+
"**/nuget.config",
130+
]);
131+
}
132+
133+
// If we get to this point, the `basePatterns` didn't find any files,
134+
// and `Feature.CsharpNewCacheKey` is either not enabled or we didn't
135+
// find any files using those patterns either.
136+
throw new NoMatchingFilesError();
137+
}
138+
91139
/**
92140
* Default caching configurations per language.
93141
*/
@@ -109,13 +157,7 @@ const defaultCacheConfigs: { [language: string]: CacheConfig } = {
109157
},
110158
csharp: {
111159
getDependencyPaths: () => [join(os.homedir(), ".nuget", "packages")],
112-
getHashPatterns: async () =>
113-
makePatternCheck([
114-
// NuGet
115-
"**/packages.lock.json",
116-
// Paket
117-
"**/paket.lock",
118-
]),
160+
getHashPatterns: getCsharpHashPatterns,
119161
},
120162
go: {
121163
getDependencyPaths: () => [join(os.homedir(), "go", "pkg", "mod")],

0 commit comments

Comments
 (0)