diff --git a/src/compiler/program.ts b/src/compiler/program.ts index ca0c7d9adaa..7074dbd45c1 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -329,7 +329,6 @@ namespace ts { let fileProcessingDiagnostics = createDiagnosticCollection(); const programDiagnostics = createDiagnosticCollection(); - let commonSourceDirectory: string; let diagnosticsProducingTypeChecker: TypeChecker; let noDiagnosticsTypeChecker: TypeChecker; let classifiableNames: Map; @@ -374,6 +373,8 @@ namespace ts { } } + // _Always_ compute a common source directory + let commonSourceDirectory = computeCommonSourceDirectory(files); verifyCompilerOptions(); // unconditionally set oldProgram to undefined to prevent it from being captured in closure @@ -1056,17 +1057,13 @@ namespace ts { // If a rootDir is specified and is valid use it as the commonSourceDirectory commonSourceDirectory = getNormalizedAbsolutePath(options.rootDir, currentDirectory); } - else { - // Compute the commonSourceDirectory from the input files - commonSourceDirectory = computeCommonSourceDirectory(files); - } + } - if (commonSourceDirectory && commonSourceDirectory[commonSourceDirectory.length - 1] !== directorySeparator) { - // Make sure directory path ends with directory separator so this string can directly - // used to replace with "" to get the relative path of the source file and the relative path doesn't - // start with / making it rooted path - commonSourceDirectory += directorySeparator; - } + if (commonSourceDirectory && commonSourceDirectory[commonSourceDirectory.length - 1] !== directorySeparator) { + // Make sure directory path ends with directory separator so this string can directly + // used to replace with "" to get the relative path of the source file and the relative path doesn't + // start with / making it rooted path + commonSourceDirectory += directorySeparator; } if (options.noEmit) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 1fffaf79332..dc5d9db30bc 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1867,7 +1867,7 @@ namespace ts { * Resolves a local path to a path which is absolute to the base of the emit */ export function getExternalModuleNameFromPath(host: EmitHost, fileName: string): string { - const dir = host.getCommonSourceDirectory(); + const dir = toPath(host.getCommonSourceDirectory(), host.getCurrentDirectory(), f => host.getCanonicalFileName(f)); const relativePath = getRelativePathToDirectoryOrUrl(dir, fileName, dir, f => host.getCanonicalFileName(f), /*isAbsolutePathAnUrl*/ false); return removeFileExtension(relativePath); }