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 1 commit
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
Next Next commit
Step 1
  • Loading branch information
timotheeguerin committed Feb 20, 2025
commit 9dbe351888f9e16c2ed5723e526733432555dbb1
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;

44 changes: 20 additions & 24 deletions packages/compiler/src/init/scaffold.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { stringify } from "yaml";
import { TypeSpecConfigFilename } from "../config/config-loader.js";
import { TypeSpecRawConfig } from "../config/types.js";
import { formatTypeSpec } from "../core/formatter.js";
import type { TypeSpecRawConfig } from "../config/types.js";
import { getDirectoryPath, joinPaths } from "../core/path-utils.js";
import { CompilerHost } from "../core/types.js";
import { PackageJson } from "../types/package-json.js";
import type { SystemHost } from "../core/types.js";
import type { PackageJson } from "../types/package-json.js";
import { readUrlOrPath, resolveRelativeUrlOrPath } from "../utils/misc.js";
import { FileTemplatingContext, createFileTemplatingContext, render } from "./file-templating.js";
import {
import { createFileTemplatingContext, render } from "./file-templating.js";
import type {
InitTemplate,
InitTemplateFile,
InitTemplateLibrary,
InitTemplateLibrarySpec,
} from "./init-template.js";

export const TypeSpecConfigFilename = "tspconfig.yaml";

export interface ScaffoldingConfig {
/** Template used to resolve that config */
template: InitTemplate;
@@ -89,7 +88,7 @@ export function makeScaffoldingConfig(
* @param host
* @param config
*/
export async function scaffoldNewProject(host: CompilerHost, config: ScaffoldingConfig) {
export async function scaffoldNewProject(host: SystemHost, config: ScaffoldingConfig) {
await host.mkdirp(config.directory);
await writePackageJson(host, config);
await writeConfig(host, config);
@@ -107,7 +106,7 @@ export function isFileSkipGeneration(fileName: string, files: InitTemplateFile[]
return false;
}

async function writePackageJson(host: CompilerHost, config: ScaffoldingConfig) {
async function writePackageJson(host: SystemHost, config: ScaffoldingConfig) {
if (isFileSkipGeneration("package.json", config.template.files ?? [])) {
return;
}
@@ -138,10 +137,7 @@ async function writePackageJson(host: CompilerHost, config: ScaffoldingConfig) {
private: true,
};

return host.writeFile(
joinPaths(config.directory, "package.json"),
JSON.stringify(packageJson, null, 2),
);
return host.writeFile("package.json", JSON.stringify(packageJson, null, 2));
}

const placeholderConfig = `
@@ -162,7 +158,7 @@ const placeholderConfig = `
# warn-as-error: true # Treat warnings as errors
# output-dir: "{project-root}/_generated" # Configure the base output directory for all emitters
`.trim();
async function writeConfig(host: CompilerHost, config: ScaffoldingConfig) {
async function writeConfig(host: SystemHost, config: ScaffoldingConfig) {
if (isFileSkipGeneration(TypeSpecConfigFilename, config.template.files ?? [])) {
return;
}
@@ -180,11 +176,11 @@ async function writeConfig(host: CompilerHost, config: ScaffoldingConfig) {
Object.entries(config.emitters).map(([key, emitter]) => [key, emitter.options]),
);
}
const content = rawConfig ? stringify(rawConfig) : placeholderConfig;
return host.writeFile(joinPaths(config.directory, TypeSpecConfigFilename), content);
const content = rawConfig ? "" : placeholderConfig;
return host.writeFile(TypeSpecConfigFilename, content);
}

async function writeMain(host: CompilerHost, config: ScaffoldingConfig) {
async function writeMain(host: SystemHost, config: ScaffoldingConfig) {
if (isFileSkipGeneration("main.tsp", config.template.files ?? [])) {
return;
}
@@ -197,7 +193,7 @@ async function writeMain(host: CompilerHost, config: ScaffoldingConfig) {
const lines = [...config.libraries.map((x) => `import "${x.name}";`), ""];
const content = lines.join("\n");

return host.writeFile(joinPaths(config.directory, "main.tsp"), await formatTypeSpec(content));
// return host.writeFile(joinPaths(config.directory, "main.tsp"), await formatTypeSpec(content));
}

const defaultGitignore = `
@@ -211,15 +207,15 @@ dist/
# Dependency directories
node_modules/
`.trim();
async function writeGitignore(host: CompilerHost, config: ScaffoldingConfig) {
async function writeGitignore(host: SystemHost, config: ScaffoldingConfig) {
if (!config.includeGitignore || isFileSkipGeneration(".gitignore", config.template.files ?? [])) {
return;
}

return host.writeFile(joinPaths(config.directory, ".gitignore"), defaultGitignore);
return host.writeFile(".gitignore", defaultGitignore);
}

async function writeFiles(host: CompilerHost, config: ScaffoldingConfig) {
async function writeFiles(host: SystemHost, config: ScaffoldingConfig) {
const templateContext = createFileTemplatingContext(config);
if (!config.template.files) {
return;
@@ -232,9 +228,9 @@ async function writeFiles(host: CompilerHost, config: ScaffoldingConfig) {
}

async function writeFile(
host: CompilerHost,
host: SystemHost,
config: ScaffoldingConfig,
context: FileTemplatingContext,
context: any,
file: InitTemplateFile,
) {
const baseDir = config.baseUri + "/";
3 changes: 3 additions & 0 deletions packages/compiler/src/internals/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// export { getTypeSpecCoreTemplates } from "../init/core-templates.js";
export { NodeSystemHost } from "../core/node-system-host.js";
export { scaffoldNewProject } from "../init/scaffold.js";
Copy link
Contributor

Choose a reason for hiding this comment

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

i think you also need to export validateTemplate method or template schema for vscode to do all the template validation.

3 changes: 2 additions & 1 deletion packages/compiler/src/server/compile-service.ts
Original file line number Diff line number Diff line change
@@ -21,7 +21,8 @@ import {
} from "../core/index.js";
import { builtInLinterLibraryName, builtInLinterRule_UnusedUsing } from "../core/linter.js";
import { compile as compileProgram } from "../core/program.js";
import { doIO, loadFile, resolveTspMain } from "../utils/misc.js";
import { doIO, loadFile } from "../utils/io.js";
import { resolveTspMain } from "../utils/misc.js";
import { serverOptions } from "./constants.js";
import { FileService } from "./file-service.js";
import { FileSystemCache } from "./file-system-cache.js";
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.