Skip to content

Commit 4381f72

Browse files
committed
Merge pull request #581 from Microsoft/instrument
Add jake task for making instrumented tsc
2 parents 5b1789b + b8b5227 commit 4381f72

File tree

5 files changed

+147
-24
lines changed

5 files changed

+147
-24
lines changed

Jakefile

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ function concatenateFiles(destinationFile, sourceFiles) {
123123
}
124124

125125
var useDebugMode = false;
126+
var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node");
127+
var compilerFilename = "tsc.js";
126128
/* Compiles a file from a list of sources
127129
* @param outFile: the target file name
128130
* @param sources: an array of the names of the source files
@@ -134,10 +136,9 @@ var useDebugMode = false;
134136
function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOutFile) {
135137
file(outFile, prereqs, function() {
136138
var dir = useBuiltCompiler ? builtLocalDirectory : LKGDirectory;
137-
var compilerFilename = "tsc.js";
138139
var options = "-removeComments --module commonjs -noImplicitAny "; //" -propagateEnumConstants "
139140

140-
var cmd = (process.env.host || process.env.TYPESCRIPT_HOST || "node") + " " + dir + compilerFilename + " " + options + " ";
141+
var cmd = host + " " + dir + compilerFilename + " " + options + " ";
141142
if (useDebugMode) {
142143
cmd = cmd + " " + path.join(harnessDirectory, "external/es5compat.ts") + " " + path.join(harnessDirectory, "external/json2.ts") + " ";
143144
}
@@ -230,7 +231,7 @@ task("generate-diagnostics", [diagnosticInfoMapTs])
230231

231232

232233
// Local target to build the compiler and services
233-
var tscFile = path.join(builtLocalDirectory, "tsc.js");
234+
var tscFile = path.join(builtLocalDirectory, compilerFilename);
234235
compileFile(tscFile, compilerSources, [builtLocalDirectory, copyright].concat(compilerSources), [copyright], /*useBuiltCompiler:*/ false);
235236

236237
var servicesFile = path.join(builtLocalDirectory, "typescriptServices.js");
@@ -312,9 +313,9 @@ function exec(cmd, completeHandler) {
312313
complete();
313314
});
314315
ex.addListener("error", function(e, status) {
315-
process.stderr.write(status);
316-
process.stderr.write(e);
317-
complete();
316+
process.stderr.write(status);
317+
process.stderr.write(e);
318+
complete();
318319
})
319320
try{
320321
ex.run();
@@ -382,9 +383,9 @@ task("runtests", ["tests", builtLocalDirectory], function() {
382383

383384
desc("Generates code coverage data via instanbul")
384385
task("generate-code-coverage", ["tests", builtLocalDirectory], function () {
385-
var cmd = 'istanbul cover node_modules/mocha/bin/_mocha -- -R min -t ' + testTimeout + ' ' + run;
386-
console.log(cmd);
387-
exec(cmd);
386+
var cmd = 'istanbul cover node_modules/mocha/bin/_mocha -- -R min -t ' + testTimeout + ' ' + run;
387+
console.log(cmd);
388+
exec(cmd);
388389
}, { async: true });
389390

390391
// Browser tests
@@ -469,7 +470,7 @@ compileFile(webhostJsPath, [webhostPath], [tscFile, webhostPath].concat(libraryT
469470

470471
desc("Builds the tsc web host");
471472
task("webhost", [webhostJsPath], function() {
472-
jake.cpR(path.join(builtLocalDirectory, "lib.d.ts"), "tests/webhost/", {silent: true});
473+
jake.cpR(path.join(builtLocalDirectory, "lib.d.ts"), "tests/webhost/", {silent: true});
473474
});
474475

475476
// Perf compiler
@@ -478,3 +479,36 @@ var perftscJsPath = "built/local/perftsc.js";
478479
compileFile(perftscJsPath, [perftscPath], [tscFile, perftscPath, "tests/perfsys.ts"].concat(libraryTargets), [], true);
479480
desc("Builds augmented version of the compiler for perf tests");
480481
task("perftsc", [perftscJsPath]);
482+
483+
// Instrumented compiler
484+
var loggedIOpath = harnessDirectory + 'loggedIO.ts';
485+
var loggedIOJsPath = builtLocalDirectory + 'loggedIO.js';
486+
file(loggedIOJsPath, [builtLocalDirectory, loggedIOpath], function() {
487+
var temp = builtLocalDirectory + 'temp';
488+
jake.mkdirP(temp);
489+
var options = "--outdir " + temp + ' ' + loggedIOpath;
490+
var cmd = host + " " + LKGDirectory + compilerFilename + " " + options + " ";
491+
console.log(cmd + "\n");
492+
var ex = jake.createExec([cmd]);
493+
ex.addListener("cmdEnd", function() {
494+
fs.renameSync(temp + '/harness/loggedIO.js', loggedIOJsPath);
495+
jake.rmRf(temp);
496+
complete();
497+
});
498+
ex.run();
499+
}, {async: true});
500+
501+
var instrumenterPath = harnessDirectory + 'instrumenter.ts';
502+
var instrumenterJsPath = builtLocalDirectory + 'instrumenter.js';
503+
compileFile(instrumenterJsPath, [instrumenterPath], [tscFile, instrumenterPath], [], true);
504+
505+
desc("Builds an instrumented tsc.js");
506+
task('tsc-instrumented', [loggedIOJsPath, instrumenterJsPath, tscFile], function() {
507+
var cmd = host + ' ' + instrumenterJsPath + ' record iocapture ' + builtLocalDirectory + compilerFilename;
508+
console.log(cmd);
509+
var ex = jake.createExec([cmd]);
510+
ex.addListener("cmdEnd", function() {
511+
complete();
512+
});
513+
ex.run();
514+
}, { async: true });

src/harness/harness.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,10 @@ module Harness {
732732

733733
var filemap: { [name: string]: ts.SourceFile; } = {};
734734
var register = (file: { unitName: string; content: string; }) => {
735-
var filename = Path.switchToForwardSlashes(file.unitName);
736-
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, options.target, /*version:*/ "0");
735+
if (file.content !== undefined) {
736+
var filename = Path.switchToForwardSlashes(file.unitName);
737+
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, options.target, /*version:*/ "0");
738+
}
737739
};
738740
inputFiles.forEach(register);
739741
otherFiles.forEach(register);
@@ -824,7 +826,7 @@ module Harness {
824826
globalErrors.forEach(err => outputErrorText(err));
825827

826828
// 'merge' the lines of each input file with any errors associated with it
827-
inputFiles.forEach(inputFile => {
829+
inputFiles.filter(f => f.content !== undefined).forEach(inputFile => {
828830
// Filter down to the errors in the file
829831
var fileErrors = diagnostics.filter(e => {
830832
var errFn = e.filename;
@@ -1253,7 +1255,7 @@ module Harness {
12531255
}
12541256

12551257
export function isLibraryFile(filePath: string): boolean {
1256-
return filePath.indexOf('lib.d.ts') >= 0 || filePath.indexOf('lib.core.d.ts') >= 0;
1258+
return (Path.getFileName(filePath) === 'lib.d.ts') || (Path.getFileName(filePath) === 'lib.core.d.ts');
12571259
}
12581260

12591261
if (Error) (<any>Error).stackTraceLimit = 1;

src/harness/instrumenter.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
declare var require: any, process: any;
2+
var fs: any = require('fs');
3+
var path: any = require('path');
4+
5+
function instrumentForRecording(fn: string, tscPath: string) {
6+
instrument(tscPath, 'sys = Playback.wrapSystem(sys); sys.startRecord("' + fn + '");', 'sys.endRecord();');
7+
}
8+
9+
function instrumentForReplay(logFilename: string, tscPath: string) {
10+
instrument(tscPath, 'sys = Playback.wrapSystem(sys); sys.startReplay("' + logFilename + '");');
11+
}
12+
13+
function instrument(tscPath: string, prepareCode: string, cleanupCode: string = '') {
14+
var bak = tscPath + '.bak';
15+
fs.exists(bak, (backupExists: boolean) => {
16+
var filename = tscPath;
17+
if (backupExists) {
18+
filename = bak;
19+
}
20+
21+
fs.readFile(filename, 'utf-8', (err: any, tscContent: string) => {
22+
if (err) throw err;
23+
24+
fs.writeFile(bak, tscContent, (err: any) => {
25+
if (err) throw err;
26+
27+
fs.readFile(path.resolve(path.dirname(tscPath) + '/loggedIO.js'), 'utf-8', (err: any, loggerContent: string) => {
28+
if (err) throw err;
29+
30+
var invocationLine = 'ts.executeCommandLine(sys.args);';
31+
var index1 = tscContent.indexOf(invocationLine);
32+
var index2 = index1 + invocationLine.length;
33+
var newContent = tscContent.substr(0, index1) + loggerContent + prepareCode + invocationLine + cleanupCode + tscContent.substr(index2) + '\r\n';
34+
fs.writeFile(tscPath, newContent);
35+
});
36+
});
37+
});
38+
});
39+
}
40+
41+
var isJson = (arg: string) => arg.indexOf(".json") > 0;
42+
43+
var record = process.argv.indexOf('record');
44+
var tscPath = process.argv[process.argv.length - 1];
45+
if (record >= 0) {
46+
console.log('Instrumenting ' + tscPath + ' for recording');
47+
instrumentForRecording(process.argv[record + 1], tscPath);
48+
} else if (process.argv.some(isJson)) {
49+
var filename = process.argv.filter(isJson)[0];
50+
instrumentForReplay(filename, tscPath);
51+
}
52+
53+

src/harness/loggedIO.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/// <reference path="..\..\src\compiler\sys.ts" />
2+
/// <reference path="..\..\src\harness\harness.ts" />
3+
/// <reference path="..\..\src\harness\runnerbase.ts" />
24

35
interface FileInformation {
46
contents: string;
@@ -93,6 +95,7 @@ module Playback {
9395

9496
function createEmptyLog(): IOLog {
9597
return {
98+
timestamp: (new Date()).toString(),
9699
arguments: [],
97100
currentDirectory: '',
98101
filesRead: [],
@@ -119,6 +122,8 @@ module Playback {
119122
};
120123
wrapper.startReplayFromData = log => {
121124
replayLog = log;
125+
// Remove non-found files from the log (shouldn't really need them, but we still record them for diganostic purposes)
126+
replayLog.filesRead = replayLog.filesRead.filter(f => f.result.contents !== undefined);
122127
};
123128

124129
wrapper.endReplay = () => {
@@ -170,22 +175,46 @@ module Playback {
170175
}
171176

172177
function findResultByPath<T>(wrapper: { resolvePath(s: string): string }, logArray: { path: string; result?: T }[], expectedPath: string, defaultValue?: T): T {
173-
var results = logArray.filter(e => pathsAreEquivalent(e.path, expectedPath, wrapper));
174-
if (results.length === 0) {
175-
if (defaultValue === undefined) {
176-
throw new Error('No matching result in log array for path: ' + expectedPath);
177-
} else {
178-
return defaultValue;
178+
var normalizedName = Harness.Path.switchToForwardSlashes(expectedPath).toLowerCase();
179+
// Try to find the result through normal filename
180+
for (var i = 0; i < logArray.length; i++) {
181+
if (Harness.Path.switchToForwardSlashes(logArray[i].path).toLowerCase() === normalizedName) {
182+
return logArray[i].result;
179183
}
180184
}
181-
return results[0].result;
185+
// Fallback, try to resolve the target paths as well
186+
if (replayLog.pathsResolved.length > 0) {
187+
var normalizedResolvedName = wrapper.resolvePath(expectedPath).toLowerCase();
188+
for (var i = 0; i < logArray.length; i++) {
189+
if (wrapper.resolvePath(logArray[i].path).toLowerCase() === normalizedResolvedName) {
190+
return logArray[i].result;
191+
}
192+
}
193+
}
194+
// If we got here, we didn't find a match
195+
if (defaultValue === undefined) {
196+
throw new Error('No matching result in log array for path: ' + expectedPath);
197+
} else {
198+
return defaultValue;
199+
}
182200
}
183201

202+
var pathEquivCache: any = {};
184203
function pathsAreEquivalent(left: string, right: string, wrapper: { resolvePath(s: string): string }) {
204+
var key = left + '-~~-' + right;
185205
function areSame(a: string, b: string) {
186206
return Harness.Path.switchToForwardSlashes(a).toLowerCase() === Harness.Path.switchToForwardSlashes(b).toLowerCase();
187207
}
188-
return areSame(left, right) || areSame(wrapper.resolvePath(left), right) || areSame(left, wrapper.resolvePath(right)) || areSame(wrapper.resolvePath(left), wrapper.resolvePath(right));
208+
function check() {
209+
if (Harness.Path.getFileName(left).toLowerCase() === Harness.Path.getFileName(right).toLowerCase()) {
210+
return areSame(left, right) || areSame(wrapper.resolvePath(left), right) || areSame(left, wrapper.resolvePath(right)) || areSame(wrapper.resolvePath(left), wrapper.resolvePath(right));
211+
}
212+
}
213+
if (pathEquivCache.hasOwnProperty(key)) {
214+
return pathEquivCache[key];
215+
} else {
216+
return pathEquivCache[key] = check();
217+
}
189218
}
190219

191220
function noOpReplay(name: string) {
@@ -258,7 +287,12 @@ module Playback {
258287
memoize((path) => findResultByFields(replayLog.pathsResolved, { path: path }, !ts.isRootedDiskPath(ts.normalizeSlashes(path)) && replayLog.currentDirectory ? replayLog.currentDirectory + '/' + path : ts.normalizeSlashes(path))));
259288

260289
wrapper.readFile = recordReplay(wrapper.readFile, underlying)(
261-
(path) => callAndRecord(underlying.readFile(path), recordLog.filesRead, { path: path, codepage: 0 }),
290+
(path) => {
291+
var result = underlying.readFile(path);
292+
var logEntry = { path: path, codepage: 0, result: { contents: result, codepage: 0 } };
293+
recordLog.filesRead.push(logEntry);
294+
return result;
295+
},
262296
memoize((path) => findResultByPath(wrapper, replayLog.filesRead, path).contents));
263297

264298
wrapper.writeFile = recordReplay(wrapper.writeFile, underlying)(

src/harness/rwcRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module RWC {
8888
}
8989
});
9090

91-
// do not use lib since we shouldnt be reading any files that arent in the ioLog
91+
// do not use lib since we already read it in above
9292
opts.options.noLib = true;
9393

9494
// Emit the results

0 commit comments

Comments
 (0)