You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Need equiv of include / exclude for directories with dagger call CLI #6155
From @cirocosta:
on ignoring/excluding, reminds me of how sometimes taking the "we'll default to following .gitignore" ends up annoying some users: ggreer/the_silver_searcher#1189
ignoring/excluding, reminds me of how sometimes taking the "we'll default to following .gitignore" ends up annoying some users
As a quick and easy idea, we could potentially re-use .dockerignore/.containerignore if it's present? It's a bit more standardized around the container ecosystem.
But I'm a little hesitant about that, .dockerignore is kind of a bit of a mess, I don't really want to propagate it's usage any more than we have to.
Could also be a "filter" that could be applied at any point on a loaded directory. filtered_dir = dir.with_exclude_filter("*.json")
I think this approach makes the most sense. We could actually just re-purpose .WithoutFile/.WithoutDirectory, which means we can avoid having to add any extra APIs, or any extra files across a filesystem.
However, as-is, these files will still be uploaded, and then removed.
What we could do though, is allow optimizing the produced LLB from a local SourceOp + FileOp into a single local SourceOp, by detecting if the FileOp is attempting to just remove files, then we can add those into the ExcludePatterns of the SourceOp. This might be easier to implement at the Dagger level instead of directly at the LLB level (but if we could do it at the LLB level, it would also affect other places as well, like in .DockerBuild).
I don't think there's any tooling in buildkit to allow easily mutating LLB for optimization passes at the moment though, so this would probably be a bit of an effort if we were to take that approach.
This just became a lot more pressing given dag.Host().Directory() is gone. There is no way now to exclude directories/files from being uploaded to buildkit. This adds to build time anywhere between 1 minute to 3-5 minutes in my case.
I want to see this at multiple levels, not just one option.
1. Global exclude
Similar to git global excludes:
dagger config --global add exclude "**/.direnv"
Saved in $HOME/.config/dagger/dagger.json. add instead of set for lists, and this command would support other global configs, like passing through proxy env vars, for example.
2. (Maybe) “project” specific excludes
I also wonder about using .gitignore, .dockerignore or .daggerignore. Similarly to how the engine loads modules by finding all dagger.json files first to get the "exclude" configs before loading the rest, it could also find .<dagger™>ignore and do the same in dag.Host() (perhaps with an option to trigger this behavior). Or in this case, do it in the CLI and pass explicitly to HostDirectoryOpts().
The CLI could also read the next-up dagger.json (if exists) and get the list of patterns to use in HostDirectoryOpts, however I’m not sure if it’ll clash with the intent that these are used for setting up the context directory (/src) in the runtime container, for a module. Possibly ok if a path in a --src flag is within the dagger.json tree and the patterns in there are generic enough.
This is the most specific (from CLI POV), and overrides anything from before.
When a function argument is src *Directory, the CLI currently adds a string flag --src to convert a path into dag.Host(path). We could add more flags (for any input where the function has options):
Note: I considered --src-exclude but that could clash with a srcExclude argument, and is better than an extra char in --src--exclude. Not fond of a custom format like --src=.,**/*.py.
So:
dagger init
dagger use <python module>
dagger use <go module>
dagger -m python call lint --src=. --src.include="**/*.py`
dagger -m call go lint --src=. --src.include="**/*.go`
Of course this makes the CLI command bigger, but we’re already trending towards that with explicit env vars, etc, so that’s where we benefit from a config with defaults to run a module’s commands (consume, not author), like above with dagger config or something similar.
4. Inside function
I also think there’s value in being able to exclude a bunch of files from a Directory instance, for example from a function argument. In the previous example, the go and python modules know which kind of files matter for what they need to do.
Even though more files may have been uploaded anyway, it still helps with cache invalidation. So if you have a dir with go and python files, the cache in go lint won’t be invalidated if python files have changed.
Can we implement this in a way where it's all done at runtime? I was able to get away with that in Bass and the semantics felt super nice - you could basically take any path and dynamically narrow it down to include/exclude globs, and all the cache busting/file syncing semantics work as you would hope:
The trick to implementing this is that you need to defer the actual file syncing to as late as possible.
The end goal would be that the user just passes in a path like ./ and the module itself narrows that path down to different glob filters when needed. This is especially useful if there are different permutations of filters that you need at different times.
(This would be different from the module source dir include/exclude which is necessary for bootstrapping without uploading everything.)
Can we implement this in a way where it's all done at runtime?
This would be my preferred place to start (point 4 above), and then work upwards (points 3, 2 and 1).
Modules should have some idea about what their contents are (e.g. a go module is looking for go code) - so they should have some idea of what to filter. The end-user shouldn't be required to guess what a module is doing, and so what files are safe to include/exclude.
However, it does feel a bit tricky - we need to lazify the loading. Today, we call LocalImport immediately on calling Host.directory (which gets called by the CLI when encountering a directory flag):
We'd probably want both of these to "squash" onto a lazy loaded directory, and modify the filters for that operation. To do this, we could add a new lazy property to the core.Directory object, and then ensuring it's evaluated before actually accessing it, and then the above operations could instead modify the parameters.
Note
💡 We could potentially do this using llb.Async, which could be quite be an elegant way of resolving this (but would need to hack around to see).
Modules should have some idea about what their contents are (e.g. a go module is looking for go code) - so they should have some idea of what to filter.
Wouldn't that go against the "module is a sandbox" idea?
Filtering can very much be an environment specific thing (eg. I have a tmp folder with all kinds of BS in it that could break go test ./..., so I want it excluded, but the module developer (eg. platform eng. team) may not expect that)
My preference would be a .daggerignore file (or something similar) as a start. It's an easy concept and easy to expand on (eg. allow other ignore files from a global dagger config, global ignores, etc)
Wouldn't that go against the "module is a sandbox" idea?
It's probably true that general-purpose modules like Go shouldn't do their own filtering unless they do it in a way that's 100% airtight for whatever hyper-specific thing they're trying to do.
What I have in mind are project-specific modules which tend to be acutely aware of what files they want and can use that domain-specific knowledge to get very precise control over cache-busting, e.g.:
If I had to put this in a .daggerignore I would probably just be too lazy. The .daggerignore approach would also force all consumers to ignore those files in all contexts which is not always what you want. I'm not a huge fan of .ignore files here for that reason. The potential use cases for including/excluding certain files are way more open-ended than with .gitignore ("don't commit to .git"). Even .dockerignore had this issue in my experience.
When a host directory is created (eg. when the CLI passes a Directory argument to a function by loading a local directory), the engine defaults to excluding everything. This effectively makes the host directory lazy: nothing gets loaded right away.
As that directory is used by the function, it may be narrowed down with subsequent calls to Directory(), File(), or WithoutDirectory. When applied to a host directory, this would lazily change the applied include/exclude filters.
Globbing support would be either retrofit into existing directory functions (eg. a new Glob: true option to WithoutDirectory()), or implemented as new functions (eg. Directory.Filter()).
In all cases, the key is that everything would be done via functions that apply to the Directory type, whether or not it is backed by llb.Local() aka Host.Directory or not.
In the end, when the resulting directory is finally used (WithExec, Terminal, AsService.Start()...), then the include/exclude are finally applied.
Benefits of this approach:
No CLI changes
No dagger.json changes
No new file formats
Might magically optimizes a bunch of existing functions. Every time your function receives a directory from the caller's local filesystem, and doesn't use all of it, it will get faster.
Throwing one option out there: what if optionally dagger.json had an ignore: [String] field? To specify what to ignore by default when passing that directory as an argument. Would be cleaner than having a separate ignore file.
Works for me, although I think it would be easy to confuse with exclude that AFAIK is still a valid option, although ever since the new source field and the fact that dagger.json is in the repo root for monorepos, I'm not actually sure what it does, but my guess is it excludes files from withing the module source, so not really relevant for excluding files from the root).
In any case, that exclude and the above list may have overlaps, but aren't the same anyway.
Works for me, although I think it would be easy to confuse with exclude that AFAIK is still a valid option
Yeah I don't know how to square that with include/exclude. I also think it's more limiting in that you can't customize individual directory args with different includes/excludes, which seems like a fairly mundane need.
I like starting with the most flexible option and then layering conveniences on top. I think #6701 + #6723 will leave us in a pretty good place in terms of flexibility and UX.
Since we don't plan on making it a CLI flag long term (because it can be optimized away in due time)
This is true, but I want to emphasize that the changes needed to do this are a pretty substantial project and, IMO, not 100% guaranteed to even be totally viable in terms of getting the same performance as having users specify include/exclude flags up front. It's a fairly fundamental rewrite of how buildkit local dir syncs work from the ground up. So we shouldn't assume it's something that we can just easily prioritize or do in the background iteratively. I think we should assume that the solution we go with for now is more than just a temp bandaid.
I'd be delighted to be completely wrong about this though of course. If we can find a way to glue together off-the-shelf tools to help implement it, maybe it can be quicker.
I like starting with the most flexible option and then layering conveniences on top. I think #6701 + #6723 will leave us in a pretty good place in terms of flexibility and UX.
I have to disagree. Flexibility isn't the first word that comes into mind about those two PRs. To me, the combination of those two are way too complex to solve a seemingly trivial problem in the context of monorepos.
Regardless of the counterarguments, a file like .daggerignore seems like a much easier solution from a user experience perspective. Arguments + some arbitrary scripting interface can hardly be called "simple" compared to that in my opinion.
It seems to me that a generic enough solution covering all cases takes priority in this conversation over UX. I'd much rather use something today that covers 80% of the use cases (AND provides a decent UX) and change it later if something better comes along.
I'm not opposed to .daggerignore and it's not mutually exclusive with the other solutions at all. But I predict that someone will soon ask "how can I use different filters for different directory args when using dagger call?". As one example, in a monorepo maybe you want to provide a directory for a node project in the repo (in which case you only need .js/.ts/etc. files) and other times you want to provide a directory for a go project (only need .go/go.mod/etc.). At that point a single .daggerignore that applies to all directory args is not gonna cut it.
I generally like going "bottom-up" with something that would cover more like 99% of cases and then layer sugar on top that makes some other subset of cases more convenient (which is what I'd view .daggerignore as). I think that often results in something more cohesive once all is said and done. But you're right we could go "middle-out" and start with .daggerignore and fill in the rest later, which could make sense since I'm sure you and others would prefer to stop the bleeding sooner than later. I just would want to make sure we have a plan on supporting the rest of the cases mentioned above.
It could be as simple as starting with .daggerignore and then adding the per-flag filtering later, with the behavior being that .daggerignore is not applicable when per-flag filters are set. Curious what others think. There's plenty of other variations possible too (e.g. specify separate .daggerignore format files per-flag).
There is only one view, so it is applied by default:
$ dagger call build --source .
The view can be applied explicitly also:
$ dagger call build --source .:default
The view is also applied when calling functions from another module (dagger.json is parsed when loading a local dir as argument, regardless of calling module):
Yeah views makes sense to me too, seems like a good middle ground 👍 (cc @jedevc, who had an initial PR for the other approach #6701).
I've also had some more thoughts on the idea of allowing users to write dagger code that runs on the host and calls out to modules (here), which hopefully could solve the more general problem here. But not mutually exclusive with views at all.
Activity
jpadams commentedon Nov 24, 2023
From @cirocosta:
on ignoring/excluding, reminds me of how sometimes taking the "we'll default to following .gitignore" ends up annoying some users: ggreer/the_silver_searcher#1189
jedevc commentedon Nov 28, 2023
As a quick and easy idea, we could potentially re-use
.dockerignore/.containerignoreif it's present? It's a bit more standardized around the container ecosystem.But I'm a little hesitant about that,
.dockerignoreis kind of a bit of a mess, I don't really want to propagate it's usage any more than we have to.I think this approach makes the most sense. We could actually just re-purpose
.WithoutFile/.WithoutDirectory, which means we can avoid having to add any extra APIs, or any extra files across a filesystem.However, as-is, these files will still be uploaded, and then removed.
What we could do though, is allow optimizing the produced LLB from a local
SourceOp+FileOpinto a single localSourceOp, by detecting if theFileOpis attempting to just remove files, then we can add those into theExcludePatternsof theSourceOp. This might be easier to implement at the Dagger level instead of directly at the LLB level (but if we could do it at the LLB level, it would also affect other places as well, like in.DockerBuild).I don't think there's any tooling in buildkit to allow easily mutating LLB for optimization passes at the moment though, so this would probably be a bit of an effort if we were to take that approach.
sagikazarmark commentedon Feb 13, 2024
This just became a lot more pressing given
dag.Host().Directory()is gone. There is no way now to exclude directories/files from being uploaded to buildkit. This adds to build time anywhere between 1 minute to 3-5 minutes in my case.jedevc commentedon Feb 13, 2024
Gonna chuck this onto the v0.10.0 milestone, since agreed, now there's no workaround at all!
(hopefully we can get this done sooner though)
helderco commentedon Feb 13, 2024
I want to see this at multiple levels, not just one option.
1. Global exclude
Similar to git global excludes:
Saved in
$HOME/.config/dagger/dagger.json.addinstead ofsetfor lists, and this command would support other global configs, like passing through proxy env vars, for example.2. (Maybe) “project” specific excludes
I also wonder about using
.gitignore,.dockerignoreor.daggerignore. Similarly to how the engine loads modules by finding alldagger.jsonfiles first to get the"exclude"configs before loading the rest, it could also find.<dagger™>ignoreand do the same indag.Host()(perhaps with an option to trigger this behavior). Or in this case, do it in the CLI and pass explicitly toHostDirectoryOpts().The CLI could also read the next-up
dagger.json(if exists) and get the list of patterns to use inHostDirectoryOpts, however I’m not sure if it’ll clash with the intent that these are used for setting up the context directory (/src) in the runtime container, for a module. Possibly ok if a path in a--srcflag is within thedagger.jsontree and the patterns in there are generic enough.For example, in a git root:
3. Extra CLI flags
This is the most specific (from CLI POV), and overrides anything from before.
When a function argument is
src *Directory, the CLI currently adds a string flag--srcto convert a path intodag.Host(path). We could add more flags (for any input where the function has options):Note: I considered
--src-excludebut that could clash with asrcExcludeargument, and is better than an extra char in--src--exclude. Not fond of a custom format like--src=.,**/*.py.So:
Of course this makes the CLI command bigger, but we’re already trending towards that with explicit env vars, etc, so that’s where we benefit from a config with defaults to run a module’s commands (consume, not author), like above with
dagger configor something similar.4. Inside function
I also think there’s value in being able to exclude a bunch of files from a
Directoryinstance, for example from a function argument. In the previous example, the go and python modules know which kind of files matter for what they need to do.Even though more files may have been uploaded anyway, it still helps with cache invalidation. So if you have a dir with go and python files, the cache in go lint won’t be invalidated if python files have changed.
vito commentedon Feb 13, 2024
Can we implement this in a way where it's all done at runtime? I was able to get away with that in Bass and the semantics felt super nice - you could basically take any path and dynamically narrow it down to include/exclude globs, and all the cache busting/file syncing semantics work as you would hope:
The trick to implementing this is that you need to defer the actual file syncing to as late as possible.
The end goal would be that the user just passes in a path like
./and the module itself narrows that path down to different glob filters when needed. This is especially useful if there are different permutations of filters that you need at different times.(This would be different from the module source dir include/exclude which is necessary for bootstrapping without uploading everything.)
jedevc commentedon Feb 14, 2024
This would be my preferred place to start (point 4 above), and then work upwards (points 3, 2 and 1).
Modules should have some idea about what their contents are (e.g. a go module is looking for go code) - so they should have some idea of what to filter. The end-user shouldn't be required to guess what a module is doing, and so what files are safe to include/exclude.
However, it does feel a bit tricky - we need to lazify the loading. Today, we call
LocalImportimmediately on callingHost.directory(which gets called by the CLI when encountering a directory flag):dagger/core/host.go
Lines 77 to 84 in fca9e78
There's a few ways of "filtering" today:
withDirectorywithinclude/excludefiltersfiltermethod as a shorthand for the above? We haveglobtoday (but it returns a list of files).withoutFile/withoutDirectorywithoutFiles- we might want this as a follow-up to ✨ Add WithFiles to Directory #6475.We'd probably want both of these to "squash" onto a lazy loaded directory, and modify the filters for that operation. To do this, we could add a new
lazyproperty to thecore.Directoryobject, and then ensuring it's evaluated before actually accessing it, and then the above operations could instead modify the parameters.Note
💡 We could potentially do this using
llb.Async, which could be quite be an elegant way of resolving this (but would need to hack around to see).sagikazarmark commentedon Feb 14, 2024
Wouldn't that go against the "module is a sandbox" idea?
Filtering can very much be an environment specific thing (eg. I have a tmp folder with all kinds of BS in it that could break
go test ./..., so I want it excluded, but the module developer (eg. platform eng. team) may not expect that)My preference would be a .daggerignore file (or something similar) as a start. It's an easy concept and easy to expand on (eg. allow other ignore files from a global dagger config, global ignores, etc)
vito commentedon Feb 14, 2024
@sagikazarmark
It's probably true that general-purpose modules like Go shouldn't do their own filtering unless they do it in a way that's 100% airtight for whatever hyper-specific thing they're trying to do.
What I have in mind are project-specific modules which tend to be acutely aware of what files they want and can use that domain-specific knowledge to get very precise control over cache-busting, e.g.:
If I had to put this in a
.daggerignoreI would probably just be too lazy. The.daggerignoreapproach would also force all consumers to ignore those files in all contexts which is not always what you want. I'm not a huge fan of.ignorefiles here for that reason. The potential use cases for including/excluding certain files are way more open-ended than with.gitignore("don't commit to .git"). Even.dockerignorehad this issue in my experience.shykes commentedon Feb 14, 2024
Here's my proposal: we make include/exclude a hidden optimization of the standard Directory API.
(As discussed with @vito on Discord today)
Directoryargument to a function by loading a local directory), the engine defaults to excluding everything. This effectively makes the host directory lazy: nothing gets loaded right away.Directory(),File(), orWithoutDirectory. When applied to a host directory, this would lazily change the applied include/exclude filters.Glob: trueoption toWithoutDirectory()), or implemented as new functions (eg.Directory.Filter()).Directorytype, whether or not it is backed byllb.Local()akaHost.Directoryor not.WithExec,Terminal,AsService.Start()...), then the include/exclude are finally applied.Benefits of this approach:
Downside:
Thoughts @jedevc @helderco @vito ?
15 remaining items
sagikazarmark commentedon Feb 23, 2024
Works for me, although I think it would be easy to confuse with
excludethat AFAIK is still a valid option, although ever since the new source field and the fact thatdagger.jsonis in the repo root for monorepos, I'm not actually sure what it does, but my guess is it excludes files from withing the module source, so not really relevant for excluding files from the root).In any case, that exclude and the above list may have overlaps, but aren't the same anyway.
sipsma commentedon Feb 24, 2024
Yeah I don't know how to square that with
include/exclude. I also think it's more limiting in that you can't customize individual directory args with different includes/excludes, which seems like a fairly mundane need.I like starting with the most flexible option and then layering conveniences on top. I think #6701 + #6723 will leave us in a pretty good place in terms of flexibility and UX.
This is true, but I want to emphasize that the changes needed to do this are a pretty substantial project and, IMO, not 100% guaranteed to even be totally viable in terms of getting the same performance as having users specify include/exclude flags up front. It's a fairly fundamental rewrite of how buildkit local dir syncs work from the ground up. So we shouldn't assume it's something that we can just easily prioritize or do in the background iteratively. I think we should assume that the solution we go with for now is more than just a temp bandaid.
sagikazarmark commentedon Feb 24, 2024
I have to disagree. Flexibility isn't the first word that comes into mind about those two PRs. To me, the combination of those two are way too complex to solve a seemingly trivial problem in the context of monorepos.
Regardless of the counterarguments, a file like
.daggerignoreseems like a much easier solution from a user experience perspective. Arguments + some arbitrary scripting interface can hardly be called "simple" compared to that in my opinion.It seems to me that a generic enough solution covering all cases takes priority in this conversation over UX. I'd much rather use something today that covers 80% of the use cases (AND provides a decent UX) and change it later if something better comes along.
sipsma commentedon Feb 25, 2024
I'm not opposed to
.daggerignoreand it's not mutually exclusive with the other solutions at all. But I predict that someone will soon ask "how can I use different filters for different directory args when usingdagger call?". As one example, in a monorepo maybe you want to provide a directory for a node project in the repo (in which case you only need.js/.ts/etc. files) and other times you want to provide a directory for a go project (only need.go/go.mod/etc.). At that point a single.daggerignorethat applies to all directory args is not gonna cut it.I generally like going "bottom-up" with something that would cover more like 99% of cases and then layer sugar on top that makes some other subset of cases more convenient (which is what I'd view
.daggerignoreas). I think that often results in something more cohesive once all is said and done. But you're right we could go "middle-out" and start with.daggerignoreand fill in the rest later, which could make sense since I'm sure you and others would prefer to stop the bleeding sooner than later. I just would want to make sure we have a plan on supporting the rest of the cases mentioned above..daggerignoreand then adding the per-flag filtering later, with the behavior being that.daggerignoreis not applicable when per-flag filters are set. Curious what others think. There's plenty of other variations possible too (e.g. specify separate.daggerignoreformat files per-flag).shykes commentedon Feb 26, 2024
@sagikazarmark @sipsma @jedevc @helderco summarizing the different options and problems.
.daggerignorefileignorefield indagger.jsoninclude/excludefields?"How about this:
dagger.json:views.PATH:[VIEW]Example:
$ cat dagger.json { [...] "views": { "default": { "exclude": [ ".direnv", ".devenv", "api/client/node/node_modules", "assets", "ci", "deploy/charts/**/charts", "docs", "examples", "tmp" ] }There is only one view, so it is applied by default:
$ dagger call build --source .The view can be applied explicitly also:
The view is also applied when calling functions from another module (
dagger.jsonis parsed when loading a local dir as argument, regardless of calling module):Things I like about this option:
Thoughts?
sagikazarmark commentedon Feb 26, 2024
Works for me. UX is good enough for me.
sipsma commentedon Mar 1, 2024
Yeah
viewsmakes sense to me too, seems like a good middle ground 👍 (cc @jedevc, who had an initial PR for the other approach #6701).I've also had some more thoughts on the idea of allowing users to write dagger code that runs on the host and calls out to modules (here), which hopefully could solve the more general problem here. But not mutually exclusive with
viewsat all.jedevc commentedon Mar 5, 2024
I think
viewsmakes sense, but to me, I'd like to suggest a way to improve it a bit further 🎉Could we allow annotating (in go, using the
// +pragmas) function inputs with a default view? For example, in my attempts to modularize our CI (https://github.com/jedevc/dagger/blob/f997c2a9d571182aa81484a76bc17112fd692adc/ci/util/sdk.go#L49-L67), I might have a directory input to generate the typescript SDK - by default I want to apply that view, but it could also work on other views.Ideally, I want users to just be able to call the module without needing to know all the different views that are provided.
sipsma commentedon Mar 8, 2024
Draft PR for the basic idea here: #6857
sagikazarmark commentedon Mar 19, 2024
YES! 🎉