Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Jan 30, 2024
1 parent 709c1b6 commit f99ff77
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 130 deletions.
33 changes: 17 additions & 16 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,8 @@ enum ProjectAncestorLoadKind {
FindCreateDelayLoad,
}

enum ConfiguredProjectKind {
/** @internal */
export enum ConfiguredProjectKind {
Find,
Create,
Reload,
Expand Down Expand Up @@ -1784,7 +1785,7 @@ export class ProjectService {
}

/** @internal */
private onConfigFileChanged(canonicalConfigFilePath: NormalizedPath, eventKind: FileWatcherEventKind) {
private onConfigFileChanged(configFileName: NormalizedPath, canonicalConfigFilePath: NormalizedPath, eventKind: FileWatcherEventKind) {
const configFileExistenceInfo = this.configFileExistenceInfoCache.get(canonicalConfigFilePath)!;
if (eventKind === FileWatcherEventKind.Deleted) {
// Update the cached status
Expand All @@ -1797,15 +1798,14 @@ export class ProjectService {
this.getConfiguredProjectByCanonicalConfigFilePath(canonicalConfigFilePath) :
undefined;
if (project) this.removeProject(project);
// TODO:: sheetal configFileForOpenFiles:: update needed when deleting ancestor project
}
else {
// Update the cached status
configFileExistenceInfo.exists = true;
}

// Update projects watching config
this.delayUpdateProjectsFromParsedConfigOnConfigFileChange(canonicalConfigFilePath, "Change in config file detected");
this.delayUpdateProjectsFromParsedConfigOnConfigFileChange(canonicalConfigFilePath, `Change in config file ${configFileName} detected`);

// Reload the configured projects for the open files in the map as they are affected by this config file
// If the configured project was deleted, we want to reload projects for all the open files including files
Expand All @@ -1814,11 +1814,11 @@ export class ProjectService {
// we would need to schedule the project reload for only the root of inferred projects
// Get open files to reload projects for
const updatedProjects = new Set<ConfiguredProject>();
// Add the already scheduled project as updated
if (configFileExistenceInfo.config?.projects.has(canonicalConfigFilePath)) {
updatedProjects.add(this.findConfiguredProjectByProjectName(configFileName)!);
}
configFileExistenceInfo.openFilesImpactedByConfigFile?.forEach((isRootOfInferredProject, path) => {
// Filter out the files that need to be ignored
// If file is added or modified, reload the project only for open files that are not root of inferred project
if (eventKind !== FileWatcherEventKind.Deleted && !isRootOfInferredProject) return;

// Invalidate default config file name for open file
this.configFileForOpenFiles.delete(path);

Expand All @@ -1833,11 +1833,10 @@ export class ProjectService {
const project = this.findConfiguredProjectByProjectName(configFileName) || this.createConfiguredProject(configFileName);
if (tryAddToSet(updatedProjects, project)) {
project.pendingUpdateLevel = ProgramUpdateLevel.Full;
project.pendingUpdateReason = "Change in config file detected";
project.pendingUpdateReason = `Change in config file ${configFileName} detected`;
this.delayUpdateProjectGraph(project);
}
// TODO:: After this update we need to ensure that we try and create Project for the open script info (through refs etc)
// TODO:: sheetal: ensureProjectForOpenFile needs to handle that?
(project.pendingOpenFileProjectUpdates ??= new Set()).add(path);
}
});

Expand Down Expand Up @@ -2087,7 +2086,7 @@ export class ProjectService {
if (!configFileExistenceInfo.watcher || configFileExistenceInfo.watcher === noopConfigFileWatcher) {
configFileExistenceInfo.watcher = this.watchFactory.watchFile(
configFileName,
(_fileName, eventKind) => this.onConfigFileChanged(canonicalConfigFilePath, eventKind),
(_fileName, eventKind) => this.onConfigFileChanged(configFileName, canonicalConfigFilePath, eventKind),
PollingInterval.High,
this.getWatchOptionsFromProjectWatchOptions(configFileExistenceInfo?.config?.parsedCommandLine?.watchOptions),
WatchType.ConfigFile,
Expand Down Expand Up @@ -2217,7 +2216,7 @@ export class ProjectService {
configFileExistenceInfo.watcher ||= canWatchDirectoryOrFile(getPathComponents(getDirectoryPath(canonicalConfigFilePath) as Path)) ?
this.watchFactory.watchFile(
configFileName,
(_filename, eventKind) => this.onConfigFileChanged(canonicalConfigFilePath, eventKind),
(_filename, eventKind) => this.onConfigFileChanged(configFileName, canonicalConfigFilePath, eventKind),
PollingInterval.High,
this.hostConfiguration.watchOptions,
WatchType.ConfigFileForInferredRoot,
Expand Down Expand Up @@ -3828,9 +3827,11 @@ export class ProjectService {
return info;
}

private tryFindDefaultConfiguredProjectForOpenScriptInfo(info: ScriptInfo, kind: ConfiguredProjectKind.Find | ConfiguredProjectKind.Create): DefaultConfiguredProjectResult | undefined;
private tryFindDefaultConfiguredProjectForOpenScriptInfo(info: ScriptInfo, kind: ConfiguredProjectKind.Reload, reloadedProjects: Set<ConfiguredProject>): DefaultConfiguredProjectResult | undefined;
private tryFindDefaultConfiguredProjectForOpenScriptInfo(info: ScriptInfo, kind: ConfiguredProjectKind, reloadedProjects?: Set<ConfiguredProject>): string | DefaultConfiguredProjectResult | undefined {
/** @internal */
tryFindDefaultConfiguredProjectForOpenScriptInfo(info: ScriptInfo, kind: ConfiguredProjectKind.Find | ConfiguredProjectKind.Create): DefaultConfiguredProjectResult | undefined;
/** @internal */
tryFindDefaultConfiguredProjectForOpenScriptInfo(info: ScriptInfo, kind: ConfiguredProjectKind.Reload, reloadedProjects: Set<ConfiguredProject>): DefaultConfiguredProjectResult | undefined;
tryFindDefaultConfiguredProjectForOpenScriptInfo(info: ScriptInfo, kind: ConfiguredProjectKind, reloadedProjects?: Set<ConfiguredProject>): string | DefaultConfiguredProjectResult | undefined {
const configFileName = this.getConfigFileNameForFile(info, kind === ConfiguredProjectKind.Find);
if (!configFileName) return;
let project = this.findConfiguredProjectByProjectName(configFileName);
Expand Down
12 changes: 12 additions & 0 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ import {
import {
ActionInvalidate,
asNormalizedPath,
ConfiguredProjectKind,
createModuleSpecifierCache,
emptyArray,
Errors,
Expand Down Expand Up @@ -2671,6 +2672,8 @@ export class ConfiguredProject extends Project {
pendingUpdateLevel: ProgramUpdateLevel | undefined;
/** @internal */
pendingUpdateReason: string | undefined;
/** @internal */
pendingOpenFileProjectUpdates: Set<Path> | undefined;

/** @internal */
openFileWatchTriggered = new Map<string, ProgramUpdateLevel>();
Expand Down Expand Up @@ -2762,6 +2765,8 @@ export class ConfiguredProject extends Project {
const isInitialLoad = this.isInitialLoadPending();
this.isInitialLoadPending = returnFalse;
const updateLevel = this.pendingUpdateLevel;
const pendingOpenFileProjectUpdates = this.pendingOpenFileProjectUpdates;
this.pendingOpenFileProjectUpdates = undefined;
this.pendingUpdateLevel = ProgramUpdateLevel.Update;
let result: boolean;
switch (updateLevel) {
Expand All @@ -2781,6 +2786,13 @@ export class ConfiguredProject extends Project {
this.compilerHost = undefined;
this.projectService.sendProjectLoadingFinishEvent(this);
this.projectService.sendProjectTelemetry(this);

pendingOpenFileProjectUpdates?.forEach(path => {
const info = this.projectService.getScriptInfoForPath(path);
if (info && this.projectService.openFiles.has(info.path)) {
this.projectService.tryFindDefaultConfiguredProjectForOpenScriptInfo(info, ConfiguredProjectKind.Create);
}
});
return result;
}

Expand Down
57 changes: 26 additions & 31 deletions src/testRunner/unittests/tsserver/projectReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ const b: B = new B();`,
/* eslint-enable local/argument-trivia */
});

it("when file is not in the first project found but is contained by project from solution", () => {
it("when file is not in the first project found but is contained by project from solution sheetal", () => {
const appDemo: File = {
path: "/home/src/projects/project/app/Component-demos.ts",
content: dedent`
Expand Down Expand Up @@ -1768,11 +1768,6 @@ const b: B = new B();`,
files: [appDemo],
session,
});
const info = session.getProjectService().getScriptInfo(appDemo.path)!;
session.logger.startGroup();
session.logger.info(`getDefaultProject for ${appDemo.path}: ${info.getDefaultProject().projectName}`);
session.logger.info(`findDefaultConfiguredProject for ${appDemo.path}: ${session.getProjectService().findDefaultConfiguredProject(info)?.projectName}`);
session.logger.endGroup();
verifyProjectManagement(); // Should not remove projects for file

closeFilesForSession([appDemo], session);
Expand Down Expand Up @@ -1800,26 +1795,30 @@ const b: B = new B();`,

baselineTsserverLogs("projectReferences", "when file is not in the first project found but is contained by project from solution", session);

function printDefaultProject() {
const info = session.getProjectService().getScriptInfo(appDemo.path);
session.logger.startGroup();
session.logger.info(`getDefaultProject: ${session.getProjectService().tryGetDefaultProjectForFile(appDemo.path as ts.server.NormalizedPath)?.projectName}`);
session.logger.info(`findDefaultConfiguredProject: ${info && session.getProjectService().findDefaultConfiguredProject(info)?.projectName}`);
session.logger.endGroup();
}

function verifyProjectManagement() {
printDefaultProject();
openFilesForSession([randomTs], session); // Verify Project management
closeFilesForSession([randomTs], session);
printDefaultProject();
}

function verifyAppConfigNotComposite(projManagementBeforeRevert: boolean) {
// Not composite
host.writeFile(appConfig.path, appConfig.content.replace(`"composite": true,`, ""));
host.runQueuedTimeoutCallbacks();
if (projManagementBeforeRevert) verifyProjectManagement();
// Revert
host.writeFile(appConfig.path, appConfig.content);
host.runQueuedTimeoutCallbacks();
if (!projManagementBeforeRevert) verifyProjectManagement();
verifyConfigChange(appConfig, appConfig.content.replace(`"composite": true,`, ""), projManagementBeforeRevert);
}

function verifySolutionConfigNotComposite(projManagementBeforeRevert: boolean) {
// Not referencing demos
host.writeFile(
solutionConfig.path,
verifyConfigChange(
solutionConfig,
jsonToReadableText({
compilerOptions: {
outDir: "./dist/",
Expand All @@ -1828,35 +1827,31 @@ const b: B = new B();`,
{ path: "./app/tsconfig.json" },
],
}),
projManagementBeforeRevert,
);
host.runQueuedTimeoutCallbacks();
if (projManagementBeforeRevert) verifyProjectManagement();
// Revert
host.writeFile(solutionConfig.path, solutionConfig.content);
host.runQueuedTimeoutCallbacks();
if (!projManagementBeforeRevert) verifyProjectManagement();
}

function verifySolutionConfigDelete(projManagementBeforeRevert: boolean) {
// Not referencing demos
host.deleteFile(solutionConfig.path);
host.runQueuedTimeoutCallbacks();
if (projManagementBeforeRevert) verifyProjectManagement();
// Revert
host.writeFile(solutionConfig.path, solutionConfig.content);
host.runQueuedTimeoutCallbacks();
if (!projManagementBeforeRevert) verifyProjectManagement();
// Delete solution file
verifyConfigChange(solutionConfig, /*change*/ undefined, projManagementBeforeRevert);
}

function verfiyDemoConfigChange(projManagementBeforeRevert: boolean) {
// Make some errors in demo::
host.writeFile(demoConfig.path, demoConfig.content.replace(`"../app/**/*-demos.*"`, ""));
verifyConfigChange(demoConfig, demoConfig.content.replace(`"../app/**/*-demos.*"`, ""), projManagementBeforeRevert);
}

function verifyConfigChange(config: File, change: string | undefined, projManagementBeforeRevert: boolean) {
if (change !== undefined) host.writeFile(config.path, change);
else host.deleteFile(config.path);
host.runQueuedTimeoutCallbacks();
if (projManagementBeforeRevert) verifyProjectManagement();
else printDefaultProject();
// Revert
host.writeFile(demoConfig.path, demoConfig.content);
host.writeFile(config.path, config.content);
host.runQueuedTimeoutCallbacks();
if (!projManagementBeforeRevert) verifyProjectManagement();
else printDefaultProject();
}
});
});
Loading

0 comments on commit f99ff77

Please sign in to comment.