Repository navigation
declare: make DeclareDir's imports file-local - #292
Merged
Merged
Conversation
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]>
Deploying dang with
|
| 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 |
vito
force-pushed
the
declaredir-file-imports
branch
from
November 10, 2026 03:10
1a863f6 to
4798f90
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DeclareDirnow evaluates each file's imports into that file's own frame at runtime, asRunDirandLoadDirdo 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 YAMLof a local module in one file replaced the builtinYAMLfor every file. This is decision Q-50 A.Change
DeclareDirevaluation. 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.DeclareDirreturns holds only the module's own declarations.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.DeclareDirandevaluateDirectoryrecord these scopes on the module frame. That coversRunDir,LoadDirandPreparedDirectory.Evaluate.The Dagger Dang SDK (dagger/dagger@c03e197,
core/sdk/dang/v2)DeclareDiris used only throughrunDangDirForModuleTypes, forModuleTypeswhen self-calls are enabled (sdk.go). Dang 2 modules always have them enabled. The resulting env feedsinitDangModule. Function calls go throughRunDir/PrepareDir, notDeclareDir.initDangModulereadsenv.Bindings(PublicVisibility). Imports are bound private, so they were never listed there. It keeps only local declarations throughenv.(*dang.Object).Modorigins (collectDangLocalTypes,isDangLocalValueBinding). The returned scope is still a*dang.Object, so this keeps working.createFunctionevaluates directive arguments inConstructorFunction.Closure(dagger issue #13440). That closure is now the declaring file's composite, which holds the file's imports, including the auto-importedDagger. So directive arguments on object methods resolve as before.createInterfaceTypeDefpasses the module env itself as the directive scope for interface methods (helpers.go,createFunction(ctx, srv, mod, fieldName, x, env, localTypes)).env. An example is an unqualified Dagger enum.dang.DeclarationScope(env, name)instead ofenv. That also fixes the same gap on the SDK'sRunDirtypedef path, where imports were already file-local.DeclareDirscope.Tests
TestDeclareDirImportsAreFileLocal:import YAMLof a local module; file B has no import;A.fromModulereturns the module's value through theDeclareDirscope;B.fromBuiltinuses the builtinYAML.encode;DeclarationScopereturns each file's scope, even when B declares a directive named like A's function;YAMLis not in the module scope.Without the fix,
fromBuiltinwould get the local module.TestDeclarationScopeResolvesInterfaceDirectiveImports: an interface method's@meta(level: Lib.Level.HIGH)doesn't evaluate in the module scope, and does evaluate inDeclarationScope(env, "Greeter").TestDeclareDirSkipsBodiesAndKeepsDaggerTypesnow reachesDaggerthroughTest's closure. It also asserts thatDaggeris 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
DeclareDirimports are file-local. At runtime, a file's imports are visible only to that file's own declarations, as inRunDirandLoadDir. The scopeDeclareDirreturns no longer binds any import, for exampleDagger. Reach an import through a declaration's closure (ConstructorFunction.Closure), or through the newdang.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 inDeclarationScope(scope, interfaceName)instead.No docs or skills describe what the
DeclareDirscope holds, so none changed.