Skip to content

import: builtin namespaces are automatic imports - #291

Merged
vito merged 12 commits into
case-type-switchfrom
builtin-namespace-imports
Nov 10, 2026
Merged

vito merged 12 commits into
case-type-switchfrom
builtin-namespace-imports

Conversation

@vito

@vito vito commented Nov 10, 2026

Copy link
Copy Markdown
Owner

JSON, YAML, TOML, Random and UUID (the static modules) become imports that Dang provides to every module, installed by the import installer, instead of Prelude objects and values. This is the "Q-41 B" follow-up to #276/#281. It also turns decoding into a type that has no constructor into a compile-time error.

Builtin imports

They are installed with installQualifiedImportTypeScope / installQualifiedImportValueScope, the qualified half of the ordinary import installer, before the module's explicit imports. They carry a qualified import origin, and nothing else checks their names.

  • Qualified only: JSON.encode(x) works. There is no bare encode/decode/int.
  • Local declarations shadow them. A type JSON {…} or scalar JSON shadows the type and the value. A let Random = … shadows the value only.
  • An import of the same name replaces them, in every mode. This covers both an explicit import ([imports.YAML] plus import YAML) and an auto-import config. Only the file importing it is affected; sibling files still get the builtin.
  • Private: an import's namespace never holds them. Test.YAML and Lib.YAML stay errors (import_qualified_codec, import_dang_qualified_codec).
  • Unqualified import names never override them. A schema's scalar JSON is reached as Dagger.JSON, and JSON stays the codec namespace. This is a general rule, not a special case for these five names: an unqualified import name (a value, a type or an enum value) never overrides an import's namespace visible from the import's frame. That covers a builtin import, and an import in an enclosing scope of a nested block's import.
    • At inference, the rule is checked from value origins (resolvesToImportNamespace).
    • At runtime, it is checked from per-frame marks set by installQualifiedImportValueScope (importNamespaceScope, implemented by Object, OverlayValueScope and therefore recordFieldScope, and ConstructorScope).
    • It replaces the job isBuiltinValueName used to do for these names. Static modules are no longer builtin values, so that skip no longer covers them.

Where they live

The builtin imports go wherever the module's explicit imports go. For each of their names, resolution is: the module's own declaration, then an import of that name, then the builtin import.

  • Single file and REPL: imports go in the module frame. NewPreludeTypeScope is module → builtin imports → prelude, and NewValueScope mirrors it.
  • Directory modules and LoadDir sessions: imports go in a per-file frame that is looked up after the module's declarations.
    • Each file's import frame gets the builtin imports first: newFileImportsScope, and installBuiltinImportValues in evaluateDirectory and NewSession.
    • The module's own chain has none: withoutBuiltinImports in prepareFileScopes, and newDirectoryValueScope. So Module.TypeEnv and Module.ValueEnv agree on leaving them out, like any file import.
  • Analysis and LSP: both use the file composite, so hover and inspect on YAML resolve to whichever import that file sees.

shadowedPreludeMemberHint also consults the builtin imports, so user_scalar_json_shadows_codec keeps its hint.

Decode targets with no constructor

Decoding into JSON!, Map[JSON!]!, Random!, a native module's namespace (Lib!) or a schema's object type (Test.ServerInfo!) used to type-check and then fail at runtime with constructor for X not found. It is now rejected by checkDecodeTarget, through a generic hasDecodableShape check, wherever a decoded value must become one: a non-null target, non-null list/map elements, or a required field.

  • cannot decode into JSON: it has no constructor
  • cannot decode Holder.inner: its type Random has no constructor to decode into

These positions still type-check:

  • Nullable positions, defaulted fields and union options: data can satisfy them by leaving the value out or null. If the data has a value there, decoding still fails at runtime.
  • Object types declared in Dang: they always have a constructor, even when their signature failed to check.

Tests

  • pkg/dang/builtin_imports_test.go checks:
    • origins and frame placement;
    • that the builtins are not in the Prelude;
    • that there is no bare encode;
    • an unqualified schema JSON vs the builtin, at both levels;
    • an auto-import named YAML, under RunDir, RunFile and LoadDir.
  • tests/builtin_shadowing_test.go, run under RunDir and RunFile:
    • declarations shadow the builtin imports;
    • imports exporting JSON/YAML, or enum values TOML/UUID, don't override them, including in blocks and record fields;
    • two imports both exporting JSON aren't ambiguous;
    • a nested block's import doesn't override an outer import's namespace;
    • [imports.YAML] + import YAML resolves to the module under RunDir, DeclareDir, RunFile and a LoadDir session, while a sibling file gets the builtin.
  • analysis.TestImportReplacesBuiltinImport: Inspect resolves YAML to the import in the importing file and to the builtin in a sibling file.
  • New error goldens: decode_into_namespace, decode_into_namespace_field, decode_into_module, decode_into_schema_object.
  • test_json_import: an optional schema-object field still decodes.
  • Every check (**) passes. I self-reviewed with the multi-agent reviewer over four rounds and fixed its findings.

Release note

Builtin namespaces are imports. JSON, YAML, TOML, Random and UUID are now imports that Dang provides to every module, not Prelude values.

  • They are reached qualified only, as before.
  • A declaration of the same name shadows one.
  • An import named like one replaces it in the file that imports it. Before, the builtin won in directory modules.
  • An unqualified name from an import never overrides one. This rule now applies to every import's namespace: in a nested block, an import's unqualified name no longer shadows an import namespace from an enclosing scope.
  • Module.TypeEnv / Module.ValueEnv (LoadDir) and the scope RunDir returns no longer include them, like any other file import.

Decoding into a type with no constructor is a type error. :: JSON!, :: Random!, Map[JSON!]!, a module namespace (Lib!) or a schema object type (Dagger.Container!) as a decode target, in a non-null position or a required field, now fails to compile: cannot decode into JSON: it has no constructor. It used to fail at runtime with constructor for JSON not found. Use Decoded! for data of any shape. A nullable field of such a type still decodes when the data leaves it out.

Not changed

DeclareDir still evaluates every file's imports into the shared module frame at runtime. This was already the case, and TestDeclareDirSkipsBodiesAndKeepsDaggerTypes relies on it. So in a module where one file imports YAML, a sibling file's function bodies would see that import if called from the DeclareDir scope. Type checking is per file, as in RunDir.

Maintainer decisions

  • Q-41 B: builtin namespaces are imports provided automatically, installed through the import installer, with no name checks downstream.
  • An explicit or auto-import named like a builtin import replaces it in every mode (option A). Banning those names would make them special.
  • Not adopted: a general rule that an import name isn't a type.

vito added 12 commits November 10, 2026 00:14
A decode into a type with no constructor or shape of its own — a
namespace such as JSON or Random, a native module's namespace, or a
schema's object type — type-checked and then failed at runtime with
"constructor for X not found". checkDecodeTarget now rejects it at
compile time, at the top or in a field, for any such type.

Signed-off-by: Alex Suraci <[email protected]>
JSON, YAML, TOML, Random and UUID (the static modules) are no longer
Prelude objects and values. The import installer installs them as
qualified-only imports, with an import origin, into a frame between the
prelude and each module's scope, in type checking and evaluation alike.
A module's declarations shadow them, and an import's namespace never
holds them.

An unqualified import name no longer overrides any import's namespace
visible from the import's frame, which is what keeps a schema's
`scalar JSON` from replacing the codec namespace. That replaces the
isBuiltinValueName skip these names used to get: the rule is general,
checked from value origins at inference and from per-frame marks at
evaluation, with no name special-cased.

Signed-off-by: Alex Suraci <[email protected]>
Describe JSON/YAML/TOML/Random/UUID as builtin imports (qualified only,
shadowed by a module's declarations, never overridden by an import's
unqualified names, not re-exported), and that a type with no
constructor is not a decode target.

Signed-off-by: Alex Suraci <[email protected]>
Review fixes for the decode-target check. A nullable or defaulted field
of a type with no constructor decodes fine when the data leaves it out,
so it is only rejected where every decoded value must become one. And a
type declared in Dang always has a constructor, so one whose signature
failed to check no longer adds a misleading no-constructor error.

Signed-off-by: Alex Suraci <[email protected]>
A directory module's per-file import frame had no parent, so two file
imports both exporting e.g. JSON installed it twice and made every
unqualified JSON ambiguous. The frame now resolves through the builtin
imports and prelude below the module, as a single file's imports do.

Signed-off-by: Alex Suraci <[email protected]>
Describe that only required fields of a type with no constructor are
rejected as decode targets, and the per-file import frames.

Signed-off-by: Alex Suraci <[email protected]>
Review fixes for the import-namespace rule. Its runtime bookkeeping is
now a method set on the value scopes, so recordFieldScope inherits it
from its embedded overlay: an import in a block inside a record field
no longer replaces a builtin import at runtime. An import's unqualified
enum values follow the rule too, so `enum Format { JSON }` doesn't
replace the JSON namespace.

Signed-off-by: Alex Suraci <[email protected]>
Say that only non-null positions are rejected at compile time, and
that a nullable one still fails at runtime when the data has a value.

Signed-off-by: Alex Suraci <[email protected]>
A `let` of a builtin import's name shadows only the value; the type and
its scoped directives still reach the builtin.

Signed-off-by: Alex Suraci <[email protected]>
An explicit or auto-import named like a builtin import (`[imports.YAML]`
plus `import YAML`) replaced it in a single file but not in a directory
module or LoadDir session, where a file's imports are looked up after
the module's chain, which held the builtin imports. Directory modules
now give each file's import frame the builtin imports first, before its
explicit imports, and keep them out of the module's own type and value
chains, so the name resolves the same way in every mode, analysis
included.

Signed-off-by: Alex Suraci <[email protected]>
Describe that an import named like a builtin import replaces it in the
file importing it, and where directory modules put the builtin imports.

Signed-off-by: Alex Suraci <[email protected]>
Module.ValueEnv no longer binds the builtin imports (they are file
imports in a directory module), so TypeEnv leaves them out too, as its
doc says of imports: code checked against one runs in the other.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito added this pull request to stack #282 November 10, 2026 01:14
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

@vito
vito force-pushed the builtin-namespace-imports branch from ecb3eca to b48d3aa Compare November 10, 2026 03:09
@vito
vito merged commit e4d33ad into main Nov 10, 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