Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.0.4

* [gracefully handle error thrown from the service](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/249)

## v1.0.3

* [use worker-rpc library for inter-process communication](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/231)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fork-ts-checker-webpack-plugin",
"version": "1.0.3",
"version": "1.0.4",
"description": "Runs typescript type checker and linter on separate process.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
12 changes: 11 additions & 1 deletion src/NormalizedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface NormalizedMessageJson {
file?: string;
line?: number;
character?: number;
stack?: string;
}

export class NormalizedMessage {
Expand All @@ -19,13 +20,16 @@ export class NormalizedMessage {
public static readonly SEVERITY_ERROR: Severity = 'error';
public static readonly SEVERITY_WARNING: Severity = 'warning';

public static readonly ERROR_CODE_INTERNAL = 'INTERNAL_ERROR';

public readonly type: ErrorType;
public readonly code: string | number;
public readonly severity: Severity;
public readonly content: string;
public readonly file?: string;
public readonly line?: number;
public readonly character?: number;
public readonly stack?: string;

constructor(data: NormalizedMessageJson) {
this.type = data.type;
Expand All @@ -35,6 +39,7 @@ export class NormalizedMessage {
this.file = data.file;
this.line = data.line;
this.character = data.character;
this.stack = data.stack;
}

public static createFromJSON(json: NormalizedMessageJson) {
Expand Down Expand Up @@ -73,6 +78,10 @@ export class NormalizedMessage {
messageA.content,
messageB.content
) ||
NormalizedMessage.compareOptionalStrings(
messageA.stack,
messageB.stack
) ||
0 /* EqualTo */
);
}
Expand Down Expand Up @@ -149,7 +158,8 @@ export class NormalizedMessage {
content: this.content,
file: this.file,
line: this.line,
character: this.character
character: this.character,
stack: this.stack
} as NormalizedMessageJson;
}

Expand Down
15 changes: 15 additions & 0 deletions src/NormalizedMessageFactories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,18 @@ export const makeCreateNormalizedMessageFromRuleFailure = () => {
};
return createNormalizedMessageFromRuleFailure;
};

export const makeCreateNormalizedMessageFromInternalError = () => {
const createNormalizedMessageFromInternalError = (error: any) => {
return new NormalizedMessage({
type: NormalizedMessage.TYPE_DIAGNOSTIC,
severity: NormalizedMessage.SEVERITY_ERROR,
code: NormalizedMessage.ERROR_CODE_INTERNAL,
content:
typeof error.message === 'string' ? error.message : error.toString(),
stack: typeof error.stack === 'string' ? error.stack : undefined,
file: '[internal]'
});
};
return createNormalizedMessageFromInternalError;
};
10 changes: 10 additions & 0 deletions src/formatter/codeframeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ export function createCodeframeFormatter(options: any) {
: colors.bold.red;
const positionColor = colors.dim;

if (message.code === NormalizedMessage.ERROR_CODE_INTERNAL) {
return (
messageColor(`INTERNAL ${message.severity.toUpperCase()}: `) +
message.content +
(message.stack
? os.EOL + 'stack trace:' + os.EOL + colors.gray(message.stack)
: '')
);
}

const file = message.file;
const source =
file && FsHelper.existsSync(file) && fs.readFileSync(file, 'utf-8');
Expand Down
10 changes: 10 additions & 0 deletions src/formatter/defaultFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ export function createDefaultFormatter() {
const fileAndNumberColor = colors.bold.cyan;
const codeColor = colors.grey;

if (message.code === NormalizedMessage.ERROR_CODE_INTERNAL) {
return (
messageColor(`INTERNAL ${message.severity.toUpperCase()}: `) +
message.content +
(message.stack
? os.EOL + 'stack trace:' + os.EOL + colors.gray(message.stack)
: '')
);
}

return [
messageColor(`${message.severity.toUpperCase()} in `) +
fileAndNumberColor(
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class ForkTsCheckerWebpackPlugin {
private measureTime: boolean;
private performance: any;
private startAt: number = 0;

private nodeArgs: string[] = [];

constructor(options?: Partial<Options>) {
options = options || ({} as Options);
this.options = { ...options };
Expand Down Expand Up @@ -573,10 +576,10 @@ class ForkTsCheckerWebpackPlugin {
),
[],
{
execArgv:
this.workersNumber > 1
? []
: ['--max-old-space-size=' + this.memoryLimit],
execArgv: (this.workersNumber > 1
? []
: ['--max-old-space-size=' + this.memoryLimit]
).concat(this.nodeArgs),
env: {
...process.env,
TYPESCRIPT_PATH: this.typescriptPath,
Expand Down
10 changes: 6 additions & 4 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { IncrementalCheckerInterface } from './IncrementalCheckerInterface';
import { ApiIncrementalChecker } from './ApiIncrementalChecker';
import {
makeCreateNormalizedMessageFromDiagnostic,
makeCreateNormalizedMessageFromRuleFailure
makeCreateNormalizedMessageFromRuleFailure,
makeCreateNormalizedMessageFromInternalError
} from './NormalizedMessageFactories';
import { RpcProvider } from 'worker-rpc';
import { RunPayload, RunResult, RUN } from './RpcTypes';
Expand All @@ -30,6 +31,7 @@ export const createNormalizedMessageFromDiagnostic = makeCreateNormalizedMessage
typescript
);
export const createNormalizedMessageFromRuleFailure = makeCreateNormalizedMessageFromRuleFailure();
export const createNormalizedMessageFromInternalError = makeCreateNormalizedMessageFromInternalError();

const checker: IncrementalCheckerInterface =
process.env.USE_INCREMENTAL_API === 'true'
Expand Down Expand Up @@ -64,9 +66,9 @@ async function run(cancellationToken: CancellationToken) {
let diagnostics: NormalizedMessage[] = [];
let lints: NormalizedMessage[] = [];

checker.nextIteration();

try {
checker.nextIteration();

diagnostics = await checker.getDiagnostics(cancellationToken);
if (checker.hasLinter()) {
lints = checker.getLints(cancellationToken);
Expand All @@ -76,7 +78,7 @@ async function run(cancellationToken: CancellationToken) {
return undefined;
}

throw error;
diagnostics.push(createNormalizedMessageFromInternalError(error));
}

if (cancellationToken.isCancellationRequested()) {
Expand Down
16 changes: 16 additions & 0 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ function makeCommonTests(useTypescriptIncrementalApi) {

describe('[INTEGRATION] specific tests for useTypescriptIncrementalApi: false', function() {
this.timeout(60000);
var plugin;

function createCompiler(
options,
Expand All @@ -390,6 +391,7 @@ describe('[INTEGRATION] specific tests for useTypescriptIncrementalApi: false',
options = options || {};
options.useTypescriptIncrementalApi = false;
var compiler = helpers.createCompiler(options, happyPackMode, entryPoint);
plugin = compiler.plugin;
return compiler.webpack;
}

Expand Down Expand Up @@ -458,4 +460,18 @@ describe('[INTEGRATION] specific tests for useTypescriptIncrementalApi: false',
callback();
});
});

it('should handle errors within the IncrementalChecker gracefully as diagnostic', callback => {
var compiler = createCompiler();
plugin.nodeArgs = [
`--require`,
`${path.resolve(__dirname, './mocks/IncrementalCheckerWithError.js')}`
];

compiler.run(function(error, stats) {
expect(stats.compilation.errors.length).to.equal(1);
expect(stats.compilation.errors[0].message).to.include("I'm an error!");
callback();
});
});
});
9 changes: 9 additions & 0 deletions test/integration/mocks/IncrementalCheckerWithError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mock = require('mock-require');

mock('../../../lib/IncrementalChecker', {
IncrementalChecker: class {
nextIteration() {
throw new Error("I'm an error!");
}
}
});