Skip to content

Commit b0952b0

Browse files
committed
Suppress compilation warnings introduced by injected trace statements (#583)
1 parent 93214db commit b0952b0

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

plugins/hardhat.plugin.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
TASK_COMPILE,
1010
TASK_COMPILE_SOLIDITY_GET_COMPILER_INPUT,
1111
TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE,
12+
TASK_COMPILE_SOLIDITY_LOG_COMPILATION_ERRORS
1213
} = require("hardhat/builtin-tasks/task-names");
1314

1415
// Toggled true for `coverage` task only.
@@ -75,6 +76,18 @@ subtask(TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE).setAction(async (_,
7576
return compilationJob;
7677
});
7778

79+
// Suppress compilation warnings because injected trace function triggers
80+
// complaint about unused variable
81+
subtask(TASK_COMPILE_SOLIDITY_LOG_COMPILATION_ERRORS).setAction(async (_, __, runSuper) => {
82+
const defaultWarn = console.warn;
83+
84+
if (measureCoverage) {
85+
console.warn = () => {};
86+
}
87+
await runSuper();
88+
console.warn = defaultWarn;
89+
});
90+
7891
/**
7992
* Coverage task implementation
8093
* @param {HardhatUserArgs} args

plugins/resources/truffle.utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,29 @@ function normalizeConfig(config){
196196
return config;
197197
}
198198

199+
/**
200+
* Replacement logger which filters out compilation warnings triggered by injected trace
201+
* function definitions.
202+
*
203+
* @type {Object}
204+
*/
205+
const filteredLogger = {
206+
log: (val) => {
207+
const loggable = typeof val === 'string' &&
208+
!val.includes('Warning:') && // solc msg grep
209+
!process.env.SILENT; // unit tests
210+
211+
loggable && console.log(val);
212+
},
213+
warn: console.warn,
214+
error: console.error
215+
}
216+
199217
module.exports = {
200218
getTestFilePaths,
201219
setNetwork,
202220
setNetworkFrom,
203221
loadLibrary,
204222
normalizeConfig,
223+
filteredLogger
205224
}

plugins/truffle.plugin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,23 @@ async function plugin(config){
9090
path.basename(config.contracts_build_directory)
9191
);
9292

93+
// Filter compilation warnings
94+
const defaultLogger = config.logger;
95+
if (!config.verbose){
96+
config.logger = truffleUtils.filteredLogger;
97+
}
98+
9399
config.all = true;
100+
config.strict = false;
94101
config.compilers.solc.settings.optimizer.enabled = false;
95102

96103
// Run pre-compile hook;
97104
await api.onPreCompile(config);
98105

99106
// Compile Instrumented Contracts
100107
await truffle.contracts.compile(config);
108+
config.logger = defaultLogger;
109+
101110
await api.onCompileComplete(config);
102111

103112
config.test_files = await truffleUtils.getTestFilePaths(config);

test/units/truffle/flags.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ describe('Truffle Plugin: command line options', function() {
190190
truffleConfig.logger = mock.testLogger;
191191

192192
truffleConfig.temp = 'special_location';
193+
truffleConfig.verbose = true;
193194

194195
mock.install('Simple', 'simple.js', solcoverConfig);
195196
await plugin(truffleConfig);

0 commit comments

Comments
 (0)