From a3e8ee830215f528111dfc8d6b66205ec95295bf Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 17 Sep 2020 14:13:13 +0530 Subject: [PATCH] fix: show warning if bail and watch are used together --- packages/webpack-cli/lib/groups/ConfigGroup.js | 6 ++++++ packages/webpack-cli/lib/utils/Compiler.js | 5 +++++ test/bail/bail-and-watch-warning.test.js | 12 ++++++++++++ test/bail/main.js | 1 + test/bail/webpack.config.js | 6 ++++++ test/core-flags/bail-flag.test.js | 8 ++++++++ 6 files changed, 38 insertions(+) create mode 100644 test/bail/bail-and-watch-warning.test.js create mode 100644 test/bail/main.js create mode 100644 test/bail/webpack.config.js diff --git a/packages/webpack-cli/lib/groups/ConfigGroup.js b/packages/webpack-cli/lib/groups/ConfigGroup.js index ed22885fce9..64cedceb59e 100644 --- a/packages/webpack-cli/lib/groups/ConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ConfigGroup.js @@ -127,6 +127,12 @@ class ConfigGroup extends GroupHelper { newOptionsObject['options'] = configOptions; } + //warn the user if bail and watch both are used together + const { bail, watch } = newOptionsObject['options']; + if (bail && watch) { + logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); + } + if (configOptions && configPath.includes('.webpack')) { const currentPath = configPath; const parentContext = dirname(currentPath).split(sep).slice(0, -1).join(sep); diff --git a/packages/webpack-cli/lib/utils/Compiler.js b/packages/webpack-cli/lib/utils/Compiler.js index 20774ddbe9e..4a2c4686e96 100644 --- a/packages/webpack-cli/lib/utils/Compiler.js +++ b/packages/webpack-cli/lib/utils/Compiler.js @@ -117,6 +117,11 @@ class Compiler { return interactive(options, outputOptions); } + //warn the user if bail and watch both are used together + if (this.compiler.options.bail && outputOptions.watch) { + logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.'); + } + if (this.compiler.compilers) { this.compiler.compilers.forEach((comp, idx) => { this.setUpHookForCompilation(comp, outputOptions, options[idx]); diff --git a/test/bail/bail-and-watch-warning.test.js b/test/bail/bail-and-watch-warning.test.js new file mode 100644 index 00000000000..9700436421d --- /dev/null +++ b/test/bail/bail-and-watch-warning.test.js @@ -0,0 +1,12 @@ +'use strict'; + +const { run } = require('../utils/test-utils'); + +describe('bail and watch warning', () => { + it('should log warning if bail and watch both options are true', () => { + const { stderr, stdout } = run(__dirname, ['-c', 'webpack.config.js']); + + expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`); + expect(stdout).toBeTruthy(); + }); +}); diff --git a/test/bail/main.js b/test/bail/main.js new file mode 100644 index 00000000000..15e0a061dc2 --- /dev/null +++ b/test/bail/main.js @@ -0,0 +1 @@ +console.log('bail and watch warning test'); diff --git a/test/bail/webpack.config.js b/test/bail/webpack.config.js new file mode 100644 index 00000000000..e066755baef --- /dev/null +++ b/test/bail/webpack.config.js @@ -0,0 +1,6 @@ +module.exports = { + entry: './main.js', + mode: 'development', + bail: true, + watch: true, +}; diff --git a/test/core-flags/bail-flag.test.js b/test/core-flags/bail-flag.test.js index 48587933155..d938bdf2609 100644 --- a/test/core-flags/bail-flag.test.js +++ b/test/core-flags/bail-flag.test.js @@ -16,4 +16,12 @@ describe('--bail flag', () => { expect(stderr).toBeFalsy(); expect(stdout).toContain('bail: false'); }); + + it('should log warning if --bail and --watch are used together', () => { + const { stderr, stdout } = run(__dirname, ['--bail', '--watch']); + + expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`); + expect(stdout).toContain('bail: true'); + expect(stdout).toContain('watch: true'); + }); });