Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export TypingsInstaller from tsserverlibrary #53394

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/jsTyping/jsTyping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
versionMajorMinor,
} from "./_namespaces/ts";

/** @internal */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of removing internal can we re-export it in server as some other name so that typescript.d.ts doesnt change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not actually work under the naive dts bundler, actually, but we'd have to try.

export interface TypingResolutionHost {
directoryExists(path: string): boolean;
fileExists(fileName: string): boolean;
Expand Down
1 change: 0 additions & 1 deletion src/jsTyping/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export interface EndInstallTypes extends InstallTypes {
readonly installSuccess: boolean;
}

/** @internal */
export interface InstallTypingHost extends JsTyping.TypingResolutionHost {
useCaseSensitiveFileNames: boolean;
writeFile(path: string, content: string): void;
Expand Down
1 change: 1 addition & 0 deletions src/server/_namespaces/ts.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Generated file to emulate the ts.server namespace. */

export * from "../../jsTyping/_namespaces/ts.server";
export * from "../../typingsInstallerCore/_namespaces/ts.server";
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
export * from "../types";
export * from "../utilitiesPublic";
export * from "../utilities";
Expand Down
1 change: 1 addition & 0 deletions src/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"references": [
{ "path": "../compiler" },
{ "path": "../jsTyping" },
{ "path": "../typingsInstallerCore" },
{ "path": "../services" },
{ "path": "../deprecatedCompat" }
],
Expand Down
4 changes: 3 additions & 1 deletion src/typingsInstallerCore/typingsInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export function getNpmCommandForInstallation(npmPath: string, tsVersion: string,
}

export type RequestCompletedAction = (success: boolean) => void;
interface PendingRequest {

export interface PendingRequest {
requestId: number;
packageNames: string[];
cwd: string;
Expand Down Expand Up @@ -146,6 +147,7 @@ export abstract class TypingsInstaller {
private readonly knownCachesSet = new Set<string>();
private readonly projectWatchers = new Map<string, ProjectWatchers>();
private safeList: JsTyping.SafeList | undefined;
/** @internal */
readonly pendingRunRequests: PendingRequest[] = [];
private readonly toCanonicalFileName: GetCanonicalFileName;
private readonly globalCachePackageJsonPath: string;
Expand Down
64 changes: 64 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ declare namespace ts {
readonly kind: EventEndInstallTypes;
readonly installSuccess: boolean;
}
interface InstallTypingHost extends JsTyping.TypingResolutionHost {
useCaseSensitiveFileNames: boolean;
writeFile(path: string, content: string): void;
createDirectory(path: string): void;
getCurrentDirectory?(): string;
watchFile?(path: string, callback: FileWatcherCallback, pollingInterval?: number, options?: WatchOptions): FileWatcher;
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean, options?: WatchOptions): FileWatcher;
}
interface SetTypings extends ProjectResponse {
readonly typeAcquisition: TypeAcquisition;
readonly compilerOptions: CompilerOptions;
Expand Down Expand Up @@ -2965,6 +2973,54 @@ declare namespace ts {
bigintLiteral = 25
}
}
namespace typingsInstaller {
interface Log {
isEnabled(): boolean;
writeLine(text: string): void;
}
type RequestCompletedAction = (success: boolean) => void;
interface PendingRequest {
requestId: number;
packageNames: string[];
cwd: string;
onRequestCompleted: RequestCompletedAction;
}
abstract class TypingsInstaller {
protected readonly installTypingHost: InstallTypingHost;
private readonly globalCachePath;
private readonly safeListPath;
private readonly typesMapLocation;
private readonly throttleLimit;
protected readonly log: Log;
private readonly packageNameToTypingLocation;
private readonly missingTypingsSet;
private readonly knownCachesSet;
private readonly projectWatchers;
private safeList;
private readonly toCanonicalFileName;
private readonly globalCachePackageJsonPath;
private installRunCount;
private inFlightRequestCount;
abstract readonly typesRegistry: Map<string, MapLike<string>>;
constructor(installTypingHost: InstallTypingHost, globalCachePath: string, safeListPath: Path, typesMapLocation: Path, throttleLimit: number, log?: Log);
closeProject(req: CloseProject): void;
private closeWatchers;
install(req: DiscoverTypings): void;
private initializeSafeList;
private processCacheLocation;
private filterTypings;
protected ensurePackageDirectoryExists(directory: string): void;
private installTypings;
private ensureDirectoryExists;
private watchFiles;
private createSetTypings;
private installTypingsAsync;
private executeWithThrottling;
protected abstract installWorker(requestId: number, packageNames: string[], cwd: string, onRequestCompleted: RequestCompletedAction): void;
protected abstract sendResponse(response: SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes): void;
protected readonly latestDistTag = "latest";
}
}
interface CompressedData {
length: number;
compressionKind: string;
Expand Down Expand Up @@ -9789,6 +9845,14 @@ declare namespace ts {
emit(writeFile?: WriteFileCallback, customTransformers?: CustomTransformers): EmitResult | BuildInvalidedProject<T> | undefined;
}
type InvalidatedProject<T extends BuilderProgram> = UpdateOutputFileStampsProject | BuildInvalidedProject<T> | UpdateBundleProject<T>;
namespace JsTyping {
interface TypingResolutionHost {
directoryExists(path: string): boolean;
fileExists(fileName: string): boolean;
readFile(path: string, encoding?: string): string | undefined;
readDirectory(rootDir: string, extensions: readonly string[], excludes: readonly string[] | undefined, includes: readonly string[] | undefined, depth?: number): string[];
}
}
function getDefaultFormatCodeSettings(newLineCharacter?: string): FormatCodeSettings;
/**
* Represents an immutable snapshot of a script at a specified time.Once acquired, the
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5846,6 +5846,14 @@ declare namespace ts {
emit(writeFile?: WriteFileCallback, customTransformers?: CustomTransformers): EmitResult | BuildInvalidedProject<T> | undefined;
}
type InvalidatedProject<T extends BuilderProgram> = UpdateOutputFileStampsProject | BuildInvalidedProject<T> | UpdateBundleProject<T>;
namespace JsTyping {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhat unfortunate that this is making it out into the non-server API given there's no actual use for it...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? The TypingResolutionHost is something I really would have to implement, iiuc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but this is typescript.d.ts, not tsserverlibrary.d.ts, and it's not going to contain the ATA stuff as far as I'm aware (note that the change to this file is type-only).

Copy link
Member

@DanielRosenwasser DanielRosenwasser Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty sure this is unnecessary if we just switch these to direct import instead of leveraging the following re-export from within src/services/_namespaces/ts.ts:

export * from "../../jsTyping/_namespaces/ts";

@zkat I think that's a reasonable change to make within this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielRosenwasser You mean doing import { TypingResolutionHost } from "./jsTyping;"? 'cause that broke the tests because it can't find it?? (sorry, I'm not entirely understanding how the namespace mapping thing works right now)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, our bundling is unfortunately far too restrictive to allow this sort of thing right now. I think it's just going to have to be gross.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hold up, why can't the tests just do a direct import as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump

Copy link
Member

@DanielRosenwasser DanielRosenwasser Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zkat if you change existing instances of TypingResolutionHost to direct imports in the test suite, does the bundler still "break"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I just realized that we (just below here) already have ts.server declared in typescript.d.ts. This isn't a regression; you can see the same thing in TS 4.9.5 too.

So, I think I'm okay with this PR as-is. It's no worse than our current API in terms of over-declaring a public API. ATA already leaks into typescript.d.ts as it is so I don't think there's really much to do here.

interface TypingResolutionHost {
directoryExists(path: string): boolean;
fileExists(fileName: string): boolean;
readFile(path: string, encoding?: string): string | undefined;
readDirectory(rootDir: string, extensions: readonly string[], excludes: readonly string[] | undefined, includes: readonly string[] | undefined, depth?: number): string[];
}
}
namespace server {
type ActionSet = "action::set";
type ActionInvalidate = "action::invalidate";
Expand Down Expand Up @@ -5911,6 +5919,14 @@ declare namespace ts {
readonly kind: EventEndInstallTypes;
readonly installSuccess: boolean;
}
interface InstallTypingHost extends JsTyping.TypingResolutionHost {
useCaseSensitiveFileNames: boolean;
writeFile(path: string, content: string): void;
createDirectory(path: string): void;
getCurrentDirectory?(): string;
watchFile?(path: string, callback: FileWatcherCallback, pollingInterval?: number, options?: WatchOptions): FileWatcher;
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean, options?: WatchOptions): FileWatcher;
}
interface SetTypings extends ProjectResponse {
readonly typeAcquisition: TypeAcquisition;
readonly compilerOptions: CompilerOptions;
Expand Down