Skip to content

Commit dba61c5

Browse files
committed
Allow dynamic files without external project and also use file names starting with ^ as dynamic file
Fixes #21204
1 parent 92d2a25 commit dba61c5

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/harness/unittests/tsserverProjectSystem.ts

+39
Original file line numberDiff line numberDiff line change
@@ -2846,6 +2846,45 @@ namespace ts.projectSystem {
28462846
const options = project.getCompilerOptions();
28472847
assert.equal(options.outDir, "C:/a/b", "");
28482848
});
2849+
2850+
it("dynamic file without external project", () => {
2851+
const file: FileOrFolder = {
2852+
path: "^walkThroughSnippet:/Users/UserName/projects/someProject/out/someFile#1.js",
2853+
content: "var x = 10;"
2854+
};
2855+
const host = createServerHost([libFile], { useCaseSensitiveFileNames: true });
2856+
const projectService = createProjectService(host);
2857+
projectService.setCompilerOptionsForInferredProjects({
2858+
module: ModuleKind.CommonJS,
2859+
allowJs: true,
2860+
allowSyntheticDefaultImports: true,
2861+
allowNonTsExtensions: true
2862+
});
2863+
projectService.openClientFile(file.path, "var x = 10;");
2864+
2865+
projectService.checkNumberOfProjects({ inferredProjects: 1 });
2866+
const project = projectService.inferredProjects[0];
2867+
checkProjectRootFiles(project, [file.path]);
2868+
checkProjectActualFiles(project, [file.path, libFile.path]);
2869+
2870+
assert.strictEqual(projectService.getDefaultProjectForFile(server.toNormalizedPath(file.path), /*ensureProject*/ true), project);
2871+
const indexOfX = file.content.indexOf("x");
2872+
assert.deepEqual(project.getLanguageService(/*ensureSynchronized*/ true).getQuickInfoAtPosition(file.path, indexOfX), {
2873+
kind: ScriptElementKind.variableElement,
2874+
kindModifiers: "",
2875+
textSpan: { start: indexOfX, length: 1 },
2876+
displayParts: [
2877+
{ text: "var", kind: "keyword" },
2878+
{ text: " ", kind: "space" },
2879+
{ text: "x", kind: "localName" },
2880+
{ text: ":", kind: "punctuation" },
2881+
{ text: " ", kind: "space" },
2882+
{ text: "number", kind: "keyword" }
2883+
],
2884+
documentation: [],
2885+
tags: []
2886+
});
2887+
});
28492888
});
28502889

28512890
describe("tsserverProjectSystem Proper errors", () => {

src/server/editorServices.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ namespace ts.server {
898898

899899
const project = this.getOrCreateInferredProjectForProjectRootPathIfEnabled(info, projectRootPath) ||
900900
this.getOrCreateSingleInferredProjectIfEnabled() ||
901-
this.createInferredProject(getDirectoryPath(info.path));
901+
this.createInferredProject(info.isDynamic ? this.currentDirectory : getDirectoryPath(info.path));
902902

903903
project.addRoot(info);
904904
project.updateGraph();
@@ -1655,7 +1655,7 @@ namespace ts.server {
16551655
}
16561656

16571657
private getOrCreateInferredProjectForProjectRootPathIfEnabled(info: ScriptInfo, projectRootPath: NormalizedPath | undefined): InferredProject | undefined {
1658-
if (!this.useInferredProjectPerProjectRoot) {
1658+
if (info.isDynamic || !this.useInferredProjectPerProjectRoot) {
16591659
return undefined;
16601660
}
16611661

src/server/scriptInfo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ namespace ts.server {
196196

197197
/*@internal*/
198198
export function isDynamicFileName(fileName: NormalizedPath) {
199-
return getBaseFileName(fileName)[0] === "^";
199+
return fileName[0] === "^" || getBaseFileName(fileName)[0] === "^";
200200
}
201201

202202
export class ScriptInfo {

0 commit comments

Comments
 (0)