Skip to content

Commit

Permalink
Update buidler plugin for Hardhat compilation pipeline (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
scnale authored and cgewecke committed Oct 31, 2020
1 parent be11494 commit 72bf6cc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
68 changes: 52 additions & 16 deletions plugins/buidler.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,21 @@ const path = require('path');
const Web3 = require('web3');

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

const {
TASK_TEST,
TASK_COMPILE,
TASK_COMPILE_GET_COMPILER_INPUT
TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT,
TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE,
} = require("@nomiclabs/buidler/builtin-tasks/task-names");

ensurePluginLoadedWithUsePlugin();

function plugin() {

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

// Unset useLiteralContent due to solc metadata size restriction
task(TASK_COMPILE_GET_COMPILER_INPUT).setAction(async (_, __, runSuper) => {
const input = await runSuper();
input.settings.metadata.useLiteralContent = false;
return input;
})
let measureCoverage = false;
let instrumentedSources;

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

Expand All @@ -42,6 +36,7 @@ function plugin() {
let ui;
let api;
let config;
instrumentedSources = {};

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

targets = api.instrument(targets);
for (const target of targets) {
instrumentedSources[target.canonicalPath] = target.source;
}
utils.reportSkipped(config, skipped);

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

utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
utils.save(targets, config.paths.sources, tempContractsDir);
utils.save(skipped, config.paths.sources, tempContractsDir);

config.paths.sources = tempContractsDir;
config.paths.artifacts = tempArtifactsDir;
config.paths.cache = buidlerUtils.tempCacheDir(config);
config.solc.optimizer.enabled = false;

measureCoverage = true;
await env.run(TASK_COMPILE);

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

} catch(e) {
error = e;
error = e;
} finally {
measureCoverage = false;
}

await buidlerUtils.finish(config, api);

if (error !== undefined ) throw error;
if (process.exitCode > 0) throw new Error(ui.generate('tests-fail', [process.exitCode]));
})
});

task(TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT).setAction(async (_, { config }, runSuper) => {
const solcInput = await runSuper();
if (measureCoverage) {
// The source name here is actually the global name in the solc input,
// but buidler uses the fully qualified contract names.
for (const [sourceName, source] of Object.entries(solcInput.sources)) {
const absolutePath = path.join(config.paths.root, sourceName);
// Patch in the instrumented source code.
if (absolutePath in instrumentedSources) {
source.content = instrumentedSources[absolutePath];
}
}
}
return solcInput;
});

// Solidity settings are best set here instead of the TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT task.
task(TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE).setAction(async (_, __, runSuper) => {
const compilationJob = await runSuper();
if (measureCoverage && typeof compilationJob === "object") {
if (compilationJob.solidityConfig.settings === undefined) {
compilationJob.solidityConfig.settings = {};
}

const { settings } = compilationJob.solidityConfig;
if (settings.metadata === undefined) {
settings.metadata = {};
}
if (settings.optimizer === undefined) {
settings.optimizer = {};
}
// Unset useLiteralContent due to solc metadata size restriction
settings.metadata.useLiteralContent = false;
// Override optimizer settings for all compilers
settings.optimizer.enabled = false;
}
return compilationJob;
});
}

module.exports = plugin;
2 changes: 1 addition & 1 deletion test/integration/projects/solc-6/buidler.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ usePlugin("@nomiclabs/buidler-truffle5");

module.exports={
defaultNetwork: "buidlerevm",
solc: {
solidity: {
version: "0.6.5"
}
};

0 comments on commit 72bf6cc

Please sign in to comment.