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

Init logic bundle the compiler logic instead of shelling out to some random client #6086

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
@@ -49,6 +49,10 @@
"./experimental/typekit": {
"types": "./dist/src/experimental/typekit/index.d.ts",
"default": "./dist/src/experimental/typekit/index.js"
},
"./internals/init": {
"types": "./dist/src/internals/init.d.ts",
"import": "./dist/src/internals/init.js"
}
},
"browser": {
3 changes: 2 additions & 1 deletion packages/compiler/src/config/config-loader.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@ import { getDirectoryPath, isPathAbsolute, joinPaths, resolvePath } from "../cor
import { createJSONSchemaValidator } from "../core/schema-validator.js";
import { createSourceFile } from "../core/source-file.js";
import { CompilerHost, Diagnostic, NoTarget, SourceFile } from "../core/types.js";
import { deepClone, deepFreeze, doIO, omitUndefined } from "../utils/misc.js";
import { doIO } from "../utils/io.js";
import { deepClone, deepFreeze, omitUndefined } from "../utils/misc.js";
import { getLocationInYamlScript } from "../yaml/index.js";
import { parseYaml } from "../yaml/parser.js";
import { YamlScript } from "../yaml/types.js";
3 changes: 2 additions & 1 deletion packages/compiler/src/config/config-to-options.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@ import { createDiagnosticCollector, getDirectoryPath, normalizePath } from "../c
import { createDiagnostic } from "../core/messages.js";
import { CompilerOptions } from "../core/options.js";
import { CompilerHost, Diagnostic, NoTarget } from "../core/types.js";
import { deepClone, doIO, omitUndefined } from "../utils/misc.js";
import { doIO } from "../utils/io.js";
import { deepClone, omitUndefined } from "../utils/misc.js";
import { expandConfigVariables } from "./config-interpolation.js";
import { loadTypeSpecConfigForPath, validateConfigPathsAbsolute } from "./config-loader.js";
import { EmitterOptions, TypeSpecConfig } from "./types.js";
2 changes: 1 addition & 1 deletion packages/compiler/src/core/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ try {
}

import yargs from "yargs";
import { typespecVersion } from "../../utils/misc.js";
import { typespecVersion } from "../../manifest.js";
import { getTypeSpecEngine } from "../engine.js";
import { installTypeSpecDependencies } from "../install.js";
import { compileAction } from "./actions/compile/compile.js";
3 changes: 2 additions & 1 deletion packages/compiler/src/core/entrypoint-resolution.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { doIO, loadFile, resolveTspMain } from "../utils/misc.js";
import { doIO, loadFile } from "../utils/io.js";
import { resolveTspMain } from "../utils/misc.js";
import { DiagnosticHandler } from "./diagnostics.js";
import { resolvePath } from "./path-utils.js";
import { CompilerHost } from "./types.js";
59 changes: 6 additions & 53 deletions packages/compiler/src/core/node-host.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { realpath } from "fs";
import { mkdir, readdir, readFile, rm, stat, writeFile } from "fs/promises";
import { mkdir, stat } from "fs/promises";
import { fileURLToPath, pathToFileURL } from "url";
import { findProjectRoot } from "../utils/misc.js";
import { findProjectRoot } from "../utils/io.js";
import { createConsoleSink } from "./logger/index.js";
import { NodeSystemHost } from "./node-system-host.js";
import { joinPaths } from "./path-utils.js";
import { createSourceFile, getSourceFileKindFromExt } from "./source-file.js";
import { CompilerHost, RmOptions } from "./types.js";
import { getSourceFileKindFromExt } from "./source-file.js";
import { CompilerHost } from "./types.js";

export const CompilerPackageRoot = (await findProjectRoot(stat, fileURLToPath(import.meta.url)))!;

@@ -14,37 +14,13 @@ export const CompilerPackageRoot = (await findProjectRoot(stat, fileURLToPath(im
* This is the the CompilerHost used by TypeSpec CLI.
*/
export const NodeHost: CompilerHost = {
readUrl: async (url: string) => {
const response = await fetch(url, { redirect: "follow" });
const text = await response.text();
return createSourceFile(text, response.url);
},
readFile: async (path: string) => createSourceFile(await readUtf8File(path), path),
writeFile: (path: string, content: string) => writeFile(path, content, { encoding: "utf-8" }),
readDir: (path: string) => readdir(path),
rm: (path: string, options: RmOptions) => rm(path, options),
...NodeSystemHost,
getExecutionRoot: () => CompilerPackageRoot,
getJsImport: (path: string) => import(pathToFileURL(path).href),
getLibDirs() {
const rootDir = this.getExecutionRoot();
return [joinPaths(rootDir, "lib/std")];
},
stat(path: string) {
return stat(path);
},
realpath(path) {
// BUG in the promise version of realpath https://github.com/microsoft/typespec/issues/2783
// Fix was only made to node 21.6 at this time. https://github.com/nodejs/node/issues/51031
return new Promise((resolve, reject) => {
realpath(path, (err, resolvedPath) => {
if (err) {
reject(err);
} else {
resolve(resolvedPath);
}
});
});
},
getSourceFileKind: getSourceFileKindFromExt,
mkdirp: (path: string) => mkdir(path, { recursive: true }),
logSink: createConsoleSink(),
@@ -53,26 +29,3 @@ export const NodeHost: CompilerHost = {
return pathToFileURL(path).href;
},
};

async function readUtf8File(path: string) {
const buffer = await readFile(path);
const len = buffer.length;
if (len >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) {
throw new InvalidEncodingError("UTF-16 BE");
}
if (len >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) {
throw new InvalidEncodingError("UTF-16 LE");
}
if (len >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
// UTF-8 byte order mark detected
return buffer.toString("utf8", 3);
}
// Default is UTF-8 with no byte order mark
return buffer.toString("utf8");
}

export class InvalidEncodingError extends Error {
constructor(encoding: string) {
super(`Invalid encoding ${encoding}. TypeSpec only supports UTF-8 and UTF-8 with bom`);
}
}
59 changes: 59 additions & 0 deletions packages/compiler/src/core/node-system-host.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { realpath } from "fs";
import { mkdir, readdir, readFile, rm, stat, writeFile } from "fs/promises";
import { createSourceFile } from "./source-file.js";
import { RmOptions, SystemHost } from "./types.js";

/**
* Implementation of the @see SystemHost using the real file system.
*/
export const NodeSystemHost: SystemHost = {
readUrl: async (url: string) => {
const response = await fetch(url, { redirect: "follow" });
const text = await response.text();
return createSourceFile(text, response.url);
},
readFile: async (path: string) => createSourceFile(await readUtf8File(path), path),
writeFile: (path: string, content: string) => writeFile(path, content, { encoding: "utf-8" }),
readDir: (path: string) => readdir(path),
rm: (path: string, options: RmOptions) => rm(path, options),
stat(path: string) {
return stat(path);
},
realpath(path) {
// BUG in the promise version of realpath https://github.com/microsoft/typespec/issues/2783
// Fix was only made to node 21.6 at this time. https://github.com/nodejs/node/issues/51031
return new Promise((resolve, reject) => {
realpath(path, (err, resolvedPath) => {
if (err) {
reject(err);
} else {
resolve(resolvedPath);
}
});
});
},
mkdirp: (path: string) => mkdir(path, { recursive: true }),
};

async function readUtf8File(path: string) {
const buffer = await readFile(path);
const len = buffer.length;
if (len >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) {
throw new InvalidEncodingError("UTF-16 BE");
}
if (len >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) {
throw new InvalidEncodingError("UTF-16 LE");
}
if (len >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
// UTF-8 byte order mark detected
return buffer.toString("utf8", 3);
}
// Default is UTF-8 with no byte order mark
return buffer.toString("utf8");
}

export class InvalidEncodingError extends Error {
constructor(encoding: string) {
super(`Invalid encoding ${encoding}. TypeSpec only supports UTF-8 and UTF-8 with bom`);
}
}
3 changes: 2 additions & 1 deletion packages/compiler/src/core/program.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,8 @@ import {
resolveModule,
} from "../module-resolver/module-resolver.js";
import { PackageJson } from "../types/package-json.js";
import { deepEquals, findProjectRoot, isDefined, mapEquals, mutate } from "../utils/misc.js";
import { findProjectRoot } from "../utils/io.js";
import { deepEquals, isDefined, mapEquals, mutate } from "../utils/misc.js";
import { createBinder } from "./binder.js";
import { Checker, createChecker } from "./checker.js";
import { createSuppressCodeFix } from "./compiler-code-fixes/suppress.codefix.js";
3 changes: 2 additions & 1 deletion packages/compiler/src/core/source-loader.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,8 @@ import {
ResolveModuleHost,
} from "../module-resolver/module-resolver.js";
import { PackageJson } from "../types/package-json.js";
import { deepEquals, doIO, resolveTspMain } from "../utils/misc.js";
import { doIO } from "../utils/io.js";
import { deepEquals, resolveTspMain } from "../utils/misc.js";
import { compilerAssert, createDiagnosticCollector } from "./diagnostics.js";
import { resolveTypeSpecEntrypointForDir } from "./entrypoint-resolution.js";
import { createDiagnostic } from "./messages.js";
26 changes: 14 additions & 12 deletions packages/compiler/src/core/types.ts
Original file line number Diff line number Diff line change
@@ -2397,18 +2397,13 @@ export interface RmOptions {
recursive?: boolean;
}

export interface CompilerHost {
export interface SystemHost {
/** read a file at the given url. */
readUrl(url: string): Promise<SourceFile>;

/** read a utf-8 or utf-8 with bom encoded file */
readFile(path: string): Promise<SourceFile>;

/**
* Optional cache to reuse the results of parsing and binding across programs.
*/
parseCache?: WeakMap<SourceFile, TypeSpecScriptNode>;

/**
* Write the file.
* @param path Path to the file.
@@ -2435,6 +2430,19 @@ export interface CompilerHost {
*/
mkdirp(path: string): Promise<string | undefined>;

// get info about a path
stat(path: string): Promise<{ isDirectory(): boolean; isFile(): boolean }>;

// get the real path of a possibly symlinked path
realpath(path: string): Promise<string>;
}

export interface CompilerHost extends SystemHost {
/**
* Optional cache to reuse the results of parsing and binding across programs.
*/
parseCache?: WeakMap<SourceFile, TypeSpecScriptNode>;

// get the directory TypeSpec is executing from
getExecutionRoot(): string;

@@ -2444,14 +2452,8 @@ export interface CompilerHost {
// get a promise for the ESM module shape of a JS module
getJsImport(path: string): Promise<Record<string, any>>;

// get info about a path
stat(path: string): Promise<{ isDirectory(): boolean; isFile(): boolean }>;

getSourceFileKind(path: string): SourceFileKind | undefined;

// get the real path of a possibly symlinked path
realpath(path: string): Promise<string>;

// convert a file URL to a path in a file system
fileURLToPath(url: string): string;

4 changes: 2 additions & 2 deletions packages/compiler/src/init/core-templates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CompilerPackageRoot } from "../core/node-host.js";
import { resolvePath } from "../core/path-utils.js";
import { CompilerHost } from "../index.js";
import type { SystemHost } from "../index.js";

export const templatesDir = resolvePath(CompilerPackageRoot, "templates");
export interface LoadedCoreTemplates {
@@ -9,7 +9,7 @@ export interface LoadedCoreTemplates {
}

let typeSpecCoreTemplates: LoadedCoreTemplates | undefined;
export async function getTypeSpecCoreTemplates(host: CompilerHost): Promise<LoadedCoreTemplates> {
export async function getTypeSpecCoreTemplates(host: SystemHost): Promise<LoadedCoreTemplates> {
if (typeSpecCoreTemplates === undefined) {
const file = await host.readFile(resolvePath(templatesDir, "scaffolding.json"));
const content = JSON.parse(file.text);
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.