Skip to content

Commit fa6cb23

Browse files
scnalecgewecke
authored andcommitted
Update buidler plugin for Hardhat compilation pipeline (#540)
1 parent 52889ff commit fa6cb23

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

plugins/buidler.plugin.js

+52-16
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,21 @@ const path = require('path');
99
const Web3 = require('web3');
1010

1111
const { task, types } = require("@nomiclabs/buidler/config");
12-
const { ensurePluginLoadedWithUsePlugin } = require("@nomiclabs/buidler/plugins");
1312

1413
const {
1514
TASK_TEST,
1615
TASK_COMPILE,
17-
TASK_COMPILE_GET_COMPILER_INPUT
16+
TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT,
17+
TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE,
1818
} = require("@nomiclabs/buidler/builtin-tasks/task-names");
1919

20-
ensurePluginLoadedWithUsePlugin();
21-
2220
function plugin() {
2321

2422
// UI for the task flags...
2523
const ui = new PluginUI();
2624

27-
// Unset useLiteralContent due to solc metadata size restriction
28-
task(TASK_COMPILE_GET_COMPILER_INPUT).setAction(async (_, __, runSuper) => {
29-
const input = await runSuper();
30-
input.settings.metadata.useLiteralContent = false;
31-
return input;
32-
})
25+
let measureCoverage = false;
26+
let instrumentedSources;
3327

3428
task("coverage", "Generates a code coverage report for tests")
3529

@@ -42,6 +36,7 @@ function plugin() {
4236
let ui;
4337
let api;
4438
let config;
39+
instrumentedSources = {};
4540

4641
try {
4742
death(buidlerUtils.finish.bind(null, config, api)); // Catch interrupt signals
@@ -91,6 +86,9 @@ function plugin() {
9186
} = utils.assembleFiles(config, skipFiles);
9287

9388
targets = api.instrument(targets);
89+
for (const target of targets) {
90+
instrumentedSources[target.canonicalPath] = target.source;
91+
}
9492
utils.reportSkipped(config, skipped);
9593

9694
// ==============
@@ -104,14 +102,11 @@ function plugin() {
104102
} = utils.getTempLocations(config);
105103

106104
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
107-
utils.save(targets, config.paths.sources, tempContractsDir);
108-
utils.save(skipped, config.paths.sources, tempContractsDir);
109105

110-
config.paths.sources = tempContractsDir;
111106
config.paths.artifacts = tempArtifactsDir;
112107
config.paths.cache = buidlerUtils.tempCacheDir(config);
113-
config.solc.optimizer.enabled = false;
114108

109+
measureCoverage = true;
115110
await env.run(TASK_COMPILE);
116111

117112
await api.onCompileComplete(config);
@@ -137,14 +132,55 @@ function plugin() {
137132
await api.onIstanbulComplete(config);
138133

139134
} catch(e) {
140-
error = e;
135+
error = e;
136+
} finally {
137+
measureCoverage = false;
141138
}
142139

143140
await buidlerUtils.finish(config, api);
144141

145142
if (error !== undefined ) throw error;
146143
if (process.exitCode > 0) throw new Error(ui.generate('tests-fail', [process.exitCode]));
147-
})
144+
});
145+
146+
task(TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT).setAction(async (_, { config }, runSuper) => {
147+
const solcInput = await runSuper();
148+
if (measureCoverage) {
149+
// The source name here is actually the global name in the solc input,
150+
// but buidler uses the fully qualified contract names.
151+
for (const [sourceName, source] of Object.entries(solcInput.sources)) {
152+
const absolutePath = path.join(config.paths.root, sourceName);
153+
// Patch in the instrumented source code.
154+
if (absolutePath in instrumentedSources) {
155+
source.content = instrumentedSources[absolutePath];
156+
}
157+
}
158+
}
159+
return solcInput;
160+
});
161+
162+
// Solidity settings are best set here instead of the TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT task.
163+
task(TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE).setAction(async (_, __, runSuper) => {
164+
const compilationJob = await runSuper();
165+
if (measureCoverage && typeof compilationJob === "object") {
166+
if (compilationJob.solidityConfig.settings === undefined) {
167+
compilationJob.solidityConfig.settings = {};
168+
}
169+
170+
const { settings } = compilationJob.solidityConfig;
171+
if (settings.metadata === undefined) {
172+
settings.metadata = {};
173+
}
174+
if (settings.optimizer === undefined) {
175+
settings.optimizer = {};
176+
}
177+
// Unset useLiteralContent due to solc metadata size restriction
178+
settings.metadata.useLiteralContent = false;
179+
// Override optimizer settings for all compilers
180+
settings.optimizer.enabled = false;
181+
}
182+
return compilationJob;
183+
});
148184
}
149185

150186
module.exports = plugin;

test/integration/projects/solc-6/buidler.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ usePlugin("@nomiclabs/buidler-truffle5");
44

55
module.exports={
66
defaultNetwork: "buidlerevm",
7-
solc: {
7+
solidity: {
88
version: "0.6.5"
99
}
1010
};

0 commit comments

Comments
 (0)