Skip to content

Make sure the script is only attached to project during createProgram execution #59354

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

Closed
Closed
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
6 changes: 5 additions & 1 deletion src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,11 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
// This was missing path earlier but now the file exists. Update the root
existingValue.info = scriptInfo;
}
scriptInfo.attachToProject(this);
// Make sure the script is only attached to project during createProgram execution
const creatingProgram = !this.languageService.getCurrentProgram(/*acceptOutdatedProgram*/ false);
if (creatingProgram) {
scriptInfo.attachToProject(this);
}
}
return scriptInfo;
}
Expand Down
10 changes: 9 additions & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,7 @@ export function createLanguageService(

const syntaxTreeCache: SyntaxTreeCache = new SyntaxTreeCache(host);
let program: Program;
let creatingProgram = false;
let lastProjectVersion: string;
let lastTypesRootVersion = 0;

Expand Down Expand Up @@ -1816,7 +1817,9 @@ export function createLanguageService(
oldProgram: program,
projectReferences,
};
creatingProgram = true;
program = createProgram(options);
creatingProgram = false;

// 'getOrCreateSourceFile' depends on caching but should be used past this point.
// After this point, the cache needs to be cleared to allow all collected snapshots to be released
Expand Down Expand Up @@ -3396,7 +3399,12 @@ export function createLanguageService(
getEmitOutput,
getNonBoundSourceFile,
getProgram,
getCurrentProgram: () => program,
getCurrentProgram: (acceptOutdatedProgram = true) => {
if (!acceptOutdatedProgram && creatingProgram) {
return undefined;
}
return program;
},
getAutoImportProvider,
updateIsDefinitionOfReferencedSymbols,
getApplicableRefactors,
Expand Down
2 changes: 1 addition & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ export interface LanguageService {
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean, forceDtsEmit?: boolean): EmitOutput;

getProgram(): Program | undefined;
/** @internal */ getCurrentProgram(): Program | undefined;
/** @internal */ getCurrentProgram(acceptOutdatedProgram?: boolean): Program | undefined;

/** @internal */ getNonBoundSourceFile(fileName: string): SourceFile;
/** @internal */ getAutoImportProvider(): Program | undefined;
Expand Down