Skip to content

Commit 4019ad6

Browse files
Make sure that solc default import callback is not being used (#4783)
1 parent 15a0d2e commit 4019ad6

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

packages/hardhat-core/src/builtin-tasks/compile.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -674,9 +674,26 @@ subtask(TASK_COMPILE_SOLIDITY_RUN_SOLCJS)
674674
subtask(TASK_COMPILE_SOLIDITY_RUN_SOLC)
675675
.addParam("input", undefined, undefined, types.any)
676676
.addParam("solcPath", undefined, undefined, types.string)
677+
.addOptionalParam("solcVersion", undefined, undefined, types.string)
677678
.setAction(
678-
async ({ input, solcPath }: { input: CompilerInput; solcPath: string }) => {
679-
const compiler = new NativeCompiler(solcPath);
679+
async ({
680+
input,
681+
solcPath,
682+
solcVersion,
683+
}: {
684+
input: CompilerInput;
685+
solcPath: string;
686+
solcVersion?: string;
687+
}) => {
688+
if (solcVersion !== undefined && semver.valid(solcVersion) === null) {
689+
throw new HardhatError(ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
690+
value: solcVersion,
691+
name: "solcVersion",
692+
type: "string",
693+
});
694+
}
695+
696+
const compiler = new NativeCompiler(solcPath, solcVersion);
680697

681698
return compiler.compile(input);
682699
}
@@ -740,6 +757,7 @@ subtask(TASK_COMPILE_SOLIDITY_COMPILE_SOLC)
740757
output = await run(TASK_COMPILE_SOLIDITY_RUN_SOLC, {
741758
input,
742759
solcPath: solcBuild.compilerPath,
760+
solcVersion,
743761
});
744762
}
745763

packages/hardhat-core/src/internal/solidity/compiler/index.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { execFile } from "child_process";
22
import * as fs from "fs";
3+
import os from "node:os";
4+
import path from "node:path";
5+
import * as semver from "semver";
36
import { CompilerInput, CompilerOutput } from "../../../types";
47
import { HardhatError } from "../../core/errors";
58
import { ERRORS } from "../../core/errors-list";
@@ -68,14 +71,31 @@ export class Compiler implements ICompiler {
6871
}
6972

7073
export class NativeCompiler implements ICompiler {
71-
constructor(private _pathToSolc: string) {}
74+
constructor(private _pathToSolc: string, private _solcVersion?: string) {}
7275

7376
public async compile(input: CompilerInput) {
77+
const args = ["--standard-json"];
78+
79+
// Logic to make sure that solc default import callback is not being used.
80+
// If solcVersion is not defined or <= 0.6.8, do not add extra args.
81+
if (this._solcVersion !== undefined) {
82+
if (semver.gte(this._solcVersion, "0.8.22")) {
83+
// version >= 0.8.22
84+
args.push("--no-import-callback");
85+
} else if (semver.gte(this._solcVersion, "0.6.9")) {
86+
// version >= 0.6.9
87+
const tmpFolder = path.join(os.tmpdir(), "hardhat-solc");
88+
fs.mkdirSync(tmpFolder, { recursive: true });
89+
args.push(`--base-path`);
90+
args.push(tmpFolder);
91+
}
92+
}
93+
7494
const output: string = await new Promise((resolve, reject) => {
7595
try {
7696
const process = execFile(
7797
this._pathToSolc,
78-
[`--standard-json`],
98+
args,
7999
{
80100
maxBuffer: 1024 * 1024 * 500,
81101
},

0 commit comments

Comments
 (0)