From aecaa73570db1dd099150f45b35a3dd07d00a08a Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 08:29:22 -0700 Subject: [PATCH 01/12] Split intrinsic from std lib --- .../compiler/lib/{lib.tsp => intrinsic.tsp} | 62 +------------------ packages/compiler/lib/main.tsp | 4 -- .../compiler/lib/{ => std}/decorators.tsp | 2 +- packages/compiler/lib/std/main.tsp | 5 ++ .../lib/{ => std}/projected-names.tsp | 0 .../compiler/lib/{ => std}/reflection.tsp | 0 packages/compiler/lib/std/types.tsp | 62 +++++++++++++++++++ packages/compiler/src/core/checker.ts | 3 - packages/compiler/src/core/node-host.ts | 2 +- packages/compiler/src/core/program.ts | 12 +++- packages/compiler/src/testing/test-host.ts | 2 +- packages/playground/src/browser-host.ts | 7 ++- 12 files changed, 86 insertions(+), 75 deletions(-) rename packages/compiler/lib/{lib.tsp => intrinsic.tsp} (66%) delete mode 100644 packages/compiler/lib/main.tsp rename packages/compiler/lib/{ => std}/decorators.tsp (99%) create mode 100644 packages/compiler/lib/std/main.tsp rename packages/compiler/lib/{ => std}/projected-names.tsp (100%) rename packages/compiler/lib/{ => std}/reflection.tsp (100%) create mode 100644 packages/compiler/lib/std/types.tsp diff --git a/packages/compiler/lib/lib.tsp b/packages/compiler/lib/intrinsic.tsp similarity index 66% rename from packages/compiler/lib/lib.tsp rename to packages/compiler/lib/intrinsic.tsp index 29a5e14c41..455d755247 100644 --- a/packages/compiler/lib/lib.tsp +++ b/packages/compiler/lib/intrinsic.tsp @@ -1,3 +1,4 @@ +// This file contains all the intrinsic types of typespec. Everything here will always be loaded namespace TypeSpec; /** @@ -149,64 +150,3 @@ model Array {} */ @indexer(string, Element) model Record {} - -/** - * Represent a URL string as described by https://url.spec.whatwg.org/ - */ -scalar url extends string; - -/** - * Represents a collection of optional properties. - * - * @template Source An object whose spread properties are all optional. - */ -@doc("The template for adding optional properties.") -@withOptionalProperties -model OptionalProperties { - ...Source; -} - -/** - * Represents a collection of updateable properties. - * - * @template Source An object whose spread properties are all updateable. - */ -@doc("The template for adding updateable properties.") -@withUpdateableProperties -model UpdateableProperties { - ...Source; -} - -/** - * Represents a collection of omitted properties. - * - * @template Source An object whose properties are spread. - * @template Keys The property keys to omit. - */ -@doc("The template for omitting properties.") -@withoutOmittedProperties(Keys) -model OmitProperties { - ...Source; -} - -/** - * Represents a collection of properties with default values omitted. - * - * @template Source An object whose spread property defaults are all omitted. - */ -@withoutDefaultValues -model OmitDefaults { - ...Source; -} - -/** - * Applies a visibility setting to a collection of properties. - * - * @template Source An object whose properties are spread. - * @template Visibility The visibility to apply to all properties. - */ -@doc("The template for setting the default visibility of key properties.") -@withDefaultKeyVisibility(Visibility) -model DefaultKeyVisibility { - ...Source; -} diff --git a/packages/compiler/lib/main.tsp b/packages/compiler/lib/main.tsp deleted file mode 100644 index 8d1a997664..0000000000 --- a/packages/compiler/lib/main.tsp +++ /dev/null @@ -1,4 +0,0 @@ -import "./lib.tsp"; -import "./decorators.tsp"; -import "./reflection.tsp"; -import "./projected-names.tsp"; diff --git a/packages/compiler/lib/decorators.tsp b/packages/compiler/lib/std/decorators.tsp similarity index 99% rename from packages/compiler/lib/decorators.tsp rename to packages/compiler/lib/std/decorators.tsp index 7af917640f..2259b95bdc 100644 --- a/packages/compiler/lib/decorators.tsp +++ b/packages/compiler/lib/std/decorators.tsp @@ -1,4 +1,4 @@ -import "../dist/src/lib/decorators.js"; +import "../../dist/src/lib/decorators.js"; using TypeSpec.Reflection; diff --git a/packages/compiler/lib/std/main.tsp b/packages/compiler/lib/std/main.tsp new file mode 100644 index 0000000000..40dc5caec9 --- /dev/null +++ b/packages/compiler/lib/std/main.tsp @@ -0,0 +1,5 @@ +// TypeSpec standard library. Everything in here can be ommited by using --no-std-lib or `noStdLib` in the config. +import "./types.tsp"; +import "./decorators.tsp"; +import "./reflection.tsp"; +import "./projected-names.tsp"; diff --git a/packages/compiler/lib/projected-names.tsp b/packages/compiler/lib/std/projected-names.tsp similarity index 100% rename from packages/compiler/lib/projected-names.tsp rename to packages/compiler/lib/std/projected-names.tsp diff --git a/packages/compiler/lib/reflection.tsp b/packages/compiler/lib/std/reflection.tsp similarity index 100% rename from packages/compiler/lib/reflection.tsp rename to packages/compiler/lib/std/reflection.tsp diff --git a/packages/compiler/lib/std/types.tsp b/packages/compiler/lib/std/types.tsp new file mode 100644 index 0000000000..5e6005a9d3 --- /dev/null +++ b/packages/compiler/lib/std/types.tsp @@ -0,0 +1,62 @@ +namespace TypeSpec; + +/** + * Represent a URL string as described by https://url.spec.whatwg.org/ + */ +scalar url extends string; + +/** + * Represents a collection of optional properties. + * + * @template Source An object whose spread properties are all optional. + */ +@doc("The template for adding optional properties.") +@withOptionalProperties +model OptionalProperties { + ...Source; +} + +/** + * Represents a collection of updateable properties. + * + * @template Source An object whose spread properties are all updateable. + */ +@doc("The template for adding updateable properties.") +@withUpdateableProperties +model UpdateableProperties { + ...Source; +} + +/** + * Represents a collection of omitted properties. + * + * @template Source An object whose properties are spread. + * @template Keys The property keys to omit. + */ +@doc("The template for omitting properties.") +@withoutOmittedProperties(Keys) +model OmitProperties { + ...Source; +} + +/** + * Represents a collection of properties with default values omitted. + * + * @template Source An object whose spread property defaults are all omitted. + */ +@withoutDefaultValues +model OmitDefaults { + ...Source; +} + +/** + * Applies a visibility setting to a collection of properties. + * + * @template Source An object whose properties are spread. + * @template Visibility The visibility to apply to all properties. + */ +@doc("The template for setting the default visibility of key properties.") +@withDefaultKeyVisibility(Visibility) +model DefaultKeyVisibility { + ...Source; +} diff --git a/packages/compiler/src/core/checker.ts b/packages/compiler/src/core/checker.ts index 539459329e..0686107d52 100644 --- a/packages/compiler/src/core/checker.ts +++ b/packages/compiler/src/core/checker.ts @@ -356,9 +356,6 @@ export function createChecker(program: Program): Checker { const typespecNamespaceBinding = globalNamespaceNode.symbol.exports!.get("TypeSpec"); if (typespecNamespaceBinding) { - // the TypeSpec namespace binding will be absent if we've passed - // the no-std-lib option. - // the first declaration here is the JS file for the TypeSpec script. initializeTypeSpecIntrinsics(); for (const file of program.sourceFiles.values()) { addUsingSymbols(typespecNamespaceBinding.exports!, file.locals); diff --git a/packages/compiler/src/core/node-host.ts b/packages/compiler/src/core/node-host.ts index 7faa50500b..4eee44f8f3 100644 --- a/packages/compiler/src/core/node-host.ts +++ b/packages/compiler/src/core/node-host.ts @@ -28,7 +28,7 @@ export const NodeHost: CompilerHost = { getJsImport: (path: string) => import(pathToFileURL(path).href), getLibDirs() { const rootDir = this.getExecutionRoot(); - return [joinPaths(rootDir, "lib")]; + return [joinPaths(rootDir, "lib/std")]; }, stat(path: string) { return stat(path); diff --git a/packages/compiler/src/core/program.ts b/packages/compiler/src/core/program.ts index bf6f711bc5..44e6d7bce6 100644 --- a/packages/compiler/src/core/program.ts +++ b/packages/compiler/src/core/program.ts @@ -34,7 +34,7 @@ import { } from "./module-resolver.js"; import { CompilerOptions } from "./options.js"; import { isImportStatement, parse, parseStandaloneTypeReference } from "./parser.js"; -import { getDirectoryPath, joinPaths } from "./path-utils.js"; +import { getDirectoryPath, joinPaths, resolvePath } from "./path-utils.js"; import { createProjector } from "./projector.js"; import { createSourceFile } from "./source-file.js"; import { @@ -332,8 +332,9 @@ export async function compile( } const binder = createBinder(program); + await loadIntrinsicTypes(); if (!options?.nostdlib) { - await loadStandardLibrary(program); + await loadStandardLibrary(); } // Load additional imports prior to compilation @@ -458,7 +459,12 @@ export async function compile( } } - async function loadStandardLibrary(program: Program) { + async function loadIntrinsicTypes() { + const locationContext: LocationContext = { type: "compiler" }; + await loadTypeSpecFile(resolvePath(host.getExecutionRoot(), "intrinsic.tsp"), locationContext, NoTarget); + } + + async function loadStandardLibrary() { const locationContext: LocationContext = { type: "compiler" }; for (const dir of host.getLibDirs()) { await loadDirectory(dir, locationContext, NoTarget); diff --git a/packages/compiler/src/testing/test-host.ts b/packages/compiler/src/testing/test-host.ts index ae371cac54..a86a99fdb3 100644 --- a/packages/compiler/src/testing/test-host.ts +++ b/packages/compiler/src/testing/test-host.ts @@ -42,7 +42,7 @@ function createTestCompilerHost( jsImports: Map>, options?: TestHostOptions ): CompilerHost { - const libDirs = [resolveVirtualPath(".tsp/lib")]; + const libDirs = [resolveVirtualPath(".tsp/lib/std")]; if (!options?.excludeTestLib) { libDirs.push(resolveVirtualPath(".tsp/test-lib")); } diff --git a/packages/playground/src/browser-host.ts b/packages/playground/src/browser-host.ts index 8915f04670..c6afc6f320 100644 --- a/packages/playground/src/browser-host.ts +++ b/packages/playground/src/browser-host.ts @@ -104,7 +104,12 @@ export async function createBrowserHost( }, getLibDirs() { - return [resolveVirtualPath("/test/node_modules/@typespec/compiler/lib")]; + if(virtualFs.has(resolveVirtualPath("/test/node_modules/@typespec/compiler/lib/std/main.tsp"))) { + return [resolveVirtualPath("/test/node_modules/@typespec/compiler/lib/std")]; + } else { + // To load older version of the compiler < 0.55.0 + return [resolveVirtualPath("/test/node_modules/@typespec/compiler/lib")]; + } }, getExecutionRoot() { From 024fbd97421f39b665e519442d51c34ca7f1eca2 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 08:31:27 -0700 Subject: [PATCH 02/12] fix --- packages/compiler/lib/std/main.tsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler/lib/std/main.tsp b/packages/compiler/lib/std/main.tsp index 40dc5caec9..1d14078ed6 100644 --- a/packages/compiler/lib/std/main.tsp +++ b/packages/compiler/lib/std/main.tsp @@ -1,4 +1,4 @@ -// TypeSpec standard library. Everything in here can be ommited by using --no-std-lib or `noStdLib` in the config. +// TypeSpec standard library. Everything in here can be ommited by using `--nostdlib` cli flag or `nostdlib` in the config. import "./types.tsp"; import "./decorators.tsp"; import "./reflection.tsp"; From d94992309d822163b3ca728955418fa6c9aec184 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 08:50:36 -0700 Subject: [PATCH 03/12] Fix --- packages/compiler/src/core/program.ts | 2 +- packages/compiler/src/testing/test-host.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/compiler/src/core/program.ts b/packages/compiler/src/core/program.ts index 44e6d7bce6..d02d4ebcc2 100644 --- a/packages/compiler/src/core/program.ts +++ b/packages/compiler/src/core/program.ts @@ -461,7 +461,7 @@ export async function compile( async function loadIntrinsicTypes() { const locationContext: LocationContext = { type: "compiler" }; - await loadTypeSpecFile(resolvePath(host.getExecutionRoot(), "intrinsic.tsp"), locationContext, NoTarget); + await loadTypeSpecFile(resolvePath(host.getExecutionRoot(), "lib/intrinsic.tsp"), locationContext, NoTarget); } async function loadStandardLibrary() { diff --git a/packages/compiler/src/testing/test-host.ts b/packages/compiler/src/testing/test-host.ts index a86a99fdb3..fe5d28803f 100644 --- a/packages/compiler/src/testing/test-host.ts +++ b/packages/compiler/src/testing/test-host.ts @@ -224,7 +224,7 @@ export const StandardTestLibrary: TypeSpecTestLibrary = { packageRoot: await findTestPackageRoot(import.meta.url), files: [ { virtualPath: "./.tsp/dist/src/lib", realDir: "./dist/src/lib", pattern: "*" }, - { virtualPath: "./.tsp/lib", realDir: "./lib", pattern: "*" }, + { virtualPath: "./.tsp/lib", realDir: "./lib", pattern: "**" }, ], }; From c6214384fd0869769ec936a151b16eb1cf10bfdf Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 08:53:49 -0700 Subject: [PATCH 04/12] Create actual-std-lib-2024-3-15-15-53-0.md --- .chronus/changes/actual-std-lib-2024-3-15-15-53-0.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .chronus/changes/actual-std-lib-2024-3-15-15-53-0.md diff --git a/.chronus/changes/actual-std-lib-2024-3-15-15-53-0.md b/.chronus/changes/actual-std-lib-2024-3-15-15-53-0.md new file mode 100644 index 0000000000..84a7efd3ad --- /dev/null +++ b/.chronus/changes/actual-std-lib-2024-3-15-15-53-0.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/compiler" +--- + +`--nostdlib` flag will now work by only applying to optional standard library types From 45f47595a887e3b3b40b234c16aa219a3868d928 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 08:58:39 -0700 Subject: [PATCH 05/12] Create actual-std-lib-2024-3-15-15-55-33.md --- .chronus/changes/actual-std-lib-2024-3-15-15-55-33.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .chronus/changes/actual-std-lib-2024-3-15-15-55-33.md diff --git a/.chronus/changes/actual-std-lib-2024-3-15-15-55-33.md b/.chronus/changes/actual-std-lib-2024-3-15-15-55-33.md new file mode 100644 index 0000000000..3325f2f0b9 --- /dev/null +++ b/.chronus/changes/actual-std-lib-2024-3-15-15-55-33.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/playground" +--- + +Add support for new intrinsic vs std lib split in the compiler. From 457f9d17ff32fdf8c749f210fd331188873945ad Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 09:16:25 -0700 Subject: [PATCH 06/12] Fix and add tests --- packages/compiler/lib/intrinsic.tsp | 16 +----- packages/compiler/lib/std/types.tsp | 15 +++++ packages/compiler/src/core/checker.ts | 3 +- packages/compiler/src/lib/decorators.ts | 11 ---- .../compiler/src/lib/intrinsic-decorators.ts | 14 +++++ packages/compiler/test/stdlib.test.ts | 57 +++++++++++++++++++ 6 files changed, 90 insertions(+), 26 deletions(-) create mode 100644 packages/compiler/src/lib/intrinsic-decorators.ts create mode 100644 packages/compiler/test/stdlib.test.ts diff --git a/packages/compiler/lib/intrinsic.tsp b/packages/compiler/lib/intrinsic.tsp index 455d755247..0a7775ab26 100644 --- a/packages/compiler/lib/intrinsic.tsp +++ b/packages/compiler/lib/intrinsic.tsp @@ -1,3 +1,5 @@ +import "../dist/src/lib/intrinsic-decorators.js"; + // This file contains all the intrinsic types of typespec. Everything here will always be loaded namespace TypeSpec; @@ -123,20 +125,6 @@ scalar duration; */ scalar boolean; -/** - * Represent a 32-bit unix timestamp datetime with 1s of granularity. - * It measures time by the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970. - */ -@encode("unixTimestamp", int32) -scalar unixTimestamp32 extends utcDateTime; - -/** - * Represent a model - */ -// Deprecated June 2023 sprint -#deprecated "object is deprecated. Please use {} for an empty model, `Record` for a record with unknown property types, `unknown[]` for an array." -model object {} - /** * @dev Array model type, equivalent to `Element[]` * @template Element The type of the array elements diff --git a/packages/compiler/lib/std/types.tsp b/packages/compiler/lib/std/types.tsp index 5e6005a9d3..d013410ff1 100644 --- a/packages/compiler/lib/std/types.tsp +++ b/packages/compiler/lib/std/types.tsp @@ -1,5 +1,20 @@ namespace TypeSpec; + +/** + * Represent a 32-bit unix timestamp datetime with 1s of granularity. + * It measures time by the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970. + */ +@encode("unixTimestamp", int32) +scalar unixTimestamp32 extends utcDateTime; + +/** + * Represent a model + */ +// Deprecated June 2023 sprint +#deprecated "object is deprecated. Please use {} for an empty model, `Record` for a record with unknown property types, `unknown[]` for an array." +model object {} + /** * Represent a URL string as described by https://url.spec.whatwg.org/ */ diff --git a/packages/compiler/src/core/checker.ts b/packages/compiler/src/core/checker.ts index 0686107d52..82d063c86a 100644 --- a/packages/compiler/src/core/checker.ts +++ b/packages/compiler/src/core/checker.ts @@ -1,4 +1,5 @@ -import { $docFromComment, getIndexer, isArrayModelType } from "../lib/decorators.js"; +import { $docFromComment, isArrayModelType } from "../lib/decorators.js"; +import { getIndexer } from "../lib/intrinsic-decorators.js"; import { MultiKeyMap, Mutable, createRekeyableMap, isArray, mutate } from "../utils/misc.js"; import { createSymbol, createSymbolTable } from "./binder.js"; import { createChangeIdentifierCodeFix } from "./compiler-code-fixes/change-identifier.codefix.js"; diff --git a/packages/compiler/src/lib/decorators.ts b/packages/compiler/src/lib/decorators.ts index d8b31012b9..d367194ab8 100644 --- a/packages/compiler/src/lib/decorators.ts +++ b/packages/compiler/src/lib/decorators.ts @@ -59,7 +59,6 @@ import { EnumMember, Interface, Model, - ModelIndexer, ModelProperty, Namespace, Operation, @@ -287,16 +286,6 @@ export const $inspectTypeName: InspectTypeNameDecorator = (context, target: Type console.log(getTypeName(target)); }; -const indexTypeKey = createStateSymbol("index"); -export const $indexer = (context: DecoratorContext, target: Type, key: Scalar, value: Type) => { - const indexer: ModelIndexer = { key, value }; - context.program.stateMap(indexTypeKey).set(target, indexer); -}; - -export function getIndexer(program: Program, target: Type): ModelIndexer | undefined { - return program.stateMap(indexTypeKey).get(target); -} - export function isStringType(program: Program | ProjectedProgram, target: Type): target is Scalar { const coreType = program.checker.getStdType("string"); const stringType = target.projector ? target.projector.projectType(coreType) : coreType; diff --git a/packages/compiler/src/lib/intrinsic-decorators.ts b/packages/compiler/src/lib/intrinsic-decorators.ts new file mode 100644 index 0000000000..6df1b9904c --- /dev/null +++ b/packages/compiler/src/lib/intrinsic-decorators.ts @@ -0,0 +1,14 @@ +import { Program } from "../core/program.js"; +import { DecoratorContext, ModelIndexer, Scalar, Type } from "../core/types.js"; + +export const namespace = "TypeSpec"; + +const indexTypeKey = Symbol.for(`TypeSpec.index`); +export const $indexer = (context: DecoratorContext, target: Type, key: Scalar, value: Type) => { + const indexer: ModelIndexer = { key, value }; + context.program.stateMap(indexTypeKey).set(target, indexer); +}; + +export function getIndexer(program: Program, target: Type): ModelIndexer | undefined { + return program.stateMap(indexTypeKey).get(target); +} diff --git a/packages/compiler/test/stdlib.test.ts b/packages/compiler/test/stdlib.test.ts new file mode 100644 index 0000000000..59abac47bd --- /dev/null +++ b/packages/compiler/test/stdlib.test.ts @@ -0,0 +1,57 @@ +import { beforeEach, describe, it } from "vitest"; +import { expectDiagnosticEmpty } from "../src/testing/index.js"; +import { createTestRunner } from "../src/testing/test-host.js"; +import { BasicTestRunner } from "../src/testing/types.js"; + +let runner: BasicTestRunner; + +beforeEach(async () => { + runner = await createTestRunner(); +}); + +const intrinsicTypes = [ + "string", + "int8", + "int16", + "int32", + "int64", + "uint64", + "decimal", + "Array", + "Record", + "string[]", +]; + +describe("with stdlib", () => { + it("compiles", async () => { + const diagnostics = await runner.diagnose(`model Bar {}`, { nostdlib: false }); + expectDiagnosticEmpty(diagnostics); + }); + + describe("can use intrinsic types", () => { + it.each(intrinsicTypes)("%s", async (type) => { + const diagnostics = await runner.diagnose(`model Foo { name: ${type}; }`, { nostdlib: true }); + expectDiagnosticEmpty(diagnostics); + }); + }); + describe("can stdlib types", () => { + it.each(["url", "unixTimestamp32"])("%s", async (type) => { + const diagnostics = await runner.diagnose(`model Foo { name: ${type}; }`, { nostdlib: true }); + expectDiagnosticEmpty(diagnostics); + }); + }); +}); + +describe("without stdlib(--nostdlib)", () => { + it("compiles", async () => { + const diagnostics = await runner.diagnose(`model Bar {}`, { nostdlib: true }); + expectDiagnosticEmpty(diagnostics); + }); + + describe("can use intrinsic types", () => { + it.each(intrinsicTypes)("%s", async (type) => { + const diagnostics = await runner.diagnose(`model Foo { name: ${type}; }`, { nostdlib: true }); + expectDiagnosticEmpty(diagnostics); + }); + }); +}); From 8f1c01b6a1f408871d03ffc54ac8a2d8e20be8cb Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 09:19:56 -0700 Subject: [PATCH 07/12] format and spell check --- packages/compiler/lib/std/main.tsp | 2 +- packages/compiler/lib/std/types.tsp | 1 - packages/compiler/src/core/program.ts | 6 +++++- packages/playground/src/browser-host.ts | 4 +++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/compiler/lib/std/main.tsp b/packages/compiler/lib/std/main.tsp index 1d14078ed6..e0abf3ef19 100644 --- a/packages/compiler/lib/std/main.tsp +++ b/packages/compiler/lib/std/main.tsp @@ -1,4 +1,4 @@ -// TypeSpec standard library. Everything in here can be ommited by using `--nostdlib` cli flag or `nostdlib` in the config. +// TypeSpec standard library. Everything in here can be omitted by using `--nostdlib` cli flag or `nostdlib` in the config. import "./types.tsp"; import "./decorators.tsp"; import "./reflection.tsp"; diff --git a/packages/compiler/lib/std/types.tsp b/packages/compiler/lib/std/types.tsp index d013410ff1..7f02346fe3 100644 --- a/packages/compiler/lib/std/types.tsp +++ b/packages/compiler/lib/std/types.tsp @@ -1,6 +1,5 @@ namespace TypeSpec; - /** * Represent a 32-bit unix timestamp datetime with 1s of granularity. * It measures time by the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970. diff --git a/packages/compiler/src/core/program.ts b/packages/compiler/src/core/program.ts index d02d4ebcc2..cfcda98e66 100644 --- a/packages/compiler/src/core/program.ts +++ b/packages/compiler/src/core/program.ts @@ -461,7 +461,11 @@ export async function compile( async function loadIntrinsicTypes() { const locationContext: LocationContext = { type: "compiler" }; - await loadTypeSpecFile(resolvePath(host.getExecutionRoot(), "lib/intrinsic.tsp"), locationContext, NoTarget); + await loadTypeSpecFile( + resolvePath(host.getExecutionRoot(), "lib/intrinsic.tsp"), + locationContext, + NoTarget + ); } async function loadStandardLibrary() { diff --git a/packages/playground/src/browser-host.ts b/packages/playground/src/browser-host.ts index c6afc6f320..4d00a01370 100644 --- a/packages/playground/src/browser-host.ts +++ b/packages/playground/src/browser-host.ts @@ -104,7 +104,9 @@ export async function createBrowserHost( }, getLibDirs() { - if(virtualFs.has(resolveVirtualPath("/test/node_modules/@typespec/compiler/lib/std/main.tsp"))) { + if ( + virtualFs.has(resolveVirtualPath("/test/node_modules/@typespec/compiler/lib/std/main.tsp")) + ) { return [resolveVirtualPath("/test/node_modules/@typespec/compiler/lib/std")]; } else { // To load older version of the compiler < 0.55.0 From fa3df180363132bfc85f7f7bdc99526e09ece59e Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 10:07:21 -0700 Subject: [PATCH 08/12] Fix --- packages/compiler/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler/package.json b/packages/compiler/package.json index 7efbeda14f..238758f194 100644 --- a/packages/compiler/package.json +++ b/packages/compiler/package.json @@ -19,7 +19,7 @@ ], "type": "module", "main": "dist/src/index.js", - "tspMain": "lib/main.tsp", + "tspMain": "lib/std/main.tsp", "exports": { ".": { "types": "./dist/src/index.d.ts", From e7d290292230f6ac71e3b7bf1981affcbcfeaf71 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 10:37:33 -0700 Subject: [PATCH 09/12] Fix compiler tests --- packages/compiler/test/semantic-walker.test.ts | 10 +++++++++- packages/compiler/test/stdlib.test.ts | 14 +++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/compiler/test/semantic-walker.test.ts b/packages/compiler/test/semantic-walker.test.ts index b49408744c..d85c9d3c2a 100644 --- a/packages/compiler/test/semantic-walker.test.ts +++ b/packages/compiler/test/semantic-walker.test.ts @@ -127,7 +127,15 @@ describe("compiler: semantic walker", () => { deepStrictEqual( result.namespaces.map((x) => getNamespaceFullName(x)), - ["", "Global", "Global.My", "Global.My.Simple", "Global.My.Parent", "Global.My.Parent.Child"] + [ + "", + "TypeSpec", + "Global", + "Global.My", + "Global.My.Simple", + "Global.My.Parent", + "Global.My.Parent.Child", + ] ); }); diff --git a/packages/compiler/test/stdlib.test.ts b/packages/compiler/test/stdlib.test.ts index 59abac47bd..10dad9ae14 100644 --- a/packages/compiler/test/stdlib.test.ts +++ b/packages/compiler/test/stdlib.test.ts @@ -1,4 +1,5 @@ import { beforeEach, describe, it } from "vitest"; +import { CompilerOptions } from "../src/index.js"; import { expectDiagnosticEmpty } from "../src/testing/index.js"; import { createTestRunner } from "../src/testing/test-host.js"; import { BasicTestRunner } from "../src/testing/types.js"; @@ -23,34 +24,37 @@ const intrinsicTypes = [ ]; describe("with stdlib", () => { + const options: CompilerOptions = { nostdlib: false }; it("compiles", async () => { - const diagnostics = await runner.diagnose(`model Bar {}`, { nostdlib: false }); + const diagnostics = await runner.diagnose(`model Bar {}`, options); expectDiagnosticEmpty(diagnostics); }); describe("can use intrinsic types", () => { it.each(intrinsicTypes)("%s", async (type) => { - const diagnostics = await runner.diagnose(`model Foo { name: ${type}; }`, { nostdlib: true }); + const diagnostics = await runner.diagnose(`model Foo { name: ${type}; }`, options); expectDiagnosticEmpty(diagnostics); }); }); describe("can stdlib types", () => { it.each(["url", "unixTimestamp32"])("%s", async (type) => { - const diagnostics = await runner.diagnose(`model Foo { name: ${type}; }`, { nostdlib: true }); + const diagnostics = await runner.diagnose(`model Foo { name: ${type}; }`, options); expectDiagnosticEmpty(diagnostics); }); }); }); describe("without stdlib(--nostdlib)", () => { + const options: CompilerOptions = { nostdlib: true }; + it("compiles", async () => { - const diagnostics = await runner.diagnose(`model Bar {}`, { nostdlib: true }); + const diagnostics = await runner.diagnose(`model Bar {}`, options); expectDiagnosticEmpty(diagnostics); }); describe("can use intrinsic types", () => { it.each(intrinsicTypes)("%s", async (type) => { - const diagnostics = await runner.diagnose(`model Foo { name: ${type}; }`, { nostdlib: true }); + const diagnostics = await runner.diagnose(`model Foo { name: ${type}; }`, options); expectDiagnosticEmpty(diagnostics); }); }); From fef28255fc1880ae2acb0c001adaab2bf3ff76c5 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 15 Apr 2024 11:32:33 -0700 Subject: [PATCH 10/12] Fix packaged --- packages/compiler/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler/package.json b/packages/compiler/package.json index 238758f194..7faca2c46a 100644 --- a/packages/compiler/package.json +++ b/packages/compiler/package.json @@ -54,7 +54,7 @@ "tsp-server": "cmd/tsp-server.js" }, "files": [ - "lib/*.tsp", + "lib/**/*.tsp", "dist/**", "templates/**", "entrypoints", From 98c216799d2ca0d07b9b501f8477dc572a90fa56 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Tue, 16 Apr 2024 17:42:24 -0700 Subject: [PATCH 11/12] fix playground --- packages/playground/src/browser-host.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/src/browser-host.ts b/packages/playground/src/browser-host.ts index 4d00a01370..817919d8cb 100644 --- a/packages/playground/src/browser-host.ts +++ b/packages/playground/src/browser-host.ts @@ -115,7 +115,7 @@ export async function createBrowserHost( }, getExecutionRoot() { - return resolveVirtualPath(".tsp"); + return resolveVirtualPath("/test/node_modules/@typespec/compiler"); }, async getJsImport(path) { From 8f0f28eec8f2a30ebdc4222305edf0b379d51b80 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 18 Apr 2024 11:53:20 -0700 Subject: [PATCH 12/12] intrinsics.tsp --- packages/compiler/lib/{intrinsic.tsp => intrinsics.tsp} | 0 packages/compiler/src/core/program.ts | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/compiler/lib/{intrinsic.tsp => intrinsics.tsp} (100%) diff --git a/packages/compiler/lib/intrinsic.tsp b/packages/compiler/lib/intrinsics.tsp similarity index 100% rename from packages/compiler/lib/intrinsic.tsp rename to packages/compiler/lib/intrinsics.tsp diff --git a/packages/compiler/src/core/program.ts b/packages/compiler/src/core/program.ts index cfcda98e66..328f10266d 100644 --- a/packages/compiler/src/core/program.ts +++ b/packages/compiler/src/core/program.ts @@ -462,7 +462,7 @@ export async function compile( async function loadIntrinsicTypes() { const locationContext: LocationContext = { type: "compiler" }; await loadTypeSpecFile( - resolvePath(host.getExecutionRoot(), "lib/intrinsic.tsp"), + resolvePath(host.getExecutionRoot(), "lib/intrinsics.tsp"), locationContext, NoTarget );