Skip to content

Commit 63a2086

Browse files
committed
Fix the relative path reference resolution
Fixes #1039
1 parent 8c2091b commit 63a2086

14 files changed

+100
-77
lines changed

src/compiler/core.ts

+4
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ module ts {
459459
return normalizedPathComponents(path, rootLength);
460460
}
461461

462+
export function getNormalizedAbsolutePath(filename: string, currentDirectory: string) {
463+
return getNormalizedPathFromPathComponents(getNormalizedPathComponents(filename, currentDirectory));
464+
}
465+
462466
export function getNormalizedPathFromPathComponents(pathComponents: string[]) {
463467
if (pathComponents && pathComponents.length) {
464468
return pathComponents[0] + pathComponents.slice(1).join(directorySeparator);

src/compiler/emitter.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module ts {
5757
var newLine = program.getCompilerHost().getNewLine();
5858

5959
function getSourceFilePathInNewDir(newDirPath: string, sourceFile: SourceFile) {
60-
var sourceFilePath = getNormalizedPathFromPathComponents(getNormalizedPathComponents(sourceFile.filename, compilerHost.getCurrentDirectory()));
60+
var sourceFilePath = getNormalizedAbsolutePath(sourceFile.filename, compilerHost.getCurrentDirectory());
6161
sourceFilePath = sourceFilePath.replace(program.getCommonSourceDirectory(), "");
6262
return combinePaths(newDirPath, sourceFilePath);
6363
}
@@ -3370,11 +3370,6 @@ module ts {
33703370
}
33713371
}
33723372

3373-
function tryResolveScriptReference(sourceFile: SourceFile, reference: FileReference) {
3374-
var referenceFileName = normalizePath(combinePaths(getDirectoryPath(sourceFile.filename), reference.filename));
3375-
return program.getSourceFile(referenceFileName);
3376-
}
3377-
33783373
// Contains the reference paths that needs to go in the declaration file.
33793374
// Collecting this separately because reference paths need to be first thing in the declaration file
33803375
// and we could be collecting these paths from multiple files into single one with --out option
@@ -3401,7 +3396,7 @@ module ts {
34013396
if (!compilerOptions.noResolve) {
34023397
var addedGlobalFileReference = false;
34033398
forEach(root.referencedFiles, fileReference => {
3404-
var referencedFile = tryResolveScriptReference(root, fileReference);
3399+
var referencedFile = tryResolveScriptReference(program, root, fileReference);
34053400

34063401
// All the references that are not going to be part of same file
34073402
if (referencedFile && ((referencedFile.flags & NodeFlags.DeclarationFile) || // This is a declare file reference
@@ -3426,7 +3421,7 @@ module ts {
34263421
// Check what references need to be added
34273422
if (!compilerOptions.noResolve) {
34283423
forEach(sourceFile.referencedFiles, fileReference => {
3429-
var referencedFile = tryResolveScriptReference(sourceFile, fileReference);
3424+
var referencedFile = tryResolveScriptReference(program, sourceFile, fileReference);
34303425

34313426
// If the reference file is a declaration file or an external module, emit that reference
34323427
if (referencedFile && (isExternalModuleOrDeclarationFile(referencedFile) &&

src/compiler/parser.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,15 @@ module ts {
634634
return false;
635635
}
636636

637+
export function tryResolveScriptReference(program: Program, sourceFile: SourceFile, reference: FileReference) {
638+
var referenceFileName = isRootedDiskPath(reference.filename) ? reference.filename : combinePaths(getDirectoryPath(sourceFile.filename), reference.filename);
639+
referenceFileName = normalizePath(referenceFileName);
640+
if (!program.getCompilerOptions().noResolve) {
641+
referenceFileName = getNormalizedAbsolutePath(referenceFileName, program.getCompilerHost().getCurrentDirectory());
642+
}
643+
return program.getSourceFile(referenceFileName);
644+
}
645+
637646
export function getAncestor(node: Node, kind: SyntaxKind): Node {
638647
switch (kind) {
639648
// special-cases that can be come first
@@ -4373,13 +4382,18 @@ module ts {
43734382
var canonicalName = host.getCanonicalFileName(filename);
43744383
if (hasProperty(filesByName, canonicalName)) {
43754384
// We've already looked for this file, use cached result
4376-
var file = filesByName[canonicalName];
4377-
if (file && host.useCaseSensitiveFileNames() && canonicalName !== file.filename) {
4378-
errors.push(createFileDiagnostic(refFile, refStart, refLength,
4379-
Diagnostics.Filename_0_differs_from_already_included_filename_1_only_in_casing, filename, file.filename));
4380-
}
4385+
return getSourceFileFromCache(filename, canonicalName, /*useAbsolutePath*/ false);
43814386
}
43824387
else {
4388+
// if --noResolve is not specified check if we have file for absolute path
4389+
if (!options.noResolve) {
4390+
var normalizedAbsolutePath = getNormalizedAbsolutePath(filename, host.getCurrentDirectory());
4391+
var canonicalAbsolutePath = host.getCanonicalFileName(normalizedAbsolutePath);
4392+
if (hasProperty(filesByName, canonicalAbsolutePath)) {
4393+
return getSourceFileFromCache(normalizedAbsolutePath, canonicalAbsolutePath, /*useAbsolutePath*/ true);
4394+
}
4395+
}
4396+
43834397
// We haven't looked for this file, do so now and cache result
43844398
var file = filesByName[canonicalName] = host.getSourceFile(filename, options.target, hostErrorMessage => {
43854399
errors.push(createFileDiagnostic(refFile, refStart, refLength,
@@ -4388,6 +4402,9 @@ module ts {
43884402
if (file) {
43894403
seenNoDefaultLib = seenNoDefaultLib || file.hasNoDefaultLib;
43904404
if (!options.noResolve) {
4405+
// Set the source file for normalized absolute path
4406+
filesByName[canonicalAbsolutePath] = file;
4407+
43914408
var basePath = getDirectoryPath(filename);
43924409
processReferencedFiles(file, basePath);
43934410
processImportedModules(file, basePath);
@@ -4404,6 +4421,18 @@ module ts {
44044421
}
44054422
}
44064423
return file;
4424+
4425+
function getSourceFileFromCache(filename: string, canonicalName: string, useAbsolutePath: boolean): SourceFile {
4426+
var file = filesByName[canonicalName];
4427+
if (file && host.useCaseSensitiveFileNames()) {
4428+
var sourceFileName = useAbsolutePath ? getNormalizedAbsolutePath(file.filename, host.getCurrentDirectory()) : file.filename;
4429+
if (canonicalName !== sourceFileName) {
4430+
errors.push(createFileDiagnostic(refFile, refStart, refLength,
4431+
Diagnostics.Filename_0_differs_from_already_included_filename_1_only_in_casing, filename, sourceFileName));
4432+
}
4433+
}
4434+
return file;
4435+
}
44074436
}
44084437

44094438
function processReferencedFiles(file: SourceFile, basePath: string) {

src/harness/harness.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ module Harness {
863863
var sourceFileName: string;
864864
if (ts.isExternalModule(sourceFile) || !options.out) {
865865
if (options.outDir) {
866-
var sourceFilePath = ts.getNormalizedPathFromPathComponents(ts.getNormalizedPathComponents(sourceFile.filename, result.currentDirectoryForProgram));
866+
var sourceFilePath = ts.getNormalizedAbsolutePath(sourceFile.filename, result.currentDirectoryForProgram);
867867
sourceFilePath = sourceFilePath.replace(result.program.getCommonSourceDirectory(), "");
868868
sourceFileName = ts.combinePaths(options.outDir, sourceFilePath);
869869
}

src/harness/projectsRunner.ts

+37-7
Original file line numberDiff line numberDiff line change
@@ -274,20 +274,50 @@ class ProjectRunner extends RunnerBase {
274274
}
275275

276276
function compileCompileDTsFiles(compilerResult: BatchCompileProjectTestCaseResult) {
277-
var inputDtsSourceFiles = ts.map(ts.filter(compilerResult.program.getSourceFiles(),
278-
sourceFile => Harness.Compiler.isDTS(sourceFile.filename)),
279-
sourceFile => {
280-
return { emittedFileName: sourceFile.filename, code: sourceFile.text };
281-
});
277+
var allInputFiles: { emittedFileName: string; code: string; }[] = [];
278+
var compilerOptions = compilerResult.program.getCompilerOptions();
279+
var compilerHost = compilerResult.program.getCompilerHost();
280+
ts.forEach(compilerResult.program.getSourceFiles(), sourceFile => {
281+
if (Harness.Compiler.isDTS(sourceFile.filename)) {
282+
allInputFiles.unshift({ emittedFileName: sourceFile.filename, code: sourceFile.text });
283+
}
284+
else if (ts.shouldEmitToOwnFile(sourceFile, compilerResult.program.getCompilerOptions())) {
285+
if (compilerOptions.outDir) {
286+
var sourceFilePath = ts.getNormalizedAbsolutePath(sourceFile.filename, compilerHost.getCurrentDirectory());
287+
sourceFilePath = sourceFilePath.replace(compilerResult.program.getCommonSourceDirectory(), "");
288+
var emitOutputFilePathWithoutExtension = ts.removeFileExtension(ts.combinePaths(compilerOptions.outDir, sourceFilePath));
289+
}
290+
else {
291+
var emitOutputFilePathWithoutExtension = ts.removeFileExtension(sourceFile.filename);
292+
}
293+
294+
var outputDtsFileName = emitOutputFilePathWithoutExtension + ".d.ts";
295+
allInputFiles.unshift(findOutpuDtsFile(outputDtsFileName));
296+
}
297+
else {
298+
var outputDtsFileName = ts.removeFileExtension(compilerOptions.out) + ".d.ts";
299+
var outputDtsFile = findOutpuDtsFile(outputDtsFileName);
300+
if (!ts.contains(allInputFiles, outputDtsFile)) {
301+
allInputFiles.unshift(outputDtsFile);
302+
}
303+
}
304+
});
305+
306+
console.log("inputFiles");
307+
ts.forEach(allInputFiles, inputFile => {
308+
console.log(inputFile.emittedFileName);
309+
});
282310

283-
var ouputDtsFiles = ts.filter(compilerResult.outputFiles, ouputFile => Harness.Compiler.isDTS(ouputFile.emittedFileName));
284-
var allInputFiles = inputDtsSourceFiles.concat(ouputDtsFiles);
285311
return compileProjectFiles(compilerResult.moduleKind,getInputFiles, getSourceFileText, writeFile);
286312

313+
function findOutpuDtsFile(fileName: string) {
314+
return ts.forEach(compilerResult.outputFiles, outputFile => outputFile.emittedFileName === fileName ? outputFile : undefined);
315+
}
287316
function getInputFiles() {
288317
return ts.map(allInputFiles, outputFile => outputFile.emittedFileName);
289318
}
290319
function getSourceFileText(filename: string): string {
320+
console.log("Reading: " + filename + ": " + (ts.forEach(allInputFiles, inputFile => inputFile.emittedFileName === filename ? inputFile.code : undefined) !== undefined));
291321
return ts.forEach(allInputFiles, inputFile => inputFile.emittedFileName === filename ? inputFile.code : undefined);
292322
}
293323

src/services/services.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -3383,11 +3383,10 @@ module ts {
33833383
/// Triple slash reference comments
33843384
var comment = forEach(sourceFile.referencedFiles, r => (r.pos <= position && position < r.end) ? r : undefined);
33853385
if (comment) {
3386-
var targetFilename = isRootedDiskPath(comment.filename) ? comment.filename : combinePaths(getDirectoryPath(filename), comment.filename);
3387-
targetFilename = normalizePath(targetFilename);
3388-
if (program.getSourceFile(targetFilename)) {
3386+
var referenceFile = tryResolveScriptReference(program, sourceFile, comment);
3387+
if (referenceFile) {
33893388
return [{
3390-
fileName: targetFilename,
3389+
fileName: referenceFile.filename,
33913390
textSpan: TypeScript.TextSpan.fromBounds(0, 0),
33923391
kind: ScriptElementKind.scriptElement,
33933392
name: comment.filename,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference path="../../../typings/tsd.d.ts" />
2+
declare class FieldManager {
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference path="../src/ts/Manager/FieldManager.d.ts" />
2+
declare class tsd {
3+
}

tests/baselines/reference/project/referenceResolutionRelativePaths/amd/referenceResolutionRelativePaths.errors.txt

-23
This file was deleted.

tests/baselines/reference/project/referenceResolutionRelativePaths/amd/referenceResolutionRelativePaths.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
"baselineCheck": true,
99
"resolvedInputFiles": [
1010
"lib.d.ts",
11-
"../../../src/ts/Manager/FieldManager.ts",
1211
"../../../typings/tsd.ts",
1312
"FieldManager.ts"
1413
],
1514
"emittedFiles": [
16-
"../../../src/ts/Manager/FieldManager.js",
1715
"../../../typings/tsd.js",
18-
"FieldManager.js"
16+
"../../../typings/tsd.d.ts",
17+
"FieldManager.js",
18+
"FieldManager.d.ts"
1919
]
2020
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference path="../../../typings/tsd.d.ts" />
2+
declare class FieldManager {
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference path="../src/ts/Manager/FieldManager.d.ts" />
2+
declare class tsd {
3+
}

tests/baselines/reference/project/referenceResolutionRelativePaths/node/referenceResolutionRelativePaths.errors.txt

-23
This file was deleted.

tests/baselines/reference/project/referenceResolutionRelativePaths/node/referenceResolutionRelativePaths.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
"baselineCheck": true,
99
"resolvedInputFiles": [
1010
"lib.d.ts",
11-
"../../../src/ts/Manager/FieldManager.ts",
1211
"../../../typings/tsd.ts",
1312
"FieldManager.ts"
1413
],
1514
"emittedFiles": [
16-
"../../../src/ts/Manager/FieldManager.js",
1715
"../../../typings/tsd.js",
18-
"FieldManager.js"
16+
"../../../typings/tsd.d.ts",
17+
"FieldManager.js",
18+
"FieldManager.d.ts"
1919
]
2020
}

0 commit comments

Comments
 (0)