A native-speed rewrite of the roblox-ts compiler in golang
rotor docs sidecar-protocol.md
5.7 kB 153 lines
Markdown

Transformer Sidecar Protocol #

Rotor runs transformer plugins (compilerOptions.plugins) through a Node worker that mirrors upstream roblox-ts plugin semantics. The worker's source of truth is tools/sidecar/, and it is embedded in the rotor binary (tools/sidecar/embed.go + internal/compile/sidecar_install.go): released binaries extract it to <user-cache>/rotor/sidecar-<content-hash>/ on first plugin build, so no repo checkout is required. ROTOR_SIDECAR_PATH overrides the worker location (repo development and tests point it at tools/sidecar).

Projects without plugins never spawn Node.

TypeScript resolution #

The worker resolves the typescript package from the project's node_modules first, falling back to the worker's own directory. This matters for correctness, not just convenience: plugins require("typescript") themselves, and factory nodes only compose with transformNodes when both sides share one module instance — upstream roblox-ts guarantees this by construction (its own pinned 5.5.3 is the hoisted copy plugins see). roblox-ts projects pin typescript@~5.5.3; if a project has no typescript install, the worker reports a typescript-not-found diagnostic.

Setup (repo development) #

cd tools/sidecar
bun install --no-save

The package pins typescript@5.5.3 to match the upstream roblox-ts plugin runtime; this copy is only the fallback for synthetic test fixtures that have no node_modules of their own.

Invocation #

node tools/sidecar/main.js

The process reads newline-delimited JSON requests from stdin and writes one newline-delimited JSON response per request to stdout.

stdout is reserved for protocol responses. main.js captures the real stdout writer and reroutes every other stdout write (plugin console.log, e.g. Flamework's logging) to stderr. Rotor streams the worker's stderr lines to the compiler log as they arrive and keeps a tail for error reporting.

Warm sessions #

Rotor keeps one worker per (projectDir, tsConfigPath) for the life of the rotor process, including across rotor build -w rebuilds — the JS program stays warm, mirroring upstream's persistent transformerWatcher. The worker exits when rotor's pipes close.

Edits are communicated via changedFiles: rotor stat-diffs the project's .ts/.tsx files against the session's last-seen stamps and ships new text for anything that changed, which bumps the worker's LanguageService script versions (upstream updateFile semantics). A request on a fresh worker sends no overlays — the worker reads from disk. If a worker dies mid-request, rotor respawns it once and retries.

Known limitation: a warm worker's plugin-visible view of an edited ambient .d.ts can be stale until the watch session restarts (stamps cover the .ts/.tsx compile surface). Rotor's own typecheck and emit always read fresh state.

Inside the worker, one in-memory project session per (projectDir, tsConfigPath) holds the overlay map and reuses the TypeScript LanguageService program across requests.

Protocol v1 #

Each request must be a single JSON object with protocol: 1.

{
  "protocol": 1,
  "tsConfigPath": "C:/abs/project/tsconfig.json",
  "projectDir": "C:/abs/project",
  "compileFileNames": [
    "C:/abs/project/src/example.ts"
  ],
  "changedFiles": [
    {
      "fileName": "C:/abs/project/src/example.ts",
      "text": "export const phase = \"memory\";\n"
    }
  ]
}

Response shape:

{
  "diagnostics": [
    {
      "category": "error",
      "code": "invalid-request",
      "message": "protocol must equal 1"
    }
  ],
  "transformed": [
    {
      "fileName": "C:/abs/project/src/example.ts",
      "text": "export const phase = \"afterDeclarations:before:after:start\";\n"
    }
  ]
}

diagnostics may contain:

  • TypeScript config/program diagnostics converted to { category, code, file, start, length, message }
  • transformer resolution warnings using code transformer-not-found
  • a typescript-not-found error when the project has no resolvable typescript package
  • request validation errors using code invalid-request
  • internal worker failures using code sidecar-internal

Semantics Mirrored From Upstream #

The worker mirrors the upstream roblox-ts transformer behavior in these areas:

  • getPluginConfigs re-reads the raw tsconfig, keeps child compilerOptions.plugins entries before parent extends entries, and only accepts plugin objects with string transform fields.
  • transformer modules resolve relative to projectDir.
  • factory invocation follows upstream type handling for program, config, checker, raw, and compilerOptions.
  • transformed files run through a single typescript.transformNodes(...) pass.
  • transformer flatten order intentionally stays after, then before, then afterDeclarations.
  • transformed SourceFiles are reprinted with typescript.createPrinter().printFile(...).

Verification #

JS worker suite (also run in CI):

cd tools/sidecar
node --test test/*.test.js

Real-package integration (Flamework + rbxts-transform-env), exercising the full production path — embedded extraction, project typescript, warm session:

cd testdata/transformers/project && bun install --no-save && cd ../../..
go test ./internal/compile -run TestTransformersFixtureFlameworkAndEnv -count=1

The JS suite covers plugin discovery through extends, named/default factory loading, checker/compilerOptions factory instantiation, the after -> before -> afterDeclarations execution quirk, stdio protocol handling with warm overlay updates, per-project typescript resolution, and the stdout-protection rule.