Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-carrots-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Merge user's solc output selection with our defaults ([#6551](https://github.com/NomicFoundation/hardhat/issues/6551))
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export async function getBuildInfo(
solcVersion: compilationJob.solcConfig.version,
solcLongVersion: compilationJob.solcLongVersion,
publicSourceNameMap,
input: compilationJob.getSolcInput(),
input: await compilationJob.getSolcInput(),
};

return buildInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {

if (options?.quiet !== true) {
if (isSuccessfulBuild) {
this.#printCompilationResult(compilationJobs);
await this.#printCompilationResult(compilationJobs);
}
}

Expand Down Expand Up @@ -430,7 +430,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
"The long version of the compiler should match the long version of the compilation job",
);

return compiler.compile(compilationJob.getSolcInput());
return compiler.compile(await compilationJob.getSolcInput());
}

public async remapCompilerError(
Expand Down Expand Up @@ -789,15 +789,15 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
}
}

#printCompilationResult(compilationJobs: CompilationJob[]) {
async #printCompilationResult(compilationJobs: CompilationJob[]) {
const jobsPerVersionAndEvmVersion = new Map<
string,
Map<string, CompilationJob[]>
>();

for (const job of compilationJobs) {
const solcVersion = job.solcConfig.version;
const solcInput = job.getSolcInput();
const solcInput = await job.getSolcInput();
const evmVersion =
solcInput.settings.evmVersion ??
`Check solc ${solcVersion}'s doc for its default evm version`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { DependencyGraph } from "../../../../types/solidity/dependency-grap
import type { ResolvedFile } from "../../../../types/solidity.js";

import { createNonCryptographicHashId } from "@nomicfoundation/hardhat-utils/crypto";
import { deepClone } from "@nomicfoundation/hardhat-utils/lang";

import { formatRemapping } from "./resolver/remappings.js";
import { getEvmVersionFromSolcVersion } from "./solc-info.js";
Expand Down Expand Up @@ -36,9 +37,9 @@ export class CompilationJobImplementation implements CompilationJob {
this.#remappings = remappings;
}

public getSolcInput(): CompilerInput {
public async getSolcInput(): Promise<CompilerInput> {
if (this.#solcInput === undefined) {
this.#solcInput = this.#buildSolcInput();
this.#solcInput = await this.#buildSolcInput();
}

return this.#solcInput;
Expand All @@ -52,9 +53,10 @@ export class CompilationJobImplementation implements CompilationJob {
return this.#buildId;
}

#getSolcInputWithoutSources(): Omit<CompilerInput, "sources"> {
async #getSolcInputWithoutSources(): Promise<Omit<CompilerInput, "sources">> {
if (this.#solcInputWithoutSources === undefined) {
this.#solcInputWithoutSources = this.#buildSolcInputWithoutSources();
this.#solcInputWithoutSources =
await this.#buildSolcInputWithoutSources();
}

return this.#solcInputWithoutSources;
Expand All @@ -71,8 +73,8 @@ export class CompilationJobImplementation implements CompilationJob {
return this.#resolvedFiles;
}

#buildSolcInput(): CompilerInput {
const solcInputWithoutSources = this.#getSolcInputWithoutSources();
async #buildSolcInput(): Promise<CompilerInput> {
const solcInputWithoutSources = await this.#getSolcInputWithoutSources();

const sources: { [sourceName: string]: { content: string } } = {};

Expand All @@ -90,7 +92,9 @@ export class CompilationJobImplementation implements CompilationJob {
};
}

#buildSolcInputWithoutSources(): Omit<CompilerInput, "sources"> {
async #buildSolcInputWithoutSources(): Promise<
Omit<CompilerInput, "sources">
> {
const settings = this.solcConfig.settings;

// Ideally we would be more selective with the output selection, so that
Expand All @@ -99,22 +103,19 @@ export class CompilationJobImplementation implements CompilationJob {
// from other files (e.g. new Foo()), and it won't output its bytecode if
// it's not asked for. This would prevent EDR from doing any runtime
// analysis.
const defaultOutputSelection: CompilerInput["settings"]["outputSelection"] =
{
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers",
"metadata",
],
"": ["ast"],
},
};

// TODO: Deep merge the user output selection with the default one
const outputSelection = defaultOutputSelection;
const outputSelection = await deepClone(settings.outputSelection ?? {});
outputSelection["*"] ??= {};
outputSelection["*"][""] ??= [];
outputSelection["*"]["*"] ??= [];

outputSelection["*"][""].push("ast");
outputSelection["*"]["*"].push(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter that there are duplicates?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have opted to defer on this question. If duplicates are a problem we can add explicit removal code.

"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers",
"metadata",
);

return {
language: "Solidity",
Expand Down Expand Up @@ -157,7 +158,7 @@ export class CompilationJobImplementation implements CompilationJob {
const preimage =
format +
this.solcLongVersion +
JSON.stringify(this.#getSolcInputWithoutSources()) +
JSON.stringify(await this.#getSolcInputWithoutSources()) +
JSON.stringify(sortedSources) +
JSON.stringify(this.solcConfig);

Expand Down
2 changes: 1 addition & 1 deletion v-next/hardhat/src/types/solidity/compilation-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface CompilationJob {
/**
* Returns the solc input to be used.
*/
getSolcInput(): CompilerInput;
getSolcInput(): Promise<CompilerInput>;

/**
* Returns the build id of the compilation job.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,38 @@ describe("CompilationJobImplementation", () => {
});
});
});

describe("getSolcInput", () => {
it("should merge the user's outputSelection with our defaults", async () => {
const newCompilationJob = new CompilationJobImplementation(
dependencyGraph,
{
...solcConfig,
settings: {
outputSelection: {
"*": {
"*": ["storageLayout"],
},
},
},
},
solcLongVersion,
remappings,
);
const solcInput = await newCompilationJob.getSolcInput();
assert.deepEqual(solcInput.settings.outputSelection, {
"*": {
"*": [
"storageLayout",
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers",
"metadata",
],
"": ["ast"],
},
});
});
});
});
Loading