Folders become a canonical source graph with providers, consumers and one compile per physical module.

Turning folders into a compiler graph

A directory is a convenience for humans. It does not tell the compiler which files form one semantic unit, which root owns them or which module must compile before another can bind a dependency.

Stage 0 answers those questions. It turns the greeting project's filesystem tree into a canonical source graph.

Nodes and edges

A graph here means nodes plus directed edges with meaning.

Nodes include modules, packages and other compilation units the build system recognises.

Edges record relationships: this module depends on that provider, this package depends on that package.

A provider supplies a public interface. A consumer binds against that interface after the provider succeeds.

Not every @ clause makes an edge between modules, though. In the running example @entry.moth and greeting.moth share a directory, so they are one module and @greeting binds a sibling source file directly. A provider edge appears when a dependency resolves to a different module or package, and even then it names a relationship rather than copying greet's body across. Think of a graft point, not a clone.

The root marks the boundary. Everything beside it in the same directory is owned source, which is why the walkthrough compiles one module rather than two.

Roots, packages and ownership

Moth marks module roots in the filesystem on purpose.

Each root file is a separate visibility scope. Its top-level declarations do not leak into another root or ordinary helper files. Helpers are shared and prepared once, and a root binds them through ordinary dependency clauses. Only $export-marked declarations and direct selections in @mod.moth or @package.moth enter a public facade.

Source-backed packages compile Moth source into ordinary module artefacts. They compile as separate package graphs with their own config, their own private @project and their own source index, and consumers bind the public facade rather than reaching inside. Every registered source package publishes its external API through a directly contained @package.moth, so @mod.moth alone is insufficient. Binding-backed packages expose typed external interfaces without Moth module bodies or a source facade. Both appear in the capability surface. Only source-backed modules produce HIR.

Filesystem layout defines module boundaries. The graph preserves those boundaries rather than inventing another topology.

Owned and semantic source sets

Stage 0 distinguishes sets that newcomers often collapse.

OwnedSourceSet. Files this module root owns on disk.

SemanticSourceSet. Files that participate in semantic compilation for the command.

Check-only orphan units. Files the tool may still parse for diagnostics even when they sit outside the semantic set for a build.

Ownership does not inject disconnected declarations into a consumer. A file can be owned and still remain outside the semantic set for a given command. Dependency visibility and ownership are different axes.

Provider-first scheduling

After the graph finalises, the build system schedules compile waves. Providers finish before consumers bind, root growth before branch binding.

A module waits until every required provider interface exists. Binding-backed interfaces may appear earlier than source providers because they do not wait on Moth module compilation.

One physical module compiles once per project or package compilation boundary. Entries later activate roots. They do not recompile the same module once per page.

Scheduling is where the build system stops. When a module is ready it builds one compiler input and calls one compiler module compilation service, then handles the outcome: success, diagnosed or an internal compiler error. Binding, ordering, AST, HIR and borrow validation happen inside that call. The build system never runs those stages step by step from its own code.

The greeting project is the simple case, and worth being precise about. @entry.moth and greeting.moth live in the same directory, so they form one canonical module that compiles once with greeting.moth as owned source. Binding @greeting greet happens inside that single compilation. Provider-first waves matter when a dependency crosses a module or package boundary, which this example never does.

Deterministic identity

Graphs need names that do not jitter when threads finish in a different order.

Stage 0 and the compiler assign stable identities to sources, modules and later declarations. Exact Rust type names may change. The rule does not: identity must not depend on worker completion order, cosmetic filename suffixes for roots or source position alone.

Determinism lets parallel compilation merge results in canonical order. Diagnostics and artefacts stay reproducible.

What can fail here

Why Moth does it this way

Alternative: global search paths, nearest-match resolution or "scan the package folder and hope."

Choice: structural roots, explicit package identity and canonical ownership.

Benefit: graph scheduling and diagnostics share the same source facts. A missing provider is a graph fact, not a surprise during type checking.

Cost: filesystem layout carries semantic constraints authors must learn.

Roadmap and current status

Compiler references

Exit state

You leave with a canonical source graph, source sets and ordered compile waves. Next: how the compiler prepares each selected source file once, starting from characters.

Compiler design / Previous / Next