Skip to content

Commit 90a1df9

Browse files
committed
Add test for failure to use correct current directory in inferred project
Test for #21040
1 parent 8bce69e commit 90a1df9

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6488,4 +6488,73 @@ namespace ts.projectSystem {
64886488
verifyWatchedDirectories(/*useProjectAtRoot*/ false);
64896489
});
64906490
});
6491+
6492+
describe("tsserverProjectSystem typingsInstaller on inferred Project", () => {
6493+
it("when projectRootPath is provided", () => {
6494+
const projects = "/users/username/projects";
6495+
const projectRootPath = `${projects}/san2`;
6496+
const file: FileOrFolder = {
6497+
path: `${projectRootPath}/x.js`,
6498+
content: "const aaaaaaav = 1;"
6499+
};
6500+
6501+
const currentDirectory = `${projects}/anotherProject`;
6502+
const packageJsonInCurrentDirectory: FileOrFolder = {
6503+
path: `${currentDirectory}/package.json`,
6504+
content: JSON.stringify({
6505+
devDependencies: {
6506+
"pkgcurrentdirectory": ""
6507+
},
6508+
})
6509+
};
6510+
const packageJsonOfPkgcurrentdirectory: FileOrFolder = {
6511+
path: `${currentDirectory}/node_modules/pkgcurrentdirectory/package.json`,
6512+
content: JSON.stringify({
6513+
name: "pkgcurrentdirectory",
6514+
main: "index.js",
6515+
typings: "index.d.ts"
6516+
})
6517+
};
6518+
const indexOfPkgcurrentdirectory: FileOrFolder = {
6519+
path: `${currentDirectory}/node_modules/pkgcurrentdirectory/index.d.ts`,
6520+
content: "export function foo() { }"
6521+
};
6522+
6523+
const typingsCache = `/users/username/Library/Caches/typescript/2.7`;
6524+
const typingsCachePackageJson: FileOrFolder = {
6525+
path: `${typingsCache}/package.json`,
6526+
content: JSON.stringify({
6527+
devDependencies: {
6528+
},
6529+
})
6530+
};
6531+
6532+
const files = [file, packageJsonInCurrentDirectory, packageJsonOfPkgcurrentdirectory, indexOfPkgcurrentdirectory, typingsCachePackageJson];
6533+
const host = createServerHost(files, { currentDirectory });
6534+
6535+
const typesRegistry = createMap<void>();
6536+
typesRegistry.set("pkgcurrentdirectory", void 0);
6537+
const typingsInstaller = new TestTypingsInstaller(typingsCache, /*throttleLimit*/ 5, host, typesRegistry);
6538+
6539+
const projectService = createProjectService(host, { typingsInstaller });
6540+
6541+
projectService.setCompilerOptionsForInferredProjects({
6542+
module: ModuleKind.CommonJS,
6543+
target: ScriptTarget.ES2016,
6544+
jsx: JsxEmit.Preserve,
6545+
experimentalDecorators: true,
6546+
allowJs: true,
6547+
allowSyntheticDefaultImports: true,
6548+
allowNonTsExtensions: true
6549+
});
6550+
6551+
projectService.openClientFile(file.path, file.content, ScriptKind.JS, projectRootPath);
6552+
6553+
const project = projectService.inferredProjects[0];
6554+
assert.isDefined(project);
6555+
6556+
// Ensure that we use result from types cache when getting ls
6557+
assert.isDefined(project.getLanguageService());
6558+
});
6559+
});
64916560
}

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ interface Array<T> {}`
547547
}
548548

549549
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[] {
550-
return ts.matchFiles(this.toNormalizedAbsolutePath(path), extensions, exclude, include, this.useCaseSensitiveFileNames, this.getCurrentDirectory(), depth, (dir) => {
550+
return ts.matchFiles(path, extensions, exclude, include, this.useCaseSensitiveFileNames, this.getCurrentDirectory(), depth, (dir) => {
551551
const directories: string[] = [];
552552
const files: string[] = [];
553553
const dirEntry = this.fs.get(this.toPath(dir));

0 commit comments

Comments
 (0)