From 03ce7b0145a190b498982d4618c0ca7667f5b1f9 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 31 Oct 2016 08:23:02 -0700 Subject: [PATCH 1/2] Fix realPathMap in test harness: Must be used in `directoryExists` --- src/harness/harness.ts | 18 ++++- .../moduleResolutionWithSymlinks.errors.txt | 21 ------ .../reference/moduleResolutionWithSymlinks.js | 2 +- .../moduleResolutionWithSymlinks.symbols | 37 ++++++++++ .../moduleResolutionWithSymlinks.trace.json | 73 +------------------ .../moduleResolutionWithSymlinks.types | 39 ++++++++++ .../compiler/moduleResolutionWithSymlinks.ts | 2 +- 7 files changed, 96 insertions(+), 96 deletions(-) delete mode 100644 tests/baselines/reference/moduleResolutionWithSymlinks.errors.txt create mode 100644 tests/baselines/reference/moduleResolutionWithSymlinks.symbols create mode 100644 tests/baselines/reference/moduleResolutionWithSymlinks.types diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 17e67b053a3ee..add3046abc21e 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1029,7 +1029,7 @@ namespace Harness { }, realpath: realPathMap && ((f: string) => { const path = ts.toPath(f, currentDirectory, getCanonicalFileName); - return realPathMap.contains(path) ? realPathMap.get(path) : path; + return realPathMap.get(path) || path; }), directoryExists: dir => { let path = ts.toPath(dir, currentDirectory, getCanonicalFileName); @@ -1037,12 +1037,20 @@ namespace Harness { if (path[path.length - 1] === "/") { path = path.substr(0, path.length - 1); } + let exists = false; - fileMap.forEachValue(key => { - if (key.indexOf(path) === 0 && key[path.length] === "/") { + fileMap.forEachValue(fileName => { + if (!exists && pathStartsWithDirectory(fileName, path)) { exists = true; } }); + if (!exists && realPathMap) { + realPathMap.forEachValue(fileName => { + if (!exists && pathStartsWithDirectory(fileName, path)) { + exists = true; + } + }); + } return exists; }, getDirectories: d => { @@ -1064,6 +1072,10 @@ namespace Harness { }; } + function pathStartsWithDirectory(fileName: string, directory: string) { + return ts.startsWith(fileName, directory) && fileName[directory.length] === "/"; + } + interface HarnessOptions { useCaseSensitiveFileNames?: boolean; includeBuiltFile?: string; diff --git a/tests/baselines/reference/moduleResolutionWithSymlinks.errors.txt b/tests/baselines/reference/moduleResolutionWithSymlinks.errors.txt deleted file mode 100644 index b51933902a469..0000000000000 --- a/tests/baselines/reference/moduleResolutionWithSymlinks.errors.txt +++ /dev/null @@ -1,21 +0,0 @@ -/src/library-b/index.ts(1,23): error TS2307: Cannot find module 'library-a'. - - -==== /src/app.ts (0 errors) ==== - import { MyClass } from "./library-a"; - import { MyClass2 } from "./library-b"; - - let x: MyClass; - let y: MyClass2; - x = y; - y = x; -==== /src/library-a/index.ts (0 errors) ==== - - export class MyClass{} - -==== /src/library-b/index.ts (1 errors) ==== - import {MyClass} from "library-a"; - ~~~~~~~~~~~ -!!! error TS2307: Cannot find module 'library-a'. - export { MyClass as MyClass2 } - \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionWithSymlinks.js b/tests/baselines/reference/moduleResolutionWithSymlinks.js index 4123430045ebb..fa6fea5520b99 100644 --- a/tests/baselines/reference/moduleResolutionWithSymlinks.js +++ b/tests/baselines/reference/moduleResolutionWithSymlinks.js @@ -2,7 +2,7 @@ //// [index.ts] -export class MyClass{} +export class MyClass { private x: number; } //// [index.ts] import {MyClass} from "library-a"; diff --git a/tests/baselines/reference/moduleResolutionWithSymlinks.symbols b/tests/baselines/reference/moduleResolutionWithSymlinks.symbols new file mode 100644 index 0000000000000..9e9e3ee8a6709 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithSymlinks.symbols @@ -0,0 +1,37 @@ +=== /src/app.ts === +import { MyClass } from "./library-a"; +>MyClass : Symbol(MyClass, Decl(app.ts, 0, 8)) + +import { MyClass2 } from "./library-b"; +>MyClass2 : Symbol(MyClass2, Decl(app.ts, 1, 8)) + +let x: MyClass; +>x : Symbol(x, Decl(app.ts, 3, 3)) +>MyClass : Symbol(MyClass, Decl(app.ts, 0, 8)) + +let y: MyClass2; +>y : Symbol(y, Decl(app.ts, 4, 3)) +>MyClass2 : Symbol(MyClass2, Decl(app.ts, 1, 8)) + +x = y; +>x : Symbol(x, Decl(app.ts, 3, 3)) +>y : Symbol(y, Decl(app.ts, 4, 3)) + +y = x; +>y : Symbol(y, Decl(app.ts, 4, 3)) +>x : Symbol(x, Decl(app.ts, 3, 3)) + +=== /src/library-a/index.ts === + +export class MyClass { private x: number; } +>MyClass : Symbol(MyClass, Decl(index.ts, 0, 0)) +>x : Symbol(MyClass.x, Decl(index.ts, 1, 22)) + +=== /src/library-b/index.ts === +import {MyClass} from "library-a"; +>MyClass : Symbol(MyClass, Decl(index.ts, 0, 8)) + +export { MyClass as MyClass2 } +>MyClass : Symbol(MyClass2, Decl(index.ts, 1, 8)) +>MyClass2 : Symbol(MyClass2, Decl(index.ts, 1, 8)) + diff --git a/tests/baselines/reference/moduleResolutionWithSymlinks.trace.json b/tests/baselines/reference/moduleResolutionWithSymlinks.trace.json index 0f4dd90569a05..7f1c289018096 100644 --- a/tests/baselines/reference/moduleResolutionWithSymlinks.trace.json +++ b/tests/baselines/reference/moduleResolutionWithSymlinks.trace.json @@ -26,74 +26,7 @@ "File '/src/library-b/node_modules/library-a.tsx' does not exist.", "File '/src/library-b/node_modules/library-a.d.ts' does not exist.", "File '/src/library-b/node_modules/library-a/package.json' does not exist.", - "File '/src/library-b/node_modules/library-a/index.ts' does not exist.", - "File '/src/library-b/node_modules/library-a/index.tsx' does not exist.", - "File '/src/library-b/node_modules/library-a/index.d.ts' does not exist.", - "File '/src/library-b/node_modules/@types/library-a.ts' does not exist.", - "File '/src/library-b/node_modules/@types/library-a.tsx' does not exist.", - "File '/src/library-b/node_modules/@types/library-a.d.ts' does not exist.", - "File '/src/library-b/node_modules/@types/library-a/package.json' does not exist.", - "File '/src/library-b/node_modules/@types/library-a/index.ts' does not exist.", - "File '/src/library-b/node_modules/@types/library-a/index.tsx' does not exist.", - "File '/src/library-b/node_modules/@types/library-a/index.d.ts' does not exist.", - "File '/src/node_modules/library-a.ts' does not exist.", - "File '/src/node_modules/library-a.tsx' does not exist.", - "File '/src/node_modules/library-a.d.ts' does not exist.", - "File '/src/node_modules/library-a/package.json' does not exist.", - "File '/src/node_modules/library-a/index.ts' does not exist.", - "File '/src/node_modules/library-a/index.tsx' does not exist.", - "File '/src/node_modules/library-a/index.d.ts' does not exist.", - "File '/src/node_modules/@types/library-a.ts' does not exist.", - "File '/src/node_modules/@types/library-a.tsx' does not exist.", - "File '/src/node_modules/@types/library-a.d.ts' does not exist.", - "File '/src/node_modules/@types/library-a/package.json' does not exist.", - "File '/src/node_modules/@types/library-a/index.ts' does not exist.", - "File '/src/node_modules/@types/library-a/index.tsx' does not exist.", - "File '/src/node_modules/@types/library-a/index.d.ts' does not exist.", - "File '/node_modules/library-a.ts' does not exist.", - "File '/node_modules/library-a.tsx' does not exist.", - "File '/node_modules/library-a.d.ts' does not exist.", - "File '/node_modules/library-a/package.json' does not exist.", - "File '/node_modules/library-a/index.ts' does not exist.", - "File '/node_modules/library-a/index.tsx' does not exist.", - "File '/node_modules/library-a/index.d.ts' does not exist.", - "File '/node_modules/@types/library-a.ts' does not exist.", - "File '/node_modules/@types/library-a.tsx' does not exist.", - "File '/node_modules/@types/library-a.d.ts' does not exist.", - "File '/node_modules/@types/library-a/package.json' does not exist.", - "File '/node_modules/@types/library-a/index.ts' does not exist.", - "File '/node_modules/@types/library-a/index.tsx' does not exist.", - "File '/node_modules/@types/library-a/index.d.ts' does not exist.", - "Loading module 'library-a' from 'node_modules' folder.", - "File '/src/library-b/node_modules/library-a.js' does not exist.", - "File '/src/library-b/node_modules/library-a.jsx' does not exist.", - "File '/src/library-b/node_modules/library-a/package.json' does not exist.", - "File '/src/library-b/node_modules/library-a/index.js' does not exist.", - "File '/src/library-b/node_modules/library-a/index.jsx' does not exist.", - "File '/src/library-b/node_modules/@types/library-a.js' does not exist.", - "File '/src/library-b/node_modules/@types/library-a.jsx' does not exist.", - "File '/src/library-b/node_modules/@types/library-a/package.json' does not exist.", - "File '/src/library-b/node_modules/@types/library-a/index.js' does not exist.", - "File '/src/library-b/node_modules/@types/library-a/index.jsx' does not exist.", - "File '/src/node_modules/library-a.js' does not exist.", - "File '/src/node_modules/library-a.jsx' does not exist.", - "File '/src/node_modules/library-a/package.json' does not exist.", - "File '/src/node_modules/library-a/index.js' does not exist.", - "File '/src/node_modules/library-a/index.jsx' does not exist.", - "File '/src/node_modules/@types/library-a.js' does not exist.", - "File '/src/node_modules/@types/library-a.jsx' does not exist.", - "File '/src/node_modules/@types/library-a/package.json' does not exist.", - "File '/src/node_modules/@types/library-a/index.js' does not exist.", - "File '/src/node_modules/@types/library-a/index.jsx' does not exist.", - "File '/node_modules/library-a.js' does not exist.", - "File '/node_modules/library-a.jsx' does not exist.", - "File '/node_modules/library-a/package.json' does not exist.", - "File '/node_modules/library-a/index.js' does not exist.", - "File '/node_modules/library-a/index.jsx' does not exist.", - "File '/node_modules/@types/library-a.js' does not exist.", - "File '/node_modules/@types/library-a.jsx' does not exist.", - "File '/node_modules/@types/library-a/package.json' does not exist.", - "File '/node_modules/@types/library-a/index.js' does not exist.", - "File '/node_modules/@types/library-a/index.jsx' does not exist.", - "======== Module name 'library-a' was not resolved. ========" + "File '/src/library-b/node_modules/library-a/index.ts' exist - use it as a name resolution result.", + "Resolving real path for '/src/library-b/node_modules/library-a/index.ts', result '/src/library-a/index.ts'", + "======== Module name 'library-a' was successfully resolved to '/src/library-a/index.ts'. ========" ] \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionWithSymlinks.types b/tests/baselines/reference/moduleResolutionWithSymlinks.types new file mode 100644 index 0000000000000..3c0ccd168dd0d --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithSymlinks.types @@ -0,0 +1,39 @@ +=== /src/app.ts === +import { MyClass } from "./library-a"; +>MyClass : typeof MyClass + +import { MyClass2 } from "./library-b"; +>MyClass2 : typeof MyClass + +let x: MyClass; +>x : MyClass +>MyClass : MyClass + +let y: MyClass2; +>y : MyClass +>MyClass2 : MyClass + +x = y; +>x = y : MyClass +>x : MyClass +>y : MyClass + +y = x; +>y = x : MyClass +>y : MyClass +>x : MyClass + +=== /src/library-a/index.ts === + +export class MyClass { private x: number; } +>MyClass : MyClass +>x : number + +=== /src/library-b/index.ts === +import {MyClass} from "library-a"; +>MyClass : typeof MyClass + +export { MyClass as MyClass2 } +>MyClass : typeof MyClass +>MyClass2 : typeof MyClass + diff --git a/tests/cases/compiler/moduleResolutionWithSymlinks.ts b/tests/cases/compiler/moduleResolutionWithSymlinks.ts index 8f6f1ca1fbd27..4819b68ffc66d 100644 --- a/tests/cases/compiler/moduleResolutionWithSymlinks.ts +++ b/tests/cases/compiler/moduleResolutionWithSymlinks.ts @@ -4,7 +4,7 @@ // @filename: /src/library-a/index.ts // @symlink: /src/library-b/node_modules/library-a/index.ts -export class MyClass{} +export class MyClass { private x: number; } // @filename: /src/library-b/index.ts import {MyClass} from "library-a"; From ebebac9c4e60fde895f82c18bf18741a82a49a5d Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 31 Oct 2016 11:08:09 -0700 Subject: [PATCH 2/2] Add helper function --- src/harness/harness.ts | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/harness/harness.ts b/src/harness/harness.ts index add3046abc21e..a6e1ffad7f71e 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1037,21 +1037,7 @@ namespace Harness { if (path[path.length - 1] === "/") { path = path.substr(0, path.length - 1); } - - let exists = false; - fileMap.forEachValue(fileName => { - if (!exists && pathStartsWithDirectory(fileName, path)) { - exists = true; - } - }); - if (!exists && realPathMap) { - realPathMap.forEachValue(fileName => { - if (!exists && pathStartsWithDirectory(fileName, path)) { - exists = true; - } - }); - } - return exists; + return mapHasFileInDirectory(path, fileMap) || mapHasFileInDirectory(path, realPathMap); }, getDirectories: d => { const path = ts.toPath(d, currentDirectory, getCanonicalFileName); @@ -1072,8 +1058,17 @@ namespace Harness { }; } - function pathStartsWithDirectory(fileName: string, directory: string) { - return ts.startsWith(fileName, directory) && fileName[directory.length] === "/"; + function mapHasFileInDirectory(directoryPath: ts.Path, map: ts.FileMap): boolean { + if (!map) { + return false; + } + let exists = false; + map.forEachValue(fileName => { + if (!exists && ts.startsWith(fileName, directoryPath) && fileName[directoryPath.length] === "/") { + exists = true; + } + }); + return exists; } interface HarnessOptions {