Skip to content

Commit c52e076

Browse files
committed
Make the compilation cache compiler type aware
1 parent 6daf3ff commit c52e076

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,8 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
596596
cacheResult === undefined ||
597597
cacheResult.jobHash !== jobHash ||
598598
cacheResult.isolated !== isolated ||
599-
cacheResult.wasm !== isWasm
599+
cacheResult.wasm !== isWasm ||
600+
cacheResult.compilerType !== compilerType
600601
) {
601602
rootFilesToCompile.add(rootFile);
602603
continue;
@@ -1201,6 +1202,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
12011202
this.#compileCache[rootFilePath] = {
12021203
jobHash,
12031204
isolated,
1205+
compilerType: individualJob.solcConfig.type ?? "solc",
12041206
artifactPaths,
12051207
buildInfoPath: emitArtifactsResult.buildInfoPath,
12061208
buildInfoOutputPath: emitArtifactsResult.buildInfoOutputPath,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type CompileCache = Record<string, CompileCacheEntry>;
1717
export interface CompileCacheEntry {
1818
jobHash: string;
1919
isolated: boolean;
20+
compilerType: string;
2021
buildInfoPath: string;
2122
buildInfoOutputPath: string;
2223
artifactPaths: string[];

v-next/hardhat/test/internal/builtin-plugins/solidity/build-system/partial-compilation/cache-hit-results.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import type { CompileCache } from "../../../../../../src/internal/builtin-plugins/solidity/build-system/cache.js";
2+
13
import assert from "node:assert/strict";
24
import { writeFile } from "node:fs/promises";
35
import path from "node:path";
46
import { describe, it } from "node:test";
57

8+
import { readJsonFile, writeJsonFile } from "@nomicfoundation/hardhat-utils/fs";
9+
import { isObject } from "micro-eth-signer/utils";
10+
611
import { FileBuildResultType } from "../../../../../../src/types/solidity/build-system.js";
712
import { useTestProjectTemplate } from "../resolver/helpers.js";
813

@@ -171,5 +176,83 @@ contract Foo {}`,
171176
FileBuildResultType.BUILD_SUCCESS,
172177
);
173178
});
179+
180+
it("should return BUILD_SUCCESS when compilerType in cache differs", async () => {
181+
await using project = await useTestProjectTemplate({
182+
name: "test-project",
183+
version: "1.0.0",
184+
files: {
185+
"contracts/Foo.sol": `// SPDX-License-Identifier: MIT
186+
pragma solidity ^0.8.0;
187+
contract Foo {}`,
188+
},
189+
});
190+
191+
const hre = await getHRE(project);
192+
const filePath = path.join(project.path, "contracts/Foo.sol");
193+
194+
// First build
195+
await hre.solidity.build([filePath], { quiet: true });
196+
197+
// Tamper with the cache to simulate a different compiler type
198+
const cachePath = path.join(project.path, "cache", "compile-cache.json");
199+
const cache: CompileCache = await readJsonFile(cachePath);
200+
for (const key of Object.keys(cache)) {
201+
cache[key].compilerType = "different-compiler";
202+
}
203+
await writeJsonFile(cachePath, cache);
204+
205+
// Second build should be a cache miss due to compiler type mismatch
206+
const result = await hre.solidity.build([filePath], { quiet: true });
207+
assert(
208+
hre.solidity.isSuccessfulBuildResult(result),
209+
"Build should succeed",
210+
);
211+
212+
assert.equal(
213+
result.get(filePath)?.type,
214+
FileBuildResultType.BUILD_SUCCESS,
215+
);
216+
});
217+
218+
it("should return BUILD_SUCCESS when compilerType is missing from cache (old format)", async () => {
219+
await using project = await useTestProjectTemplate({
220+
name: "test-project",
221+
version: "1.0.0",
222+
files: {
223+
"contracts/Foo.sol": `// SPDX-License-Identifier: MIT
224+
pragma solidity ^0.8.0;
225+
contract Foo {}`,
226+
},
227+
});
228+
229+
const hre = await getHRE(project);
230+
const filePath = path.join(project.path, "contracts/Foo.sol");
231+
232+
// First build
233+
await hre.solidity.build([filePath], { quiet: true });
234+
235+
// Remove compilerType from cache to simulate old cache format
236+
const cachePath = path.join(project.path, "cache", "compile-cache.json");
237+
const cache = await readJsonFile(cachePath);
238+
assert.ok(isObject(cache), "Cache should be an object");
239+
240+
for (const key of Object.keys(cache)) {
241+
delete cache[key].compilerType;
242+
}
243+
await writeJsonFile(cachePath, cache);
244+
245+
// Second build should be a cache miss due to missing compiler type
246+
const result = await hre.solidity.build([filePath], { quiet: true });
247+
assert(
248+
hre.solidity.isSuccessfulBuildResult(result),
249+
"Build should succeed",
250+
);
251+
252+
assert.equal(
253+
result.get(filePath)?.type,
254+
FileBuildResultType.BUILD_SUCCESS,
255+
);
256+
});
174257
});
175258
});

0 commit comments

Comments
 (0)