Skip to content

Commit

Permalink
wip: try moving the artifacts higher
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Jan 10, 2025
1 parent edad266 commit db3ac09
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
force: options?.force,
quiet: options?.quiet,
};
const results: CompilerOutput[] = await pMap(
const results: Array<CompilerOutput | undefined> = await pMap(
compilationJobs,
(compilationJob) =>
this.runCompilationJob(compilationJob, runCompilationJobOptions),
async (compilationJob) =>
(await this.#artifactsCache.has(compilationJob.getBuildId()))
? undefined
: this.runCompilationJob(compilationJob, runCompilationJobOptions),
{
concurrency: options?.concurrency ?? this.#defaultConcurrenty,
// An error when running the compiler is not a compilation failure, but
Expand All @@ -142,7 +144,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
);

const isSuccessfulBuild = results.every(
(result) => !this.#hasCompilationErrors(result),
(result) => result === undefined || !this.#hasCompilationErrors(result),
);

const contractArtifactsGeneratedByCompilationJob: Map<
Expand All @@ -158,11 +160,28 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
};
await Promise.all(
compilationJobs.map(async (compilationJob, i) => {
const artifactsPerFile = await this.emitArtifacts(
compilationJob,
results[i],
emitArtifactsOptions,
);
const result = results[i];
let artifactsPerFile;
if (result === undefined) {
const cachedFiles = await this.#artifactsCache.getFiles(
compilationJob.getBuildId(),
this.#options.artifactsPath,
);

assertHardhatInvariant(
cachedFiles !== undefined,
"We checked if the compilation job was cached before",
);

artifactsPerFile =
await this.#groupEmitArtifactsResults(cachedFiles);
} else {
artifactsPerFile = await this.emitArtifacts(
compilationJob,
result,
emitArtifactsOptions,
);
}

contractArtifactsGeneratedByCompilationJob.set(
compilationJob,
Expand All @@ -174,7 +193,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {

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

for (let i = 0; i < results.length; i++) {
for (let i = 0; i < compilationJobs.length; i++) {
const compilationJob = compilationJobs[i];
const result = results[i];

Expand All @@ -189,15 +208,19 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {

const buildId = compilationJob.getBuildId();

const errors = await Promise.all(
(result.errors ?? []).map((error) =>
this.remapCompilerError(compilationJob, error, true),
),
);
const errors =
result !== undefined
? await Promise.all(
(result.errors ?? []).map((error) =>
this.remapCompilerError(compilationJob, error, true),
),
)
: [];

this.#printSolcErrorsAndWarnings(errors);

const successfulResult = !this.#hasCompilationErrors(result);
const successfulResult =
result === undefined || !this.#hasCompilationErrors(result);

for (const [publicSourceName, root] of compilationJob.dependencyGraph
.getRoots()
Expand All @@ -212,6 +235,17 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
continue;
}

if (result === undefined) {
resultsMap.set(formatRootPath(publicSourceName, root), {
type: FileBuildResultType.CACHE_HIT,
buildId,
contractArtifactsGenerated:
contractArtifactsGenerated.get(publicSourceName) ?? [],
});

continue;
}

resultsMap.set(formatRootPath(publicSourceName, root), {
type: FileBuildResultType.BUILD_SUCCESS,
buildId,
Expand Down Expand Up @@ -406,6 +440,32 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
};
}

async #groupEmitArtifactsResults(
filePaths: string[],
): Promise<Map<string, string[]>> {
const result = new Map<string, string[]>();

for (const filePath of filePaths) {
const relativePath = path.relative(this.#options.artifactsPath, filePath);
if (
path.dirname(relativePath) === "build-info" ||
path.basename(relativePath) === "artifacts.d.ts"
) {
continue;
}
if (await isDirectory(filePath)) {
result.set(relativePath, []);
} else {
const publicSourceName = path.dirname(relativePath);
const paths = result.get(publicSourceName) ?? [];
paths.push(filePath);
result.set(publicSourceName, paths);
}
}

return result;
}

public async emitArtifacts(
compilationJob: CompilationJob,
compilerOutput: CompilerOutput,
Expand All @@ -421,27 +481,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
);
if (cachedFiles !== undefined) {
log(`Using cached artifacts for build ${buildId}`);
for (const filePath of cachedFiles) {
const relativePath = path.relative(
this.#options.artifactsPath,
filePath,
);
if (
path.dirname(relativePath) === "build-info" ||
path.basename(relativePath) === "artifacts.d.ts"
) {
continue;
}
if (await isDirectory(filePath)) {
result.set(relativePath, []);
} else {
const publicSourceName = path.dirname(relativePath);
const paths = result.get(publicSourceName) ?? [];
paths.push(filePath);
result.set(publicSourceName, paths);
}
}
return result;
return this.#groupEmitArtifactsResults(cachedFiles);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ export class Cache {
return path.join(this.#basePath, this.#namespace, key);
}

public async has(key: string): Promise<boolean> {
return exists(this.#getPath(key));
}

public async setJson<T>(key: string, value: T): Promise<void> {
const filePath = this.#getPath(key);
await writeJsonFile(filePath, value);
}

public async getJson<T>(key: string): Promise<T | undefined> {
const filePath = this.#getPath(key);
return (await exists(filePath)) ? readJsonFile<T>(filePath) : undefined;
return (await this.has(key)) ? readJsonFile<T>(filePath) : undefined;
}

public async setFiles(
Expand Down Expand Up @@ -57,8 +61,8 @@ export class Cache {
key: string,
rootPath: string,
): Promise<string[] | undefined> {
const zipFilePath = this.#getPath(key);
if (await exists(zipFilePath)) {
if (await this.has(key)) {
const zipFilePath = this.#getPath(key);
const zip = new AdmZip(zipFilePath);
zip.extractAllTo(rootPath, true);
const filePaths = zip
Expand Down

0 comments on commit db3ac09

Please sign in to comment.