Skip to content

Use .tsbuildinfo to build with tsc and tsc --w when not using build mode #30232

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

Merged
merged 7 commits into from
Mar 9, 2019
Prev Previous commit
Next Next commit
Add incremental to normal tsc
  • Loading branch information
sheetalkamat committed Mar 8, 2019
commit e41cbb6316f9f973f2a25ab578a956e697b20472
8 changes: 1 addition & 7 deletions src/compiler/tsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,13 +1233,7 @@ namespace ts {
function getOldProgram(proj: ResolvedConfigFileName, parsed: ParsedCommandLine) {
const value = builderPrograms.getValue(proj);
if (value) return value;
const buildInfoPath = getOutputPathForBuildInfo(parsed.options);
if (!buildInfoPath) return undefined;
const content = readFileWithCache(buildInfoPath);
if (!content) return undefined;
const buildInfo = getBuildInfo(content);
if (buildInfo.version !== version) return undefined;
return buildInfo.program && createBuildProgramUsingProgramBuildInfo(buildInfo.program) as any as T;
return readBuilderProgram(parsed.options, readFileWithCache) as any as T;
}

function updateBundle(proj: ResolvedConfigFileName): BuildResultFlags {
Expand Down
27 changes: 14 additions & 13 deletions src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,19 @@ namespace ts {
host.projectReferences = projectReferences;
return host;
}

export function readBuilderProgram(compilerOptions: CompilerOptions, readFile: (path: string) => string | undefined) {
if (compilerOptions.out || compilerOptions.outFile) return undefined;
if (!isIncrementalCompilation(compilerOptions)) return undefined;
const buildInfoPath = getOutputPathForBuildInfo(compilerOptions);
if (!buildInfoPath) return undefined;
const content = readFile(buildInfoPath);
if (!content) return undefined;
const buildInfo = getBuildInfo(content);
if (buildInfo.version !== version) return undefined;
if (!buildInfo.program) return undefined;
return createBuildProgramUsingProgramBuildInfo(buildInfo.program);
}
}

namespace ts {
Expand Down Expand Up @@ -645,7 +658,7 @@ namespace ts {
((typeDirectiveNames, containingFile, redirectedReference) => resolutionCache.resolveTypeReferenceDirectives(typeDirectiveNames, containingFile, redirectedReference));
const userProvidedResolution = !!host.resolveModuleNames || !!host.resolveTypeReferenceDirectives;

readBuilderProgram();
readBuilderProgram(compilerOptions, path => compilerHost.readFile(path));
synchronizeProgram();

// Update the wild card directory watch
Expand Down Expand Up @@ -677,18 +690,6 @@ namespace ts {
}
}

function readBuilderProgram() {
if (compilerOptions.out || compilerOptions.outFile) return;
if (!isIncrementalCompilation(compilerOptions)) return;
const buildInfoPath = getOutputPathForBuildInfo(compilerOptions);
if (!buildInfoPath) return;
const content = directoryStructureHost.readFile(buildInfoPath);
if (!content) return;
const buildInfo = JSON.parse(content) as BuildInfo;
if (!buildInfo.program) return;
builderProgram = createBuildProgramUsingProgramBuildInfo(buildInfo.program) as any as T;
}

function getCurrentBuilderProgram() {
return builderProgram;
}
Expand Down
41 changes: 41 additions & 0 deletions src/tsc/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ namespace ts {
reportWatchModeWithoutSysSupport();
createWatchOfConfigFile(configParseResult, commandLineOptions);
}
else if (isIncrementalCompilation(configParseResult.options)) {
performIncrementalCompilation(configParseResult);
}
else {
performCompilation(configParseResult.fileNames, configParseResult.projectReferences, configParseResult.options, getConfigFileParsingDiagnostics(configParseResult));
}
Expand Down Expand Up @@ -254,6 +257,44 @@ namespace ts {
return sys.exit(exitStatus);
}

function performIncrementalCompilation(config: ParsedCommandLine) {
const { options, fileNames, projectReferences } = config;
const host = createCompilerHost(options);
const currentDirectory = host.getCurrentDirectory();
const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames());
changeCompilerHostLikeToUseCache(host, fileName => toPath(fileName, currentDirectory, getCanonicalFileName));
enableStatistics(options);
const oldProgram = readBuilderProgram(options, path => host.readFile(path));
const configFileParsingDiagnostics = getConfigFileParsingDiagnostics(config);
const programOptions: CreateProgramOptions = {
rootNames: fileNames,
options,
projectReferences,
host,
configFileParsingDiagnostics: getConfigFileParsingDiagnostics(config),
};
const program = createProgram(programOptions);
const builderProgram = createEmitAndSemanticDiagnosticsBuilderProgram(
program,
{
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
createHash: maybeBind(sys, sys.createHash),
writeFile: (path, data, writeByteOrderMark) => sys.writeFile(path, data, writeByteOrderMark)
},
oldProgram,
configFileParsingDiagnostics
);

const exitStatus = emitFilesAndReportErrors(
builderProgram,
reportDiagnostic,
s => sys.write(s + sys.newLine),
createReportErrorSummary(options)
);
reportStatistics(program);
return sys.exit(exitStatus);
}

function updateCreateProgram<T extends BuilderProgram>(host: { createProgram: CreateProgram<T>; }) {
const compileUsingBuilder = host.createProgram;
host.createProgram = (rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences) => {
Expand Down