Skip to content

Commit ef5b171

Browse files
authored
Merge pull request #21107 from Microsoft/inferredProjectTypings
Use the currentDirectory of the project as projectRootPath in the typings request
2 parents 1023683 + 667751d commit ef5b171

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6488,4 +6488,76 @@ 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+
// Verify that the pkgcurrentdirectory from the current directory isnt picked up
6560+
checkProjectActualFiles(project, [file.path]);
6561+
});
6562+
});
64916563
}

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));

src/server/utilities.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,14 @@ namespace ts.server {
3434
export type Types = Msg;
3535
}
3636

37-
function getProjectRootPath(project: Project): Path {
38-
switch (project.projectKind) {
39-
case ProjectKind.Configured:
40-
return <Path>getDirectoryPath(project.getProjectName());
41-
case ProjectKind.Inferred:
42-
// TODO: fixme
43-
return <Path>"";
44-
case ProjectKind.External:
45-
const projectName = normalizeSlashes(project.getProjectName());
46-
return <Path>getDirectoryPath(projectName);
47-
}
48-
}
49-
5037
export function createInstallTypingsRequest(project: Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>, cachePath?: string): DiscoverTypings {
5138
return {
5239
projectName: project.getProjectName(),
5340
fileNames: project.getFileNames(/*excludeFilesFromExternalLibraries*/ true, /*excludeConfigFiles*/ true).concat(project.getExcludedFiles() as NormalizedPath[]),
5441
compilerOptions: project.getCompilationSettings(),
5542
typeAcquisition,
5643
unresolvedImports,
57-
projectRootPath: getProjectRootPath(project),
44+
projectRootPath: project.getCurrentDirectory() as Path,
5845
cachePath,
5946
kind: "discover"
6047
};

0 commit comments

Comments
 (0)