Skip to content

Commit

Permalink
Cherry-pick PR #40026 into release-4.0 (#40028)
Browse files Browse the repository at this point in the history
Component commits:
8f442ba Revert to including only open files in partial semantic server mode

Co-authored-by: Sheetal Nandi <shkamat@microsoft.com>
  • Loading branch information
typescript-bot and sheetalkamat committed Aug 13, 2020
1 parent 6620b25 commit a192551
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 169 deletions.
18 changes: 0 additions & 18 deletions src/compiler/program.ts
Expand Up @@ -807,8 +807,6 @@ namespace ts {
let mapFromFileToProjectReferenceRedirects: ESMap<Path, Path> | undefined;
let mapFromToProjectReferenceRedirectSource: ESMap<Path, SourceOfProjectReferenceRedirect> | undefined;

let skippedTrippleSlashReferences: Set<Path> | undefined;

const useSourceOfProjectReferenceRedirect = !!host.useSourceOfProjectReferenceRedirect?.() &&
!options.disableSourceOfProjectReferenceRedirect;
const { onProgramCreateComplete, fileExists, directoryExists } = updateHostForUseSourceOfProjectReferenceRedirect({
Expand Down Expand Up @@ -931,7 +929,6 @@ namespace ts {
getSourceFiles: () => files,
getMissingFilePaths: () => missingFilePaths!, // TODO: GH#18217
getRefFileMap: () => refFileMap,
getSkippedTrippleSlashReferences: () => skippedTrippleSlashReferences,
getFilesByNameMap: () => filesByName,
getCompilerOptions: () => options,
getSyntacticDiagnostics,
Expand Down Expand Up @@ -1275,7 +1272,6 @@ namespace ts {
const oldSourceFiles = oldProgram.getSourceFiles();
const enum SeenPackageName { Exists, Modified }
const seenPackageNames = new Map<string, SeenPackageName>();
const oldSkippedTrippleSlashReferences = oldProgram.getSkippedTrippleSlashReferences();

for (const oldSourceFile of oldSourceFiles) {
let newSourceFile = host.getSourceFileByPath
Expand Down Expand Up @@ -1348,11 +1344,6 @@ namespace ts {
oldProgram.structureIsReused = StructureIsReused.SafeModules;
}

if (oldSkippedTrippleSlashReferences?.has(oldSourceFile.path) && includeTripleslashReferencesFrom(newSourceFile)) {
// tripleslash reference resolution is now allowed
oldProgram.structureIsReused = StructureIsReused.SafeModules;
}

// check imports and module augmentations
collectExternalModuleReferences(newSourceFile);
if (!arrayIsEqualTo(oldSourceFile.imports, newSourceFile.imports, moduleNameIsEqualTo)) {
Expand Down Expand Up @@ -1440,7 +1431,6 @@ namespace ts {

missingFilePaths = oldProgram.getMissingFilePaths();
refFileMap = oldProgram.getRefFileMap();
skippedTrippleSlashReferences = oldSkippedTrippleSlashReferences;

// update fileName -> file mapping
Debug.assert(newSourceFiles.length === oldProgram.getSourceFiles().length);
Expand Down Expand Up @@ -2660,15 +2650,7 @@ namespace ts {
return projectReferenceRedirects.get(projectReferencePath) || undefined;
}

function includeTripleslashReferencesFrom(file: SourceFile) {
return !host.includeTripleslashReferencesFrom || host.includeTripleslashReferencesFrom(file.originalFileName);
}

function processReferencedFiles(file: SourceFile, isDefaultLib: boolean) {
if (!includeTripleslashReferencesFrom(file)) {
(skippedTrippleSlashReferences ||= new Set()).add(file.path);
return;
}
forEach(file.referencedFiles, (ref, index) => {
const referencedFileName = resolveTripleslashReference(ref.fileName, file.fileName);
processSourceFile(
Expand Down
53 changes: 3 additions & 50 deletions src/compiler/resolutionCache.ts
Expand Up @@ -11,7 +11,6 @@ namespace ts {

invalidateResolutionsOfFailedLookupLocations(): boolean;
invalidateResolutionOfFile(filePath: Path): void;
removeRelativeNoResolveResolutionsOfFile(filePath: Path): boolean;
removeResolutionsOfFile(filePath: Path): void;
removeResolutionsFromProjectReferenceRedirects(filePath: Path): void;
setFilesWithInvalidatedNonRelativeUnresolvedImports(filesWithUnresolvedImports: ESMap<Path, readonly string[]>): void;
Expand Down Expand Up @@ -142,21 +141,7 @@ namespace ts {
type GetResolutionWithResolvedFileName<T extends ResolutionWithFailedLookupLocations = ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName = ResolutionWithResolvedFileName> =
(resolution: T) => R | undefined;

export enum ResolutionKind {
All,
RelativeReferencesInOpenFileOnly
}

const noResolveResolvedModule: ResolvedModuleWithFailedLookupLocations = {
resolvedModule: undefined,
failedLookupLocations: []
};
const noResolveResolvedTypeReferenceDirective: ResolvedTypeReferenceDirectiveWithFailedLookupLocations = {
resolvedTypeReferenceDirective: undefined,
failedLookupLocations: []
};

export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootDirForResolution: string | undefined, resolutionKind: ResolutionKind, logChangesWhenResolvingModule: boolean): ResolutionCache {
export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootDirForResolution: string | undefined, logChangesWhenResolvingModule: boolean): ResolutionCache {
let filesWithChangedSetOfUnresolvedImports: Path[] | undefined;
let filesWithInvalidatedResolutions: Set<Path> | undefined;
let filesWithInvalidatedNonRelativeUnresolvedImports: ReadonlyESMap<Path, readonly string[]> | undefined;
Expand Down Expand Up @@ -221,7 +206,6 @@ namespace ts {
hasChangedAutomaticTypeDirectiveNames: () => hasChangedAutomaticTypeDirectiveNames,
invalidateResolutionOfFile,
invalidateResolutionsOfFailedLookupLocations,
removeRelativeNoResolveResolutionsOfFile,
setFilesWithInvalidatedNonRelativeUnresolvedImports,
createHasInvalidatedResolution,
updateTypeRootsWatch,
Expand Down Expand Up @@ -357,12 +341,11 @@ namespace ts {
shouldRetryResolution: (t: T) => boolean;
reusedNames?: readonly string[];
logChanges?: boolean;
noResolveResolution: T;
}
function resolveNamesWithLocalCache<T extends ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName>({
names, containingFile, redirectedReference,
cache, perDirectoryCacheWithRedirects,
loader, getResolutionWithResolvedFileName, noResolveResolution,
loader, getResolutionWithResolvedFileName,
shouldRetryResolution, reusedNames, logChanges
}: ResolveNamesWithLocalCacheInput<T, R>): (R | undefined)[] {
const path = resolutionHost.toPath(containingFile);
Expand Down Expand Up @@ -399,10 +382,7 @@ namespace ts {
resolution = resolutionInDirectory;
}
else {
resolution = resolutionKind === ResolutionKind.All ||
(isExternalModuleNameRelative(name) && resolutionHost.fileIsOpen(path)) ?
loader(name, containingFile, compilerOptions, resolutionHost.getCompilerHost?.() || resolutionHost, redirectedReference) :
noResolveResolution;
resolution = loader(name, containingFile, compilerOptions, resolutionHost.getCompilerHost?.() || resolutionHost, redirectedReference);
perDirectoryResolution.set(name, resolution);
}
resolutionsInFile.set(name, resolution);
Expand Down Expand Up @@ -461,7 +441,6 @@ namespace ts {
loader: resolveTypeReferenceDirective,
getResolutionWithResolvedFileName: getResolvedTypeReferenceDirective,
shouldRetryResolution: resolution => resolution.resolvedTypeReferenceDirective === undefined,
noResolveResolution: noResolveResolvedTypeReferenceDirective,
});
}

Expand All @@ -477,7 +456,6 @@ namespace ts {
shouldRetryResolution: resolution => !resolution.resolvedModule || !resolutionExtensionIsTSOrJson(resolution.resolvedModule.extension),
reusedNames,
logChanges: logChangesWhenResolvingModule,
noResolveResolution: noResolveResolvedModule,
});
}

Expand Down Expand Up @@ -763,31 +741,6 @@ namespace ts {
}
}

function removeRelativeNoResolveResolutionsOfFileFromCache<T extends ResolutionWithFailedLookupLocations>(
cache: ESMap<Path, ESMap<string, T>>,
filePath: Path,
noResolveResolution: T,
) {
Debug.assert(resolutionKind === ResolutionKind.RelativeReferencesInOpenFileOnly);
// Deleted file, stop watching failed lookups for all the resolutions in the file
const resolutions = cache.get(filePath);
if (!resolutions) return false;
let invalidated = false;
resolutions.forEach((resolution, name) => {
if (resolution === noResolveResolution && isExternalModuleNameRelative(name)) {
resolutions.delete(name);
invalidated = true;
}
});
return invalidated;
}

function removeRelativeNoResolveResolutionsOfFile(filePath: Path) {
let invalidated = removeRelativeNoResolveResolutionsOfFileFromCache(resolvedModuleNames, filePath, noResolveResolvedModule);
invalidated = removeRelativeNoResolveResolutionsOfFileFromCache(resolvedTypeReferenceDirectives, filePath, noResolveResolvedTypeReferenceDirective) || invalidated;
return invalidated;
}

function setFilesWithInvalidatedNonRelativeUnresolvedImports(filesMap: ReadonlyESMap<Path, readonly string[]>) {
Debug.assert(filesWithInvalidatedNonRelativeUnresolvedImports === filesMap || filesWithInvalidatedNonRelativeUnresolvedImports === undefined);
filesWithInvalidatedNonRelativeUnresolvedImports = filesMap;
Expand Down
3 changes: 0 additions & 3 deletions src/compiler/types.ts
Expand Up @@ -3687,8 +3687,6 @@ namespace ts {
/* @internal */
getRefFileMap(): MultiMap<Path, RefFile> | undefined;
/* @internal */
getSkippedTrippleSlashReferences(): Set<Path> | undefined;
/* @internal */
getFilesByNameMap(): ESMap<string, SourceFile | false | undefined>;

/**
Expand Down Expand Up @@ -6232,7 +6230,6 @@ namespace ts {
* This method is a companion for 'resolveModuleNames' and is used to resolve 'types' references to actual type declaration files
*/
resolveTypeReferenceDirectives?(typeReferenceDirectiveNames: string[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions): (ResolvedTypeReferenceDirective | undefined)[];
/* @internal */ includeTripleslashReferencesFrom?(containingFile: string): boolean;
getEnvironmentVariable?(name: string): string | undefined;
/* @internal */ onReleaseOldSourceFile?(oldSourceFile: SourceFile, oldOptions: CompilerOptions, hasSourceFileByPath: boolean): void;
/* @internal */ hasInvalidatedResolution?: HasInvalidatedResolution;
Expand Down
1 change: 0 additions & 1 deletion src/compiler/watchPublic.ts
Expand Up @@ -320,7 +320,6 @@ namespace ts {
configFileName ?
getDirectoryPath(getNormalizedAbsolutePath(configFileName, currentDirectory)) :
currentDirectory,
ResolutionKind.All,
/*logChangesWhenResolvingModule*/ false
);
// Resolve module using host module resolution strategy if provided otherwise use resolution cache to resolve module names
Expand Down
14 changes: 1 addition & 13 deletions src/server/editorServices.ts
Expand Up @@ -3029,15 +3029,7 @@ namespace ts.server {
let retainProjects: ConfiguredProject[] | ConfiguredProject | undefined;
let projectForConfigFileDiag: ConfiguredProject | undefined;
let defaultConfigProjectIsCreated = false;
if (this.serverMode === LanguageServiceMode.PartialSemantic) {
// Invalidate resolutions in the file since this file is now open
info.containingProjects.forEach(project => {
if (project.resolutionCache.removeRelativeNoResolveResolutionsOfFile(info.path)) {
project.markAsDirty();
}
});
}
else if (!project && this.serverMode === LanguageServiceMode.Semantic) { // Checking semantic mode is an optimization
if (!project && this.serverMode === LanguageServiceMode.Semantic) { // Checking semantic mode is an optimization
configFileName = this.getConfigFileNameForFile(info);
if (configFileName) {
project = this.findConfiguredProjectByProjectName(configFileName);
Expand Down Expand Up @@ -3124,10 +3116,6 @@ namespace ts.server {
Debug.assert(this.openFiles.has(info.path));
this.assignOrphanScriptInfoToInferredProject(info, this.openFiles.get(info.path));
}
else if (this.serverMode === LanguageServiceMode.PartialSemantic && info.cacheSourceFile?.sourceFile.referencedFiles.length) {
// This file was just opened and references in this file will previously not been resolved so schedule update
info.containingProjects.forEach(project => project.markAsDirty());
}
Debug.assert(!info.isOrphan());
return { configFileName, configFileErrors, retainProjects };
}
Expand Down
16 changes: 1 addition & 15 deletions src/server/project.ts
Expand Up @@ -285,6 +285,7 @@ namespace ts.server {
break;
case LanguageServiceMode.PartialSemantic:
this.languageServiceEnabled = true;
this.compilerOptions.noResolve = true;
this.compilerOptions.types = [];
break;
case LanguageServiceMode.Syntactic:
Expand All @@ -310,7 +311,6 @@ namespace ts.server {
this.resolutionCache = createResolutionCache(
this,
currentDirectory && this.currentDirectory,
projectService.serverMode === LanguageServiceMode.Semantic ? ResolutionKind.All : ResolutionKind.RelativeReferencesInOpenFileOnly,
/*logChangesWhenResolvingModule*/ true
);
this.languageService = createLanguageService(this, this.documentRegistry, this.projectService.serverMode);
Expand Down Expand Up @@ -466,20 +466,6 @@ namespace ts.server {
return this.resolutionCache.resolveTypeReferenceDirectives(typeDirectiveNames, containingFile, redirectedReference);
}

/*@internal*/
includeTripleslashReferencesFrom(containingFile: string) {
switch (this.projectService.serverMode) {
case LanguageServiceMode.Semantic:
return true;
case LanguageServiceMode.PartialSemantic:
return this.fileIsOpen(this.toPath(containingFile));
case LanguageServiceMode.Syntactic:
return false;
default:
Debug.assertNever(this.projectService.serverMode);
}
}

directoryExists(path: string): boolean {
return this.directoryStructureHost.directoryExists!(path); // TODO: GH#18217
}
Expand Down
1 change: 0 additions & 1 deletion src/services/services.ts
Expand Up @@ -1344,7 +1344,6 @@ namespace ts {
onReleaseOldSourceFile,
hasInvalidatedResolution,
hasChangedAutomaticTypeDirectiveNames,
includeTripleslashReferencesFrom: maybeBind(host, host.includeTripleslashReferencesFrom),
trace: maybeBind(host, host.trace),
resolveModuleNames: maybeBind(host, host.resolveModuleNames),
resolveTypeReferenceDirectives: maybeBind(host, host.resolveTypeReferenceDirectives),
Expand Down
1 change: 0 additions & 1 deletion src/services/types.ts
Expand Up @@ -272,7 +272,6 @@ namespace ts {
resolveModuleNames?(moduleNames: string[], containingFile: string, reusedNames: string[] | undefined, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions): (ResolvedModule | undefined)[];
getResolvedModuleWithFailedLookupLocationsFromCache?(modulename: string, containingFile: string): ResolvedModuleWithFailedLookupLocations | undefined;
resolveTypeReferenceDirectives?(typeDirectiveNames: string[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions): (ResolvedTypeReferenceDirective | undefined)[];
/* @internal */ includeTripleslashReferencesFrom?(containingFile: string): boolean;
/* @internal */ hasInvalidatedResolution?: HasInvalidatedResolution;
/* @internal */ hasChangedAutomaticTypeDirectiveNames?: HasChangedAutomaticTypeDirectiveNames;
/* @internal */
Expand Down

0 comments on commit a192551

Please sign in to comment.