Skip to content

Commit 7f81d73

Browse files
committed
Use additional files for C# key hashes if Feature.CsharpNewCacheKey is enabled
1 parent f6e3125 commit 7f81d73

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
@@ -87,6 +87,54 @@ async function makePatternCheck(patterns: string[]): Promise<string[]> {
8787
return patterns;
8888
}
8989

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

0 commit comments

Comments
 (0)