On this page

0319. Single-File Manifestless #! Scripts (B10)

Context

Tenon packages require a tenon.toml manifest and tenon.lock lockfile before they can import edition-2026 plugins or use derive. For quick operational scripts, CI glue, data-wrangling one-offs, and agent-authored executables, creating a full package directory with tenon.toml, tenon.lock, .tenon/, and gen/ is unnecessary ceremony.

Developers and AI agents need to write a single executable .tn file with #!/usr/bin/env tenon, optionally declare its edition, capability ceiling, and embedded edition-2026 dependencies via //! header directives, and run it directly (./script.tn args..., tenon run script.tn, tenon interp script.tn, tenon check script.tn, tenon test script.tn) with zero disk clutter in the script's directory and ~1–3 ms warm native startup.

Decision

  1. Line-1 #! Shebang Tolerance (crates/tenon-frontend/src/source.rs):

    • A leading #!... line on line 1 is neutralized in-place to //... (identical 2-byte length) during script parsing so ordinary lexing and span computation preserve 100% exact 1-based line numbers, column numbers, and byte offsets.
  2. Top-of-File //! Script Header Directives (crates/tenon-compiler/src/packages/script.rs):

    • Optional directives before imports (after optional #! and blank lines):
      • //! edition = "2026" (defaults to "2026")
      • //! capabilities = ["fs", "process", "stdout"] (defaults to ["stdin", "stdout", "stderr"] when #! or //! is present)
      • //! deps = ["std_data", "std_validate", "std_regex", "json"]
    • Standalone scripts may be authored in either readable ordinary Tenon syntax or canonical compact ~1 syntax.
  3. Deterministic Offline Synthetic Manifest & Global Cache ($TENON_CACHE_DIR/scripts/<digest>/):

    • Keyed by SHA-256 of (format version, toolchain VERSION, script bytes).
    • Synthesizes an offline tenon.toml against embedded edition = "2026" plugins and materializes tenon.lock / .tenon/ strictly inside $TENON_CACHE_DIR/scripts/<digest>/ (default ~/.cache/tenon/scripts/<digest>/), leaving zero files in the script's own directory.
    • derive companions inside standalone scripts are materialized implicitly via ADR-0318 with zero gen/ directory in the script's directory.
    • Compiled native executables are cached at $TENON_CACHE_DIR/scripts/<digest>/bin-<mode> so warm #! / tenon run invocations skip parsing, typechecking, and C compilation entirely (~1–3 ms).
  4. CLI & Direct Shebang Invocation (crates/tenon/src/):

    • Direct invocation tenon <script.tn> [args...] (triggered by #!/usr/bin/env tenon) intercepts .tn script paths before CLI flag parsing so all trailing arguments (including --help, --json, -v, etc.) pass verbatim to the script.
    • tenon run <script.tn> [-- args...], tenon interp <script.tn> [-- args...], tenon check <script.tn>, and tenon test <script.tn> all transparently detect standalone scripts (no enclosing tenon.toml + #! or //! header or ordinary syntax) and run them against the cached synthetic workspace.
  5. Fail-Closed Diagnostics (E1230–E1249):

    • E1230: Malformed //! script header directive (syntax error, duplicate key, or unknown key).
    • E1231: //! script header directive appears after code or imports.
    • E1232: Unknown or invalid capability in //! capabilities.
    • E1233: Unsupported edition in //! edition.
    • E1234: Unknown plugin in //! deps (not in embedded edition catalog).
    • E1235: Dependency in //! deps requires a capability not declared in //! capabilities.
    • E1236: Standalone script cache or materialization I/O error.

Consequences