forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildmessage.js
376 lines (328 loc) · 11.1 KB
/
buildmessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
var _ = require('underscore');
var files = require('./files.js');
var parseStack = require('./parse-stack.js');
var debugBuild = !!process.env.METEOR_DEBUG_BUILD;
// A job is something like "building package foo". It contains the set
// of messages generated by tha job. A given build run could contain
// several jobs. Each job has an (absolute) path associated with
// it. Filenames in messages within a job are to be interpreted
// relative to that path.
var Job = function (options) {
var self = this;
self.messages = [];
// Should be something like "building package 'foo'"
// Should look good in "While $title:\n[messages]"
self.title = options.title;
self.rootPath = options.rootPath;
// Array of Job (jobs created inside this job)
self.children = [];
};
_.extend(Job.prototype, {
// options may include type ("error"), message, func, file, line,
// column, stack (in the format returned by parseStack.parse())
addMessage: function (options) {
var self = this;
self.messages.push(options);
},
hasMessages: function () {
var self = this;
return self.messages.length > 0;
},
// Returns a multi-line string suitable for displaying to the user
formatMessages: function (indent) {
var self = this;
var out = "";
var already = {};
indent = new Array((indent || 0) + 1).join(' ');
_.each(self.messages, function (message) {
var stack = message.stack || [];
var line = indent;
if (message.file) {
line+= message.file;
if (message.line) {
line += ":" + message.line;
if (message.column) {
// XXX maybe exclude unless specifically requested (eg,
// for an automated tool that's parsing our output?)
line += ":" + message.column;
}
}
line += ": ";
} else {
// not sure how to display messages without a filenanme.. try this?
line += "error: ";
}
// XXX line wrapping would be nice..
line += message.message;
if (message.func && stack.length <= 1) {
line += " (at " + message.func + ")";
}
line += "\n";
if (stack.length > 1) {
_.each(stack, function (frame) {
// If a nontrivial stack trace (more than just the file and line
// we already complained about), print it.
var where = "";
if (frame.file) {
where += frame.file;
if (frame.line) {
where += ":" + frame.line;
if (frame.column) {
where += ":" + frame.column;
}
}
}
if (! frame.func && ! where)
return; // that's a pretty lame stack frame
line += " at ";
if (frame.func)
line += frame.func + " (" + where + ")\n";
else
line += where + "\n";
});
line += "\n";
}
// Deduplicate messages (only when exact duplicates, including stack)
if (! (line in already)) {
out += line;
already[line] = true;
}
});
return out;
}
});
// A MessageSet contains a set of jobs, which in turn each contain a
// set of messages.
var MessageSet = function () {
var self = this;
self.jobs = [];
};
_.extend(MessageSet.prototype, {
formatMessages: function () {
var self = this;
var jobsWithMessages = _.filter(self.jobs, function (job) {
return job.hasMessages();
});
return _.map(jobsWithMessages, function (job) {
var out = '';
out += "While " + job.title + ":\n";
out += job.formatMessages(0);
return out;
}).join('\n'); // blank line between jobs
},
hasMessages: function () {
var self = this;
return !! _.find(self.jobs, function (job) {
return job.hasMessages();
});
},
// Copy all of the messages in another MessageSet into this
// MessageSet. If the other MessageSet is subsequently mutated,
// results are undefined.
//
// XXX rather than this, the user should be able to create a
// MessageSet and pass it into capture(), and functions such as
// bundle() should take and mutate, rather than return, a
// MessageSet.
merge: function (messageSet) {
var self = this;
_.each(messageSet.jobs, function (j) {
self.jobs.push(j);
});
}
});
var currentMessageSet = null;
var currentJob = null;
// Create a new MessageSet, run `f` with that as the current
// MessageSet for the purpose of accumulating and recovering from
// errors (see error()), and then discard the return value of `f` and
// return the MessageSet.
//
// Note that you must also create a job (with enterJob) to actually
// begin capturing errors. Alternately you may pass `options`
// (otherwise optional) and a job will be created for you based on
// `options`.
//
// ** Not compatible with multifiber environments **
// Using a single fiber to block on i/o is fine however.
var capture = function (options, f) {
var originalMessageSet = currentMessageSet;
var messageSet = new MessageSet;
currentMessageSet = messageSet;
var originalJob = currentJob;
var job = null;
if (typeof options === "object") {
job = new Job(options);
currentMessageSet.jobs.push(job);
} else {
f = options; // options not actually provided
}
currentJob = job;
if (debugBuild)
console.log("START CAPTURE: " + options.title);
try {
f();
} finally {
currentMessageSet = originalMessageSet;
currentJob = originalJob;
if (debugBuild)
console.log("DONE CAPTURE: " + options.title);
}
return messageSet;
};
// Called from inside capture(), creates a new Job inside the current
// MessageSet and run `f` inside of it, so that any messages emitted
// by `f` are logged in the Job. Returns the return value of `f`. May
// be called recursively.
//
// Called not from inside capture(), does nothing (except call f).
//
// options:
// - title: a title for the job (required)
// - rootPath: the absolute path relative to which paths in messages
// in this job should be interpreted (omit if there is no way to map
// files that this job talks about back to files on disk)
var enterJob = function (options, f) {
if (typeof options === "function") {
f = options;
options = {};
}
if (! currentMessageSet) {
return f();
}
var job = new Job(options);
var originalJob = currentJob;
if (originalJob)
originalJob.children.push(job);
currentMessageSet.jobs.push(job);
currentJob = job;
if (debugBuild)
console.log("START: " + options.title);
try {
var ret = f();
} finally {
if (debugBuild)
console.log("DONE: " + options.title);
currentJob = originalJob;
}
return ret;
};
// If not inside a job, return false. Otherwise, return true if any
// messages (presumably errors) have been recorded for this job
// (including subjobs created inside this job), else false.
var jobHasMessages = function () {
var search = function (job) {
if (job.hasMessages())
return true;
return !! _.find(job.children, search);
};
return currentJob ? search(currentJob) : false;
};
// Given a function f, return a "marked" version of f. The mark
// indicates that stack traces should stop just above f. So if you
// mark a user-supplied callback function before calling it, you'll be
// able to show the user just the "user portion" of the stack trace
// (the part inside their own code, and not all of the innards of the
// code that called it).
var markBoundary = function (f) {
return parseStack.markBottom(f);
};
// Record a build error. If inside a job, add the error to the current
// job and return (caller should do its best to recover and
// continue). Otherwise, throws an exception based on the error.
//
// options may include
// - file: the file containing the error, relative to the root of the build
// (this must be agreed upon out of band)
// - line: the (1-indexed) line in the file that contains the error
// - column: the (1-indexed) column in that line where the error begins
// - func: the function containing the code that triggered the error
// - useMyCaller: true to capture information the caller (function
// name, file, and line). It captures not the information of the
// caller of error(), but that caller's caller. It saves them in
// 'file', 'line', and 'column' (overwriting any values passed in
// for those). It also captures the user portion of the stack,
// starting at and including the caller's caller.
// If this is a number instead of 'true', skips that many stack frames.
// - downcase: if true, the first character of `message` will be
// converted to lower case.
// - secondary: ignore this error if there are are already other
// errors in this job (the implication is that it's probably
// downstream of the other error, ie, a consequence of our attempt
// to continue past other errors)
var error = function (message, options) {
options = options || {};
if (options.downcase)
message = message.slice(0,1).toLowerCase() + message.slice(1);
if (! currentJob)
throw new Error("Error: " + message);
if (options.secondary && jobHasMessages())
return; // skip it
var info = _.extend({
message: message
}, options);
if ('useMyCaller' in info) {
if (info.useMyCaller) {
info.stack = parseStack.parse(new Error()).slice(2);
var howManyToSkip = (
typeof info.useMyCaller === "number" ? info.useMyCaller : 0);
var caller = info.stack[howManyToSkip];
info.func = caller.func;
info.file = caller.file;
info.line = caller.line;
info.column = caller.column;
}
delete info.useMyCaller;
}
currentJob.addMessage(info);
};
// Record an exception. The message as well as any file and line
// information be read directly out of the exception. If not in a job,
// throws the exception instead. Also capture the user portion of the stack.
//
// There is special handling for files.FancySyntaxError exceptions. We
// will grab the file and location information where the syntax error
// actually occurred, rather than the place where the exception was
// thrown.
var exception = function (error) {
if (! currentJob) {
// XXX this may be the wrong place to do this, but it makes syntax errors in
// files loaded via unipackage.load have context.
if (error instanceof files.FancySyntaxError) {
error.message = "Syntax error: " + error.message + " at " +
error.file + ":" + error.line + ":" + error.column;
}
throw error;
}
var message = error.message;
if (error instanceof files.FancySyntaxError) {
// No stack, because FancySyntaxError isn't a real Error and has no stack
// property!
currentJob.addMessage({
message: message,
file: error.file,
line: error.line,
column: error.column
});
} else {
var stack = parseStack.parse(error);
var locus = stack[0];
currentJob.addMessage({
message: message,
stack: stack,
func: locus.func,
file: locus.file,
line: locus.line,
column: locus.column
});
}
};
var buildmessage = exports;
_.extend(exports, {
capture: capture,
enterJob: enterJob,
markBoundary: markBoundary,
error: error,
exception: exception,
jobHasMessages: jobHasMessages
});