Repository navigation
Description
Problem
There are two types of Dagger modules: those that are standalone software projects, and those that exist in the context of another software project. Let's call those standalone modules and contextual modules, respectively.
Dagger is designed to support both, which is good, but causes friction for users in some areas.
- Contextual modules almost always need access to their context directory. Today this requires an explicit argument, (which usually looks like
dagger call MYFUNC --source=.), which is verbose. - Contextual modules often need to filter their context directory, usually to avoid the prohibitive cost of uploading a huge monorepo on every task.
- The best practice for contextual modules is to make the context directory itself a module, and store the module source code in a configurable subdirectory. This additional concept of "module source" being different from "module root", adds complexity for the user.
- For standalone modules, the recommended value for
sourceis.. For contextual modules, it is./dagger. The current default value is./dagger, which hurts the experience of creating a standalone module. If we change it to., it will hurt the experience of creating contextual modules. Either way, the user experience suffers.
Solution
I propose making contextual modules a first-class concept in Dagger. Here's how it would work.
What is a contextual module?
A contextual module is a module that exists in the context of a larger software project, and needs special access to its context directory to perform tasks.
This is similar to Dockerfiles, which usually exist in a context directory, which they can access with operations such as COPY. These Dockerfiles are contextual.
A module that doesn't have a context is called a standalone module.
Conventions for contextual modules
The only difference between contextual and standalone module is its path. The contents of the module directory is always the same.
A contextual module is recognizable by its directory name: .dagger. The module's parent directory (which contains .dagger) is the context.
Creating a contextual module
By default, dagger init will create a contextual module: the current directory is used as context, and .dagger is created to contain the module. Use this when using Dagger to configure your project's CI.
To create a standalone module, call dagger init --standalone. This will initialize the module in the current directory, without creating .dagger. Use this when creating a standalone module that is its own software project.
Loading
Contextual modules can be loaded directly (at their exact path), or indirectly (at the path of their context).
If the context for a module is itself a module, the context wins.
To summarize the loader algorithm:
flowchart LR
start([Start]) --> check1{Is there a module at $target?}
check1 -->|Yes| return1[Return that module ✅]
check1 -->|No| check2{Is there a module at $target/.dagger?}
check2 -->|Yes| return2[Return that module ✅]
check2 -->|No| error[Return an error ❌]
Installing a contextual module
Since a contextual module can be loaded (either directly or indirectly), it can also be installed.
Accessing the context directory
Functions in a contextual module can access their context directory with a new core API call:
{
"Interact with the current context"
context {
"Access the context directory"
directory(path: String!="."): Directory!
"Access a file in the context directory"
file(path: String!): File!
}
}Example in Go:
func (m *MyModule) Source() *Directory {
return dag.
Context().
Directory()
}Filtering the context directory
A module's context directory can be quite large (example: cache files, assets, etc). To avoid uploading unnecessary data to the engine, the context directory is filtered as follows:
- Any subdirectory containing
dagger.jsonis recursively filtered out .gitignoreis applied by default. This can be disabled in the module configuration
Future expansion of context API
In the future, the context() API could be expanded to centralize access to the current execution context.
These are not in scope for this proposal, but here are examples to give a general idea of what could be added later:
context().service()to connect to network services in the caller's context (this would replacehost.service())context().module()to replacecurrentModule()context().terminal()to access the caller's terminalcontext().status()for a more advanced status API than "return error or not"? Perhaps a possible bridge to integrating with eg. Github Checks and CI job status- `context().config() to expose config files in the context directory as a GraphQL schema?
context().ssh().agent()to get an ssh agent socketcontext().docker().socket()to get a docker engine unix socketcontext().docker().auth()to get docker credentialscontext().aws().auth()to get aws credentialscontext().platform()to get client's platform informationcontext().gpu()to access GPUs (insert future webgpu device streaming here)context().watch()to watch for changes on the context directory (for running dev environments)context().environment()to get environment variables from the context (this is not an endorsement! my objections to - doing this are well-known... but listing for completeness)context().editor()to open the user's file editor (for IDE integration?)context().window()orcontext.dom()to render a web window to the user (to add GUI capabilities, backstage-style)context().cache()for hypothetical future interaction with the caching subsystem (?)context().peer()for hypothetical future clustering features: lookup another engine to dispatch jobs to. A peer exposes its own dag object recursively.
FYI @vito @sipsma @jedevc @helderco, lots of speculation and extrapolation in this part, let me know if any of them triggers a positive or negative reaction. ☝️
Do standalone modules have a context?
- Every module has an execution context, which includes a context directory.
- But only contextual modules (modules in a
.daggerdirectory) have a non-empty context directory. - Standalone modules can access their context directory, but it will always be empty
Deprecations
With native support for contextual modules, some features become redundant and can be deprecated:
- Deprecate
dagger develop --sourceanddagger init --source - Deprecate the
sourcefield indagger.json - Deprecate "directory views". They were meant to be a stopgap anyway
Status
Request For Bikeshedding :)
cc @sagikazarmark @helderco @vito @sipsma @jedevc @kpenfound @jpadams
Activity
jpadams commentedon Apr 26, 2024
deprecating dir views: #6857
Yes, would love to keep things simpler.
In a standalone module, the
dagger.jsonwould live at the root of the module, right?Where does the
dagger.jsonlive in a contextual module? Inside the.daggerdirectory, I'm guessing, right? Thus the module dir contents are the same in either case.jpadams commentedon Apr 26, 2024
This also allowed, right?
The top-level module (under
contextual/) could invokeand perhaps dagger-in-dagger call that module's functions which would have access to the
foo/directory throughdag.client().context().sagikazarmark commentedon Apr 26, 2024
I like the idea of calling it "context". It's familiar terminology from Docker/Buildkit.
It would probably make sense to default to contextual modules in
dagger init. I'd expect most people to write those, not standalone/reusable modules.Standalone modules could be created with
dagger init --standalone.Although I've been advocating for making contextual modules first-class citizen in Dagger, I'm not sure how I would explain the difference to someone. Starting with the name: what does contextual mean? There is a context for standalone modules after all.
When you think about "modules" in the software world in general, there is no such distinction between reusable packages and applications (eg. Go modules).
So while I like the idea of making the user experience better for this use case, I'm not sure about the name and how to explain the difference to others.
sagikazarmark commentedon Apr 26, 2024
What happens when I try to
dagger installa contextual module? Should that be allowed?sagikazarmark commentedon Apr 26, 2024
???
That was fast! 😄
shykes commentedon Apr 26, 2024
Sorry I fat-fingered while reading your insightful comments 😁
smolinari commentedon Apr 26, 2024
I'd just like to confirm this suggestion is a start to simplify my "Dagger functioning in a Temporal Activity" use case, where the Temporal Activity is the "wrapping context" needing a
./daggerinstallation of Dagger. Is this correct?From my lurking the community, it has been said by the Dagger team a couple of times IIRC that this kind of usage is considered the "niche" way to use Dagger (and why Dagger has gone CLI-centric). However, I also agree with your suggestion and if my question above is confirmed, I applaud this push!!! 👏 😁
Scott
wingyplus commentedon Apr 26, 2024
I just hit this problem with monorepo at my work recently. I really like the idea. And I also have a questions
dagger.jsonto determine root context directory? Like we dodagger initadd the root repository then we accessing to context directory api. So we don't need to separate between standalone and module library.shykes commentedon Apr 26, 2024
dagger.jsonalways lives at the root of the module. The contents of a module is always the same: the only difference between a standalone and contextual module is their path.This part is correct:
But this part is not:
standalone ~(dir contents accessible via dag.client().context())A standalone module by definition doesn't have a context. If it tries to access its context, it will get either an error or an empty directory (TBD).
Yes exactly.
I renamed
contextualtocontext, to clarify that.daggeris the contextual module.shykes commentedon Apr 26, 2024
Yes, all modules should always be loadable, and therefore installable. See the section "Loading":
So, in the context of your project:
If you remove the root
dagger.json, these two install commands would be equivalent:dagger install github.com/openmeterio/openmeter(no standalone module, fall back to contextual module)dagger install github.com/openmeterio/openmeter/.dagger(import contextual module directly)shykes commentedon Apr 26, 2024
Agreed, I added this to the proposal. Thanks!
I tried to address this in the proposal, by making the comparison to Dockerfiles more explicit.
shykes commentedon Apr 26, 2024
I'm guessing you mean the module under
contextual/.dagger, just double-checking. Also as mentioned in my other reply, the directorycontextualis actually the context.Yes that would be possible, BUT it would not be necessary, because you can simply
dagger install ../fooand letfooworry about accessing its context.Example:
This way the include/exclude logic is neatly encapsulated into each module.
This greatly improves the experience in large monorepos, because the dependency graph of the monorepo's components can be modeled directly as a dependency graph of dagger modules.
helderco commentedon Apr 27, 2024
From #7199 (comment):
Not in Go, but it’s actually pretty common in my experience, when you have a package registry and a manifest file. I see the terms “library” vs “application” more commonly. Libraries are meant for distribution (and thus installing and importing) while applications are meant to be facing users directly.
There’s not necessarily a technical limitation that prevents you to distribute an application or vice versa, it’s just that to distribute you have more requirements because you need the right package metadata or file structure. But there’s also all sorts of conventions and best practices when building a library, for example for not using upper bounds version constraints.
In our case it’s mostly intent, besides more access to the current “environment”. If it’s a reusable module you want to be a good citizen in thinking how others will use it, and not make any assumptions about their environment. While an application module is likely to be supporting a specific code base and so it benefits from having easier access to it and you don’t care that it’s reusable or not.
This brings me back to one of the early ideas in:
So I’m wondering if “contextual modules” could have a bit more access and solve a few other issues as well. As for the trust model, we could for example have a limitation that a main module can’t be installed unless it’s from a relative path inside the same git repo. That would allow bringing together application silos in a monorepo (like our own repo with the SDKs), but not be able to run a module off the Internet that could be accessing something that it shouldn’t.
Not necessarily pushing for this specific idea to solve that, especially not right now (my preference is in another solution). Just food for thought as contextual modules looks like a step in that direction by starting this distinction.
27 remaining items
shykes commentedon May 11, 2024
@sipsma just double-checking how you feel about my response to this particular concern of yours?
sipsma commentedon May 13, 2024
@shykes
What I was imagining is that whether a module is contextual or not is just a setting in
dagger.json, which is where we currently encode all the other information relevant to module loading.I think that would all still be possible though whether or not a module is configured as contextual via a
.daggerdir or via a setting indagger.json, right?shykes commentedon May 14, 2024
To me that's like adding a
CONTEXTcommand in a Dockerfile. Technically possible, but less clear than just agreeing on a straightforward convention: the context directory is the parent directory.shykes commentedon May 14, 2024
Today @sipsma explained to me why dynamically filtering the context directory is very difficult to do. So I updated the proposal, with a new section called "Filtering the context directory".
helderco commentedon May 15, 2024
I’m not sure what you mean. Today, each SDK (runtime module) returns a list of required paths to include when loading a module. The Go SDK includes the following ones from anywhere in the repo and mounts them into
/src:dagger/core/schema/sdk.go
Lines 394 to 407 in 2516b3a
You can see these patterns in
ModuleSource.resolveFromCaller:That’s because there’s been a need/use case for a Dagger Module in Go to be able to read a
go.modfrom a parent directory (not necessarily the immediate parent), in a monorepo. That’s why there’s a context dir today afaik.To be clear, this does not have anything to do with the module’s code and functions. It’s a part of loading the module itself, in particular for installing dependencies while building the runtime container, before executing it.
@sipsma, if the context directory becomes just the immediate parent, don’t we still need this other “internal context” for this use case?
TypeScript followed along, but I don’t know that anyone’s taking advantage of it:
dagger/sdk/typescript/runtime/main.go
Lines 37 to 40 in 2516b3a
I removed it in Python because it's increases the change for cache invalidation and it’s not useful as is:
dagger/sdk/python/runtime/main.go
Line 84 in 2516b3a
That’s because there’s more fragmentation in the ecosystem so it could be multiple kinds of files that need this. It would be better with some kind of per-module configuration.
I wonder if there’s a more simple solution to this as it adds complexity.
helderco commentedon May 15, 2024
Shoot, I was excited about that part!
To me, the best parts of the proposal are around simplicity:
sourceorrootfields.!**/.dagger/sdk)..gitignoreby default for filtering.dag.Contextas a more appropriate and generic term to get access to some resources.I have a few itches though:
dagger.moddirectory instead, but it’s not enough to push against if others prefer.dagger.I have a few outstanding questions as well. That last point above takes me to the first one.
Question 1 - re: Consistency
What if a "standalone" module was just a daggerized empty directory?
Before
or
After
(actually looks like a merge between the two “before” cases 😄)
It would resolve a few inconsistencies between a contextual module and a standalone one (which one takes precedence in loading, etc…):
- Originally posted in #7199 (comment)
No longer an issue since all modules would be targeted and installed based on
.dagger’s parent directory. If you’re inside.dagger, the find-up logic looking for the closest parent directory with.daggeris consistent here and without conflict.On the other hand, it can still introduce another weirdness for standalone modules: Do I put this requirements file in the context directory or source directory? I know what I’d do but the difference can be subtle for most, and a potential source of confusion.
We could automatically detect if the context directory only contains
.daggerin order to distinguish a contextual module from a standalone one (i.e., adding any file to the context makes it contextual?), but at this point I’m wondering about the next question (i.e., do we really need the distinction or can we do it another way?).Question 2 - re: Standalone use case
What are the advantages for standalone modules in comparison to a contextual module with an empty context directory (apart from having
.dagger)?The proposal mentions that the difference is only in the path, but there’s a section for future expansion of the context API. I suppose the big difference then would be access or not to this API?
If so, access to those APIs would return an error, unless it’s the context directory, in which case it returns an empty directory?
Question 3 - re: Exclusion patterns
So
dagger.jsonhas"include"/"exclude"fields for what to load, relative from the directory wheredagger.jsonis. How would you override (or disable) a.gitignorefrom the module’s configuration, if we can’t set the patterns in code?A separate patterns field for the context directory?
.daggerdirectory patterns (root/source)Merging the first two doesn’t make sense.
Question 4 - re: Flatness limitation
I keep wondering if anyone really needs a
"source"to a subdir at any depth, in a way that wouldn’t be possible with this design.@sipsma, were there any particular use cases that you can remember for that? Or was it just because there was no reason not to be flexible here?
Not a blocker of course, just wondering.
helderco commentedon May 16, 2024
Another question...
- Originally posted in #7199 (comment)
What's the status on the writing part as it's not in the description? Just a thought experiment or viable?
TomChv commentedon May 21, 2024
Is this proposal ready for an implementation? I plan to work on it once it's ready.
We can split this feature in multiple part:
CurrentModuleAPI toContext.dagger.jsonin it instead of the current directory when the command is executed.include/excludefromdagger.json??.gitignore..daggerdirectory, however I guess thedagger.jsonstays in place.shykes commentedon Jun 13, 2024
I am closing this in favor of the more narrowly scoped context directory proposal (#7647). Once that is shipped, we can revisit this issue, and look for the next chunk to ship.
smolinari commentedon Jun 13, 2024
@shykes - The link to the issue isn't a link. 😁
Scott
shykes commentedon Jun 13, 2024
Oops, fixed, thanks!