Skip to content

Commit 2fd64fd

Browse files
committed
Fail the build when noEmitOnError is set and there's a type error
1 parent 3d0fb1f commit 2fd64fd

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ install:
2121
- yarn
2222

2323
cache:
24-
# - "%LOCALAPPDATA%\\Yarn"
24+
- "%LOCALAPPDATA%\\Yarn"
2525

2626
# Post-install test scripts.
2727
test_script:

lib/incremental-typescript-compiler.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ module.exports = class IncrementalTypescriptCompiler {
3434

3535
this._buildDeferred = RSVP.defer();
3636
this._isSynced = false;
37+
this._pendingErrors = [];
3738
this._triggerDir = `${this.outDir()}/.rebuild`;
3839
this._pendingAutoresolve = null;
3940
this._didAutoresolve = false;
41+
this._watchProgram = null;
4042
}
4143

4244
treeForHost() {
@@ -108,7 +110,7 @@ module.exports = class IncrementalTypescriptCompiler {
108110
let project = this.project;
109111
let outDir = this.outDir();
110112

111-
compile(project, { outDir, watch: true }, {
113+
this._watchProgram = compile(project, { outDir, watch: true }, {
112114
reportWatchStatus: (diagnostic) => {
113115
let text = diagnostic.messageText;
114116

@@ -135,11 +137,17 @@ module.exports = class IncrementalTypescriptCompiler {
135137

136138
reportDiagnostic: (diagnostic) => {
137139
if (diagnostic.category !== 2) {
138-
this.project.ui.write(ts.formatDiagnostic(diagnostic, {
140+
let message = ts.formatDiagnostic(diagnostic, {
139141
getCanonicalFileName: path => path,
140142
getCurrentDirectory: ts.sys.getCurrentDirectory,
141143
getNewLine: () => ts.sys.newLine,
142-
}));
144+
});
145+
146+
if (this._shouldFailOnTypeError()) {
147+
this.didError(message);
148+
} else {
149+
this.project.ui.write(message);
150+
}
143151
}
144152
}
145153
});
@@ -163,9 +171,27 @@ module.exports = class IncrementalTypescriptCompiler {
163171
}
164172
}
165173

174+
didError(message) {
175+
this._pendingErrors.push(message);
176+
}
177+
166178
didSync() {
167179
this._isSynced = true;
168-
this._buildDeferred.resolve();
180+
if (this._pendingErrors.length) {
181+
this._buildDeferred.reject(new Error(this._pendingErrors.join('\n')));
182+
this._pendingErrors = [];
183+
} else {
184+
this._buildDeferred.resolve();
185+
}
186+
}
187+
188+
getProgram() {
189+
return this._watchProgram.getProgram();
190+
}
191+
192+
_shouldFailOnTypeError() {
193+
let options = this.getProgram().getCompilerOptions();
194+
return !!options.noEmitOnError;
169195
}
170196

171197
_mirageDirectory() {

lib/utilities/compile.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,24 @@ module.exports = function compile(project, tsOptions, callbacks) {
2323
fullOptions,
2424
buildWatchHooks(ts.sys),
2525
createProgram,
26-
callbacks.reportDiagnostic,
27-
callbacks.reportWatchStatus
26+
diagnosticCallback(callbacks.reportDiagnostic),
27+
diagnosticCallback(callbacks.reportWatchStatus)
2828
);
2929

3030
return ts.createWatchProgram(host);
3131
};
3232

33+
function diagnosticCallback(callback) {
34+
if (callback) {
35+
// The initial callbacks may be synchronously invoked during instantiation of the
36+
// WatchProgram, which is annoying if those callbacks want to _reference_ it, so
37+
// we always force invocation to be asynchronous for consistency.
38+
return (diagnostic) => {
39+
process.nextTick(() => callback(diagnostic));
40+
};
41+
}
42+
}
43+
3344
function buildWatchHooks(sys) {
3445
let watchedFiles = new Map();
3546

@@ -48,7 +59,7 @@ function buildWatchHooks(sys) {
4859
if (!fs.existsSync(dir)) return;
4960

5061
let ignored = /\/(\..*?|dist|node_modules|tmp)\//;
51-
let watcher = chokidar.watch(dir, { ignored });
62+
let watcher = chokidar.watch(dir, { ignored, ignoreInitial: true });
5263

5364
watcher.on('all', (type, path) => {
5465
path = path.replace(/\\/g, '/'); // Normalize Windows

0 commit comments

Comments
 (0)