Fix the relative path reference resolution

Fixes #1039
This commit is contained in:
Sheetal Nandi 2014-11-11 22:58:47 -08:00
parent 8c2091bca5
commit 63a20863c1
14 changed files with 100 additions and 77 deletions

View file

@ -459,6 +459,10 @@ module ts {
return normalizedPathComponents(path, rootLength);
}
export function getNormalizedAbsolutePath(filename: string, currentDirectory: string) {
return getNormalizedPathFromPathComponents(getNormalizedPathComponents(filename, currentDirectory));
}
export function getNormalizedPathFromPathComponents(pathComponents: string[]) {
if (pathComponents && pathComponents.length) {
return pathComponents[0] + pathComponents.slice(1).join(directorySeparator);

View file

@ -57,7 +57,7 @@ module ts {
var newLine = program.getCompilerHost().getNewLine();
function getSourceFilePathInNewDir(newDirPath: string, sourceFile: SourceFile) {
var sourceFilePath = getNormalizedPathFromPathComponents(getNormalizedPathComponents(sourceFile.filename, compilerHost.getCurrentDirectory()));
var sourceFilePath = getNormalizedAbsolutePath(sourceFile.filename, compilerHost.getCurrentDirectory());
sourceFilePath = sourceFilePath.replace(program.getCommonSourceDirectory(), "");
return combinePaths(newDirPath, sourceFilePath);
}
@ -3370,11 +3370,6 @@ module ts {
}
}
function tryResolveScriptReference(sourceFile: SourceFile, reference: FileReference) {
var referenceFileName = normalizePath(combinePaths(getDirectoryPath(sourceFile.filename), reference.filename));
return program.getSourceFile(referenceFileName);
}
// Contains the reference paths that needs to go in the declaration file.
// Collecting this separately because reference paths need to be first thing in the declaration file
// and we could be collecting these paths from multiple files into single one with --out option
@ -3401,7 +3396,7 @@ module ts {
if (!compilerOptions.noResolve) {
var addedGlobalFileReference = false;
forEach(root.referencedFiles, fileReference => {
var referencedFile = tryResolveScriptReference(root, fileReference);
var referencedFile = tryResolveScriptReference(program, root, fileReference);
// All the references that are not going to be part of same file
if (referencedFile && ((referencedFile.flags & NodeFlags.DeclarationFile) || // This is a declare file reference
@ -3426,7 +3421,7 @@ module ts {
// Check what references need to be added
if (!compilerOptions.noResolve) {
forEach(sourceFile.referencedFiles, fileReference => {
var referencedFile = tryResolveScriptReference(sourceFile, fileReference);
var referencedFile = tryResolveScriptReference(program, sourceFile, fileReference);
// If the reference file is a declaration file or an external module, emit that reference
if (referencedFile && (isExternalModuleOrDeclarationFile(referencedFile) &&

View file

@ -634,6 +634,15 @@ module ts {
return false;
}
export function tryResolveScriptReference(program: Program, sourceFile: SourceFile, reference: FileReference) {
var referenceFileName = isRootedDiskPath(reference.filename) ? reference.filename : combinePaths(getDirectoryPath(sourceFile.filename), reference.filename);
referenceFileName = normalizePath(referenceFileName);
if (!program.getCompilerOptions().noResolve) {
referenceFileName = getNormalizedAbsolutePath(referenceFileName, program.getCompilerHost().getCurrentDirectory());
}
return program.getSourceFile(referenceFileName);
}
export function getAncestor(node: Node, kind: SyntaxKind): Node {
switch (kind) {
// special-cases that can be come first
@ -4373,13 +4382,18 @@ module ts {
var canonicalName = host.getCanonicalFileName(filename);
if (hasProperty(filesByName, canonicalName)) {
// We've already looked for this file, use cached result
var file = filesByName[canonicalName];
if (file && host.useCaseSensitiveFileNames() && canonicalName !== file.filename) {
errors.push(createFileDiagnostic(refFile, refStart, refLength,
Diagnostics.Filename_0_differs_from_already_included_filename_1_only_in_casing, filename, file.filename));
}
return getSourceFileFromCache(filename, canonicalName, /*useAbsolutePath*/ false);
}
else {
// if --noResolve is not specified check if we have file for absolute path
if (!options.noResolve) {
var normalizedAbsolutePath = getNormalizedAbsolutePath(filename, host.getCurrentDirectory());
var canonicalAbsolutePath = host.getCanonicalFileName(normalizedAbsolutePath);
if (hasProperty(filesByName, canonicalAbsolutePath)) {
return getSourceFileFromCache(normalizedAbsolutePath, canonicalAbsolutePath, /*useAbsolutePath*/ true);
}
}
// We haven't looked for this file, do so now and cache result
var file = filesByName[canonicalName] = host.getSourceFile(filename, options.target, hostErrorMessage => {
errors.push(createFileDiagnostic(refFile, refStart, refLength,
@ -4388,6 +4402,9 @@ module ts {
if (file) {
seenNoDefaultLib = seenNoDefaultLib || file.hasNoDefaultLib;
if (!options.noResolve) {
// Set the source file for normalized absolute path
filesByName[canonicalAbsolutePath] = file;
var basePath = getDirectoryPath(filename);
processReferencedFiles(file, basePath);
processImportedModules(file, basePath);
@ -4404,6 +4421,18 @@ module ts {
}
}
return file;
function getSourceFileFromCache(filename: string, canonicalName: string, useAbsolutePath: boolean): SourceFile {
var file = filesByName[canonicalName];
if (file && host.useCaseSensitiveFileNames()) {
var sourceFileName = useAbsolutePath ? getNormalizedAbsolutePath(file.filename, host.getCurrentDirectory()) : file.filename;
if (canonicalName !== sourceFileName) {
errors.push(createFileDiagnostic(refFile, refStart, refLength,
Diagnostics.Filename_0_differs_from_already_included_filename_1_only_in_casing, filename, sourceFileName));
}
}
return file;
}
}
function processReferencedFiles(file: SourceFile, basePath: string) {

View file

@ -863,7 +863,7 @@ module Harness {
var sourceFileName: string;
if (ts.isExternalModule(sourceFile) || !options.out) {
if (options.outDir) {
var sourceFilePath = ts.getNormalizedPathFromPathComponents(ts.getNormalizedPathComponents(sourceFile.filename, result.currentDirectoryForProgram));
var sourceFilePath = ts.getNormalizedAbsolutePath(sourceFile.filename, result.currentDirectoryForProgram);
sourceFilePath = sourceFilePath.replace(result.program.getCommonSourceDirectory(), "");
sourceFileName = ts.combinePaths(options.outDir, sourceFilePath);
}

View file

@ -274,20 +274,50 @@ class ProjectRunner extends RunnerBase {
}
function compileCompileDTsFiles(compilerResult: BatchCompileProjectTestCaseResult) {
var inputDtsSourceFiles = ts.map(ts.filter(compilerResult.program.getSourceFiles(),
sourceFile => Harness.Compiler.isDTS(sourceFile.filename)),
sourceFile => {
return { emittedFileName: sourceFile.filename, code: sourceFile.text };
});
var allInputFiles: { emittedFileName: string; code: string; }[] = [];
var compilerOptions = compilerResult.program.getCompilerOptions();
var compilerHost = compilerResult.program.getCompilerHost();
ts.forEach(compilerResult.program.getSourceFiles(), sourceFile => {
if (Harness.Compiler.isDTS(sourceFile.filename)) {
allInputFiles.unshift({ emittedFileName: sourceFile.filename, code: sourceFile.text });
}
else if (ts.shouldEmitToOwnFile(sourceFile, compilerResult.program.getCompilerOptions())) {
if (compilerOptions.outDir) {
var sourceFilePath = ts.getNormalizedAbsolutePath(sourceFile.filename, compilerHost.getCurrentDirectory());
sourceFilePath = sourceFilePath.replace(compilerResult.program.getCommonSourceDirectory(), "");
var emitOutputFilePathWithoutExtension = ts.removeFileExtension(ts.combinePaths(compilerOptions.outDir, sourceFilePath));
}
else {
var emitOutputFilePathWithoutExtension = ts.removeFileExtension(sourceFile.filename);
}
var outputDtsFileName = emitOutputFilePathWithoutExtension + ".d.ts";
allInputFiles.unshift(findOutpuDtsFile(outputDtsFileName));
}
else {
var outputDtsFileName = ts.removeFileExtension(compilerOptions.out) + ".d.ts";
var outputDtsFile = findOutpuDtsFile(outputDtsFileName);
if (!ts.contains(allInputFiles, outputDtsFile)) {
allInputFiles.unshift(outputDtsFile);
}
}
});
console.log("inputFiles");
ts.forEach(allInputFiles, inputFile => {
console.log(inputFile.emittedFileName);
});
var ouputDtsFiles = ts.filter(compilerResult.outputFiles, ouputFile => Harness.Compiler.isDTS(ouputFile.emittedFileName));
var allInputFiles = inputDtsSourceFiles.concat(ouputDtsFiles);
return compileProjectFiles(compilerResult.moduleKind,getInputFiles, getSourceFileText, writeFile);
function findOutpuDtsFile(fileName: string) {
return ts.forEach(compilerResult.outputFiles, outputFile => outputFile.emittedFileName === fileName ? outputFile : undefined);
}
function getInputFiles() {
return ts.map(allInputFiles, outputFile => outputFile.emittedFileName);
}
function getSourceFileText(filename: string): string {
console.log("Reading: " + filename + ": " + (ts.forEach(allInputFiles, inputFile => inputFile.emittedFileName === filename ? inputFile.code : undefined) !== undefined));
return ts.forEach(allInputFiles, inputFile => inputFile.emittedFileName === filename ? inputFile.code : undefined);
}

View file

@ -3383,11 +3383,10 @@ module ts {
/// Triple slash reference comments
var comment = forEach(sourceFile.referencedFiles, r => (r.pos <= position && position < r.end) ? r : undefined);
if (comment) {
var targetFilename = isRootedDiskPath(comment.filename) ? comment.filename : combinePaths(getDirectoryPath(filename), comment.filename);
targetFilename = normalizePath(targetFilename);
if (program.getSourceFile(targetFilename)) {
var referenceFile = tryResolveScriptReference(program, sourceFile, comment);
if (referenceFile) {
return [{
fileName: targetFilename,
fileName: referenceFile.filename,
textSpan: TypeScript.TextSpan.fromBounds(0, 0),
kind: ScriptElementKind.scriptElement,
name: comment.filename,

View file

@ -0,0 +1,3 @@
/// <reference path="../../../typings/tsd.d.ts" />
declare class FieldManager {
}

View file

@ -0,0 +1,3 @@
/// <reference path="../src/ts/Manager/FieldManager.d.ts" />
declare class tsd {
}

View file

@ -1,23 +0,0 @@
../../../src/ts/Manager/FieldManager.ts(3,7): error TS2300: Duplicate identifier 'FieldManager'.
FieldManager.ts(3,7): error TS2300: Duplicate identifier 'FieldManager'.
==== ../../../src/ts/Manager/FieldManager.ts (1 errors) ====
/// <reference path="../../../typings/tsd.ts" />
class FieldManager {
~~~~~~~~~~~~
!!! error TS2300: Duplicate identifier 'FieldManager'.
}
==== ../../../typings/tsd.ts (0 errors) ====
/// <reference path="../src/ts/Manager/FieldManager.ts" />
// This is tsd.ts
class tsd {
}
==== FieldManager.ts (1 errors) ====
/// <reference path="../../../typings/tsd.ts" />
class FieldManager {
~~~~~~~~~~~~
!!! error TS2300: Duplicate identifier 'FieldManager'.
}

View file

@ -8,13 +8,13 @@
"baselineCheck": true,
"resolvedInputFiles": [
"lib.d.ts",
"../../../src/ts/Manager/FieldManager.ts",
"../../../typings/tsd.ts",
"FieldManager.ts"
],
"emittedFiles": [
"../../../src/ts/Manager/FieldManager.js",
"../../../typings/tsd.js",
"FieldManager.js"
"../../../typings/tsd.d.ts",
"FieldManager.js",
"FieldManager.d.ts"
]
}

View file

@ -0,0 +1,3 @@
/// <reference path="../../../typings/tsd.d.ts" />
declare class FieldManager {
}

View file

@ -0,0 +1,3 @@
/// <reference path="../src/ts/Manager/FieldManager.d.ts" />
declare class tsd {
}

View file

@ -1,23 +0,0 @@
../../../src/ts/Manager/FieldManager.ts(3,7): error TS2300: Duplicate identifier 'FieldManager'.
FieldManager.ts(3,7): error TS2300: Duplicate identifier 'FieldManager'.
==== ../../../src/ts/Manager/FieldManager.ts (1 errors) ====
/// <reference path="../../../typings/tsd.ts" />
class FieldManager {
~~~~~~~~~~~~
!!! error TS2300: Duplicate identifier 'FieldManager'.
}
==== ../../../typings/tsd.ts (0 errors) ====
/// <reference path="../src/ts/Manager/FieldManager.ts" />
// This is tsd.ts
class tsd {
}
==== FieldManager.ts (1 errors) ====
/// <reference path="../../../typings/tsd.ts" />
class FieldManager {
~~~~~~~~~~~~
!!! error TS2300: Duplicate identifier 'FieldManager'.
}

View file

@ -8,13 +8,13 @@
"baselineCheck": true,
"resolvedInputFiles": [
"lib.d.ts",
"../../../src/ts/Manager/FieldManager.ts",
"../../../typings/tsd.ts",
"FieldManager.ts"
],
"emittedFiles": [
"../../../src/ts/Manager/FieldManager.js",
"../../../typings/tsd.js",
"FieldManager.js"
"../../../typings/tsd.d.ts",
"FieldManager.js",
"FieldManager.d.ts"
]
}