diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 23798635671a8..673db399d0dbe 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3999,16 +3999,6 @@ namespace ts { [option: string]: string[] | boolean | undefined; } - export interface DiscoverTypingsInfo { - fileNames: string[]; // The file names that belong to the same project. - projectRootPath: string; // The path to the project root directory - safeListPath: string; // The path used to retrieve the safe list - packageNameToTypingLocation: Map; // The map of package names to their cached typing locations - typeAcquisition: TypeAcquisition; // Used to customize the type acquisition process - compilerOptions: CompilerOptions; // Used as a source for typing inference - unresolvedImports: ReadonlyArray; // List of unresolved module ids from imports - } - export enum ModuleKind { None = 0, CommonJS = 1, diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index fe7a2095b86f1..90c381868282c 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -63,7 +63,7 @@ namespace ts.projectSystem { readonly globalTypingsCacheLocation: string, throttleLimit: number, installTypingHost: server.ServerHost, - readonly typesRegistry = createMap(), + readonly typesRegistry = createMap>(), log?: TI.Log) { super(installTypingHost, globalTypingsCacheLocation, safeList.path, customTypesMap.path, throttleLimit, log); } @@ -126,6 +126,25 @@ namespace ts.projectSystem { return JSON.stringify({ dependencies }); } + export function createTypesRegistry(...list: string[]): Map> { + const versionMap = { + "latest": "1.3.0", + "ts2.0": "1.0.0", + "ts2.1": "1.0.0", + "ts2.2": "1.2.0", + "ts2.3": "1.3.0", + "ts2.4": "1.3.0", + "ts2.5": "1.3.0", + "ts2.6": "1.3.0", + "ts2.7": "1.3.0" + }; + const map = createMap>(); + for (const l of list) { + map.set(l, versionMap); + } + return map; + } + export function toExternalFile(fileName: string): protocol.ExternalFile { return { fileName }; } @@ -6528,12 +6547,18 @@ namespace ts.projectSystem { }, }) }; + const typingsCachePackageLockJson: FileOrFolder = { + path: `${typingsCache}/package-lock.json`, + content: JSON.stringify({ + dependencies: { + }, + }) + }; - const files = [file, packageJsonInCurrentDirectory, packageJsonOfPkgcurrentdirectory, indexOfPkgcurrentdirectory, typingsCachePackageJson]; + const files = [file, packageJsonInCurrentDirectory, packageJsonOfPkgcurrentdirectory, indexOfPkgcurrentdirectory, typingsCachePackageJson, typingsCachePackageLockJson]; const host = createServerHost(files, { currentDirectory }); - const typesRegistry = createMap(); - typesRegistry.set("pkgcurrentdirectory", void 0); + const typesRegistry = createTypesRegistry("pkgcurrentdirectory"); const typingsInstaller = new TestTypingsInstaller(typingsCache, /*throttleLimit*/ 5, host, typesRegistry); const projectService = createProjectService(host, { typingsInstaller }); diff --git a/src/harness/unittests/typingsInstaller.ts b/src/harness/unittests/typingsInstaller.ts index a84520095a475..b5265c5e5f290 100644 --- a/src/harness/unittests/typingsInstaller.ts +++ b/src/harness/unittests/typingsInstaller.ts @@ -1,6 +1,7 @@ /// /// /// +/// namespace ts.projectSystem { import TI = server.typingsInstaller; @@ -10,15 +11,7 @@ namespace ts.projectSystem { interface InstallerParams { globalTypingsCacheLocation?: string; throttleLimit?: number; - typesRegistry?: Map; - } - - function createTypesRegistry(...list: string[]): Map { - const map = createMap(); - for (const l of list) { - map.set(l, undefined); - } - return map; + typesRegistry?: Map>; } class Installer extends TestTypingsInstaller { @@ -50,7 +43,7 @@ namespace ts.projectSystem { const logs: string[] = []; return { log(message) { - logs.push(message); + logs.push(message); }, finish() { return logs; @@ -1053,6 +1046,142 @@ namespace ts.projectSystem { const version2 = proj.getCachedUnresolvedImportsPerFile_TestOnly().getVersion(); assert.notEqual(version1, version2, "set of unresolved imports should change"); }); + + it("expired cache entry (inferred project, should install typings)", () => { + const file1 = { + path: "/a/b/app.js", + content: "" + }; + const packageJson = { + path: "/a/b/package.json", + content: JSON.stringify({ + name: "test", + dependencies: { + jquery: "^3.1.0" + } + }) + }; + const jquery = { + path: "/a/data/node_modules/@types/jquery/index.d.ts", + content: "declare const $: { x: number }" + }; + const cacheConfig = { + path: "/a/data/package.json", + content: JSON.stringify({ + dependencies: { + "types-registry": "^0.1.317" + }, + devDependencies: { + "@types/jquery": "^1.0.0" + } + }) + }; + const cacheLockConfig = { + path: "/a/data/package-lock.json", + content: JSON.stringify({ + dependencies: { + "@types/jquery": { + version: "1.0.0" + } + } + }) + }; + const host = createServerHost([file1, packageJson, jquery, cacheConfig, cacheLockConfig]); + const installer = new (class extends Installer { + constructor() { + super(host, { typesRegistry: createTypesRegistry("jquery") }); + } + installWorker(_requestId: number, _args: string[], _cwd: string, cb: TI.RequestCompletedAction) { + const installedTypings = ["@types/jquery"]; + const typingFiles = [jquery]; + executeCommand(this, host, installedTypings, typingFiles, cb); + } + })(); + + const projectService = createProjectService(host, { useSingleInferredProject: true, typingsInstaller: installer }); + projectService.openClientFile(file1.path); + + checkNumberOfProjects(projectService, { inferredProjects: 1 }); + const p = projectService.inferredProjects[0]; + checkProjectActualFiles(p, [file1.path]); + + installer.installAll(/*expectedCount*/ 1); + + checkNumberOfProjects(projectService, { inferredProjects: 1 }); + checkProjectActualFiles(p, [file1.path, jquery.path]); + }); + + it("non-expired cache entry (inferred project, should not install typings)", () => { + const file1 = { + path: "/a/b/app.js", + content: "" + }; + const packageJson = { + path: "/a/b/package.json", + content: JSON.stringify({ + name: "test", + dependencies: { + jquery: "^3.1.0" + } + }) + }; + const timestamps = { + path: "/a/data/timestamps.json", + content: JSON.stringify({ + entries: { + "@types/jquery": Date.now() + } + }) + }; + const cacheConfig = { + path: "/a/data/package.json", + content: JSON.stringify({ + dependencies: { + "types-registry": "^0.1.317" + }, + devDependencies: { + "@types/jquery": "^1.3.0" + } + }) + }; + const cacheLockConfig = { + path: "/a/data/package-lock.json", + content: JSON.stringify({ + dependencies: { + "@types/jquery": { + version: "1.3.0" + } + } + }) + }; + const jquery = { + path: "/a/data/node_modules/@types/jquery/index.d.ts", + content: "declare const $: { x: number }" + }; + const host = createServerHost([file1, packageJson, timestamps, cacheConfig, cacheLockConfig, jquery]); + const installer = new (class extends Installer { + constructor() { + super(host, { typesRegistry: createTypesRegistry("jquery") }); + } + installWorker(_requestId: number, _args: string[], _cwd: string, cb: TI.RequestCompletedAction) { + const installedTypings: string[] = []; + const typingFiles: FileOrFolder[] = []; + executeCommand(this, host, installedTypings, typingFiles, cb); + } + })(); + + const projectService = createProjectService(host, { useSingleInferredProject: true, typingsInstaller: installer }); + projectService.openClientFile(file1.path); + + checkNumberOfProjects(projectService, { inferredProjects: 1 }); + const p = projectService.inferredProjects[0]; + checkProjectActualFiles(p, [file1.path]); + + installer.installAll(/*expectedCount*/ 0); + + checkNumberOfProjects(projectService, { inferredProjects: 1 }); + checkProjectActualFiles(p, [file1.path]); + }); }); describe("Validate package name:", () => { @@ -1132,7 +1261,7 @@ namespace ts.projectSystem { const host = createServerHost([app, jquery, chroma]); const logger = trackingLogger(); - const result = JsTyping.discoverTypings(host, logger.log, [app.path, jquery.path, chroma.path], getDirectoryPath(app.path), safeList, emptyMap, { enable: true }, emptyArray); + const result = JsTyping.discoverTypings(host, logger.log, [app.path, jquery.path, chroma.path], getDirectoryPath(app.path), safeList, emptyMap, { enable: true }, emptyArray, emptyMap); const finish = logger.finish(); assert.deepEqual(finish, [ 'Inferred typings from file names: ["jquery","chroma-js"]', @@ -1148,11 +1277,11 @@ namespace ts.projectSystem { content: "" }; const host = createServerHost([f]); - const cache = createMap(); + const cache = createMap(); for (const name of JsTyping.nodeCoreModuleList) { const logger = trackingLogger(); - const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(f.path), emptySafeList, cache, { enable: true }, [name, "somename"]); + const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(f.path), emptySafeList, cache, { enable: true }, [name, "somename"], emptyMap); assert.deepEqual(logger.finish(), [ 'Inferred typings from unresolved imports: ["node","somename"]', 'Result: {"cachedTypingPaths":[],"newTypingNames":["node","somename"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}', @@ -1171,9 +1300,10 @@ namespace ts.projectSystem { content: "" }; const host = createServerHost([f, node]); - const cache = createMapFromTemplate({ node: node.path }); + const cache = createMapFromTemplate({ node: { typingLocation: node.path, version: Semver.parse("1.3.0") } }); + const registry = createTypesRegistry("node"); const logger = trackingLogger(); - const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(f.path), emptySafeList, cache, { enable: true }, ["fs", "bar"]); + const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(f.path), emptySafeList, cache, { enable: true }, ["fs", "bar"], registry); assert.deepEqual(logger.finish(), [ 'Inferred typings from unresolved imports: ["node","bar"]', 'Result: {"cachedTypingPaths":["/a/b/node.d.ts"],"newTypingNames":["bar"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}', @@ -1196,9 +1326,9 @@ namespace ts.projectSystem { content: JSON.stringify({ name: "b" }), }; const host = createServerHost([app, a, b]); - const cache = createMap(); + const cache = createMap(); const logger = trackingLogger(); - const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(app.path), emptySafeList, cache, { enable: true }, /*unresolvedImports*/ []); + const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(app.path), emptySafeList, cache, { enable: true }, /*unresolvedImports*/ [], emptyMap); assert.deepEqual(logger.finish(), [ 'Searching for typing names in /node_modules; all files: ["/node_modules/a/package.json"]', ' Found package names: ["a"]', @@ -1211,6 +1341,94 @@ namespace ts.projectSystem { filesToWatch: ["/bower_components", "/node_modules"], }); }); + + it("should install expired typings", () => { + const app = { + path: "/a/app.js", + content: "" + }; + const cachePath = "/a/cache/"; + const commander = { + path: cachePath + "node_modules/@types/commander/index.d.ts", + content: "export let x: number" + }; + const node = { + path: cachePath + "node_modules/@types/node/index.d.ts", + content: "export let y: number" + }; + const host = createServerHost([app]); + const cache = createMapFromTemplate({ + node: { typingLocation: node.path, version: Semver.parse("1.3.0") }, + commander: { typingLocation: commander.path, version: Semver.parse("1.0.0") } + }); + const registry = createTypesRegistry("node", "commander"); + const logger = trackingLogger(); + const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(app.path), emptySafeList, cache, { enable: true }, ["http", "commander"], registry); + assert.deepEqual(logger.finish(), [ + 'Inferred typings from unresolved imports: ["node","commander"]', + 'Result: {"cachedTypingPaths":["/a/cache/node_modules/@types/node/index.d.ts"],"newTypingNames":["commander"],"filesToWatch":["/a/bower_components","/a/node_modules"]}', + ]); + assert.deepEqual(result.cachedTypingPaths, [node.path]); + assert.deepEqual(result.newTypingNames, ["commander"]); + }); + + it("should install expired typings with prerelease version of tsserver", () => { + const app = { + path: "/a/app.js", + content: "" + }; + const cachePath = "/a/cache/"; + const node = { + path: cachePath + "node_modules/@types/node/index.d.ts", + content: "export let y: number" + }; + const host = createServerHost([app]); + const cache = createMapFromTemplate({ + node: { typingLocation: node.path, version: Semver.parse("1.0.0") } + }); + const registry = createTypesRegistry("node"); + registry.delete(`ts${ts.versionMajorMinor}`); + const logger = trackingLogger(); + const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(app.path), emptySafeList, cache, { enable: true }, ["http"], registry); + assert.deepEqual(logger.finish(), [ + 'Inferred typings from unresolved imports: ["node"]', + 'Result: {"cachedTypingPaths":[],"newTypingNames":["node"],"filesToWatch":["/a/bower_components","/a/node_modules"]}', + ]); + assert.deepEqual(result.cachedTypingPaths, []); + assert.deepEqual(result.newTypingNames, ["node"]); + }); + + + it("prerelease typings are properly handled", () => { + const app = { + path: "/a/app.js", + content: "" + }; + const cachePath = "/a/cache/"; + const commander = { + path: cachePath + "node_modules/@types/commander/index.d.ts", + content: "export let x: number" + }; + const node = { + path: cachePath + "node_modules/@types/node/index.d.ts", + content: "export let y: number" + }; + const host = createServerHost([app]); + const cache = createMapFromTemplate({ + node: { typingLocation: node.path, version: Semver.parse("1.3.0-next.0") }, + commander: { typingLocation: commander.path, version: Semver.parse("1.3.0-next.0") } + }); + const registry = createTypesRegistry("node", "commander"); + registry.get("node")[`ts${ts.versionMajorMinor}`] = "1.3.0-next.1"; + const logger = trackingLogger(); + const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(app.path), emptySafeList, cache, { enable: true }, ["http", "commander"], registry); + assert.deepEqual(logger.finish(), [ + 'Inferred typings from unresolved imports: ["node","commander"]', + 'Result: {"cachedTypingPaths":[],"newTypingNames":["node","commander"],"filesToWatch":["/a/bower_components","/a/node_modules"]}', + ]); + assert.deepEqual(result.cachedTypingPaths, []); + assert.deepEqual(result.newTypingNames, ["node", "commander"]); + }); }); describe("telemetry events", () => { @@ -1273,12 +1491,22 @@ namespace ts.projectSystem { path: "/a/package.json", content: JSON.stringify({ dependencies: { commander: "1.0.0" } }) }; + const packageLockFile = { + path: "/a/cache/package-lock.json", + content: JSON.stringify({ + dependencies: { + "@types/commander": { + version: "1.0.0" + } + } + }) + }; const cachePath = "/a/cache/"; const commander = { path: cachePath + "node_modules/@types/commander/index.d.ts", content: "export let x: number" }; - const host = createServerHost([f1, packageFile]); + const host = createServerHost([f1, packageFile, packageLockFile]); let beginEvent: server.BeginInstallTypes; let endEvent: server.EndInstallTypes; const installer = new (class extends Installer { diff --git a/src/harness/virtualFileSystemWithWatch.ts b/src/harness/virtualFileSystemWithWatch.ts index 921d4674231da..06e1b45a688f4 100644 --- a/src/harness/virtualFileSystemWithWatch.ts +++ b/src/harness/virtualFileSystemWithWatch.ts @@ -383,9 +383,11 @@ interface Array {}` ensureFileOrFolder(fileOrDirectory: FileOrFolder, ignoreWatchInvokedWithTriggerAsFileCreate?: boolean) { if (isString(fileOrDirectory.content)) { const file = this.toFile(fileOrDirectory); - Debug.assert(!this.fs.get(file.path)); - const baseFolder = this.ensureFolder(getDirectoryPath(file.fullPath)); - this.addFileOrFolderInFolder(baseFolder, file, ignoreWatchInvokedWithTriggerAsFileCreate); + // file may already exist when updating existing type declaration file + if (!this.fs.get(file.path)) { + const baseFolder = this.ensureFolder(getDirectoryPath(file.fullPath)); + this.addFileOrFolderInFolder(baseFolder, file, ignoreWatchInvokedWithTriggerAsFileCreate); + } } else { const fullPath = getNormalizedAbsolutePath(fileOrDirectory.path, this.currentDirectory); diff --git a/src/server/server.ts b/src/server/server.ts index 8e53c4d51092a..6d5dfafbe8ec9 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -252,7 +252,7 @@ namespace ts.server { private requestMap = createMap(); // Maps operation ID to newest requestQueue entry with that ID /** We will lazily request the types registry on the first call to `isKnownTypesPackageName` and store it in `typesRegistryCache`. */ private requestedRegistry: boolean; - private typesRegistryCache: Map | undefined; + private typesRegistryCache: Map> | undefined; // This number is essentially arbitrary. Processing more than one typings request // at a time makes sense, but having too many in the pipe results in a hang diff --git a/src/server/types.ts b/src/server/types.ts index 32132ed278b22..6f773955d4528 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -77,7 +77,7 @@ declare namespace ts.server { /* @internal */ export interface TypesRegistryResponse extends TypingInstallerResponse { readonly kind: EventTypesRegistry; - readonly typesRegistry: MapLike; + readonly typesRegistry: MapLike>; } export interface PackageInstalledResponse extends ProjectResponse { diff --git a/src/server/typingsInstaller/nodeTypingsInstaller.ts b/src/server/typingsInstaller/nodeTypingsInstaller.ts index 36f5adab40051..e51ec68561c2f 100644 --- a/src/server/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/server/typingsInstaller/nodeTypingsInstaller.ts @@ -41,15 +41,15 @@ namespace ts.server.typingsInstaller { } interface TypesRegistryFile { - entries: MapLike; + entries: MapLike>; } - function loadTypesRegistryFile(typesRegistryFilePath: string, host: InstallTypingHost, log: Log): Map { + function loadTypesRegistryFile(typesRegistryFilePath: string, host: InstallTypingHost, log: Log): Map> { if (!host.fileExists(typesRegistryFilePath)) { if (log.isEnabled()) { log.writeLine(`Types registry file '${typesRegistryFilePath}' does not exist`); } - return createMap(); + return createMap>(); } try { const content = JSON.parse(host.readFile(typesRegistryFilePath)); @@ -59,7 +59,7 @@ namespace ts.server.typingsInstaller { if (log.isEnabled()) { log.writeLine(`Error when loading types registry file '${typesRegistryFilePath}': ${(e).message}, ${(e).stack}`); } - return createMap(); + return createMap>(); } } @@ -77,7 +77,7 @@ namespace ts.server.typingsInstaller { export class NodeTypingsInstaller extends TypingsInstaller { private readonly nodeExecSync: ExecSync; private readonly npmPath: string; - readonly typesRegistry: Map; + readonly typesRegistry: Map>; private delayedInitializationError: InitializationFailedResponse | undefined; @@ -141,7 +141,7 @@ namespace ts.server.typingsInstaller { this.closeProject(req); break; case "typesRegistry": { - const typesRegistry: { [key: string]: void } = {}; + const typesRegistry: { [key: string]: MapLike } = {}; this.typesRegistry.forEach((value, key) => { typesRegistry[key] = value; }); diff --git a/src/server/typingsInstaller/typingsInstaller.ts b/src/server/typingsInstaller/typingsInstaller.ts index 283770d1dc862..465f281006e4d 100644 --- a/src/server/typingsInstaller/typingsInstaller.ts +++ b/src/server/typingsInstaller/typingsInstaller.ts @@ -1,6 +1,7 @@ /// /// /// +/// /// /// @@ -9,6 +10,10 @@ namespace ts.server.typingsInstaller { devDependencies: MapLike; } + interface NpmLock { + dependencies: { [packageName: string]: { version: string } }; + } + export interface Log { isEnabled(): boolean; writeLine(text: string): void; @@ -42,7 +47,7 @@ namespace ts.server.typingsInstaller { } export abstract class TypingsInstaller { - private readonly packageNameToTypingLocation: Map = createMap(); + private readonly packageNameToTypingLocation: Map = createMap(); private readonly missingTypingsSet: Map = createMap(); private readonly knownCachesSet: Map = createMap(); private readonly projectWatchers: Map = createMap(); @@ -52,7 +57,7 @@ namespace ts.server.typingsInstaller { private installRunCount = 1; private inFlightRequestCount = 0; - abstract readonly typesRegistry: Map; + abstract readonly typesRegistry: Map>; constructor( protected readonly installTypingHost: InstallTypingHost, @@ -117,7 +122,8 @@ namespace ts.server.typingsInstaller { this.safeList, this.packageNameToTypingLocation, req.typeAcquisition, - req.unresolvedImports); + req.unresolvedImports, + this.typesRegistry); if (this.log.isEnabled()) { this.log.writeLine(`Finished typings discovery: ${JSON.stringify(discoverTypingsResult)}`); @@ -156,23 +162,30 @@ namespace ts.server.typingsInstaller { if (this.log.isEnabled()) { this.log.writeLine(`Processing cache location '${cacheLocation}'`); } - if (this.knownCachesSet.get(cacheLocation)) { + if (this.knownCachesSet.has(cacheLocation)) { if (this.log.isEnabled()) { this.log.writeLine(`Cache location was already processed...`); } return; } const packageJson = combinePaths(cacheLocation, "package.json"); + const packageLockJson = combinePaths(cacheLocation, "package-lock.json"); if (this.log.isEnabled()) { this.log.writeLine(`Trying to find '${packageJson}'...`); } - if (this.installTypingHost.fileExists(packageJson)) { + if (this.installTypingHost.fileExists(packageJson) && this.installTypingHost.fileExists(packageLockJson)) { const npmConfig = JSON.parse(this.installTypingHost.readFile(packageJson)); + const npmLock = JSON.parse(this.installTypingHost.readFile(packageLockJson)); if (this.log.isEnabled()) { this.log.writeLine(`Loaded content of '${packageJson}': ${JSON.stringify(npmConfig)}`); + this.log.writeLine(`Loaded content of '${packageLockJson}'`); } - if (npmConfig.devDependencies) { + if (npmConfig.devDependencies && npmLock.dependencies) { for (const key in npmConfig.devDependencies) { + if (!hasProperty(npmLock.dependencies, key)) { + // if package in package.json but not package-lock.json, skip adding to cache so it is reinstalled on next use + continue; + } // key is @types/ const packageName = getBaseFileName(key); if (!packageName) { @@ -184,10 +197,11 @@ namespace ts.server.typingsInstaller { continue; } const existingTypingFile = this.packageNameToTypingLocation.get(packageName); - if (existingTypingFile === typingFile) { - continue; - } if (existingTypingFile) { + if (existingTypingFile.typingLocation === typingFile) { + continue; + } + if (this.log.isEnabled()) { this.log.writeLine(`New typing for package ${packageName} from '${typingFile}' conflicts with existing typing file '${existingTypingFile}'`); } @@ -195,7 +209,11 @@ namespace ts.server.typingsInstaller { if (this.log.isEnabled()) { this.log.writeLine(`Adding entry into typings cache: '${packageName}' => '${typingFile}'`); } - this.packageNameToTypingLocation.set(packageName, typingFile); + const info = getProperty(npmLock.dependencies, key); + const version = info && info.version; + const semver = Semver.parse(version); + const newTyping: JsTyping.CachedTyping = { typingLocation: typingFile, version: semver }; + this.packageNameToTypingLocation.set(packageName, newTyping); } } } @@ -211,10 +229,6 @@ namespace ts.server.typingsInstaller { if (this.log.isEnabled()) this.log.writeLine(`'${typing}' is in missingTypingsSet - skipping...`); return false; } - if (this.packageNameToTypingLocation.get(typing)) { - if (this.log.isEnabled()) this.log.writeLine(`'${typing}' already has a typing - skipping...`); - return false; - } const validationResult = JsTyping.validatePackageName(typing); if (validationResult !== JsTyping.PackageNameValidationResult.Ok) { // add typing name to missing set so we won't process it again @@ -226,6 +240,10 @@ namespace ts.server.typingsInstaller { if (this.log.isEnabled()) this.log.writeLine(`Entry for package '${typing}' does not exist in local types registry - skipping...`); return false; } + if (this.packageNameToTypingLocation.get(typing) && JsTyping.isTypingUpToDate(this.packageNameToTypingLocation.get(typing), this.typesRegistry.get(typing))) { + if (this.log.isEnabled()) this.log.writeLine(`'${typing}' already has an up-to-date typing - skipping...`); + return false; + } return true; }); } @@ -294,9 +312,12 @@ namespace ts.server.typingsInstaller { this.missingTypingsSet.set(packageName, true); continue; } - if (!this.packageNameToTypingLocation.has(packageName)) { - this.packageNameToTypingLocation.set(packageName, typingFile); - } + + // packageName is guaranteed to exist in typesRegistry by filterTypings + const distTags = this.typesRegistry.get(packageName); + const newVersion = Semver.parse(distTags[`ts${ts.versionMajorMinor}`] || distTags[latestDistTag]); + const newTyping: JsTyping.CachedTyping = { typingLocation: typingFile, version: newVersion }; + this.packageNameToTypingLocation.set(packageName, newTyping); installedTypingFiles.push(typingFile); } if (this.log.isEnabled()) { @@ -390,4 +411,6 @@ namespace ts.server.typingsInstaller { export function typingsName(packageName: string): string { return `@types/${packageName}@ts${versionMajorMinor}`; } + + const latestDistTag = "latest"; } \ No newline at end of file diff --git a/src/services/jsTyping.ts b/src/services/jsTyping.ts index 1c2f87fa428c5..bd6c2e5cb9f9c 100644 --- a/src/services/jsTyping.ts +++ b/src/services/jsTyping.ts @@ -4,6 +4,7 @@ /// /// /// +/// /* @internal */ namespace ts.JsTyping { @@ -26,6 +27,17 @@ namespace ts.JsTyping { typings?: string; } + export interface CachedTyping { + typingLocation: string; + version: Semver; + } + + /* @internal */ + export function isTypingUpToDate(cachedTyping: JsTyping.CachedTyping, availableTypingVersions: MapLike) { + const availableVersion = Semver.parse(getProperty(availableTypingVersions, `ts${ts.versionMajorMinor}`) || getProperty(availableTypingVersions, "latest")); + return !availableVersion.greaterThan(cachedTyping.version); + } + /* @internal */ export const nodeCoreModuleList: ReadonlyArray = [ "buffer", "querystring", "events", "http", "cluster", @@ -60,7 +72,7 @@ namespace ts.JsTyping { * @param fileNames are the file names that belong to the same project * @param projectRootPath is the path to the project root directory * @param safeListPath is the path used to retrieve the safe list - * @param packageNameToTypingLocation is the map of package names to their cached typing locations + * @param packageNameToTypingLocation is the map of package names to their cached typing locations and installed versions * @param typeAcquisition is used to customize the typing acquisition process * @param compilerOptions are used as a source for typing inference */ @@ -70,9 +82,10 @@ namespace ts.JsTyping { fileNames: string[], projectRootPath: Path, safeList: SafeList, - packageNameToTypingLocation: ReadonlyMap, + packageNameToTypingLocation: ReadonlyMap, typeAcquisition: TypeAcquisition, - unresolvedImports: ReadonlyArray): + unresolvedImports: ReadonlyArray, + typesRegistry: ReadonlyMap>): { cachedTypingPaths: string[], newTypingNames: string[], filesToWatch: string[] } { if (!typeAcquisition || !typeAcquisition.enable) { @@ -122,9 +135,9 @@ namespace ts.JsTyping { addInferredTypings(module, "Inferred typings from unresolved imports"); } // Add the cached typing locations for inferred typings that are already installed - packageNameToTypingLocation.forEach((typingLocation, name) => { - if (inferredTypings.has(name) && inferredTypings.get(name) === undefined) { - inferredTypings.set(name, typingLocation); + packageNameToTypingLocation.forEach((typing, name) => { + if (inferredTypings.has(name) && inferredTypings.get(name) === undefined && isTypingUpToDate(typing, typesRegistry.get(name))) { + inferredTypings.set(name, typing.typingLocation); } }); diff --git a/src/services/semver.ts b/src/services/semver.ts new file mode 100644 index 0000000000000..1c58da8c8f7a7 --- /dev/null +++ b/src/services/semver.ts @@ -0,0 +1,61 @@ +/* @internal */ +namespace ts { + function stringToInt(str: string): number { + const n = parseInt(str, 10); + if (isNaN(n)) { + throw new Error(`Error in parseInt(${JSON.stringify(str)})`); + } + return n; + } + + const isPrereleaseRegex = /^(.*)-next.\d+/; + const prereleaseSemverRegex = /^(\d+)\.(\d+)\.0-next.(\d+)$/; + const semverRegex = /^(\d+)\.(\d+)\.(\d+)$/; + + export class Semver { + static parse(semver: string): Semver { + const isPrerelease = isPrereleaseRegex.test(semver); + const result = Semver.tryParse(semver, isPrerelease); + if (!result) { + throw new Error(`Unexpected semver: ${semver} (isPrerelease: ${isPrerelease})`); + } + return result; + } + + static fromRaw({ major, minor, patch, isPrerelease }: Semver): Semver { + return new Semver(major, minor, patch, isPrerelease); + } + + // This must parse the output of `versionString`. + private static tryParse(semver: string, isPrerelease: boolean): Semver | undefined { + // Per the semver spec : + // "A normal version number MUST take the form X.Y.Z where X, Y, and Z are non-negative integers, and MUST NOT contain leading zeroes." + const rgx = isPrerelease ? prereleaseSemverRegex : semverRegex; + const match = rgx.exec(semver); + return match ? new Semver(stringToInt(match[1]), stringToInt(match[2]), stringToInt(match[3]), isPrerelease) : undefined; + } + + private constructor( + readonly major: number, readonly minor: number, readonly patch: number, + /** + * If true, this is `major.minor.0-next.patch`. + * If false, this is `major.minor.patch`. + */ + readonly isPrerelease: boolean) { } + + get versionString(): string { + return this.isPrerelease ? `${this.major}.${this.minor}.0-next.${this.patch}` : `${this.major}.${this.minor}.${this.patch}`; + } + + equals(sem: Semver): boolean { + return this.major === sem.major && this.minor === sem.minor && this.patch === sem.patch && this.isPrerelease === sem.isPrerelease; + } + + greaterThan(sem: Semver): boolean { + return this.major > sem.major || this.major === sem.major + && (this.minor > sem.minor || this.minor === sem.minor + && (!this.isPrerelease && sem.isPrerelease || this.isPrerelease === sem.isPrerelease + && this.patch > sem.patch)); + } + } +} \ No newline at end of file diff --git a/src/services/shims.ts b/src/services/shims.ts index fda698785b35e..1b2f84035c0d6 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -24,6 +24,17 @@ let debugObjectHost: { CollectGarbage(): void } = (function (this: any) { return /* @internal */ namespace ts { + interface DiscoverTypingsInfo { + fileNames: string[]; // The file names that belong to the same project. + projectRootPath: string; // The path to the project root directory + safeListPath: string; // The path used to retrieve the safe list + packageNameToTypingLocation: Map; // The map of package names to their cached typing locations and installed versions + typeAcquisition: TypeAcquisition; // Used to customize the type acquisition process + compilerOptions: CompilerOptions; // Used as a source for typing inference + unresolvedImports: ReadonlyArray; // List of unresolved module ids from imports + typesRegistry: ReadonlyMap>; // The map of available typings in npm to maps of TS versions to their latest supported versions + } + export interface ScriptSnapshotShim { /** Gets a portion of the script snapshot specified by [start, end). */ getText(start: number, end: number): string; @@ -1161,7 +1172,8 @@ namespace ts { this.safeList, info.packageNameToTypingLocation, info.typeAcquisition, - info.unresolvedImports); + info.unresolvedImports, + info.typesRegistry); }); } } diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index d73014a93a24a..a0a81f0042cf9 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -61,6 +61,7 @@ "services.ts", "transform.ts", "transpile.ts", + "semver.ts", "shims.ts", "signatureHelp.ts", "symbolDisplay.ts", diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 3eabea4435a54..d5a5c7a1a7231 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2308,15 +2308,6 @@ declare namespace ts { exclude?: string[]; [option: string]: string[] | boolean | undefined; } - interface DiscoverTypingsInfo { - fileNames: string[]; - projectRootPath: string; - safeListPath: string; - packageNameToTypingLocation: Map; - typeAcquisition: TypeAcquisition; - compilerOptions: CompilerOptions; - unresolvedImports: ReadonlyArray; - } enum ModuleKind { None = 0, CommonJS = 1, diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 630b7a08a28db..fef3baf9e3680 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2308,15 +2308,6 @@ declare namespace ts { exclude?: string[]; [option: string]: string[] | boolean | undefined; } - interface DiscoverTypingsInfo { - fileNames: string[]; - projectRootPath: string; - safeListPath: string; - packageNameToTypingLocation: Map; - typeAcquisition: TypeAcquisition; - compilerOptions: CompilerOptions; - unresolvedImports: ReadonlyArray; - } enum ModuleKind { None = 0, CommonJS = 1,