Skip to content

fix(client): type import.meta.main for run-if-main idioms (#2811)#2955

Merged
viniciusdacal merged 2 commits intomainfrom
fix/bun-types-import-meta-main-2811
Apr 22, 2026
Merged

fix(client): type import.meta.main for run-if-main idioms (#2811)#2955
viniciusdacal merged 2 commits intomainfrom
fix/bun-types-import-meta-main-2811

Conversation

@viniciusdacal
Copy link
Copy Markdown
Contributor

Closes #2811.

Summary

Follow-up to #2777. The vtz runtime (via deno_core) already sets import.meta.main on every module — true for the entry module, false for imports — so the standard "run if main" guard works without any polyfill. This PR types it in vertz/client so apps don't need bun-types just to satisfy TypeScript.

// src/api/server.ts
const app = createServer({ /* ... */ });
export default app;

if (import.meta.main) app.listen(env.PORT);  // ✅ typed as boolean

Public API Changes

Addition: ImportMeta.main: boolean declared in packages/ui/client.d.ts. Projects with "types": ["vertz/client"] (or "@vertz/ui/client") get it automatically. Scaffolded apps already have this tsconfig entry.

Declared as mutable (not readonly) on purpose — bun-types declares the same property as main: boolean. Declaration merging requires identical modifiers (TS2687), so matching bun-types avoids a conflict for anyone who keeps both in their tsconfig types.

Breaking / deferred: None.

Changes

Acceptance criteria (from #2811)

  • Framework stance: Endorse import.meta.main — it's already a runtime guarantee via deno_core; now a typed framework-standard idiom.
  • Type augmentation: Added to vertz/client / @vertz/ui/client with JSDoc.
  • Migration: Not applicable — call sites continue to use import.meta.main.
  • Docs updated: guides/env.mdx (both mint-docs and site) reflect the decision.

Test plan

  • cargo test --test v8_integration test_import_meta_main — 2 new Rust integration tests pass (main/side modules).
  • vtz test in packages/ui, packages/vertz, packages/create-vertz-app, packages/icons — all green.
  • tsc --noEmit in packages/ui, packages/vertz, sites/dev-orchestrator — clean (dev-orchestrator has one pre-existing unrelated error in agent-detail.tsx).
  • vtz run format + vtz run lint — clean.
  • cargo fmt --all -- --check + cargo clippy --all-targets -- -D warnings — clean.
  • Pre-push hooks (build-typecheck, lint, rust-clippy, rust-fmt, rust-test, test, trojan-source) — all green.

Reviews

Adversarial self-review surfaced a latent declaration-merge conflict between readonly main and bun-types' mutable main. Fixed in commit 6117fd9.

viniciusdacal and others added 2 commits April 22, 2026 09:39
Closes #2811.

Follow-up to #2777. The vtz runtime (via deno_core) already sets
`import.meta.main` on every module — true for the entry module, false
for imports — so the standard "run if main" guard works without polyfill.

Previously the type was only visible to projects that pulled in bun-types.
Add `readonly main: boolean` to the client augmentation so any tsconfig
that includes `"types": ["vertz/client"]` (or `"@vertz/ui/client"`) gets
it automatically. Scaffolded apps already have this entry.

Drop bun-types from sites/dev-orchestrator where it was only kept for
this type. examples/entity-todo and packages/icons keep their existing
type resolution — entity-todo still uses Bun.Transpiler in its test
compiler plugin, and icons picks up types globally.

Also add Rust integration tests verifying the runtime guarantee:
`test_import_meta_main_true_for_entry_module` and
`test_import_meta_main_false_for_imported_module`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Review follow-up. bun-types declares `ImportMeta.main` as mutable
`main: boolean` (not readonly). Declaration merging requires identical
modifiers across declarations, so a project that keeps BOTH bun-types
and vertz/client in its tsconfig `types` would hit `TS2687: All
declarations must have identical modifiers` once `skipLibCheck` is
turned off. The monorepo's skipLibCheck: true silences it today, but
it's a latent footgun for downstream users.

Drop `readonly` to match bun-types exactly. `import.meta.main` isn't
meant to be mutated by anyone anyway.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@viniciusdacal viniciusdacal merged commit 2c1616c into main Apr 22, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audit bun-types reliance: migrate import.meta.main usages

1 participant