Skip to content

refactor(compiler): introduce TemplateSource abstraction for tsp init#11303

Merged
timotheeguerin merged 5 commits into
mainfrom
tsp-init-template-source
Jul 20, 2026
Merged

refactor(compiler): introduce TemplateSource abstraction for tsp init#11303
timotheeguerin merged 5 commits into
mainfrom
tsp-init-template-source

Conversation

@timotheeguerin

Copy link
Copy Markdown
Member

Extracts the self-contained tsp init template-loading refactor out of #11299 into its own PR.

What

Replaces getExecutionRoot()-based template resolution in tsp init with a first-class TemplateSource abstraction, with three implementations behind one interface:

  • FileSystemTemplateSource — the default (installed/node) path; reads templates/ from the compiler package root. No getExecutionRoot() dependency.
  • InMemoryTemplateSource — serves the index and every template file from an in-memory map (used by the standalone single-executable to serve templates offline).
  • RemoteTemplateSource--templates-url; keeps the untrusted-source confirmation and relative-file resolution.

Why

Template loading previously went through the CompilerHost filesystem contract anchored at getExecutionRoot(), which also serves the stdlib. Serving templates from an embedded/offline source required masquerading an in-memory map as a real filesystem and overloading getExecutionRoot(). This refactor decouples template resolution from the host FS contract so each context supplies its own source, and leaves getExecutionRoot() solely for the stdlib.

Notes

Replace getExecutionRoot()-based template loading in `tsp init` with a
first-class TemplateSource abstraction (filesystem, in-memory, and remote
implementations). This decouples template resolution from the CompilerHost
filesystem contract and enables offline/embedded template sources.

Extracted from #11299 as an independent, self-contained refactor.
@microsoft-github-policy-service microsoft-github-policy-service Bot added compiler:core Issues for @typespec/compiler ide Issues for VS, VSCode, Monaco, etc. labels Jul 18, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/compiler@11303

commit: a828ad8

@github-actions

github-actions Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/compiler
Show changes

@typespec/compiler - internal ✏️

Refactor tsp init template loading around a URI-based TemplateSource abstraction. A UriTemplateSource handles local and remote templates (paths and URLs), while built-in ("core") templates are addressed through an internal: scheme that resolves to an injectable provider. This lets alternative hosts (e.g. an offline single-executable compiler) serve bundled templates via an InMemoryTemplateSource without coupling template loading to the CompilerHost filesystem.

typespec-vscode - internal ✏️

Reuse the compiler's UriTemplateSource.loadIndex() to load tsp init template indexes instead of the extension's own file/URL reading and JSON parsing, unifying how core and configured templates are loaded.

@azure-sdk-automation

azure-sdk-automation Bot commented Jul 18, 2026

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@timotheeguerin
timotheeguerin added this pull request to the merge queue Jul 20, 2026
Merged via the queue into main with commit 025142f Jul 20, 2026
32 of 33 checks passed
@timotheeguerin
timotheeguerin deleted the tsp-init-template-source branch July 20, 2026 17:55
timotheeguerin pushed a commit to Azure/typespec-azure that referenced this pull request Jul 21, 2026
The core bump replaces ScaffoldingConfig.baseUri with source?: TemplateSource
(microsoft/typespec#11303). Build a UriTemplateSource from the azure
scaffolding index and pass it as source (mirrors #4965).
msyyc pushed a commit to msyyc/typespec-azure that referenced this pull request Jul 21, 2026
…Azure#4968)

Bumps the `core` submodule to latest main, which pulls in tspd's
per-rule reference page generation (microsoft/typespec#11221). Left
as-is, that would orphan Azure's rich hand-written rule pages by
emitting barebones pages at a new location.

Instead, this migrates each rule to the tspd `docs` field and keeps the
generated pages at their existing URLs (net-zero URL change).

## Changes

- **core**: bump submodule to latest main — includes the merged tspd
`--rules-dir` change (microsoft/typespec#11316)
- **86 rules**: add `docs:
fileRef.fromPackageRoot("src/rules/<name>.md")`; rich content now lives
in `packages/*/src/rules/*.md`
- **3 `package.json`** regen-docs scripts: pass `--rules-dir ../rules`
so generated pages stay at `libraries/<lib>/rules/<name>`
- **astro.config**: 5 redirects for rules whose historical slug differs
from the rule name (e.g. `prevent-format` → `no-format`)
- **.gitignore**: ignore the generated `libraries/*/rules/` pages
(regenerated during `build:web`)
- **pnpm-lock**: add `@typespec/events` / `@typespec/sse` workspace
links (new `http-specs` peer deps from the bump)
- **eng/feeds e2e**: adapt `init-templates.e2e.ts` to the compiler's new
`TemplateSource` scaffold API — `ScaffoldingConfig.baseUri` was replaced
by `source?: TemplateSource` (microsoft/typespec#11303). Supersedes
Azure#4965.
- Contributing guide, `create-linter-rule` skill + scaffolder updated to
the `docs`-field workflow

## Validation

- `typespec-azure-core`, `typespec-azure-resource-manager`,
`typespec-client-generator-core` tests pass
- `oxlint` clean
- `pnpm install --frozen-lockfile` passes
- `pnpm regen-docs` clean (no drift)
- init-templates e2e: scaffolding tests pass via the new
`TemplateSource` path (the 2 `validate` tests that `npm install` an
unpublished `@azure-tools/typespec-autorest` version fail only on
registry resolution, unrelated to this change)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:core Issues for @typespec/compiler ide Issues for VS, VSCode, Monaco, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants