Skip to content

import: keep the prelude out of module imports - #279

Merged
vito merged 1 commit into
schema-values-over-builtinsfrom
dang-module-exports
Nov 9, 2026
Merged

vito merged 1 commit into
schema-values-over-builtinsfrom
dang-module-exports

Conversation

@vito

@vito vito commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

A native Dang module import ([imports.Lib] path = "./lib") now exposes only the module's own public declarations. Dang's builtins and prelude, which the module's own code still uses as usual, are no longer re-exported through Lib.

Problem

The import's namespace was the module's own type scope and value scope. Both hang off the prelude and the builtins, so any of their names resolved through the import, both in type checking and at runtime:

  • Lib.toString(1)
  • Lib.Path("a")
  • let p: Lib.Path! = …

The same leak let a module's top-level let be read qualified (Lib.salutation, Lib.{{salutation}}), even though the docs say lets stay private across the module boundary.

Change

  • Type checking: importers see the module's declared layer alone, without the prelude it was checked against. That layer is wrapped in an overlay with nothing beneath it, so a module namespace keeps its identity as an import instead of unifying structurally like a record: Lib == Lib2 is still cannot compare Lib! with Lib2!.
  • Runtime: the import binds a copy of the public bindings in the module scope's own frame, plus its constructors. Top-level bindings are all forced by the end of module evaluation and immutable afterwards, so a copy is enough.
  • Privacy: checkMemberAccessible rejects a module's top-level let through its namespace, covering both . and {{ }}.
  • Messages: the namespace prints as the import that loaded it, not as a record listing the module's lets. Examples:
    • field "Path" not found in Lib
    • type "Path" not found in Lib
    • "salutation" is private to module Lib and cannot be accessed from another module

Inside the module, nothing changes: its code sees the prelude and builtins as before.

Tests

  • New error goldens:
    • import_dang_qualified_builtin covers Lib.toString(1).
    • import_dang_qualified_prelude covers Lib.Path("a/b").
    • import_dang_qualified_prelude_type covers let p: Lib.Path! = ….
    • import_dang_private_toplevel and import_dang_private_toplevel_selection cover Lib.salutation and Lib.{{salutation}}.
  • tests/importlib now uses toString and Path internally. test_import_dang_module.dang checks that those results reach importers through the module's own pub names.
  • New pkg/dang/import_dang_test.go:
    • The exports' type and value scopes hold the module's public names and none of toString, print, Path, Random, JSON or its lets.
    • Two same-shaped modules don't unify.

Maintainer decisions

  • An import's namespace exposes only what the imported thing defines. For a native Dang module, that is its pub declarations, not the prelude or builtins; inside the module, the prelude stays visible as usual.
  • This applies the same rule as schema imports, but it is reviewed on its own because it needs a different mechanism: an exports view over the module's own scope, plus a runtime copy of its public frame.

A native Dang module import's namespace was the module's own type and value
scope, which hang off the prelude and the builtins, so `Lib.toString(1)`,
`Lib.Path("a")` and `let p: Lib.Path! = …` all resolved through it. Like a
schema import's, the namespace now holds only what the module defines.

Importers see the module's declarations alone: its type scope's primary
layer, behind an overlay with nothing beneath so it keeps an import's
identity instead of unifying like a record, and at runtime a copy of its
scope's own public frame. The module's own code keeps the prelude in scope
as before.

A top-level `let` was also reachable qualified (`Lib.salutation`,
`Lib.{{salutation}}`), though the docs promise `let`s stay private across
the boundary; checkMemberAccessible now rejects it, and it is left out of
the runtime namespace. The namespace prints as the import that loaded it
(`field "Path" not found in Lib`, `cannot compare Lib! with Lib2!`) rather
than as a record listing the module's `let`s.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito added this pull request to stack #280 November 9, 2026 15:11
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 225bfa7
Status: ✅  Deploy successful!
Preview URL: https://e7e51499.dang-3kk.pages.dev
Branch Preview URL: https://dang-module-exports.dang-3kk.pages.dev

View logs

@vito
vito merged commit fb3d3a2 into main Nov 9, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant