Skip to content

Commit

Permalink
fix: exit process in case of schema errors
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Oct 10, 2020
1 parent 02b6d21 commit 71e89b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
16 changes: 11 additions & 5 deletions packages/webpack-cli/lib/utils/Compiler.js
Expand Up @@ -129,10 +129,17 @@ class Compiler {
this.compiler = await webpack(options);
this.compilerOptions = options;
} catch (err) {
process.stdout.write('\n');
logger.error(`${err.name}: ${err.message}`);
process.stdout.write('\n');
return;
// https://github.com/webpack/webpack/blob/master/lib/index.js#L267
// https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
const ValidationError = webpack.ValidationError ? webpack.ValidationError : webpack.WebpackOptionsValidationError;
// In case of schema errors print and exit process
// For webpack@4 and webpack@5
if (err instanceof ValidationError) {
logger.error(`\n${err.message}`);
} else {
logger.error(`\n${err}`);
}
process.exit(1);
}
}

Expand All @@ -153,7 +160,6 @@ class Compiler {
const interactive = require('./interactive');
return interactive(options, outputOptions);
}

if (this.compiler.compilers) {
this.compiler.compilers.forEach((comp, idx) => {
bailAndWatchWarning(comp); //warn the user if bail and watch both are used together
Expand Down
2 changes: 1 addition & 1 deletion test/core-flags/devtool-flag.test.js
Expand Up @@ -13,6 +13,6 @@ describe('--devtool flag', () => {
it('should throw error for invalid config', () => {
const { stderr } = run(__dirname, ['--devtool', 'invalid']);

expect(stderr).toContain('ValidationError: Invalid configuration object');
expect(stderr).toContain('Invalid configuration object');
});
});
23 changes: 23 additions & 0 deletions test/invalid-schema/invalid-schema.test.js
@@ -0,0 +1,23 @@
'use strict';
const { run, isWindows } = require('../utils/test-utils');

describe('invalid schema', () => {
it('should log webpack error and exit process on invalid config', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--config', './webpack.config.mock.js']);
console.log({ stderr, stdout, exitCode });
expect(stderr).toContain('Invalid configuration object');
// TODO - Fix exitcode check on windows
if (!isWindows) {
expect(exitCode).toEqual(1);
}
});

it('should log webpack error and exit process on invalid flag', () => {
const { stderr, exitCode } = run(__dirname, ['--mode', 'Yukihira']);
expect(stderr).toContain('Invalid configuration object');
// TODO - Fix exitcode check on windows
if (!isWindows) {
expect(exitCode).toEqual(1);
}
});
});
3 changes: 3 additions & 0 deletions test/invalid-schema/webpack.config.mock.js
@@ -0,0 +1,3 @@
module.exports = {
mode: 'Nishinoya Yuu',
};
9 changes: 6 additions & 3 deletions test/stats/cli-flags/stats.test.js
@@ -1,7 +1,7 @@
/* eslint-disable node/no-extraneous-require */
/* eslint-disable node/no-unpublished-require */
'use strict';
const { run, isWebpack5 } = require('../../utils/test-utils');
const { run, isWebpack5, isWindows } = require('../../utils/test-utils');

const presets = ['normal', 'detailed', 'errors-only', 'errors-warnings', 'minimal', 'verbose', 'none'];

Expand Down Expand Up @@ -33,7 +33,7 @@ describe('stats flag', () => {
});

it('should warn when an unknown flag stats value is passed', () => {
const { stderr, stdout } = run(__dirname, ['--stats', 'foo']);
const { stderr, exitCode } = run(__dirname, ['--stats', 'foo']);
expect(stderr).toBeTruthy();
expect(stderr).toContain('* configuration.stats should be one of these:');
if (isWebpack5) {
Expand All @@ -43,6 +43,9 @@ describe('stats flag', () => {
} else {
expect(stderr).toContain('"none" | "errors-only" | "minimal" | "normal" | "detailed" | "verbose" | "errors-warnings"');
}
expect(stdout).toBeTruthy();
// TODO - Fix exitcode check on windows
if (!isWindows) {
expect(exitCode).toEqual(1);
}
});
});
2 changes: 1 addition & 1 deletion test/target/flag-test/target-flag.test.js
Expand Up @@ -43,7 +43,7 @@ describe('--target flag', () => {
it(`should throw error with invalid value for --target`, () => {
const { stderr } = run(__dirname, ['--target', 'invalid']);
if (isWebpack5) {
expect(stderr).toContain(`Error: Unknown target 'invalid'`);
expect(stderr).toContain(`Unknown target 'invalid'`);
} else {
expect(stderr).toContain('Invalid configuration object');
}
Expand Down

0 comments on commit 71e89b4

Please sign in to comment.