• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts.projectSystem {
2    describe("unittests:: tsserver:: searching for config file", () => {
3        it("should stop at projectRootPath if given", () => {
4            const f1 = {
5                path: "/a/file1.ts",
6                content: ""
7            };
8            const configFile = {
9                path: "/tsconfig.json",
10                content: "{}"
11            };
12            const host = createServerHost([f1, configFile]);
13            const service = createProjectService(host);
14            service.openClientFile(f1.path, /*fileContent*/ undefined, /*scriptKind*/ undefined, "/a");
15
16            checkNumberOfConfiguredProjects(service, 0);
17            checkNumberOfInferredProjects(service, 1);
18
19            service.closeClientFile(f1.path);
20            service.openClientFile(f1.path);
21            checkNumberOfConfiguredProjects(service, 1);
22            checkNumberOfInferredProjects(service, 0);
23        });
24
25        it("should use projectRootPath when searching for inferred project again", () => {
26            const projectDir = "/a/b/projects/project";
27            const configFileLocation = `${projectDir}/src`;
28            const f1 = {
29                path: `${configFileLocation}/file1.ts`,
30                content: ""
31            };
32            const configFile = {
33                path: `${configFileLocation}/tsconfig.json`,
34                content: "{}"
35            };
36            const configFile2 = {
37                path: "/a/b/projects/tsconfig.json",
38                content: "{}"
39            };
40            const host = createServerHost([f1, libFile, configFile, configFile2]);
41            const service = createProjectService(host, { logger: createLoggerWithInMemoryLogs(host) });
42            service.openClientFile(f1.path, /*fileContent*/ undefined, /*scriptKind*/ undefined, projectDir);
43
44            // Delete config file - should create inferred project and not configured project
45            host.deleteFile(configFile.path);
46            host.runQueuedTimeoutCallbacks();
47            checkNumberOfProjects(service, { inferredProjects: 1 });
48            baselineTsserverLogs("configFileSearch", "should use projectRootPath when searching for inferred project again", service);
49        });
50
51        it("should use projectRootPath when searching for inferred project again 2", () => {
52            const projectDir = "/a/b/projects/project";
53            const configFileLocation = `${projectDir}/src`;
54            const f1 = {
55                path: `${configFileLocation}/file1.ts`,
56                content: ""
57            };
58            const configFile = {
59                path: `${configFileLocation}/tsconfig.json`,
60                content: "{}"
61            };
62            const configFile2 = {
63                path: "/a/b/projects/tsconfig.json",
64                content: "{}"
65            };
66            const host = createServerHost([f1, libFile, configFile, configFile2]);
67            const service = createProjectService(host, {
68                useSingleInferredProject: true,
69                useInferredProjectPerProjectRoot: true,
70                logger: createLoggerWithInMemoryLogs(host),
71            });
72            service.openClientFile(f1.path, /*fileContent*/ undefined, /*scriptKind*/ undefined, projectDir);
73
74            // Delete config file - should create inferred project with project root path set
75            host.deleteFile(configFile.path);
76            host.runQueuedTimeoutCallbacks();
77            baselineTsserverLogs("configFileSearch", "should use projectRootPath when searching for inferred project again 2", service);
78        });
79
80        describe("when the opened file is not from project root", () => {
81            const projectRoot = "/a/b/projects/project";
82            const file: File = {
83                path: `${projectRoot}/src/index.ts`,
84                content: "let y = 10"
85            };
86            const tsconfig: File = {
87                path: `${projectRoot}/tsconfig.json`,
88                content: "{}"
89            };
90            function openClientFile(files: File[]) {
91                const host = createServerHost(files);
92                const projectService = createProjectService(host, { logger: createLoggerWithInMemoryLogs(host) });
93                projectService.openClientFile(file.path, /*fileContent*/ undefined, /*scriptKind*/ undefined, "/a/b/projects/proj");
94                return { host, projectService };
95            }
96
97            it("tsconfig for the file exists", () => {
98                const { host, projectService } = openClientFile([file, libFile, tsconfig]);
99
100                host.deleteFile(tsconfig.path);
101                host.runQueuedTimeoutCallbacks();
102
103                host.writeFile(tsconfig.path, tsconfig.content);
104                host.runQueuedTimeoutCallbacks();
105
106                baselineTsserverLogs("configFileSearch", "tsconfig for the file exists", projectService);
107            });
108
109            it("tsconfig for the file does not exist", () => {
110                const { host, projectService } = openClientFile([file, libFile]);
111
112                host.writeFile(tsconfig.path, tsconfig.content);
113                host.runQueuedTimeoutCallbacks();
114
115                host.deleteFile(tsconfig.path);
116                host.runQueuedTimeoutCallbacks();
117
118                baselineTsserverLogs("configFileSearch", "tsconfig for the file does not exist", projectService);
119            });
120        });
121
122        describe("should not search and watch config files from directories that cannot be watched", () => {
123            function verifyConfigFileWatch(scenario: string, projectRootPath: string | undefined) {
124                it(scenario, () => {
125                    const path = `/root/teams/VSCode68/Shared Documents/General/jt-ts-test-workspace/x.js`;
126                    const host = createServerHost([libFile, { path, content: "const x = 10" }], { useCaseSensitiveFileNames: true });
127                    const service = createProjectService(host, { logger: createLoggerWithInMemoryLogs(host) });
128                    service.openClientFile(path, /*fileContent*/ undefined, /*scriptKind*/ undefined, projectRootPath);
129                    baselineTsserverLogs("configFileSearch", scenario, service);
130                });
131            }
132            verifyConfigFileWatch("when projectRootPath is not present", /*projectRootPath*/ undefined);
133            verifyConfigFileWatch("when projectRootPath is present but file is not from project root", "/a/b");
134        });
135    });
136}
137