Repository navigation
Modules and Imports
This document defines the minimal, deterministic module/import story for Curlee.
-
import ...;is supported. - Parser enforces that imports must appear before any functions in a file.
- Resolver now performs local, deterministic resolution:
- import roots (v1):
- the importing file’s directory
- the entry file’s directory (only if different)
- zero or more explicit stdlib roots (
--stdlib-root,CURLEE_STDLIB_ROOT) in declaration order
- module-to-file mapping:
import foo.bar;→foo/bar.curlee - missing modules produce a diagnostic
- ambiguous modules (multiple roots match) are rejected with a diagnostic
- import roots (v1):
- Optional import aliasing is supported:
import foo.bar as baz;. - Module-qualified references/calls are supported:
-
alias.memberfor an aliased import -
foo.bar.memberfor a path-qualified import
-
This is intentional: Curlee should not “guess” meaning for unresolved modules.
Remote/network module resolution policy (v1):
- Curlee v1 resolves imports only from deterministic local roots (
importing,entry, and explicit stdlib roots). - Remote registries, URL-based imports, and network fetch during import resolution are out of scope.
Top-level imports only:
import foo;
import foo.bar;
-
importmust appear at the top level. -
importdeclarations must appear before anyfndeclarations. - The module path is a dotted identifier sequence.
- A trailing
;is required.
Import aliasing:
import foo.bar as baz;
-
as <alias>is optional. - If present,
<alias>introduces a module qualifier name that can be used to reference declarations from the imported module. - Aliases must be unique in the file’s top-level scope; conflicts are errors.
Qualified references:
import foo.bar;
import foo.bar as baz;
fn main() -> Unit {
// Path-qualified:
foo.bar.helper();
// Alias-qualified:
baz.helper();
return 0;
}
Resolver rules (deterministic):
- A dot-chain expression like
a.b.cis treated as a module-qualified reference iff the qualifier portion matches an imported module:- alias-qualified:
ais an alias fromimport ... as a; - path-qualified:
a.bexactly matches a module path from animport a.b;
- alias-qualified:
- If the qualifier matches an imported module but the final member name does not exist in that module’s exports, the resolver must emit a clear error.
- Otherwise, dot-chains are treated as normal member access.
This section describes planned expansions while keeping today’s behavior deterministic.
- Deterministic resolution: module resolution must not depend on network access or ambient state.
- Search paths: v1 uses two deterministic roots (importing directory, then entry directory). Post‑MVP, additional roots must be explicitly specified and must not rely on heuristics.
-
File mapping:
import foo.bar;maps tofoo/bar.curlee(and/orfoo/bar/index.curleeonly if we explicitly add that rule later). -
No dynamic imports: importing must be static and fully known at
checktime. - No implicit side effects: importing does not execute code; it only makes declarations available for checking.
This algorithm is used for workspace source runs (curlee check / curlee run without --bundle).
Given:
- the entry file path (the file passed on the CLI), and
- a module import
import a.b.c;inside some importing fileF,
Curlee constructs an ordered list of import roots:
-
dir(F)(the importing file’s directory) -
dir(entry)(the entry file’s directory), only if it differs fromdir(F) - each explicit stdlib root, in the order provided
For each root, Curlee probes the single mapped path:
<root>/a/b/c.curlee
Rules:
- If exactly one root contains the module file, that file is used.
- If no roots contain the module file, Curlee errors with
import not found: 'a.b.c'. - If multiple roots contain the module file, Curlee errors with
ambiguous import: 'a.b.c'and lists each matching path.
This is intentionally strict: Curlee must not “guess” which module you meant.
Debugging:
- If
CURLEE_DEBUG_IMPORTSis set, the CLI prints each probed path.
CLI/env inputs for workspace runs:
-
--stdlib-root <path>and--stdlib-root=<path>append import roots (repeatable). -
CURLEE_STDLIB_ROOTprovides additional roots as a:-separated list. - Effective precedence is deterministic by ordered root list: local, entry, then stdlib roots.
Running a bundle (curlee run --bundle <file.bundle> <file.curlee>) does not resolve imports from the filesystem.
- Imports are resolved when the bundle is built/checked.
- The bundle manifest records a deterministic list of import pins (
path:hash) for the sources used to produce the bundled bytecode. - At bundle runtime, Curlee executes the bundled bytecode; import roots/search paths are irrelevant.
-
--stdlib-rootandCURLEE_STDLIB_ROOTdo not affect module resolution at bundle runtime.
Curlee now supports deterministic dependency lockfiles for workspace source builds.
Commands:
curlee deps lock [--root <dir>] [--stdlib-root <dir>] <entry.curlee> <deps.lock>curlee deps verify [--root <dir>] [--stdlib-root <dir>] <entry.curlee> <deps.lock>curlee deps update [--root <dir>] [--stdlib-root <dir>] <entry.curlee> <deps.lock>
Behavior:
-
deps lockruns the same static check pipeline and writes a lockfile containing:-
entry_hash: hash of entry source contents -
imports: deterministic module pins (module-id:content-hash)
-
-
deps verifyrecomputes the snapshot and fails on:- entry hash drift,
- import pin drift,
- invalid lockfile format.
Update lifecycle:
- Use
curlee deps update ...to refresh an existing lockfile after intentional dependency changes. -
curlee deps lock ...is still valid for initial lockfile creation. - Re-run
curlee deps verify ...in CI to enforce reproducibility.
Deterministic module IDs:
- For project-local modules under entry root:
entry/<relative-module-path-without-.curlee> - For stdlib roots (in configured order):
stdlib<N>/<relative-module-path-without-.curlee>
This avoids host-specific absolute paths in lockfile pins and keeps dependency snapshots portable across machines.
Troubleshooting:
- If lock generation fails with deterministic pin errors, ensure imports resolve under explicit
--root/--stdlib-rootdirectories. - Keep stdlib roots ordered consistently between
deps lockanddeps verifyruns.
Curlee v1 does not use an implicit ambient stdlib path.
- Source-level stdlib/module lookup is opt-in and explicit via
--stdlib-rootand/orCURLEE_STDLIB_ROOT. - Builtins (e.g. capability types and VM intrinsics) are still provided by the compiler/VM.
- Any stdlib modules that participate in compilation are represented in bundle manifests through import pins/hashes.
- Packages/registries
- Version solving
- Incremental compilation
- Cyclic dependency handling beyond clear diagnostics
This section specifies deterministic package layout conventions so that different tools (CLI, LSP, tests) agree on module-to-file mapping.
Resolution should use an explicit, ordered list of import roots.
- An import root is a directory path supplied explicitly (e.g. via CLI flags) or derived via a deterministic rule.
- The same module path must resolve to the same file for the same set/order of import roots.
- Import roots must be treated as read-only inputs to compilation.
For compatibility with current behavior, a minimal deterministic rule is:
- The directory of the importing file is always considered an import root for that file.
Additional roots (e.g. project root, CLI entry file directory) must be specified by an explicit, deterministic rule (preferably a CLI flag) rather than by heuristics.
Within an import root, import foo.bar; maps to:
<root>/foo/bar.curlee
Rules:
- Module path segments map to path segments.
- The file extension is always
.curlee. - No implicit
index.curleerule unless explicitly introduced. - No implicit case conversions.
- No probing multiple extensions.
- No network access.
If multiple import roots contain a matching module file:
- This is an error (ambiguous import). Curlee must reject rather than silently choose.
This section defines incremental compilation boundaries at the conceptual level, without committing to a particular cache format.
An incremental compilation boundary is the smallest unit that can be cached and reused without changing compilation results.
For Curlee, the natural boundary is a module file plus the compiler configuration that affects its meaning.
At minimum, the cache key for a module should include:
- Canonical module file path (as resolved through import roots)
- Content hash of the module file
- Compiler version (or an internal schema/version for cached artifacts)
- Relevant compiler options that affect parsing/resolution/typechecking/verification/codegen
- Import root list and ordering (or a hash of it)
If any key component changes, the cached results for that module are invalid.
Candidate cache layers (each layer depends on the previous ones):
- Lexed token stream
- Parsed AST
- Resolved names (module exports + imports)
- Typed IR / type inference results
- Verification obligations (and solver results, if sound to cache)
- Lowered bytecode / VM program image
Incremental compilation must be correct under changes to any transitive dependency.
- If a module changes, any importer whose semantics depends on the changed module must be invalidated.
- A conservative approach is acceptable: invalidate the entire reverse-dependency slice.
- The dependency graph must be derived deterministically from source and import roots.
Incremental compilation must not introduce ambient nondeterminism:
- Do not use wall-clock timestamps in cache keys.
- Do not depend on environment variables unless they are explicit compiler inputs.
- Ensure stable ordering (imports, filesystem listings, diagnostics) via explicit sorting.
This issue is design/documentation only. Before implementing non-trivial module search paths or incremental compilation, the project must add a hard gate so we do not drift into ad-hoc behavior.
Minimum gate:
- Add golden/unit tests that lock module-to-file mapping (including multiple import roots and collision diagnostics).
- Add tests that lock cache invalidation behavior for simple dependency graphs.
- Define the exact CLI surface for import roots (flags) and ensure LSP uses the same rule.
- Document any new rules here before they ship.