Skip to content

[I] CompilerHost

Host capabilities required to compile TypeSpec, extending file I/O with module loading, source-kind detection, standard-library discovery, and log delivery.

  • SystemHost
PropertyTypeDescription
logSinkLogSinkReceives processed log messages produced by compiler logging.
parseCache?WeakMap<SourceFile, TypeSpecScriptNode>Optional cache to reuse the results of parsing and binding across programs. Entries are keyed by source-file identity, not path; reuse a source snapshot only while its text is unchanged.
fileURLToPath(url): string;

Converts a file URL into a path understood by this host’s file-system operations.

ParameterType
urlstring

string


getExecutionRoot(): string;

Returns the compiler’s installation root, used to locate bundled compiler resources.

string


getJsImport(path): Promise<Record<string, any>>;

Loads a JavaScript module and returns its module namespace object, including TypeSpec hook exports.

ParameterType
pathstring

Promise<Record<string, any>>


getLibDirs(): string[];

Returns directories containing standard-library entry points to load for compilation.

string[]


getSourceFileKind(path): SourceFileKind | undefined;

Classifies a source path for loading, or returns undefined when the host does not recognize it.

ParameterType
pathstring

SourceFileKind | undefined


mkdirp(path): Promise<string | undefined>;

create directory recursively.

ParameterTypeDescription
pathstringPath to the directory.

Promise<string | undefined>

SystemHost.mkdirp

pathToFileURL(path): string;

Converts a host file-system path into a file URL.

ParameterType
pathstring

string


readDir(path): Promise<string[]>;

Read directory.

ParameterTypeDescription
pathstringPath to the directory.

Promise<string[]>

list of file/directory in the given directory. Returns the name not the full path.

SystemHost.readDir

readFile(path): Promise<SourceFile>;

read a utf-8 or utf-8 with bom encoded file

ParameterType
pathstring

Promise<SourceFile>

SystemHost.readFile

readUrl(url): Promise<SourceFile>;

read a file at the given url.

ParameterType
urlstring

Promise<SourceFile>

SystemHost.readUrl

realpath(path): Promise<string>;

Resolves a possibly symlinked path to its real file-system path.

ParameterType
pathstring

Promise<string>

SystemHost.realpath

rm(path, options?): Promise<void>;

Deletes a directory or file.

ParameterTypeDescription
pathstringPath to the directory or file.
options?RmOptions-

Promise<void>

SystemHost.rm

stat(path): Promise<{
isDirectory: boolean;
isFile: boolean;
}>;

Inspects a path; the returned predicates identify directories and regular files respectively.

ParameterType
pathstring

Promise<{ isDirectory: boolean; isFile: boolean; }>

SystemHost.stat

writeFile(path, content): Promise<void>;

Write the file.

ParameterTypeDescription
pathstringPath to the file.
contentstringContent of the file.

Promise<void>

SystemHost.writeFile