Skip to content

declare: make DeclareDir's imports file-local - #292

Merged
vito merged 4 commits into
builtin-namespace-importsfrom
declaredir-file-imports
Nov 10, 2026
Merged

vito merged 4 commits into
builtin-namespace-importsfrom
declaredir-file-imports

Conversation

@vito

@vito vito commented Nov 10, 2026

Copy link
Copy Markdown
Owner

DeclareDir now evaluates each file's imports into that file's own frame at runtime, as RunDir and LoadDir do since #291. Its type checking was already per file. Until now its evaluation put every file's imports into the shared module frame. So a sibling file's functions, called through the returned scope, saw another file's imports. For example, import YAML of a local module in one file replaced the builtin YAML for every file. This is decision Q-50 A.

Change

  • DeclareDir evaluation. Each file's imports go into a frame of its own: the builtin imports first, then the file's explicit imports. That frame sits under the module scope (newDirectoryValueScope). Each file's declarations are evaluated in, and close over, that file composite.
    • The scope DeclareDir returns holds only the module's own declarations.
    • Declarations are still evaluated file by file, as before.
  • New exported API: dang.DeclarationScope(scope, name). It returns the file scope that a top-level declaration was evaluated in: the module scope plus that file's imports.
    • DeclareDir and evaluateDirectory record these scopes on the module frame. That covers RunDir, LoadDir and PreparedDirectory.Evaluate.
    • Directives are not recorded, because they have their own namespace.
    • It exists for declarations that keep no closure, i.e. interfaces. Their methods' directive arguments may name the file's imports and must be evaluated in that scope.

The Dagger Dang SDK (dagger/dagger@c03e197, core/sdk/dang/v2)

  • DeclareDir is used only through runDangDirForModuleTypes, for ModuleTypes when self-calls are enabled (sdk.go). Dang 2 modules always have them enabled. The resulting env feeds initDangModule. Function calls go through RunDir/PrepareDir, not DeclareDir.
  • initDangModule reads env.Bindings(PublicVisibility). Imports are bound private, so they were never listed there. It keeps only local declarations through env.(*dang.Object).Mod origins (collectDangLocalTypes, isDangLocalValueBinding). The returned scope is still a *dang.Object, so this keeps working.
  • Object types: createFunction evaluates directive arguments in ConstructorFunction.Closure (dagger issue #13440). That closure is now the declaring file's composite, which holds the file's imports, including the auto-imported Dagger. So directive arguments on object methods resolve as before.
  • Interfaces need a change at the next dang bump. createInterfaceTypeDef passes the module env itself as the directive scope for interface methods (helpers.go, createFunction(ctx, srv, mod, fieldName, x, env, localTypes)).
    • Until now that env happened to hold the files' imports, through the shared frame.
    • After this PR, a directive argument on an interface method that names an import no longer resolves through env. An example is an unqualified Dagger enum.
    • The fix is one line: pass dang.DeclarationScope(env, name) instead of env. That also fixes the same gap on the SDK's RunDir typedef path, where imports were already file-local.
  • No other SDK code reads an import's name through the DeclareDir scope.

Tests

  • TestDeclareDirImportsAreFileLocal:

    • file A has import YAML of a local module; file B has no import;
    • A.fromModule returns the module's value through the DeclareDir scope;
    • B.fromBuiltin uses the builtin YAML.encode;
    • DeclarationScope returns each file's scope, even when B declares a directive named like A's function;
    • YAML is not in the module scope.

    Without the fix, fromBuiltin would get the local module.

  • TestDeclarationScopeResolvesInterfaceDirectiveImports: an interface method's @meta(level: Lib.Level.HIGH) doesn't evaluate in the module scope, and does evaluate in DeclarationScope(env, "Greeter").

  • TestDeclareDirSkipsBodiesAndKeepsDaggerTypes now reaches Dagger through Test's closure. It also asserts that Dagger is not bound on the returned scope.

  • Every check (**) passes. I ran the multi-agent self-review three times and fixed its findings: the interface directive scope, and a directive/value name collision in the recorded scopes.

Release note

DeclareDir imports are file-local. At runtime, a file's imports are visible only to that file's own declarations, as in RunDir and LoadDir. The scope DeclareDir returns no longer binds any import, for example Dagger. Reach an import through a declaration's closure (ConstructorFunction.Closure), or through the new dang.DeclarationScope(scope, name). That function returns the file scope a top-level declaration was evaluated in. Embedders that evaluate an interface method's directive arguments against the module scope should evaluate them in DeclarationScope(scope, interfaceName) instead.

No docs or skills describe what the DeclareDir scope holds, so none changed.

vito added 4 commits November 10, 2026 01:31
DeclareDir checked each file's imports per file but evaluated them all
into the shared module frame, so a sibling file's functions called
through the returned scope saw another file's imports (an `import YAML`
of a local module replaced the builtin YAML for every file). Each file
now evaluates its imports into its own frame, after the builtin
imports, as RunDir and LoadDir do; declarations close over that file
scope, and the module scope holds only the module's own declarations.

Signed-off-by: Alex Suraci <[email protected]>
A directory module's imports are file-local, so a declaration that keeps
no closure (an interface, whose methods' directive arguments may name an
import) can't have them evaluated in the module scope. DeclarationScope
returns the file scope a top-level declaration was evaluated in, which
RunDir, DeclareDir, LoadDir and PreparedDirectory record on the module
frame. The Dagger Dang SDK evaluates interface directive arguments in
the module scope; this is what it can use instead.

Signed-off-by: Alex Suraci <[email protected]>
Directives have a namespace of their own, so a directive in one file no
longer takes the declaration-scope entry of a same-named value or type
declared in another.

Signed-off-by: Alex Suraci <[email protected]>
The evaluateDirectoryFiles doc claimed every declaration captures its
file scope; interfaces don't, which is what DeclarationScope is for.

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

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

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4798f90
Status: ✅  Deploy successful!
Preview URL: https://ef269074.dang-3kk.pages.dev
Branch Preview URL: https://declaredir-file-imports.dang-3kk.pages.dev

View logs

@vito
vito force-pushed the declaredir-file-imports branch from 1a863f6 to 4798f90 Compare November 10, 2026 03:10
@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