Modules and Packages
Real programs outgrow one file. rnx keeps growth boring on purpose: one namespace per file, imports that say exactly what they take, and a manifest that fits on an index card.
One file, one namespace
Each file is its own namespace. Import names, aliases, whole namespaces, or a file for its side effects:
import { Map } from "@std/collections";
import { AtomicInt } from "@std/sync";
let scores = new Map<String, Int>();
scores.set("ore", 7);
AtomicInt.byId(1).set(scores.len());
println!(scores.get("ore"));That prints 7. Relative imports (./math, ./shapes) wire up multi-file projects, and two files can each define a helper with the same name without colliding. Names live in their file's namespace. Imports are per file: if lib.rnx names something it never imported, it fails, even when the entry file imports it. Dependencies travel with the file that declares them, never with the program around them.
An imported namespace is also a value. Passing it to print lists its exports, read-only:
import io from "@std/io";
println!(io);That prints the module listing ([Module io] { clear: ..., write: ..., ... }, the exact set grows with the stdlib). Assigning to a namespace field is an error. Namespaces are snapshots, not objects.
Every file also sees @std/prelude without importing it: Int, Float, String, Bool, Array, print, assert resolve from there, and only the ones you use reach the binary.
Entry files run; imported files declare
Only the entry file (the entries.main file in Project.config, or the file handed to rnx run) executes loose top-level statements. That is where scripts live: no main wrapper needed for a quick task, top-level await for async work, and return <Int> for the exit code:
import { Map } from "@std/collections";
let scores = new Map<String, Int>();
scores.set("ore", 7);
let total = await Promise.resolve(scores.len());
println!("entries:", total);
if total == 0 {
return 1;
}That prints entries: 1. Every file pulled in through import is declarative-only: classes, structs, enums, functions, and constants. A loose print or let there fails with E112. Move it into a function and call it from the entry file. An explicit fn main()/fn Main() in the entry still works exactly as before.
Common mistake: putting script logic in an imported helper file and wondering why nothing runs, or worse, getting E112 and deleting the code. Imported files declare; only the entry executes. Move the statements into a function, import it, call it.
Two foreign import forms
Most imports name a module path ("./util", "@std/fs"). Two from forms declare functions the package does not define:
import { fn puts(text: String): Int } from native "c";
import { fn __rnx_clock_mono(): Int } from runtime "time";from native "lib" declares a foreign C function for the linker or JIT (see Hardware and FFI). from runtime "mod" declares a compiler runtime internal, and it only works inside registry-verified @std/* sources, where the signature is checked against the runtime manifest (module, arity, and types). Anywhere else it fails closed with E108 naming the owning @std/* wrapper, and a bare __rnx_* use with no declaration is an E303 scope error pointing at the declared form. User code never needs the second line: call the @std/* wrapper and the capability audit follows the call through it.
The standard library is seeded, not embedded
@std/ imports resolve from a global cache the installer seeds. Nothing to install by hand, versions pinned automatically. Twenty-five modules ship with the compiler:
| Module | What it holds |
|---|---|
@std/foundation | Foundation types and Array/String extensions, with no io or log dependency |
@std/collections | Generic Map<K, V> and Set<T> |
@std/config | Parser and writer for the sandboxed .config document format |
@std/async | Promise, PromiseWithResolvers, PromiseResult |
@std/sync | AtomicInt, Mutex, Channel, Barrier |
@std/fs | File and Path with text and binary I/O, one-shot operations with *Async pool twins, and mmap |
@std/bytes | ByteBuffer fixed-size raw byte buffers |
@std/process | Process host control and child lifecycles |
@std/os | Platform queries (OS.platform(), OS.cpuCount(), ...) |
@std/simd | Vec4f lanes and reductions |
@std/math | Trig, Vec2, and numeric helpers |
@std/time | Clocks, durations, and the Date catalog stub |
@std/random | Seeded random generation |
@std/env | Environment variables and RnxHost |
@std/testing | Test helpers |
@std/web | URL, Headers, status codes, query strings |
@std/json | JSON parsing and stringification |
@std/net | TcpStream, TcpListener, TLS, DNS |
@std/io | Terminal streams with pretty-printing, line input, size queries, and raw mode |
@std/log | Leveled logging macros writing to stderr, filtered by RNX_LOG |
@std/uefi | Prelude and console output for x86_64-unknown-uefi firmware applications |
@std/wasi | Prelude and runtime surface for wasm32-unknown-wasi |
@std/ui | Declarative View values, layout, controls, text, themes, and accessibility |
@std/app | Native window, frame clock, input, routing, and the headless Driver |
@std/prelude | Foundation types (auto-imported) |
Each module has a generated reference page under its module docs, rendered from the same /** */ doc comments you write with rnx doc. Every registry package gets the same treatment: /docs/@scope/name/overview serves the overview, getting-started, and API reference for any published scoped package, std included, and unscoped packages serve the same pages at /docs/<name>/overview. Standard library packages also ship docs/ guides with each release, readable on the same pages and on the package's Guides tab.
The cache is the offline story: with a seeded machine, builds never touch the network. Missing entries download once from the registry, integrity-checked like any other package, and an explicit dependencies entry for an @std/* package overrides the pinned version. A cold cache with no network fails loudly (E108 naming the registry, with an rnx download hint); rnx download seeds the cache and rnx doctor --repair-std repairs it. Offline labs are served by distribution. Seeded installers and pre-warmed images carry the standard library.
The web playground works the same way, minus the filesystem. Its compiler engine downloads first, then preloads @std/prelude and fetches each further @std/* import from the same site's registry at the engine-pinned version, caching every module in the browser beside the engine bytes. Module paths stay identical, so code that checks in the playground checks the same on the command line.
Under npm run dev, the playground reads @std/* from compiler/stdlib/src in the checkout and skips the browser cache. The local registry route is preferred over the seeded database, so a stdlib edit is visible on the next run without publishing or restarting the site.
A taste of the platform side: child processes and OS queries compose like everything else:
import { Process } from "@std/process";
import { OS } from "@std/os";
println!(OS.platform(), OS.cpuCount());
let out = Process.run("echo", ["hello"]);
println!(out.exitCode, out.stdoutText().contains("hello"));On a Linux box that prints something like linux 16 followed by 0 true. That is exit code zero, and yes, the output contained "hello".
The project manifest
Project.config is an .rnx module exporting a default object. It declares the package name, version, and entry file. Dependencies use SemVer ranges, local paths, or git pins. The manifest language is the sandboxed subset described in Packages and Registries; programs that read or write the same format at runtime use @std/config:
export default {
project: {
name: "colony",
version: "0.4.0"
},
entries: {
main: "src/main.rnx"
},
dependencies: {
sqlite3: "^3.45.0",
physics_2d: { path: "../physics_2d" }
}
}rnx lock writes a deterministic Project.deplock checksum file; rnx run --locked and rnx build --locked re-verify every checksum before executing. Workspaces (workspace with members) hold monorepos whose members resolve to each other by name.
Testing from day one
Tests live beside your code in test fn blocks: no parameters, no return, stripped from normal builds:
fn add(a: Int, b: Int): Int {
return a + b;
}
test fn addsUp() {
assert(add(20, 22) == 42, "addition");
}
println!(add(20, 22));rnx test ✓ adds_up 0.02 ms
tests: 1 passed, 0 failed in 0.14 msassert takes a Bool and a message. A failing assertion marks that test failed, but the rest of the suite keeps running. Write the test next to the function while the behavior is fresh in your head. Coming back to add tests later rarely happens.
Where to go from here
You now have the whole working language. Two directions remain:
- Build something real: the standard library docs cover every module, and the Playground runs everything in your browser.
- Understand the machinery: the Language Manual covers the numeric model, deterministic ARC, protocols, concurrency, and the toolchain spec, normatively.