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

Emit build info even on noEmitOnError or tsc --build #38853

Merged
merged 3 commits into from Jun 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/compiler/builder.ts
Expand Up @@ -148,7 +148,7 @@ namespace ts {
/**
* true if build info is emitted
*/
emittedBuildInfo?: boolean;
buildInfoEmitPending: boolean;
/**
* Already seen emitted files
*/
Expand Down Expand Up @@ -250,14 +250,14 @@ namespace ts {
BuilderState.getAllFilesExcludingDefaultLibraryFile(state, newProgram, /*firstSourceFile*/ undefined)
.forEach(file => state.changedFilesSet.set(file.resolvedPath, true));
}
else if (oldCompilerOptions && compilerOptionsAffectEmit(compilerOptions, oldCompilerOptions)) {
else if (oldCompilerOptions && !outFile(compilerOptions) && compilerOptionsAffectEmit(compilerOptions, oldCompilerOptions)) {
// Add all files to affectedFilesPendingEmit since emit changed
newProgram.getSourceFiles().forEach(f => addToAffectedFilesPendingEmit(state, f.resolvedPath, BuilderFileEmit.Full));
Debug.assert(!state.seenAffectedFiles || !state.seenAffectedFiles.size);
state.seenAffectedFiles = state.seenAffectedFiles || createMap<true>();
}

state.emittedBuildInfo = !state.changedFilesSet.size && !state.affectedFilesPendingEmit;
state.buildInfoEmitPending = !!state.changedFilesSet.size;
return state;
}

Expand Down Expand Up @@ -611,7 +611,7 @@ namespace ts {
isBuildInfoEmit?: boolean
) {
if (isBuildInfoEmit) {
state.emittedBuildInfo = true;
state.buildInfoEmitPending = false;
}
else if (affected === state.program) {
state.changedFilesSet.clear();
Expand All @@ -624,6 +624,7 @@ namespace ts {
}
if (isPendingEmit) {
state.affectedFilesPendingEmitIndex!++;
state.buildInfoEmitPending = true;
}
else {
state.affectedFilesIndex!++;
Expand Down Expand Up @@ -688,12 +689,14 @@ namespace ts {
}

export type ProgramBuildInfoDiagnostic = string | [string, readonly ReusableDiagnostic[]];
export type ProgramBuilderInfoFilePendingEmit = [string, BuilderFileEmit];
export interface ProgramBuildInfo {
fileInfos: MapLike<BuilderState.FileInfo>;
options: CompilerOptions;
referencedMap?: MapLike<string[]>;
exportedModulesMap?: MapLike<string[]>;
semanticDiagnosticsPerFile?: ProgramBuildInfoDiagnostic[];
affectedFilesPendingEmit?: ProgramBuilderInfoFilePendingEmit[];
}

/**
Expand Down Expand Up @@ -751,6 +754,17 @@ namespace ts {
result.semanticDiagnosticsPerFile = semanticDiagnosticsPerFile;
}

if (state.affectedFilesPendingEmit) {
const affectedFilesPendingEmit: ProgramBuilderInfoFilePendingEmit[] = [];
const seenFiles = createMap<true>();
for (const path of state.affectedFilesPendingEmit.slice(state.affectedFilesPendingEmitIndex).sort(compareStringsCaseSensitive)) {
if (addToSeen(seenFiles, path)) {
affectedFilesPendingEmit.push([relativeToBuildInfo(path), state.affectedFilesPendingEmitKind!.get(path)!]);
}
}
result.affectedFilesPendingEmit = affectedFilesPendingEmit;
}

return result;

function relativeToBuildInfoEnsuringAbsolutePath(path: string) {
Expand Down Expand Up @@ -916,13 +930,23 @@ namespace ts {
else if (kind === BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram) {
(builderProgram as EmitAndSemanticDiagnosticsBuilderProgram).getSemanticDiagnosticsOfNextAffectedFile = getSemanticDiagnosticsOfNextAffectedFile;
(builderProgram as EmitAndSemanticDiagnosticsBuilderProgram).emitNextAffectedFile = emitNextAffectedFile;
builderProgram.emitBuildInfo = emitBuildInfo;
}
else {
notImplemented();
}

return builderProgram;

function emitBuildInfo(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult {
if (state.buildInfoEmitPending) {
const result = Debug.checkDefined(state.program).emitBuildInfo(writeFile || maybeBind(host, host.writeFile), cancellationToken);
state.buildInfoEmitPending = false;
return result;
}
return emitSkippedWithNoDiagnostics;
}

/**
* Emits the next affected file's emit result (EmitResult and sourceFiles emitted) or returns undefined if iteration is complete
* The first of writeFile if provided, writeFile of BuilderProgramHost if provided, writeFile of compiler host
Expand All @@ -936,7 +960,7 @@ namespace ts {
if (!outFile(state.compilerOptions)) {
const pendingAffectedFile = getNextAffectedFilePendingEmit(state);
if (!pendingAffectedFile) {
if (state.emittedBuildInfo) {
if (!state.buildInfoEmitPending) {
return undefined;
}

Expand Down Expand Up @@ -993,7 +1017,7 @@ namespace ts {
function emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult {
if (kind === BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram) {
assertSourceFileOkWithoutNextAffectedCall(state, targetSourceFile);
const result = handleNoEmitOptions(builderProgram, targetSourceFile, cancellationToken);
const result = handleNoEmitOptions(builderProgram, targetSourceFile, writeFile, cancellationToken);
if (result) return result;
if (!targetSourceFile) {
// Emit and report any errors we ran into.
Expand Down Expand Up @@ -1142,7 +1166,10 @@ namespace ts {
referencedMap: getMapOfReferencedSet(program.referencedMap, toPath),
exportedModulesMap: getMapOfReferencedSet(program.exportedModulesMap, toPath),
semanticDiagnosticsPerFile: program.semanticDiagnosticsPerFile && arrayToMap(program.semanticDiagnosticsPerFile, value => toPath(isString(value) ? value : value[0]), value => isString(value) ? emptyArray : value[1]),
hasReusableDiagnostic: true
hasReusableDiagnostic: true,
affectedFilesPendingEmit: map(program.affectedFilesPendingEmit, value => toPath(value[0])),
affectedFilesPendingEmitKind: program.affectedFilesPendingEmit && arrayToMap(program.affectedFilesPendingEmit, value => toPath(value[0]), value => value[1]),
affectedFilesPendingEmitIndex: program.affectedFilesPendingEmit && 0,
};
return {
getState: () => state,
Expand All @@ -1165,6 +1192,7 @@ namespace ts {
getCurrentDirectory: notImplemented,
emitNextAffectedFile: notImplemented,
getSemanticDiagnosticsOfNextAffectedFile: notImplemented,
emitBuildInfo: notImplemented,
close: noop,
};

Expand Down Expand Up @@ -1195,6 +1223,7 @@ namespace ts {
getDeclarationDiagnostics: (sourceFile, cancellationToken) => getProgram().getDeclarationDiagnostics(sourceFile, cancellationToken),
getSemanticDiagnostics: (sourceFile, cancellationToken) => getProgram().getSemanticDiagnostics(sourceFile, cancellationToken),
emit: (sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers) => getProgram().emit(sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers),
emitBuildInfo: (writeFile, cancellationToken) => getProgram().emitBuildInfo(writeFile, cancellationToken),
getAllDependencies: notImplemented,
getCurrentDirectory: () => getProgram().getCurrentDirectory(),
close: noop,
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/builderPublic.ts
Expand Up @@ -99,6 +99,8 @@ namespace ts {
* in that order would be used to write the files
*/
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
/*@internal*/
emitBuildInfo(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult;
/**
* Get the current directory of the program
*/
Expand Down
27 changes: 19 additions & 8 deletions src/compiler/program.ts
Expand Up @@ -1585,7 +1585,7 @@ namespace ts {

function emitWorker(program: Program, sourceFile: SourceFile | undefined, writeFileCallback: WriteFileCallback | undefined, cancellationToken: CancellationToken | undefined, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers, forceDtsEmit?: boolean): EmitResult {
if (!forceDtsEmit) {
const result = handleNoEmitOptions(program, sourceFile, cancellationToken);
const result = handleNoEmitOptions(program, sourceFile, writeFileCallback, cancellationToken);
if (result) return result;
}

Expand Down Expand Up @@ -3638,11 +3638,17 @@ namespace ts {
}

/*@internal*/
export function handleNoEmitOptions(program: ProgramToEmitFilesAndReportErrors, sourceFile: SourceFile | undefined, cancellationToken: CancellationToken | undefined): EmitResult | undefined {
export const emitSkippedWithNoDiagnostics: EmitResult = { diagnostics: emptyArray, sourceMaps: undefined, emittedFiles: undefined, emitSkipped: true };

/*@internal*/
export function handleNoEmitOptions(
program: ProgramToEmitFilesAndReportErrors,
sourceFile: SourceFile | undefined,
writeFile: WriteFileCallback | undefined,
cancellationToken: CancellationToken | undefined
): EmitResult | undefined {
const options = program.getCompilerOptions();
if (options.noEmit) {
return { diagnostics: emptyArray, sourceMaps: undefined, emittedFiles: undefined, emitSkipped: true };
}
if (options.noEmit) return emitSkippedWithNoDiagnostics;

// If the noEmitOnError flag is set, then check if we have any errors so far. If so,
// immediately bail out. Note that we pass 'undefined' for 'sourceFile' so that we
Expand All @@ -3659,9 +3665,14 @@ namespace ts {
diagnostics = program.getDeclarationDiagnostics(/*sourceFile*/ undefined, cancellationToken);
}

return diagnostics.length > 0 ?
{ diagnostics, sourceMaps: undefined, emittedFiles: undefined, emitSkipped: true } :
undefined;
if (!diagnostics.length) return undefined;
let emittedFiles: string[] | undefined;
if (!sourceFile && !outFile(options)) {
const emitResult = program.emitBuildInfo(writeFile, cancellationToken);
if (emitResult.diagnostics) diagnostics = [...diagnostics, ...emitResult.diagnostics];
emittedFiles = emitResult.emittedFiles;
}
return { diagnostics, sourceMaps: undefined, emittedFiles, emitSkipped: true };
}

/*@internal*/
Expand Down