Skip to content

Commit 3bf21be

Browse files
Merge pull request #24655 from uniqueiniquity/port-ti-changes
Port typings installer changes
2 parents be50eea + fd7411b commit 3bf21be

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/harness/unittests/typingsInstaller.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,27 @@ namespace ts.projectSystem {
13401340
assert.deepEqual(result.newTypingNames, ["bar"]);
13411341
});
13421342

1343+
it("should gracefully handle packages that have been removed from the types-registry", () => {
1344+
const f = {
1345+
path: "/a/b/app.js",
1346+
content: ""
1347+
};
1348+
const node = {
1349+
path: "/a/b/node.d.ts",
1350+
content: ""
1351+
};
1352+
const host = createServerHost([f, node]);
1353+
const cache = createMapFromTemplate<JsTyping.CachedTyping>({ node: { typingLocation: node.path, version: Semver.parse("1.3.0") } });
1354+
const logger = trackingLogger();
1355+
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), emptySafeList, cache, { enable: true }, ["fs", "bar"], emptyMap);
1356+
assert.deepEqual(logger.finish(), [
1357+
'Inferred typings from unresolved imports: ["node","bar"]',
1358+
'Result: {"cachedTypingPaths":[],"newTypingNames":["node","bar"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
1359+
]);
1360+
assert.deepEqual(result.cachedTypingPaths, []);
1361+
assert.deepEqual(result.newTypingNames, ["node", "bar"]);
1362+
});
1363+
13431364
it("should search only 2 levels deep", () => {
13441365
const app = {
13451366
path: "/app.js",

src/server/typingsInstaller/nodeTypingsInstaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ namespace ts.server.typingsInstaller {
106106
if (this.log.isEnabled()) {
107107
this.log.writeLine(`Updating ${typesRegistryPackageName} npm package...`);
108108
}
109-
this.execSyncAndLog(`${this.npmPath} install --ignore-scripts ${typesRegistryPackageName}`, { cwd: globalTypingsCacheLocation });
109+
this.execSyncAndLog(`${this.npmPath} install --ignore-scripts ${typesRegistryPackageName}@${this.latestDistTag}`, { cwd: globalTypingsCacheLocation });
110110
if (this.log.isEnabled()) {
111111
this.log.writeLine(`Updated ${typesRegistryPackageName} npm package`);
112112
}

src/server/typingsInstaller/typingsInstaller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ namespace ts.server.typingsInstaller {
349349
}
350350

351351
// packageName is guaranteed to exist in typesRegistry by filterTypings
352-
const distTags = this.typesRegistry.get(packageName);
353-
const newVersion = Semver.parse(distTags[`ts${versionMajorMinor}`] || distTags[latestDistTag]);
352+
const distTags = this.typesRegistry.get(packageName)!;
353+
const newVersion = Semver.parse(distTags[`ts${versionMajorMinor}`] || distTags[this.latestDistTag]);
354354
const newTyping: JsTyping.CachedTyping = { typingLocation: typingFile, version: newVersion };
355355
this.packageNameToTypingLocation.set(packageName, newTyping);
356356
installedTypingFiles.push(typingFile);
@@ -523,12 +523,12 @@ namespace ts.server.typingsInstaller {
523523

524524
protected abstract installWorker(requestId: number, packageNames: string[], cwd: string, onRequestCompleted: RequestCompletedAction): void;
525525
protected abstract sendResponse(response: SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes): void;
526+
527+
protected readonly latestDistTag = "latest";
526528
}
527529

528530
/* @internal */
529531
export function typingsName(packageName: string): string {
530532
return `@types/${packageName}@ts${versionMajorMinor}`;
531533
}
532-
533-
const latestDistTag = "latest";
534534
}

src/services/jsTyping.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ namespace ts.JsTyping {
160160
}
161161
// Add the cached typing locations for inferred typings that are already installed
162162
packageNameToTypingLocation.forEach((typing, name) => {
163-
if (inferredTypings.has(name) && inferredTypings.get(name) === undefined && isTypingUpToDate(typing, typesRegistry.get(name))) {
163+
const registryEntry = typesRegistry.get(name);
164+
if (inferredTypings.has(name) && inferredTypings.get(name) === undefined && registryEntry !== undefined && isTypingUpToDate(typing, registryEntry)) {
164165
inferredTypings.set(name, typing.typingLocation);
165166
}
166167
});

0 commit comments

Comments
 (0)