From fa6cb23a6cfc15e2a7d046849c39186c27414b34 Mon Sep 17 00:00:00 2001 From: scnale Date: Tue, 27 Oct 2020 15:25:50 -0300 Subject: [PATCH] Update buidler plugin for Hardhat compilation pipeline (#540) --- plugins/buidler.plugin.js | 68 ++++++++++++++----- .../projects/solc-6/buidler.config.js | 2 +- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/plugins/buidler.plugin.js b/plugins/buidler.plugin.js index 87bddbce..cb1bdfef 100644 --- a/plugins/buidler.plugin.js +++ b/plugins/buidler.plugin.js @@ -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") @@ -42,6 +36,7 @@ function plugin() { let ui; let api; let config; + instrumentedSources = {}; try { death(buidlerUtils.finish.bind(null, config, api)); // Catch interrupt signals @@ -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); // ============== @@ -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); @@ -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; diff --git a/test/integration/projects/solc-6/buidler.config.js b/test/integration/projects/solc-6/buidler.config.js index d7348483..3aee4562 100644 --- a/test/integration/projects/solc-6/buidler.config.js +++ b/test/integration/projects/solc-6/buidler.config.js @@ -4,7 +4,7 @@ usePlugin("@nomiclabs/buidler-truffle5"); module.exports={ defaultNetwork: "buidlerevm", - solc: { + solidity: { version: "0.6.5" } };