Skip to content

Commit 0787fcc

Browse files
committed
chore: clean cache only after all the writes are complete
1 parent 100579e commit 0787fcc

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/build-system.ts

+41-28
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ import { SolcConfigSelector } from "./solc-config-selection.js";
6161
const log = debug("hardhat:core:solidity:build-system");
6262

6363
interface CompilationResult {
64-
compilerOutput: CompilerOutput,
65-
cached: boolean
64+
compilationJob: CompilationJob;
65+
compilerOutput: CompilerOutput;
66+
cached: boolean;
6667
}
6768

6869
export interface SolidityBuildSystemOptions {
@@ -144,19 +145,20 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
144145
if (cachedCompilerOutput !== undefined) {
145146
log(`Using cached compiler output for build ${buildId}`);
146147
return {
148+
compilationJob,
147149
compilerOutput: cachedCompilerOutput,
148150
cached: true,
149151
};
150152
}
151153
}
152154

153-
const compilerOutput = await this.runCompilationJob(compilationJob, runCompilationJobOptions);
154-
155-
if (!this.#hasCompilationErrors(compilerOutput)) {
156-
await this.#compilerOutputCache.setJson(buildId, compilerOutput);
157-
}
155+
const compilerOutput = await this.runCompilationJob(
156+
compilationJob,
157+
runCompilationJobOptions,
158+
);
158159

159160
return {
161+
compilationJob,
160162
compilerOutput,
161163
cached: false,
162164
};
@@ -169,12 +171,27 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
169171
},
170172
);
171173

172-
void this.#compilerOutputCache.clean();
173-
174-
const isSuccessfulBuild = results.every(
174+
const uncachedResults = results.filter((result) => !result.cached);
175+
const uncachedSuccessfulResults = uncachedResults.filter(
175176
(result) => !this.#hasCompilationErrors(result.compilerOutput),
176177
);
177178

179+
// NOTE: We're not waiting for the writes and clean to finish because we
180+
// will only care about the result of these operations in subsequent runs
181+
void Promise.all(
182+
uncachedSuccessfulResults.map(async (result) => {
183+
return this.#compilerOutputCache.setJson(
184+
result.compilationJob.getBuildId(),
185+
result.compilerOutput,
186+
);
187+
}),
188+
).then(() => {
189+
return this.#compilerOutputCache.clean();
190+
});
191+
192+
const isSuccessfulBuild =
193+
uncachedResults.length === uncachedSuccessfulResults.length;
194+
178195
const contractArtifactsGeneratedByCompilationJob: Map<
179196
CompilationJob,
180197
ReadonlyMap<string, string[]>
@@ -204,38 +221,34 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
204221

205222
const resultsMap: Map<string, FileBuildResult> = new Map();
206223

207-
for (let i = 0; i < compilationJobs.length; i++) {
208-
const compilationJob = compilationJobs[i];
209-
const result = results[i];
210-
224+
for (const result of results) {
211225
const contractArtifactsGenerated = isSuccessfulBuild
212-
? contractArtifactsGeneratedByCompilationJob.get(compilationJob)
226+
? contractArtifactsGeneratedByCompilationJob.get(result.compilationJob)
213227
: new Map();
214228

215229
assertHardhatInvariant(
216230
contractArtifactsGenerated !== undefined,
217231
"We emitted contract artifacts for all the jobs if the build was successful",
218232
);
219233

220-
const buildId = compilationJob.getBuildId();
234+
const buildId = result.compilationJob.getBuildId();
221235

222-
const errors =
223-
result !== undefined
224-
? await Promise.all(
225-
(result.compilerOutput.errors ?? []).map((error) =>
226-
this.remapCompilerError(compilationJob, error, true),
227-
),
228-
)
229-
: [];
236+
const errors = await Promise.all(
237+
(result.compilerOutput.errors ?? []).map((error) =>
238+
this.remapCompilerError(result.compilationJob, error, true),
239+
),
240+
);
230241

231242
this.#printSolcErrorsAndWarnings(errors);
232243

233244
const successfulResult =
234-
result === undefined || !this.#hasCompilationErrors(result.compilerOutput);
245+
result === undefined ||
246+
!this.#hasCompilationErrors(result.compilerOutput);
235247

236-
for (const [publicSourceName, root] of compilationJob.dependencyGraph
237-
.getRoots()
238-
.entries()) {
248+
for (const [
249+
publicSourceName,
250+
root,
251+
] of result.compilationJob.dependencyGraph.getRoots().entries()) {
239252
if (!successfulResult) {
240253
resultsMap.set(formatRootPath(publicSourceName, root), {
241254
type: FileBuildResultType.BUILD_FAILURE,

v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/cache.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ export class Cache {
4545

4646
const sortedFileInfos = fileInfos.sort((a, b) => a.atimeMs - b.atimeMs);
4747

48-
let size = sortedFileInfos.reduce((acc, fileInfo) => acc + fileInfo.size, 0);
49-
const minAtimeMs = (new Date(0 - this.#maxAgeMs)).getTime();
48+
let size = sortedFileInfos.reduce(
49+
(acc, fileInfo) => acc + fileInfo.size,
50+
0,
51+
);
52+
const minAtimeMs = new Date(0 - this.#maxAgeMs).getTime();
5053

5154
const filesToRemove: string[] = [];
5255

0 commit comments

Comments
 (0)