Skip to content

Gate builtin import types before Dang 2.4 - #294

Closed
vito wants to merge 5 commits into
mainfrom
gate-builtin-import-types
Closed

vito wants to merge 5 commits into
mainfrom
gate-builtin-import-types

Conversation

@vito

@vito vito commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Speculative: how hard is it to keep 2.1-pinned Dagger entrypoints compiling on Dang 2.4?

The problem

Every Dang entrypoint the Dagger Go SDK generates (dang = "2.1", Dagger auto-imported) spells Dagger's JSON scalar bare:

pub call(receiverType: String!, receiverValue: JSON, fnName: String!, fnArgs: JSON!, callDigest: String): JSON! {
  let request = JSON.encode({{ ... }})
  ...
  (result :: JSON!)
}

The engine's ModuleEntrypoint contract (core/sdk/dang/v2/entrypoint.go) is also Dang source with bare JSON, written beside it and run at the same pinned version. On v2.4.0 this fails with type hint mismatch: expression has type String!, but hint expects JSON!.

What v2.3.0 actually did, measured at v2.3.0 and at 00dc380 (the pseudo-version dagger vendors): bare JSON was never Dagger.JSON. It was Dang's own Prelude scalar JSON, a separate type. pub f(x: JSON!): Dagger.JSON! { x } fails there with "declared Dagger.JSON!, inferred JSON!". The entrypoints worked because (s :: JSON!) from a String! works for any scalar, and the engine only checks that the result's scalar is named JSON. 2.4 broke them by turning the builtin JSON/YAML/TOML from scalars into namespaces (8b182d2, 16e1985, 5cba6f8). Import precedence did not change.

The gate

For source before 2.4 (Version.importTypesOverBuiltinImports), a type an import brings in unqualified takes the place of a builtin import's namespace of the same name, in type position only. So with Dagger imported, a bare JSON type is Dagger.JSON. The contract and call resolve it the same way. Values are unchanged: JSON.encode is the builtin in every version. An explicit import named like a builtin ([imports.YAML] + import YAML) still replaces it in every version. From 2.4, behaviour is v2.4.0's.

It is a generic import-precedence rule:

  • It lives in the import installer (installUnqualifiedImportTypesFromModule): an existing binding that is the builtin import of that name (isBuiltinImportNamespace, an identity check against StaticModules() that treats every builtin import the same) no longer blocks the import's type.
  • Nothing in hm, unification, assignability, type resolution or scalar kinds changes, and no format is named.

The parser can't see imports, so it can't desugar this. It records the rule on FileBlock.ImportTypesOverBuiltinImports (tagged dang:"version"; migration AST comparison skips it). FileBlock.Infer, prepareFileScopes/runDirectoryPhases (per file, nested-block imports included) and LoadDir sessions put the rule on the context. The installer reads it there. Pathless source (the REPL) falls back to VersionFromContext, and the REPL builds its import scopes through the new BuildScopesFromImportsForVersion. This covers RunFile, RunDir, DeclareDir, LoadDir sessions, analysis/LSP and the REPL. Each file follows its own version, imported Dang modules included.

Directive scopes (@JSON.field) now resolve as the namespace value does (directiveNamespaceScope), not through the type. Otherwise the gate would have broken codec directives in 2.1 Dagger modules.

dang migrate to 2.4

This is done, and it turned out to be the larger half. PlanMigrations takes MigrateOptions.ImportedTypes(configPath, dir), which reports what a directory's imports bring in. BuiltinImportTypes computes it from import configs, introspecting schemas or compiling Dang modules. The migration then qualifies bare types in type positions and type patterns by their import's name: receiverValue: JSON → receiverValue: Dagger.JSON.

  • Imports are only resolved for a directory that has a file spelling such a type bare and not declaring it. In the CLI, resolving can start a Dagger session: the CLI resolves imports as the LSP does, dang.toml plus Dagger declared or detected from dagger.json.
  • These are left alone: types the module declares, a name an explicit import of the same name replaces, and directive scopes.
  • These are refused with a line number: a type two imports both bring in, if the file spells it bare, and a bare type left unqualified in the result (a #nofmt region).
  • MigrateSource (no imports) is unchanged. MigrateSourceWithImports is the import-aware variant. There is a 2.1-to-2.4/builtin_import_types.dang fixture, with a schema import, that runs before and after.

Difficulty, honestly

  • The gate itself is small: about 40 lines in the installer, version, FileBlock and context plumbing. Most of the diffstat is the regenerated dang.peg.go (+2219/−2218 for one line in the grammar action), tests, and the migration.
  • It is the first gate that breaks the language-versions rule that inference never consults the version and migration needs no per-change code. The language-versions skill now says so. The check is still a flag the parser records rather than a version compare in inference, apart from the pathless fallback.
  • The migration has to resolve imports, so dang migrate is no longer purely offline for projects that spell such a type bare.
  • Edge case: in a 2.1 file whose import brings in a type named like a builtin import, qualified types through that builtin in type position (Random.Charset) resolve through the import's type. No known schema does this.

What is not restored

2.1 source that uses a bare builtin scalar type with no import bringing in a type of that name (e.g. a script's (s :: JSON!)) still fails as on v2.4.0. Restoring it would need the builtin JSON/YAML/TOML scalars back as types of their own, an identity-specific builtin the maintainer's principles rule out. How common: of ~171 .dang files in dagger/dagger, 18 spell bare JSON as a type. All are generated entrypoints or engine entrypoint testdata, run with Dagger imported, so the gate covers them. None use bare JSON/YAML/TOML/Random/UUID as a type without such an import.

Tests

  • pkg/dang/builtin_import_types_test.go:
    • The entrypoint call end to end at runtime: JSON.encode of a record holding receiverValue: JSON, (request :: JSON!) from a String, and a result the engine's entrypointJSONResult accepts. Covered in RunDir, RunFile, DeclareDir (signature), a LoadDir session, the ContextWithVersion-pinned unpinned dir the engine uses, and 2.0/2.1.
    • The 2.4 failure, and its qualified form passing.
    • Per-file versions across an import, an explicit import replacing the builtin in both versions and import orders, codec directives, and REPL scopes.
  • pkg/dang/migrate_import_types_test.go: the rewrite, auto-imports, declared/explicit names left alone, ambiguity, #nofmt, per-directory resolution, and BuiltinImportTypes for schemas and Dang modules.
  • Analysis: TestImportTypesOverBuiltinImportsBefore2_4.
  • Ran go test for pkg/dang, pkg/dang/analysis, pkg/lsp, cmd/dang and tests (TestDang, incl. migrations), the go generate stale checks for pkg/dang and treesitter (grammar.json unchanged), and fmt/stale.

Release note

Dang source written for a version before 2.4 (e.g. dang = "2.1") that spells a type bare, where an import brings in a type of that name and the name is also a builtin import (JSON, YAML, TOML, Random, UUID), compiles again: in type position, the bare name names the import's type. With Dagger imported, a bare JSON type is Dagger.JSON, so the Dang entrypoints the Dagger Go SDK generates work on Dang 2.4. JSON.encode/JSON.decode and @JSON.field still name the builtin. From Dang 2.4, the bare name is the builtin in type position too. dang migrate --to 2.4 rewrites such types qualified (Dagger.JSON). It resolves the project's imports to find them, which can start a Dagger session. Pre-2.4 source that spelled the old builtin JSON/YAML/TOML scalar types with no import of that name is not restored and must be migrated by hand (e.g. to String! or Decoded).

vito added 5 commits November 10, 2026 04:33
Up to Dang 2.3 the builtin JSON, YAML and TOML were scalars, so 2.1 code
spells a scalar type bare: every Dagger Go SDK entrypoint has
`fnArgs: JSON!` and `(result :: JSON!)`, and the engine's
ModuleEntrypoint contract, which it writes as Dang source beside them,
does too. 2.4 made them namespaces, which no value has as its type, so
those entrypoints stopped compiling even when pinned to `dang = "2.1"`.

Before 2.4, a type an import brings in unqualified now takes the place
of a builtin import's namespace of the same name in type position, so
with Dagger's `scalar JSON` imported a bare `JSON` type is
`Dagger.JSON`. The value keeps naming the builtin namespace, so
`JSON.encode` works in every version, and an explicit import of the
same name still replaces the builtin. The rule is about import
precedence, for every builtin import, and names none of them.

No spelling tells the two apart, so the parser records the rule on the
FileBlock and the import installer reads it from the context, for every
file of RunFile, RunDir, DeclareDir, LoadDir sessions and analysis. The
REPL's import scopes follow its version through
BuildScopesFromImportsForVersion.

Signed-off-by: Alex Suraci <[email protected]>
Moving source to Dang 2.4 changes what a bare type named like a builtin
import means wherever an import brings in a type of that name
unqualified: `receiverValue: JSON` named Dagger's scalar and comes to
name the builtin JSON namespace. No spelling differs, so parsing as one
version and formatting as the other can't see it.

PlanMigrations now takes MigrateOptions.ImportedTypes, which reports
what a project's imports bring in (BuiltinImportTypes computes it from
import configs, introspecting or compiling them as needed), and
qualifies those types by their import's name in type positions and type
patterns: `receiverValue: Dagger.JSON`. It is only asked for projects
that spell such a type bare. Types the module declares, or that an
import of the same name replaces, are left alone, and a type two imports
bring in is refused. `dang migrate` resolves the imports as running the
project would, which for a Dagger import means starting a session.

Signed-off-by: Alex Suraci <[email protected]>
A directive's scope names a namespace, as `JSON.encode` does, but it was
resolved as a type. Before Dang 2.4 a type an import brings in can take
a builtin import's name in type position, so with Dagger's
`scalar JSON` imported `@JSON.field` looked for the directive on
Dagger.JSON and failed. A scope that names an import's namespace now
resolves as that name's value does.

Also documents the exception in the dang-internals skill, and that
pre-2.4 source spelling a builtin scalar bare with no import of that
type is not kept working.

Signed-off-by: Alex Suraci <[email protected]>
Resolving a project's imports can start a Dagger session, and a type two
imports bring in only matters where a file spells it bare. The
migration now resolves imports, and refuses an ambiguous type, only for
a file that spells a type named like a builtin import bare, which a
codec directive's scope (`@JSON.field`) is not. A file preview resolves
them as lazily.

`dang migrate` also detects Dagger from a Dagger module, as the LSP
does, since that is where Dang entrypoints get Dagger's `scalar JSON`
from.

Signed-off-by: Alex Suraci <[email protected]>
Review follow-ups for the 2.4 import-type migration:

- A bare type that needed qualifying but is still bare in the result,
  as in a #nofmt region, which is echoed as written, now fails the
  migration with its line, rather than coming to name the builtin.
- Imports resolve per directory, so a Dagger module in a subdirectory
  of a project gets Dagger's types; a preview resolves from the file's
  directory alike.
- A file whose bare types its module declares resolves nothing.

The user-facing skill references mention the pre-2.4 rule and that
`dang migrate` qualifies such types.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

@vito

vito commented Nov 11, 2026

Copy link
Copy Markdown
Owner Author

Closed in favor of #295

@vito vito closed this Nov 11, 2026
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