Modules, Packages and Visibility
Open as a notebook (.crush-nb, how to run it)
Verified against crush-ast
v0.13.0(dc0d0278, 2026-12-03). Every behaviour on this page was run with that build. The programs live in the verified corpus,examples/verified/modules/, each with the modules it imports in aNAME.files/directory beside it, andscripts/verify-examplesre-runs them.
A Crush program can be split over several files. import loads another .crush
file at compile time (crush-ast CRUSH-110): the compiler parses it and merges
its definitions into the program before checking it, so imported functions are
checked like your own, and the capability check sees imported code exactly as if
it were local. Nothing is loaded at run time, and no capability is involved.
Because an import needs a file to search from, it works when you compile or run a
file (crush run, crush-run run, crushc, crush-debug, the REPL from its
working directory). It does not work in the browser: the Run buttons in this book
compile a string, and an import there is refused with “import x was not
resolved: … this compile has no file path to search from”. The examples on this
page are therefore shown, not runnable; their output is the corpus’s.
The three forms
Given geom.crush and util/strings.crush next to the program:
// geom.crush
fn area(w, h) { return w * h }
fn scaled(x) { return __unit() * x }
fn __unit() { return 10 }
// util/strings.crush
fn shout(s) { return str.to_upper(s) + "!" }
fn twice(s) { return s + s }
import geom
import util.strings as s
import util.strings { twice }
print(geom.area(3, 4))
print(geom.scaled(2))
print(s.shout("hi"))
print(twice("ab"))
let f = geom.area
print(f(2, 5))
12
20
HI!
abab
10
| Form | Binds | Call |
|---|---|---|
import geom | the qualifier geom (the last segment of the path) | geom.area(3, 4) |
import util.strings | the qualifier strings; loads util/strings.crush | strings.shout("hi") |
import util.strings as s | the qualifier s instead | s.shout("hi") |
import util.strings { twice } | the bare name twice (and the qualifier only when as x is also given) | twice("ab") |
geom.area without a call is a function value, as for a local function. Inside a
module, a bare call finds the module’s own functions first (scaled calls
__unit above), then the module’s own imports, then the built-ins and
capabilities.
What a module may contain
A module is definitions only: fn, struct, impl, trait (with @kind),
and imports of other modules. Its main, if it has one, is ignored. Any other
top-level statement is a compile error naming the file and line, so a module has
no init code and there is no hidden init order. Write a function for a constant
(fn limit() { return 10 }):
[compile] state.crush:2:1: a top-level `let` in imported module `state`: a module may only define functions, structs, impls and traits, and import other Crush modules — it has no init code (move the statement into a function)
export is such a statement too: a module that contains export f is refused
(a top-level statement in imported module). Exporting is not how modules work
in Crush; see Visibility for what decides what another file
can call.
Imports go at the top level of a file. One inside a function, fn main
included, is refused (`import` inside `fn main`: imports go at the top level of a file; corpus modules/import-in-main). Up to v0.11 an import inside fn main
was accepted; it is an error since v0.12.0.
Where modules are found
import a.b looks for a/b.crush, in this order; the first hit wins:
- the directory of the file that contains the
import; - each
-L DIR(--lib-path) given tocrush-run,crushc,crush-debugorcrush-repl(crush runand friends pass it on), in order; - each directory in
CRUSH_PATH(colon-separated).
There is no a/b/mod.crush form. A module that is not found is a compile error
that lists every directory searched:
[compile] main.crush:1:1: `import consts`: module not found — no consts.crush in any of:
. (/home/me/proj)
libdir (/home/me/proj/libdir)
crush-run run -L lib main.crush # lib/text/greet.crush for `import text.greet`
CRUSH_PATH=shared crush-run run main.crush # shared/consts.crush for `import consts`
(Corpus: modules/lib-path, modules/crush-path, modules/import-not-found.)
Each file is loaded once per compile, so two modules that import a third share one
copy. Two modules that import each other are a compile error that shows the cycle
(import cycle: ca.crush → cb.crush → ca.crush).
Errors that name the problem
A name the module does not define lists what it does define (names starting with
__ are left out of the list):
[compile] main.crush:5:7: `geom.perimeter`: module `geom` (geom.crush) defines no function `perimeter` (it defines: area, scaled)
A selectively imported name that collides with a function of the importing file is refused, naming both:
[compile] main.crush:3:1: `import geom { area }` collides with `fn area` (line 4) of the same name in this file
Structs across files
A module’s structs and their methods work in the importer. Build values through
a function of the module: a struct literal is recognised only in the file that
declares the struct, so Rect { w: 1, h: 2 } in the importer is
[compile] Undefined variable: Rect.
// shapes.crush
struct Rect { w: Int, h: Int }
impl Rect {
fn area(self) { return self.w * self.h }
}
fn rect(w, h) { return Rect { w: w, h: h } }
import shapes
let r = shapes.rect(2, 3)
print(r)
print(r.area())
r.w = 10
print(r.area())
{w: 2, h: 3}
6
30
Fields are public: the importer can read and write them (r.w = 10 above).
One namespace for struct and trait names
Function names are qualified by their module, but struct and trait names are
one global namespace across all the files of a program. The same name declared
in two files, whether two modules or a module and the file that imports it, is a
compile error that names both files (crush-ast #527):
// plane.crush
struct Point { x: Int, y: Int }
fn origin() { return Point { x: 0, y: 0 } }
// map.crush
struct Point { lat: Float, lon: Float }
fn here() { return Point { lat: 27.7, lon: 85.3 } }
import plane
import map
print(plane.origin())
[compile] map.crush:1:1: struct `Point` is also declared in plane.crush:1:1: struct and trait names are one global namespace across imports, so two files cannot both declare `Point` (rename one; per-module Kinds are planned, #523)
Before #527 this compiled, the later Point silently replaced the earlier one,
and plane.origin() failed at run time. Rename one of the two. A redeclaration
inside a single file is not covered by this rule. Per-module kind names are
planned (crush-ast #523).
Packages
crush-pkg (crush-ast’s package manager) builds a package from a capsule.toml.
crush-pkg new NAME scaffolds an application, crush-pkg new NAME --lib a library
whose entry is src/lib.crush. A package lists its dependencies as
[[dependencies]] rows with a path (a local directory) or a git source:
[capsule]
name = "app"
version = "0.1.0"
entry = "src/main.crush"
language = "crush"
[[dependencies]]
name = "mathlib"
path = "../mathlib"
When the entry file has an import, every dependency, and the package itself,
can be imported by name (crush-ast CRUSH-367): import mathlib loads the
dependency’s entry file, and import mathlib.extra loads extra.crush in the
directory of that entry. The entry’s own directory (src/) is searched for the
package’s other files, as for any file. A package name wins over a file of the same
name on the search path.
// app/src/main.crush
import mathlib { greet } // ../mathlib/src/lib.crush
import mathlib.extra // ../mathlib/src/extra.crush
import util // src/util.crush
fn main() {
print(greet("app"))
print(extra.twice(21))
print(util.shout("ok"))
}
$ crush-pkg run
building app v0.1.0
resolved dep mathlib -> …/mathlib/src/lib.crush
…
running app v0.1.0
hello, app
42
ok!
This was run by hand at the pinned commit (the corpus runs crush-run only), with
mathlib/src/lib.crush as crush-pkg new mathlib --lib writes it
(pub fn greet(who) { return "hello, " + who }; the scaffold marks it pub since
v0.12.0, see Visibility), mathlib/src/extra.crush defining
twice, and app/src/util.crush defining shout. Package names resolve only
through crush-pkg: crush-run run src/main.crush in the same directory says
`import mathlib`: module not found. An entry file with no import at all is
built the older way, with the dependencies’ entry files pasted into one source.
Dependencies of dependencies
Since v0.13.0 (crush-ast #613) crush-pkg follows each dependency’s own
[[dependencies]] (path or git) recursively into one graph. With app depending on
mid and mid depending on base, only app/capsule.toml needs the mid row:
$ cd app && crush-pkg run
building app v0.1.0
resolved dep mid -> /tmp/tp/app/../mid/src/lib.crush
resolved dep base -> /tmp/tp/app/../mid/../base/src/lib.crush
compiled /tmp/tp/app/src/main.crush -> 3 function(s), 47 byte(s)
deps: mid, base
wrote /tmp/tp/app/target/app.cvm1
wrote /tmp/tp/app/target/app.casm
running app v0.1.0
hello, app!
hello, direct
(mid/src/lib.crush has import base { greet } and pub fn shout(who) { return greet(who) + "!" };
app/src/main.crush has import mid { shout } and import base { greet } and prints
shout("app") and greet("direct").) The graph is flat: app can import base
directly, although it does not list it. Two cases are refused:
crush-pkg[E-BUILDER]: dependency cycle detected: mid -> base -> mid
crush-pkg[E-BUILDER]: dependency 'base' conflict: 'mid' requires path /tmp/tp/base but 'app' requires path /tmp/tp/base2
(a cycle, and the same dependency name required from two different sources, tags
or revisions). Run by hand with crush-pkg built from v0.13.0; as with the rest of
this section, the corpus cannot re-check it.
Visibility and pub
Visibility is per package (crush-ast #519, since v0.12.0). A package is a property of the file, not of how it was imported: the longest of these roots that contains the file’s real path.
- the root package: the directory of the file you run, and everything under it;
- each crush-pkg package (a dependency’s own directory);
- each
-L/CRUSH_PATHdirectory: one package per directory.
So main.crush and a sibling util.crush share a package, and a library found
through -L lib is another one.
- Without
pub, a function is visible to every file of its own package. pub fn(andpub async fn) exports it to other packages.- Inside one package
pubchanges nothing, so a function withpuband one without behave the same for a sibling file (corpusmodules/pub-fn).
pub fn add(a, b) { return a + b }
fn sub(a, b) { return a - b }
print(add(2, 3))
print(sub(2, 3))
5
-1
// shapes.crush, next to this file, has `pub fn square(n)` and plain `fn cube(n)`:
// the same package, so both are callable
import shapes
print(shapes.square(3))
print(shapes.cube(2))
9
8
Using a private function from another package
A use of a function that is not pub, from another package (q.f(…), a reference
q.f, import q { f }), is the warning W-VIS01. It goes to stderr, and the
program compiles and runs exactly as before (corpus modules/pub-cross-package: a
library under -L lib with pub fn square, pub fn use_cube and a private
fn cube, used from the program). The compiler prints:
[warning] [W-VIS01] m1.crush:4: `cube` is not `pub` in lib/shapes/shapes.crush, so it is private to the library directory lib; this use (the call `shapes.cube(…)`) is in the root package
hint: add `pub` to `fn cube` in lib/shapes/shapes.crush if other packages may use it (this warning becomes an error before 1.0, #524)
lib/shapes/shapes.crush has a `pub`, so the names it does not mark are private to the library directory lib: cube, twice_square (`pub`: square, use_cube)
and the program’s stdout is still 9, 8, 8. crushc -C --deny-warnings fails
on W-VIS01; crush-run has no such flag, so its run goes on.
Two more diagnostics:
N-VIS01, a note: a module with nopubanywhere stays all-public (the legacy rule, so old libraries keep working) and gets one note when another package uses it (corpusmodules/pub-legacy). As soon as the module marks one functionpub, the others are private.W-VIS02:pub fn __x. The__prefix says “internal”,pubsays “exported”, so it is flagged. A leading__is otherwise only a naming convention: it hides the name from the “it defines: …” lists in error messages (corpusmodules/private-convention).
pub struct, pub trait, and what pub does not cover
pub struct and pub trait parse and run (they were Undefined variable: pub
before v0.12.0; corpus modules/pub-struct, modules/pub-trait). pub is still a
contextual word, not a keyword: it means something only in front of fn,
async fn, struct and trait.
- Only functions are checked. Kind (
struct/trait) names stay one global namespace and are not visibility-checked yet (crush-ast #523). - Fields cannot be marked
pub:struct P { pub x: Int }is a parse error (Expected `}` but found `x`), becausepubis read as a field name with no type.struct P { pub: Int }is legal and declares a field calledpub. Member visibility is crush-ast #525; fields are writable from anywhere.
The stage-2 gate for library authors
If you publish a library, check that it has no hidden reliance on the legacy rule:
-
crushc --list-exports FILE|DIRlists each module’spuband private names, orlegacy: no pub, everything public:lib/legacy/legacy.crush: legacy: no pub, everything public public: area lib/shapes/shapes.crush: pub pub: square, use_cube private: cube, twice_square -
crushc -C --deny-legacy-visibility [-L DIR] FILEturnsW-VIS01into an error and a used legacy module intoE-VIS01(exit 1); with--list-exportsit fails on every importable legacy module.crush-pkg build --deny-legacy-visibilitydoes both for a package.crush-pkg new --libscaffolds apub fn greet, so a new library passes it.
Before 1.0 (feature freeze 2026-12-31) the legacy rule is to end and W-VIS01 to
become an error (crush-ast #524; both are still warnings and notes at v0.13.0). Mark
every function other packages should call with pub now.
Remote imports and other forms
import @http "…", import @git "…", import @file "…" and use @mcp "…" are
compile errors: they are not supported yet. use @lang python "math" is a
different thing that works: it writes import math into every later @python
block (see Polyglot). See Import Styles
for each form with its current result.