Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: implement --failAfterWarnings flag #3712

Merged
merged 11 commits into from
Aug 14, 2020
1 change: 1 addition & 0 deletions cli/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Basic options:
--preserveSymlinks Do not follow symlinks when resolving files
--shimMissingExports Create shim variables for missing exports
--silent Don't print warnings
--failAfterWarnings Exit with an error code if there was a warning during the build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to extend documentation in the docs folder, just check where and how e.g. --enivronment is documented.

--sourcemapExcludeSources Do not include source code in source maps
--sourcemapFile <file> Specify bundle position for source maps
--no-stdin do not read "-" from stdin
Expand Down
9 changes: 8 additions & 1 deletion cli/run/batchWarnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ export interface BatchWarnings {
add: (warning: RollupWarning) => void;
readonly count: number;
flush: () => void;
readonly warningOccurred: boolean;
}

export default function batchWarnings() {
let deferredWarnings = new Map<keyof typeof deferredHandlers, RollupWarning[]>();
let count = 0;
let deferredWarnings = new Map<keyof typeof deferredHandlers, RollupWarning[]>();
let warningOccurred = false;

return {
get count() {
return count;
},

get warningOccurred() {
return warningOccurred;
},

add: (warning: RollupWarning) => {
count += 1;
warningOccurred = true;

if (warning.code! in deferredHandlers) {
getOrCreate(deferredWarnings, warning.code!, () => []).push(warning);
Expand Down
7 changes: 7 additions & 0 deletions cli/run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ export default async function runRollup(command: any) {
for (const inputOptions of options) {
await build(inputOptions, warnings, command.silent);
}
if (command.failAfterWarnings && warnings.warningOccurred) {
warnings.flush();
handleError({
code: 'FAIL_AFTER_WARNINGS',
message: 'Warnings occurred and --failAfterWarnings flag present'
});
}
} catch (err) {
warnings.flush();
handleError(err);
Expand Down
5 changes: 5 additions & 0 deletions docs/01-command-line-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ Many options have command line equivalents. In those cases, any arguments passed
--preserveSymlinks Do not follow symlinks when resolving files
--shimMissingExports Create shim variables for missing exports
--silent Don't print warnings
--failAfterWarnings Exit with an error code if there was a warning during the build
--sourcemapExcludeSources Do not include source code in source maps
--sourcemapFile <file> Specify bundle position for source maps
--no-stdin do not read "-" from stdin
Expand Down Expand Up @@ -387,6 +388,10 @@ _Note: While in watch mode, the `ROLLUP_WATCH` environment variable will be set

Don't print warnings to the console. If your configuration file contains an `onwarn` handler, this handler will still be called. To manually prevent that, you can access the command line options in your configuration file as described at the end of [Configuration Files](guide/en/#configuration-files).

#### `--failAfterWarnings`

Exit the build with an error if any warnings occurred, once the build is complete.

#### `--environment <values>`

Pass additional settings to the config file via `process.ENV`.
Expand Down
1 change: 1 addition & 0 deletions src/utils/options/mergeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function mergeOptions(
'environment',
'plugin',
'silent',
'failAfterWarnings',
'stdin',
'waitForBundleInput'
),
Expand Down
11 changes: 11 additions & 0 deletions test/cli/samples/fail-after-warnings/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { assertIncludes } = require('../../../utils.js');

module.exports = {
description: 'errors on warnings with --failAfterWarnings',
command: 'rollup -i main.js --failAfterWarnings',
error: () => true,
stderr: stderr => {
assertIncludes(stderr, '[!] Warnings occurred and --failAfterWarnings flag present');
return true;
}
};
3 changes: 3 additions & 0 deletions test/cli/samples/fail-after-warnings/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'unknown';

console.log(42);
3 changes: 3 additions & 0 deletions test/cli/samples/fail-after-warnings/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import something from 'unknown';

console.log(42);
11 changes: 0 additions & 11 deletions test/cli/samples/node-config-not-found/_expected.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/cli/samples/stdout-only-inline-sourcemaps/_expected.js

This file was deleted.

9 changes: 6 additions & 3 deletions test/misc/optionList.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.