Skip to content

Commit

Permalink
Detect the input file of referenced project with fileNames from parse…
Browse files Browse the repository at this point in the history
…d command

Fixes #25864 and #26054
  • Loading branch information
sheetalkamat committed Sep 18, 2018
1 parent d51b8d9 commit 0d5aeee
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 22 deletions.
39 changes: 18 additions & 21 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ namespace ts {

// A parallel array to projectReferences storing the results of reading in the referenced tsconfig files
let resolvedProjectReferences: (ResolvedProjectReference | undefined)[] | undefined = projectReferences ? [] : undefined;
const projectReferenceRedirects: Map<string> = createMap();
let projectReferenceRedirects: ParsedCommandLine[] | undefined;

const shouldCreateNewSourceFile = shouldProgramCreateNewSourceFiles(oldProgram, options);
const structuralIsReused = tryReuseStructureFromOldProgram();
Expand All @@ -681,7 +681,7 @@ namespace ts {
const dtsOutfile = changeExtension(out, ".d.ts");
processSourceFile(dtsOutfile, /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, /*packageId*/ undefined);
}
addProjectReferenceRedirects(parsedRef.commandLine, projectReferenceRedirects);
addProjectReferenceRedirects(parsedRef.commandLine);
}
}
}
Expand Down Expand Up @@ -1252,7 +1252,7 @@ namespace ts {
if (resolvedProjectReferences) {
resolvedProjectReferences.forEach(ref => {
if (ref) {
addProjectReferenceRedirects(ref.commandLine, projectReferenceRedirects);
addProjectReferenceRedirects(ref.commandLine);
}
});
}
Expand Down Expand Up @@ -2179,20 +2179,24 @@ namespace ts {
}

function getProjectReferenceRedirect(fileName: string): string | undefined {
const path = toPath(fileName);
// Ignore dts or any of the non ts files
if (!projectReferenceRedirects || fileExtensionIs(fileName, Extension.Dts) || !fileExtensionIsOneOf(fileName, supportedTSExtensions)) {
return undefined;
}

// If this file is produced by a referenced project, we need to rewrite it to
// look in the output folder of the referenced project rather than the input
const normalized = getNormalizedAbsolutePath(fileName, path);
let result: string | undefined;
projectReferenceRedirects.forEach((v, k) => {
if (result !== undefined) {
return forEach(projectReferenceRedirects, referencedProject => {
// not input file from the referenced project, ignore
if (!contains(referencedProject.fileNames, fileName, isSameFile)) {
return undefined;
}
if (normalized.indexOf(k) === 0) {
result = changeExtension(fileName.replace(k, v), ".d.ts");
}

const out = referencedProject.options.outFile || referencedProject.options.out;
return out ?
changeExtension(out, Extension.Dts) :
getOutputDeclarationFileName(fileName, referencedProject);
});
return result;
}

function processReferencedFiles(file: SourceFile, isDefaultLib: boolean) {
Expand Down Expand Up @@ -2396,15 +2400,8 @@ namespace ts {
return { commandLine, sourceFile };
}

function addProjectReferenceRedirects(referencedProject: ParsedCommandLine, target: Map<string>) {
const rootDir = normalizePath(referencedProject.options.rootDir || getDirectoryPath(referencedProject.options.configFilePath!)); // TODO: GH#18217
target.set(rootDir, getDeclarationOutputDirectory(referencedProject));
}

function getDeclarationOutputDirectory(proj: ParsedCommandLine) {
return proj.options.declarationDir ||
proj.options.outDir ||
getDirectoryPath(proj.options.configFilePath!); // TODO: GH#18217
function addProjectReferenceRedirects(referencedProject: ParsedCommandLine) {
(projectReferenceRedirects || (projectReferenceRedirects = [])).push(referencedProject);
}

function verifyCompilerOptions() {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/tsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ namespace ts {
return getOrCreateValueFromConfigFileMap<Map<T>>(configFileMap, resolved, createMap);
}

function getOutputDeclarationFileName(inputFileName: string, configFile: ParsedCommandLine) {
export function getOutputDeclarationFileName(inputFileName: string, configFile: ParsedCommandLine) {
const relativePath = getRelativePathFromDirectory(rootDirOfOptions(configFile.options, configFile.options.configFilePath!), inputFileName, /*ignoreCase*/ true);
const outputPath = resolvePath(configFile.options.declarationDir || configFile.options.outDir || getDirectoryPath(configFile.options.configFilePath!), relativePath);
return changeExtension(outputPath, Extension.Dts);
Expand Down
19 changes: 19 additions & 0 deletions src/testRunner/unittests/tsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,25 @@ export class cNew {}`);
]);
});
});

describe("tsbuild - with rootDir of project reference in parentDirectory", () => {
const projFs = loadProjectFromDisk("tests/projects/projectReferenceWithRootDirInParent");
const allExpectedOutputs = [
"/src/dist/other/other.js", "/src/dist/other/other.d.ts",
"/src/dist/main/a.js", "/src/dist/main/a.d.ts",
"/src/dist/main/b.js", "/src/dist/main/b.d.ts"
];
it("verify that it builds correctly", () => {
const fs = projFs.shadow();
const host = new fakes.SolutionBuilderHost(fs);
const builder = createSolutionBuilder(host, ["/src/src/main", "/src/src/other"], {});
builder.buildAllProjects();
host.assertDiagnosticMessages(/*empty*/);
for (const output of allExpectedOutputs) {
assert(fs.existsSync(output), `Expect file ${output} to exist`);
}
});
});
}

export namespace OutFile {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { b } from './b';
const a = b;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.base.json",
"references": [
{ "path": "../other" }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const Other = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.base.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"composite": true,
"declaration": true,
"rootDir": "./src/",
"outDir": "./dist/"
},
"exclude": [
"node_modules"
]
}

0 comments on commit 0d5aeee

Please sign in to comment.