Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2893,7 +2893,7 @@ export class ProjectService {
path = normalizedPathToPath(fileName, this.currentDirectory, this.toCanonicalFileName);
const existingValue = projectRootFilesMap.get(path);
if (existingValue) {
if (existingValue.info) {
if (existingValue.info?.path === path) {
project.removeFile(existingValue.info, /*fileExists*/ false, /*detachFromProject*/ true);
existingValue.info = undefined;
}
Expand Down
38 changes: 20 additions & 18 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ import {
noopFileWatcher,
normalizePath,
normalizeSlashes,
orderedRemoveItem,
PackageJsonAutoImportPreference,
PackageJsonInfo,
ParsedCommandLine,
Expand Down Expand Up @@ -309,8 +308,7 @@ const enum TypingWatcherType {
type TypingWatchers = Map<Path, FileWatcher> & { isInvoked?: boolean; };

export abstract class Project implements LanguageServiceHost, ModuleResolutionHost {
private rootFiles: ScriptInfo[] = [];
private rootFilesMap = new Map<string, ProjectRootFile>();
private rootFilesMap = new Map<Path, ProjectRootFile>();
private program: Program | undefined;
private externalFiles: SortedReadonlyArray<string> | undefined;
private missingFilesMap: Map<Path, FileWatcher> | undefined;
Expand Down Expand Up @@ -641,7 +639,7 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
}

getScriptFileNames() {
if (!this.rootFiles) {
if (!this.rootFilesMap.size) {
return ts.emptyArray;
}

Expand All @@ -667,7 +665,6 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
const existingValue = this.rootFilesMap.get(scriptInfo.path);
if (existingValue && existingValue.info !== scriptInfo) {
// This was missing path earlier but now the file exists. Update the root
this.rootFiles.push(scriptInfo);
existingValue.info = scriptInfo;
}
scriptInfo.attachToProject(this);
Expand Down Expand Up @@ -1079,12 +1076,9 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
// Release external files
forEach(this.externalFiles, externalFile => this.detachScriptInfoIfNotRoot(externalFile));
// Always remove root files from the project
for (const root of this.rootFiles) {
root.detachFromProject(this);
}
this.rootFilesMap.forEach(root => root.info?.detachFromProject(this));
this.projectService.pendingEnsureProjectForOpenFiles = true;

this.rootFiles = undefined!;
this.rootFilesMap = undefined!;
this.externalFiles = undefined;
this.program = undefined;
Expand Down Expand Up @@ -1135,20 +1129,20 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
}

isClosed() {
return this.rootFiles === undefined;
return this.rootFilesMap === undefined;
}

hasRoots() {
return this.rootFiles && this.rootFiles.length > 0;
return !!this.rootFilesMap?.size;
}

/** @internal */
isOrphan() {
return false;
}

getRootFiles() {
return this.rootFiles && this.rootFiles.map(info => info.fileName);
getRootFiles(): NormalizedPath[] {
return this.rootFilesMap && arrayFrom(ts.mapDefinedIterator(this.rootFilesMap.values(), value => value.info?.fileName));
}

/** @internal */
Expand All @@ -1157,13 +1151,13 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
}

getRootScriptInfos() {
return this.rootFiles;
return arrayFrom(ts.mapDefinedIterator(this.rootFilesMap.values(), value => value.info));
}

getScriptInfos(): ScriptInfo[] {
if (!this.languageServiceEnabled) {
// if language service is not enabled - return just root files
return this.rootFiles;
return this.getRootScriptInfos();
}
return map(this.program!.getSourceFiles(), sourceFile => {
const scriptInfo = this.projectService.getScriptInfoForPath(sourceFile.resolvedPath);
Expand Down Expand Up @@ -1256,13 +1250,12 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
}

isRoot(info: ScriptInfo) {
return this.rootFilesMap && this.rootFilesMap.get(info.path)?.info === info;
return this.rootFilesMap?.get(info.path)?.info === info;
}

// add a root file to project
addRoot(info: ScriptInfo, fileName?: NormalizedPath) {
Debug.assert(!this.isRoot(info));
this.rootFiles.push(info);
this.rootFilesMap.set(info.path, { fileName: fileName || info.fileName, info });
info.attachToProject(this);

Expand Down Expand Up @@ -1586,6 +1579,16 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
});
}

// Update roots
this.rootFilesMap.forEach((value, path) => {
const file = this.program!.getSourceFileByPath(path);
const info = value.info;
if (!file || value.info?.path === file.resolvedPath) return;
value.info = this.projectService.getScriptInfo(file.fileName)!;
Debug.assert(value.info.isAttached(this));
info?.detachFromProject(this);
});

// Update the missing file paths watcher
updateMissingFilePathsWatch(
this.program,
Expand Down Expand Up @@ -2006,7 +2009,6 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo

// remove a root file from project
protected removeRoot(info: ScriptInfo): void {
orderedRemoveItem(this.rootFiles, info);
this.rootFilesMap.delete(info.path);
}

Expand Down
5 changes: 1 addition & 4 deletions src/testRunner/unittests/tsserver/dynamicFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ describe("unittests:: tsserver:: dynamicFiles:: ", () => {
}], session);
}
catch (e) {
assert.strictEqual(
e.message.replace(/\r?\n/, "\n"),
`Debug Failure. False expression.\nVerbose Debug Information: {"fileName":"^walkThroughSnippet:/Users/UserName/projects/someProject/out/someFile#1.js","currentDirectory":"/user/username/projects/myproject","hostCurrentDirectory":"/","openKeys":[]}\nDynamic files must always be opened with service's current directory or service should support inferred project per projectRootPath.`,
);
session.logger.info(e.message);
}
const file2Path = file.path.replace("#1", "#2");
openFilesForSession([{ file: file2Path, content: file.content }], session);
Expand Down
53 changes: 53 additions & 0 deletions src/testRunner/unittests/tsserver/projectReferences.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ts from "../../_namespaces/ts.js";
import { dedent } from "../../_namespaces/Utils.js";
import { jsonToReadableText } from "../helpers.js";
import { libContent } from "../helpers/contents.js";
import { solutionBuildWithBaseline } from "../helpers/solutionBuilder.js";
import {
baselineTsserverLogs,
Expand Down Expand Up @@ -1930,4 +1931,56 @@ const b: B = new B();`,
});
}
});

it("with dts file next to ts file", () => {
const indexDts: File = {
path: "/home/src/projects/project/src/index.d.ts",
content: dedent`
declare global {
interface Window {
electron: ElectronAPI
api: unknown
}
}
`,
};
const host = createServerHost({
[indexDts.path]: indexDts.content,
"/home/src/projects/project/src/index.ts": dedent`
const api = {}
`,
"/home/src/projects/project/tsconfig.json": jsonToReadableText({
include: [
"src/*.d.ts",
],
references: [{ path: "./tsconfig.node.json" }],
}),
"/home/src/projects/project/tsconfig.node.json": jsonToReadableText({
include: ["src/**/*"],
compilerOptions: {
composite: true,
},
}),
[libFile.path]: libContent,
});
const session = new TestSession(host);
openFilesForSession([{ file: indexDts, projectRootPath: "/home/src/projects/project" }], session);
session.executeCommandSeq<ts.server.protocol.DocumentHighlightsRequest>({
command: ts.server.protocol.CommandTypes.DocumentHighlights,
arguments: {
...protocolFileLocationFromSubstring(indexDts, "global"),
filesToSearch: ["/home/src/projects/project/src/index.d.ts"],
},
});
session.executeCommandSeq<ts.server.protocol.EncodedSemanticClassificationsRequest>({
command: ts.server.protocol.CommandTypes.EncodedSemanticClassificationsFull,
arguments: {
file: indexDts.path,
start: 0,
length: indexDts.content.length,
format: "2020",
},
});
baselineTsserverLogs("projectReferences", "with dts file next to ts file", session);
});
});
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ describe("unittests:: tsserver:: projects::", () => {
});
}
catch (e) {
assert.isTrue(e.message.indexOf("Debug Failure. False expression: Found script Info still attached to project") === 0);
session.logger.log(e.message);
}
baselineTsserverLogs("projects", "assert when removing project", session);
});
Expand Down
3 changes: 1 addition & 2 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2756,7 +2756,6 @@ declare namespace ts {
private compilerOptions;
compileOnSaveEnabled: boolean;
protected watchOptions: WatchOptions | undefined;
private rootFiles;
private rootFilesMap;
private program;
private externalFiles;
Expand Down Expand Up @@ -2837,7 +2836,7 @@ declare namespace ts {
private detachScriptInfoIfNotRoot;
isClosed(): boolean;
hasRoots(): boolean;
getRootFiles(): ts.server.NormalizedPath[];
getRootFiles(): NormalizedPath[];
getRootScriptInfos(): ts.server.ScriptInfo[];
getScriptInfos(): ScriptInfo[];
getExcludedFiles(): readonly NormalizedPath[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Info seq [hh:mm:ss:mss] request:
"seq": 1,
"type": "request"
}
Info seq [hh:mm:ss:mss] Debug Failure. False expression.
Verbose Debug Information: {"fileName":"^walkThroughSnippet:/Users/UserName/projects/someProject/out/someFile#1.js","currentDirectory":"/user/username/projects/myproject","hostCurrentDirectory":"/","openKeys":[]}
Dynamic files must always be opened with service's current directory or service should support inferred project per projectRootPath.
Before request

Info seq [hh:mm:ss:mss] request:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ Info seq [hh:mm:ss:mss] Files (4)
Info seq [hh:mm:ss:mss] -----------------------------------------------
Info seq [hh:mm:ss:mss] Open files:
Info seq [hh:mm:ss:mss] FileName: /user/username/projects/project/src/common/input/keyboard.ts ProjectRootPath: undefined
Info seq [hh:mm:ss:mss] Projects: /user/username/projects/project/src/common/tsconfig.json,/user/username/projects/project/src/tsconfig.json
Info seq [hh:mm:ss:mss] Projects: /user/username/projects/project/src/common/tsconfig.json
Info seq [hh:mm:ss:mss] FileName: /user/username/projects/project/src/terminal.ts ProjectRootPath: undefined
Info seq [hh:mm:ss:mss] Projects: /user/username/projects/project/src/tsconfig.json
Info seq [hh:mm:ss:mss] response:
Expand Down Expand Up @@ -777,16 +777,14 @@ ScriptInfos::
version: Text-1
containingProjects: 1
/user/username/projects/project/src/tsconfig.json
/user/username/projects/project/src/common/input/keyboard.test.ts *changed*
/user/username/projects/project/src/common/input/keyboard.test.ts
version: Text-1
containingProjects: 2 *changed*
containingProjects: 1
/user/username/projects/project/src/common/tsconfig.json
/user/username/projects/project/src/tsconfig.json *new*
/user/username/projects/project/src/common/input/keyboard.ts (Open) *changed*
/user/username/projects/project/src/common/input/keyboard.ts (Open)
version: SVC-1-0
containingProjects: 2 *changed*
containingProjects: 1
/user/username/projects/project/src/common/tsconfig.json *default*
/user/username/projects/project/src/tsconfig.json *new*
/user/username/projects/project/src/terminal.ts (Open) *new*
version: SVC-1-0
containingProjects: 1
Expand Down Expand Up @@ -972,9 +970,8 @@ Projects::
projectProgramVersion: 1
documentPositionMappers: 1 *changed*
/user/username/projects/project/out/input/keyboard.d.ts: DocumentPositionMapper1 *new*
originalConfiguredProjects: 2 *changed*
originalConfiguredProjects: 1 *changed*
/user/username/projects/project/src/common/tsconfig.json *new*
/user/username/projects/project/src/tsconfig.json *new*

ScriptInfos::
/a/lib/lib.d.ts
Expand All @@ -1000,14 +997,12 @@ ScriptInfos::
/user/username/projects/project/src/tsconfig.json
/user/username/projects/project/src/common/input/keyboard.test.ts
version: Text-1
containingProjects: 2
containingProjects: 1
/user/username/projects/project/src/common/tsconfig.json
/user/username/projects/project/src/tsconfig.json
/user/username/projects/project/src/common/input/keyboard.ts (Open)
version: SVC-1-0
containingProjects: 2
containingProjects: 1
/user/username/projects/project/src/common/tsconfig.json *default*
/user/username/projects/project/src/tsconfig.json
/user/username/projects/project/src/terminal.ts (Open)
version: SVC-1-0
containingProjects: 1
Expand Down
Loading