Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect the input file of referenced project with fileNames from parsed command line #27195

Merged
merged 2 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
]
}