{
 "meta": {
  "title": "Modules, Packages and Visibility — The Crush Language Guide",
  "description": "Generated from crush/modules.md of The Crush Language Guide (https://docs.crushlang.org/crush/modules.html). Edit the guide, not this file: it is regenerated on every build.",
  "frontends": [
   "crush"
  ],
  "author": "The Crush Language Guide",
  "tags": {
   "source": "crush/modules.md",
   "guide_url": "https://docs.crushlang.org/crush/modules.html",
   "generator": "scripts/build-notebooks.py"
  }
 },
 "cells": [
  {
   "id": "crush-modules-md-1",
   "kind": {
    "type": "markdown"
   },
   "source": "# Modules, Packages and Visibility\n\n> Verified against crush-ast `v0.13.0` (`dc0d0278`, 2026-12-03). Every behaviour on this\n> page was run with that build. The programs live in the verified corpus,\n> [`examples/verified/modules/`](https://github.com/nixpt/crush-language-guide/tree/main/examples/verified/modules),\n> each with the modules it imports in a `NAME.files/` directory beside it, and\n> `scripts/verify-examples` re-runs them.\n\nA Crush program can be split over several files. `import` loads another `.crush`\nfile **at compile time** (crush-ast CRUSH-110): the compiler parses it and merges\nits definitions into the program before checking it, so imported functions are\nchecked like your own, and the capability check sees imported code exactly as if\nit were local. Nothing is loaded at run time, and no capability is involved.\n\nBecause an import needs a file to search from, it works when you compile or run a\nfile (`crush run`, `crush-run run`, `crushc`, `crush-debug`, the REPL from its\nworking directory). It does not work in the browser: the Run buttons in this book\ncompile a string, and an `import` there is refused with \"`import x` was not\nresolved: … this compile has no file path to search from\". The examples on this\npage are therefore shown, not runnable; their output is the corpus's.\n\n## The three forms\n\nGiven `geom.crush` and `util/strings.crush` next to the program:\n\n```crush\n// geom.crush\nfn area(w, h) { return w * h }\nfn scaled(x) { return __unit() * x }\nfn __unit() { return 10 }\n```\n\n```crush\n// util/strings.crush\nfn shout(s) { return str.to_upper(s) + \"!\" }\nfn twice(s) { return s + s }\n```\n\n```crush\nimport geom\nimport util.strings as s\nimport util.strings { twice }\nprint(geom.area(3, 4))\nprint(geom.scaled(2))\nprint(s.shout(\"hi\"))\nprint(twice(\"ab\"))\nlet f = geom.area\nprint(f(2, 5))\n```\n\n```text\n12\n20\nHI!\nabab\n10\n```\n\n| Form | Binds | Call |\n|---|---|---|\n| `import geom` | the qualifier `geom` (the last segment of the path) | `geom.area(3, 4)` |\n| `import util.strings` | the qualifier `strings`; loads `util/strings.crush` | `strings.shout(\"hi\")` |\n| `import util.strings as s` | the qualifier `s` instead | `s.shout(\"hi\")` |\n| `import util.strings { twice }` | the bare name `twice` (and the qualifier only when `as x` is also given) | `twice(\"ab\")` |\n\n`geom.area` without a call is a function value, as for a local function. Inside a\nmodule, a bare call finds the module's own functions first (`scaled` calls\n`__unit` above), then the module's own imports, then the built-ins and\ncapabilities.\n\n## What a module may contain\n\nA module is **definitions only**: `fn`, `struct`, `impl`, `trait` (with `@kind`),\nand `import`s of other modules. Its `main`, if it has one, is ignored. Any other\ntop-level statement is a compile error naming the file and line, so a module has\nno init code and there is no hidden init order. Write a function for a constant\n(`fn limit() { return 10 }`):\n\n```text\n[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)\n```\n\n`export` is such a statement too: a module that contains `export f` is refused\n(`a top-level statement in imported module`). Exporting is not how modules work\nin Crush; see [Visibility](#visibility-and-pub) for what decides what another file\ncan call.\n\nImports go at the **top level** of a file. One inside a function, `fn main`\nincluded, is refused (`` `import` inside `fn main`: imports go at the top level of a\nfile ``; corpus `modules/import-in-main`). Up to v0.11 an import inside `fn main`\nwas accepted; it is an error since v0.12.0.\n\n## Where modules are found\n\n`import a.b` looks for `a/b.crush`, in this order; the first hit wins:\n\n1. the directory of the file that contains the `import`;\n2. each `-L DIR` (`--lib-path`) given to `crush-run`, `crushc`, `crush-debug` or\n   `crush-repl` (`crush run` and friends pass it on), in order;\n3. each directory in `CRUSH_PATH` (colon-separated).\n\nThere is no `a/b/mod.crush` form. A module that is not found is a compile error\nthat lists every directory searched:\n\n```text\n[compile] main.crush:1:1: `import consts`: module not found — no consts.crush in any of:\n  . (/home/me/proj)\n  libdir (/home/me/proj/libdir)\n```\n\n```bash\ncrush-run run -L lib main.crush             # lib/text/greet.crush for `import text.greet`\nCRUSH_PATH=shared crush-run run main.crush   # shared/consts.crush for `import consts`\n```\n\n(Corpus: `modules/lib-path`, `modules/crush-path`, `modules/import-not-found`.)\n\nEach file is loaded once per compile, so two modules that import a third share one\ncopy. Two modules that import each other are a compile error that shows the cycle\n(`import cycle: ca.crush → cb.crush → ca.crush`).\n\n## Errors that name the problem\n\nA name the module does not define lists what it does define (names starting with\n`__` are left out of the list):\n\n```text\n[compile] main.crush:5:7: `geom.perimeter`: module `geom` (geom.crush) defines no function `perimeter` (it defines: area, scaled)\n```\n\nA selectively imported name that collides with a function of the importing file\nis refused, naming both:\n\n```text\n[compile] main.crush:3:1: `import geom { area }` collides with `fn area` (line 4) of the same name in this file\n```\n\n## Structs across files\n\nA module's `struct`s and their methods work in the importer. Build values through\na function of the module: a struct **literal** is recognised only in the file that\ndeclares the struct, so `Rect { w: 1, h: 2 }` in the importer is\n`[compile] Undefined variable: Rect`.\n\n```crush\n// shapes.crush\nstruct Rect { w: Int, h: Int }\nimpl Rect {\n    fn area(self) { return self.w * self.h }\n}\nfn rect(w, h) { return Rect { w: w, h: h } }\n```\n\n```crush\nimport shapes\nlet r = shapes.rect(2, 3)\nprint(r)\nprint(r.area())\nr.w = 10\nprint(r.area())\n```\n\n```text\n{w: 2, h: 3}\n6\n30\n```\n\nFields are public: the importer can read and write them (`r.w = 10` above).\n\n### One namespace for struct and trait names\n\nFunction names are qualified by their module, but `struct` and `trait` names are\n**one global namespace** across all the files of a program. The same name declared\nin two files, whether two modules or a module and the file that imports it, is a\ncompile error that names both files (crush-ast #527):\n\n```crush\n// plane.crush\nstruct Point { x: Int, y: Int }\nfn origin() { return Point { x: 0, y: 0 } }\n```\n\n```crush\n// map.crush\nstruct Point { lat: Float, lon: Float }\nfn here() { return Point { lat: 27.7, lon: 85.3 } }\n```\n\n```crush\nimport plane\nimport map\nprint(plane.origin())\n```\n\n```text\n[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)\n```\n\nBefore #527 this compiled, the later `Point` silently replaced the earlier one,\nand `plane.origin()` failed at run time. Rename one of the two. A redeclaration\ninside a single file is not covered by this rule. Per-module kind names are\nplanned (crush-ast #523).\n\n## Packages\n\n`crush-pkg` (crush-ast's package manager) builds a package from a `capsule.toml`.\n`crush-pkg new NAME` scaffolds an application, `crush-pkg new NAME --lib` a library\nwhose entry is `src/lib.crush`. A package lists its dependencies as\n`[[dependencies]]` rows with a `path` (a local directory) or a `git` source:\n\n```toml\n[capsule]\nname = \"app\"\nversion = \"0.1.0\"\nentry = \"src/main.crush\"\nlanguage = \"crush\"\n\n[[dependencies]]\nname = \"mathlib\"\npath = \"../mathlib\"\n```\n\nWhen the entry file has an `import`, every dependency, and the package itself,\ncan be imported **by name** (crush-ast CRUSH-367): `import mathlib` loads the\ndependency's entry file, and `import mathlib.extra` loads `extra.crush` in the\ndirectory of that entry. The entry's own directory (`src/`) is searched for the\npackage's other files, as for any file. A package name wins over a file of the same\nname on the search path.\n\n```crush\n// app/src/main.crush\nimport mathlib { greet }      // ../mathlib/src/lib.crush\nimport mathlib.extra          // ../mathlib/src/extra.crush\nimport util                   // src/util.crush\n\nfn main() {\n    print(greet(\"app\"))\n    print(extra.twice(21))\n    print(util.shout(\"ok\"))\n}\n```\n\n```text\n$ crush-pkg run\nbuilding app v0.1.0\n  resolved dep mathlib -> …/mathlib/src/lib.crush\n  …\nrunning app v0.1.0\nhello, app\n42\nok!\n```\n\nThis was run by hand at the pinned commit (the corpus runs `crush-run` only), with\n`mathlib/src/lib.crush` as `crush-pkg new mathlib --lib` writes it\n(`pub fn greet(who) { return \"hello, \" + who }`; the scaffold marks it `pub` since\nv0.12.0, see [Visibility](#visibility-and-pub)), `mathlib/src/extra.crush` defining\n`twice`, and `app/src/util.crush` defining `shout`. Package names resolve only\nthrough `crush-pkg`: `crush-run run src/main.crush` in the same directory says\n`` `import mathlib`: module not found ``. An entry file with no `import` at all is\nbuilt the older way, with the dependencies' entry files pasted into one source.\n\n### Dependencies of dependencies\n\nSince v0.13.0 (crush-ast #613) `crush-pkg` follows each dependency's own\n`[[dependencies]]` (path or git) recursively into one graph. With `app` depending on\n`mid` and `mid` depending on `base`, only `app/capsule.toml` needs the `mid` row:\n\n```text\n$ cd app && crush-pkg run\nbuilding app v0.1.0\n  resolved dep mid -> /tmp/tp/app/../mid/src/lib.crush\n  resolved dep base -> /tmp/tp/app/../mid/../base/src/lib.crush\n  compiled /tmp/tp/app/src/main.crush -> 3 function(s), 47 byte(s)\n  deps: mid, base\n  wrote /tmp/tp/app/target/app.cvm1\n  wrote /tmp/tp/app/target/app.casm\nrunning app v0.1.0\nhello, app!\nhello, direct\n```\n\n(`mid/src/lib.crush` has `import base { greet }` and `pub fn shout(who) { return greet(who) + \"!\" }`;\n`app/src/main.crush` has `import mid { shout }` and `import base { greet }` and prints\n`shout(\"app\")` and `greet(\"direct\")`.) The graph is flat: `app` can import `base`\ndirectly, although it does not list it. Two cases are refused:\n\n```text\ncrush-pkg[E-BUILDER]: dependency cycle detected: mid -> base -> mid\ncrush-pkg[E-BUILDER]: dependency 'base' conflict: 'mid' requires path /tmp/tp/base but 'app' requires path /tmp/tp/base2\n```\n\n(a cycle, and the same dependency name required from two different sources, tags\nor revisions). Run by hand with `crush-pkg` built from v0.13.0; as with the rest of\nthis section, the corpus cannot re-check it.\n\n## Visibility and `pub`\n\nVisibility is **per package** (crush-ast #519, since v0.12.0). A *package* is a\nproperty of the file, not of how it was imported: the longest of these roots that\ncontains the file's real path.\n\n- the **root package**: the directory of the file you run, and everything under it;\n- each **crush-pkg package** (a dependency's own directory);\n- each **`-L` / `CRUSH_PATH` directory**: one package per directory.\n\nSo `main.crush` and a sibling `util.crush` share a package, and a library found\nthrough `-L lib` is another one.\n\n- Without `pub`, a function is visible to every file of **its own package**.\n- `pub fn` (and `pub async fn`) **exports** it to other packages.\n- Inside one package `pub` changes nothing, so a function with `pub` and one without\n  behave the same for a sibling file (corpus `modules/pub-fn`).\n",
   "state": {
    "status": "done"
   },
   "meta": {},
   "outputs": []
  },
  {
   "id": "crush-modules-1",
   "kind": {
    "type": "crush"
   },
   "source": "pub fn add(a, b) { return a + b }\nfn sub(a, b) { return a - b }\nprint(add(2, 3))\nprint(sub(2, 3))\n",
   "state": {
    "status": "pending"
   },
   "meta": {
    "tags": [
     "guide",
     "runnable"
    ],
    "extra": {
     "guide": {
      "src": "crush/modules.md:325",
      "browser": "run",
      "expect_output": "5\n-1\n"
     }
    }
   },
   "outputs": []
  },
  {
   "id": "crush-modules-md-3",
   "kind": {
    "type": "markdown"
   },
   "source": "```crush\n// shapes.crush, next to this file, has `pub fn square(n)` and plain `fn cube(n)`:\n// the same package, so both are callable\nimport shapes\nprint(shapes.square(3))\nprint(shapes.cube(2))\n```\n\n```text\n9\n8\n```\n\n### Using a private function from another package\n\nA use of a function that is not `pub`, from another package (`q.f(…)`, a reference\n`q.f`, `import q { f }`), is the **warning `W-VIS01`**. It goes to stderr, and the\nprogram compiles and runs exactly as before (corpus `modules/pub-cross-package`: a\nlibrary under `-L lib` with `pub fn square`, `pub fn use_cube` and a private\n`fn cube`, used from the program). The compiler prints:\n\n```text\n[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\n  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)\n  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)\n```\n\nand the program's stdout is still `9`, `8`, `8`. `crushc -C --deny-warnings` fails\non `W-VIS01`; `crush-run` has no such flag, so its run goes on.\n\nTwo more diagnostics:\n\n- **`N-VIS01`**, a note: a module with **no `pub` anywhere** stays all-public (the\n  legacy rule, so old libraries keep working) and gets one note when another package\n  uses it (corpus `modules/pub-legacy`). As soon as the module marks one function\n  `pub`, the others are private.\n- **`W-VIS02`**: `pub fn __x`. The `__` prefix says \"internal\", `pub` says\n  \"exported\", so it is flagged. A leading `__` is otherwise only a naming\n  convention: it hides the name from the \"it defines: …\" lists in error messages\n  (corpus `modules/private-convention`).\n\n### `pub struct`, `pub trait`, and what `pub` does not cover\n\n`pub struct` and `pub trait` parse and run (they were `Undefined variable: pub`\nbefore v0.12.0; corpus `modules/pub-struct`, `modules/pub-trait`). `pub` is still a\ncontextual word, not a keyword: it means something only in front of `fn`,\n`async fn`, `struct` and `trait`.\n\n- **Only functions are checked.** Kind (`struct` / `trait`) names stay one global\n  namespace and are not visibility-checked yet (crush-ast #523).\n- **Fields** cannot be marked `pub`: `struct P { pub x: Int }` is a parse error\n  (``Expected `}` but found `x` ``), because `pub` is read as a field name with no\n  type. `struct P { pub: Int }` is legal and declares a field called `pub`. Member\n  visibility is crush-ast #525; fields are writable from anywhere.\n\n### The stage-2 gate for library authors\n\nIf you publish a library, check that it has no hidden reliance on the legacy rule:\n\n- `crushc --list-exports FILE|DIR` lists each module's `pub` and private names,\n  or `legacy: no pub, everything public`:\n\n  ```text\n  lib/legacy/legacy.crush: legacy: no pub, everything public\n    public: area\n  lib/shapes/shapes.crush: pub\n    pub: square, use_cube\n    private: cube, twice_square\n  ```\n\n- `crushc -C --deny-legacy-visibility [-L DIR] FILE` turns `W-VIS01` into an\n  error and a used legacy module into `E-VIS01` (exit 1); with `--list-exports` it\n  fails on every importable legacy module. `crush-pkg build --deny-legacy-visibility`\n  does both for a package. `crush-pkg new --lib` scaffolds a `pub fn greet`, so a new\n  library passes it.\n\nBefore 1.0 (feature freeze 2026-12-31) the legacy rule is to end and `W-VIS01` to\nbecome an error (crush-ast #524; both are still warnings and notes at v0.13.0). Mark\nevery function other packages should call with `pub` now.\n\n## Remote imports and other forms\n\n`import @http \"…\"`, `import @git \"…\"`, `import @file \"…\"` and `use @mcp \"…\"` are\ncompile errors: they are not supported yet. `use @lang python \"math\"` is a\ndifferent thing that works: it writes `import math` into every later `@python`\nblock (see [Polyglot](https://docs.crushlang.org/crush/polyglot.html)). See [Import Styles](https://docs.crushlang.org/examples/imports.html)\nfor each form with its current result.\n",
   "state": {
    "status": "done"
   },
   "meta": {},
   "outputs": []
  }
 ]
}
