Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Fix issue with errors being swallowed in child compilations #750

Merged
merged 4 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions build/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,39 @@ const {
const mergeChunkMetadata = require('./merge-chunk-metadata');
const loadFusionRC = require('./load-fusionrc.js');

function getErrors(info) {
let errors = [].concat(info.errors);
if (info.children.length) {
Copy link
Member

Choose a reason for hiding this comment

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

Ah, good catch!

errors = errors.concat(
info.children.reduce((x, child) => {
return x.concat(getErrors(child));
}, [])
);
}
return dedupeErrors(errors);
}

function getWarnings(info) {
let warnings = [].concat(info.warnings);
if (info.children.length) {
warnings = warnings.concat(
info.children.reduce((x, child) => {
return x.concat(getWarnings(child));
}, [])
);
}
return dedupeErrors(warnings);
}

function dedupeErrors(items) {
const re = /BabelLoaderError(.|\n)+( {4}at transpile)/gim;
return items.map(item => item.replace(re, '$2'));
}

function getStatsLogger({dir, logger, env}) {
return (err, stats) => {
// syntax errors are logged 4 times (once by webpack, once by babel, once on server and once on client)
// we only want to log each syntax error once
function dedupeErrors(items) {
const re = /BabelLoaderError(.|\n)+( {4}at transpile)/gim;
return items.map(item => item.replace(re, '$2'));
}

const isProd = env === 'production';

if (err) {
Expand All @@ -50,7 +74,7 @@ function getStatsLogger({dir, logger, env}) {
fs.writeFile(file, JSON.stringify(info, null, 2), () => {});

if (stats.hasErrors()) {
dedupeErrors(info.errors).forEach(e => logger.error(e));
getErrors(info).forEach(e => logger.error(e));
}
// TODO(#13): These logs seem to be kinda noisy for dev.
if (isProd) {
Expand All @@ -71,7 +95,7 @@ function getStatsLogger({dir, logger, env}) {
});
}
if (stats.hasWarnings()) {
dedupeErrors(info.warnings).forEach(e => logger.warn(e));
getWarnings(info).forEach(e => logger.warn(e));
}
};
}
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/colocated-tests-error/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`syntax error (colocated tests) prod: production-error 1`] = `5`;

exports[`syntax error (colocated tests) prod: production-warn 1`] = `0`;
23 changes: 18 additions & 5 deletions test/e2e/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ function testDev(title, dir) {
const env = 'development';
const entryPath = `.fusion/dist/${env}/server/server-main.js`;
const entry = path.resolve(dir, entryPath);
const logger = {
warn: jest.fn(),
error: jest.fn(),
info: jest.fn(),
};

const compiler = new Compiler({env, dir});
const compiler = new Compiler({env, dir, logger});
await compiler.clean();

const compilationError = await new Promise(resolve => {
Expand All @@ -35,10 +40,11 @@ function testDev(title, dir) {
return resolve(err || new Error('Compiler stats included errors.'));
}

expect(logger.warn.mock.calls.length).toMatchSnapshot(`dev-warn`);
expect(logger.error.mock.calls.length).toMatchSnapshot(`dev-error`);
return resolve(false);
});
});

t.ok(compilationError, 'Should produce compilation error');
// $FlowFixMe
t.throws(() => require(entry), 'Should throw');
Expand All @@ -53,8 +59,12 @@ function testProd(title, dir) {
const env = 'production';
const entryPath = `.fusion/dist/${env}/server/server-main.js`;
const entry = path.resolve(dir, entryPath);

const compiler = new Compiler({env, dir});
const logger = {
warn: jest.fn(),
error: jest.fn(),
info: jest.fn(),
};
const compiler = new Compiler({env, dir, logger});
await compiler.clean();

const compilationError = await new Promise(resolve => {
Expand All @@ -66,8 +76,11 @@ function testProd(title, dir) {
return resolve(false);
});
});

t.ok(compilationError, 'Should produce compilation error');
expect(logger.warn.mock.calls.length).toMatchSnapshot(`production-warn`);
expect(logger.error.mock.calls.length).toMatchSnapshot(
`production-error`
);

// $FlowFixMe
t.throws(() => require(entry), 'Should throw');
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/missing-module/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`missing module prod: production-error 1`] = `5`;

exports[`missing module prod: production-warn 1`] = `0`;
5 changes: 5 additions & 0 deletions test/e2e/syntax-error/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`syntax error prod: production-error 1`] = `5`;

exports[`syntax error prod: production-warn 1`] = `0`;