diff --git a/src/harness/harness.ts b/src/harness/harness.ts index ba84a3c9ffd..ecae4ee47a4 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -810,7 +810,9 @@ module Harness { export function createCompilerHost(inputFiles: { unitName: string; content: string; }[], writeFile: (fn: string, contents: string, writeByteOrderMark: boolean) => void, scriptTarget: ts.ScriptTarget, - useCaseSensitiveFileNames: boolean): ts.CompilerHost { + useCaseSensitiveFileNames: boolean, + // the currentDirectory is needed for rwcRunner to passed in specified current directory to compiler host + currentDirectory?: string): ts.CompilerHost { // Local get canonical file name function, that depends on passed in parameter for useCaseSensitiveFileNames function getCanonicalFileName(fileName: string): string { @@ -818,6 +820,8 @@ module Harness { } var filemap: { [filename: string]: ts.SourceFile; } = {}; + var getCurrentDirectory = currentDirectory === undefined ? ts.sys.getCurrentDirectory : () => currentDirectory; + // Register input files function register(file: { unitName: string; content: string; }) { if (file.content !== undefined) { @@ -828,11 +832,15 @@ module Harness { inputFiles.forEach(register); return { - getCurrentDirectory: ts.sys.getCurrentDirectory, + getCurrentDirectory, getSourceFile: (fn, languageVersion) => { if (Object.prototype.hasOwnProperty.call(filemap, getCanonicalFileName(fn))) { return filemap[getCanonicalFileName(fn)]; } + else if (currentDirectory) { + var canonicalAbsolutePath = getCanonicalFileName(ts.getNormalizedAbsolutePath(fn, currentDirectory)); + return Object.prototype.hasOwnProperty.call(filemap, getCanonicalFileName(canonicalAbsolutePath)) ? filemap[canonicalAbsolutePath] : undefined; + } else if (fn === fourslashFilename) { var tsFn = 'tests/cases/fourslash/' + fourslashFilename; fourslashSourceFile = fourslashSourceFile || ts.createSourceFile(tsFn, Harness.IO.readFile(tsFn), scriptTarget); @@ -909,7 +917,9 @@ module Harness { otherFiles: { unitName: string; content: string }[], onComplete: (result: CompilerResult, program: ts.Program) => void, settingsCallback?: (settings: ts.CompilerOptions) => void, - options?: ts.CompilerOptions) { + options?: ts.CompilerOptions, + // Current directory is needed for rwcRunner to be able to use currentDirectory defined in json file + currentDirectory?: string) { options = options || { noResolve: false }; options.target = options.target || ts.ScriptTarget.ES3; @@ -1063,8 +1073,7 @@ module Harness { var programFiles = inputFiles.map(file => file.unitName); var program = ts.createProgram(programFiles, options, createCompilerHost(inputFiles.concat(otherFiles), (fn, contents, writeByteOrderMark) => fileOutputs.push({ fileName: fn, code: contents, writeByteOrderMark: writeByteOrderMark }), - options.target, - useCaseSensitiveFileNames)); + options.target, useCaseSensitiveFileNames, currentDirectory)); var checker = program.getTypeChecker(/*produceDiagnostics*/ true); @@ -1095,7 +1104,9 @@ module Harness { otherFiles: { unitName: string; content: string; }[], result: CompilerResult, settingsCallback?: (settings: ts.CompilerOptions) => void, - options?: ts.CompilerOptions) { + options?: ts.CompilerOptions, + // Current directory is needed for rwcRunner to be able to use currentDirectory defined in json file + currentDirectory?: string) { if (options.declaration && result.errors.length === 0 && result.declFilesCode.length !== result.files.length) { throw new Error('There were no errors and declFiles generated did not match number of js files generated'); } @@ -1108,9 +1119,8 @@ module Harness { ts.forEach(inputFiles, file => addDtsFile(file, declInputFiles)); ts.forEach(otherFiles, file => addDtsFile(file, declOtherFiles)); - this.compileFiles(declInputFiles, declOtherFiles, function (compileResult) { - declResult = compileResult; - }, settingsCallback, options); + this.compileFiles(declInputFiles, declOtherFiles, function (compileResult) { declResult = compileResult; }, + settingsCallback, options, currentDirectory); return { declInputFiles, declOtherFiles, declResult }; } diff --git a/src/harness/rwcRunner.ts b/src/harness/rwcRunner.ts index 085f66f1a3e..b506cdd66b6 100644 --- a/src/harness/rwcRunner.ts +++ b/src/harness/rwcRunner.ts @@ -28,6 +28,7 @@ module RWC { var compilerOptions: ts.CompilerOptions; var baselineOpts: Harness.Baseline.BaselineOptions = { Subfolder: 'rwc' }; var baseName = /(.*)\/(.*).json/.exec(ts.normalizeSlashes(jsonPath))[2]; + var currentDirectory: string; after(() => { // Mocha holds onto the closure environment of the describe callback even after the test is done. @@ -38,6 +39,7 @@ module RWC { compilerOptions = undefined; baselineOpts = undefined; baseName = undefined; + currentDirectory = undefined; }); it('can compile', () => { @@ -45,6 +47,7 @@ module RWC { var opts: ts.ParsedCommandLine; var ioLog: IOLog = JSON.parse(Harness.IO.readFile(jsonPath)); + currentDirectory = ioLog.currentDirectory; runWithIOLog(ioLog, () => { opts = ts.parseCommandLine(ioLog.arguments); assert.equal(opts.errors.length, 0); @@ -52,7 +55,6 @@ module RWC { runWithIOLog(ioLog, () => { harnessCompiler.reset(); - // Load the files ts.forEach(opts.filenames, fileName => { inputFiles.push(getHarnessCompilerInputUnit(fileName)); @@ -81,7 +83,11 @@ module RWC { // Emit the results compilerOptions = harnessCompiler.compileFiles(inputFiles, otherFiles, compileResult => { compilerResult = compileResult; - }, /*settingsCallback*/ undefined, opts.options); + }, + /*settingsCallback*/ undefined, opts.options, + // Since all Rwc json file specified current directory in its json file, we need to pass this information to compilerHost + // so that when the host is asked for current directory, it should give the value from json rather than from process + currentDirectory); }); function getHarnessCompilerInputUnit(fileName: string) { @@ -145,7 +151,8 @@ module RWC { it('has the expected errors in generated declaration files', () => { if (compilerOptions.declaration && !compilerResult.errors.length) { Harness.Baseline.runBaseline('has the expected errors in generated declaration files', baseName + '.dts.errors.txt', () => { - var declFileCompilationResult = Harness.Compiler.getCompiler().compileDeclarationFiles(inputFiles, otherFiles, compilerResult, /*settingscallback*/ undefined, compilerOptions); + var declFileCompilationResult = Harness.Compiler.getCompiler().compileDeclarationFiles(inputFiles, otherFiles, compilerResult, + /*settingscallback*/ undefined, compilerOptions, currentDirectory); if (declFileCompilationResult.declResult.errors.length === 0) { return null; }