From 43ccd3b181333653e0b1dcbd015e299f686e9097 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Fri, 4 Dec 2020 13:28:34 -0800 Subject: [PATCH] Suppress compilation warnings introduced by injected trace statements (#583) --- plugins/hardhat.plugin.js | 13 +++++++++++++ plugins/resources/truffle.utils.js | 19 +++++++++++++++++++ plugins/truffle.plugin.js | 9 +++++++++ test/units/truffle/flags.js | 1 + 4 files changed, 42 insertions(+) diff --git a/plugins/hardhat.plugin.js b/plugins/hardhat.plugin.js index 6a00f63..d695be3 100644 --- a/plugins/hardhat.plugin.js +++ b/plugins/hardhat.plugin.js @@ -9,6 +9,7 @@ const { TASK_COMPILE, TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT, TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE, + TASK_COMPILE_SOLIDITY_LOG_COMPILATION_ERRORS } = require("hardhat/builtin-tasks/task-names"); // Toggled true for `coverage` task only. @@ -75,6 +76,18 @@ subtask(TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE).setAction(async (_, return compilationJob; }); +// Suppress compilation warnings because injected trace function triggers +// complaint about unused variable +subtask(TASK_COMPILE_SOLIDITY_LOG_COMPILATION_ERRORS).setAction(async (_, __, runSuper) => { + const defaultWarn = console.warn; + + if (measureCoverage) { + console.warn = () => {}; + } + await runSuper(); + console.warn = defaultWarn; +}); + /** * Coverage task implementation * @param {HardhatUserArgs} args diff --git a/plugins/resources/truffle.utils.js b/plugins/resources/truffle.utils.js index a08376b..8e2cc70 100644 --- a/plugins/resources/truffle.utils.js +++ b/plugins/resources/truffle.utils.js @@ -196,10 +196,29 @@ function normalizeConfig(config){ return config; } +/** + * Replacement logger which filters out compilation warnings triggered by injected trace + * function definitions. + * + * @type {Object} + */ +const filteredLogger = { + log: (val) => { + const loggable = typeof val === 'string' && + !val.includes('Warning:') && // solc msg grep + !process.env.SILENT; // unit tests + + loggable && console.log(val); + }, + warn: console.warn, + error: console.error +} + module.exports = { getTestFilePaths, setNetwork, setNetworkFrom, loadLibrary, normalizeConfig, + filteredLogger } diff --git a/plugins/truffle.plugin.js b/plugins/truffle.plugin.js index aebd9df..c1850df 100644 --- a/plugins/truffle.plugin.js +++ b/plugins/truffle.plugin.js @@ -90,7 +90,14 @@ async function plugin(config){ path.basename(config.contracts_build_directory) ); + // Filter compilation warnings + const defaultLogger = config.logger; + if (!config.verbose){ + config.logger = truffleUtils.filteredLogger; + } + config.all = true; + config.strict = false; config.compilers.solc.settings.optimizer.enabled = false; // Run pre-compile hook; @@ -98,6 +105,8 @@ async function plugin(config){ // Compile Instrumented Contracts await truffle.contracts.compile(config); + config.logger = defaultLogger; + await api.onCompileComplete(config); config.test_files = await truffleUtils.getTestFilePaths(config); diff --git a/test/units/truffle/flags.js b/test/units/truffle/flags.js index af726eb..2365041 100644 --- a/test/units/truffle/flags.js +++ b/test/units/truffle/flags.js @@ -190,6 +190,7 @@ describe('Truffle Plugin: command line options', function() { truffleConfig.logger = mock.testLogger; truffleConfig.temp = 'special_location'; + truffleConfig.verbose = true; mock.install('Simple', 'simple.js', solcoverConfig); await plugin(truffleConfig);