Files
matcha/src/runtime.rs
T
chrnxandClaude Opus 5 a3250da225 feat(edge): a Cloudflare Workers target, and a doctor for the quiet failures
`matcha new --runtime edge` scaffolds a Worker: `nodejs_compat`, `edgeHandler`
as the default export, and wrangler's `[assets]` serving `./public` so `/` is the
same page as on the other three runtimes and `/zen` the same stream from the
worker. `matcha run` is `wrangler dev`, which is real workerd, so edge stops
being a deploy-only target. Runtime detection puts `wrangler.toml` ahead of
`package.json`, since an edge project has both, and the graph resolves under
node — `src/app.ts` imports core, never `edgeHandler`.

Getting there turned up a core defect, filed as Green-Tea/core#93: the npm ESM
build cannot load on workerd at all. tsup's `createRequire(import.meta.url)`
banner runs at module scope and `import.meta.url` is undefined there, so
`@green-tea/core/edge` throws before any of its own code runs — while
`src/node-require.ts` is written precisely to make that lazy and survivable.
core's own edge test cannot see it: it bundles `src/`, so `dist/` is the one
artifact users install and the one nothing exercises. The scaffold installs from
JSR, which publishes source with no banner and boots clean; the `.npmrc` says why
and links the issue, and both it and the alias can go once it is fixed.

`matcha doctor` reports the misconfigurations that fail confusingly rather than
loudly: `experimentalDecorators` missing, which turns every `@Route` into a
syntax error blaming the decorator; a core pin that cannot resolve or that does
not match what is installed; an entry that does not export `app`; and `@Ws` or
multipart routes whose lazy-loaded peer dependency is absent, which compile and
then fail at the first request. Every check is a file read — no runtime spawned,
nothing fetched — so it works before the project installs. It reads the installed
version from the package's own manifest rather than core's exported `VERSION`,
which is stale in the published artifact (Green-Tea/core#92) and would give the
wrong answer to the one check that exists to catch a wrong version.

The starter smoke and the CI matrix gained edge as a fourth runtime.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012x1UPw987zNSxjH2DxHy2C
2026-09-05 15:09:55 -05:00

119 lines
3.3 KiB
Rust

use std::path::Path;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Runtime {
Node,
Deno,
Bun,
/// Cloudflare Workers. Unlike the other three this is a deploy target as
/// much as a local one — `wrangler dev` runs it in real workerd, but the
/// graph is resolved under node, since introspection never touches the
/// edge handler.
Edge,
}
impl Runtime {
pub fn from_str(s: &str) -> Option<Runtime> {
match s.trim() {
"node" => Some(Runtime::Node),
"deno" => Some(Runtime::Deno),
"bun" => Some(Runtime::Bun),
"edge" => Some(Runtime::Edge),
_ => None,
}
}
}
fn exists(dir: &Path, name: &str) -> bool {
dir.join(name).exists()
}
/// Reads `runtime = "..."` from matcha.toml without a TOML dep (one field only).
fn override_from_toml(dir: &Path) -> Option<Runtime> {
let text = std::fs::read_to_string(dir.join("matcha.toml")).ok()?;
for line in text.lines() {
let line = line.trim();
if let Some(rest) = line.strip_prefix("runtime") {
let val = rest.trim().trim_start_matches('=').trim().trim_matches('"');
return Runtime::from_str(val);
}
}
None
}
pub fn detect(dir: &Path) -> Option<Runtime> {
if let Some(r) = override_from_toml(dir) {
return Some(r);
}
// Before package.json, which an edge project also has.
if exists(dir, "wrangler.toml") || exists(dir, "wrangler.jsonc") {
return Some(Runtime::Edge);
}
if exists(dir, "deno.json") || exists(dir, "deno.jsonc") {
return Some(Runtime::Deno);
}
if exists(dir, "bun.lockb") || exists(dir, "bun.lock") {
return Some(Runtime::Bun);
}
if exists(dir, "package.json") {
return Some(Runtime::Node);
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;
fn touch(dir: &std::path::Path, name: &str) {
fs::write(dir.join(name), "{}").unwrap();
}
#[test]
fn deno_wins_over_package_json() {
let d = tempdir().unwrap();
touch(d.path(), "package.json");
touch(d.path(), "deno.json");
assert_eq!(detect(d.path()), Some(Runtime::Deno));
}
#[test]
fn bun_lock_selects_bun() {
let d = tempdir().unwrap();
touch(d.path(), "package.json");
touch(d.path(), "bun.lockb");
assert_eq!(detect(d.path()), Some(Runtime::Bun));
}
#[test]
fn package_json_falls_back_to_node() {
let d = tempdir().unwrap();
touch(d.path(), "package.json");
assert_eq!(detect(d.path()), Some(Runtime::Node));
}
#[test]
fn matcha_toml_overrides() {
let d = tempdir().unwrap();
touch(d.path(), "deno.json");
fs::write(d.path().join("matcha.toml"), "runtime = \"bun\"\n").unwrap();
assert_eq!(detect(d.path()), Some(Runtime::Bun));
}
#[test]
fn wrangler_toml_wins_over_package_json() {
let d = tempdir().unwrap();
touch(d.path(), "package.json");
fs::write(d.path().join("wrangler.toml"), "name = \"x\"\n").unwrap();
assert_eq!(detect(d.path()), Some(Runtime::Edge));
}
#[test]
fn nothing_detected() {
let d = tempdir().unwrap();
assert_eq!(detect(d.path()), None);
}
}