Skip to content

Need equiv of include / exclude for directories with dagger call CLI #6155

Closed

Description

@jpadams

An idea that has been looked at (and rejected) in the past are a dagger.ignore file.

Other approach could be to use some sort of include / exclude patterns on CLI, though it may be verbose/complex DX.

Another approach could be a top-level "main" module that had host filesystem access and include/excludes could be used there. cc @vito

Could also be a "filter" that could be applied at any point on a loaded directory. filtered_dir = dir.with_exclude_filter("*.json")

Activity

  1. jpadams commented on Nov 24, 2023

    @jpadams
    ContributorAuthor

    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

  2. jedevc commented on Nov 28, 2023

    @jedevc
    Contributor

    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.

  3. added
    area/cli
    on Jan 8, 2024
  4. sagikazarmark commented on Feb 13, 2024

    @sagikazarmark
    Contributor

    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.

  5. jedevc commented on Feb 13, 2024

    @jedevc
    Contributor

    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)

  6. added this to the v0.10.0 milestone on Feb 13, 2024
  7. helderco commented on Feb 13, 2024

    @helderco
    Contributor

    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.

    For example, in a git root:

    dagger init
    dagger use <awesome module>
    dagger config add exclude "./tests"  # add to ./dagger.json
    dagger -m awesome call --src=. lint
    

    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 --src to convert a path into dag.Host(path). We could add more flags (for any input where the function has options):

    --src         string
    --src.include []string
    --src.exclude []string
    

    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.

  8. vito commented on Feb 13, 2024

    @vito
    Contributor

    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.)

  9. jedevc commented on Feb 14, 2024

    @jedevc
    Contributor

    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):

    dagger/core/host.go

    Lines 77 to 84 in fca9e78

    _, desc, err := host.Query.Buildkit.LocalImport(
    ctx,
    subRecorder,
    host.Query.Platform.Spec(),
    dirPath,
    filter.Exclude,
    filter.Include,
    )

    There's a few ways of "filtering" today:

    • Copy into an empty directory using withDirectory with include/exclude filters
      • Potentially, we could have a new filter method as a shorthand for the above? We have glob today (but it returns a list of files).
    • Manually call withoutFile/withoutDirectory

    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).

  10. sagikazarmark commented on Feb 14, 2024

    @sagikazarmark
    Contributor

    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)

  11. vito commented on Feb 14, 2024

    @vito
    Contributor

    @sagikazarmark

    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.:

            (cd (glob src ./nix/ ./flake.nix ./flake.lock ./default.nix)
              ($ nix build ".#depsImage"))
      (defn go-files [src]
        (glob src
              ./**/*.go
              ./**/go.mod
              ./**/go.sum
              ./std/*.bass
              ./pkg/bass/testdata/**/*
              ./pkg/runtimes/testdata/**/*
              ./pkg/lsp/testdata/**/*
              ./Makefile
              ! ./hack/vendor/))

    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.

  12. shykes commented on Feb 14, 2024

    @shykes
    Contributor

    Here's my proposal: we make include/exclude a hidden optimization of the standard Directory API.

    (As discussed with @vito on Discord today)

    • 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:

    1. No CLI changes
    2. No dagger.json changes
    3. No new file formats
    4. 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.

    Downside:

    1. Maybe difficult to implement?

    Thoughts @jedevc @helderco @vito ?

  13. 15 remaining items

  14. sagikazarmark commented on Feb 23, 2024

    @sagikazarmark
    Contributor

    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.

  15. sipsma commented on Feb 24, 2024

    @sipsma
    Contributor

    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.
  16. sagikazarmark commented on Feb 24, 2024

    @sagikazarmark
    Contributor

    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.

  17. sipsma commented on Feb 25, 2024

    @sipsma
    Contributor

    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).
  18. shykes commented on Feb 26, 2024

    @shykes
    Contributor

    @sagikazarmark @sipsma @jedevc @helderco summarizing the different options and problems.

    Idea Problem
    Make it all explicit in the command-line That's a very long command-line for @sagikazarmark
    Create a new .daggerignore file 1) One more file.. 2) "how can I use different filters for different directory args"?
    Create a new ignore field in dagger.json "What's the difference with include/exclude fields?"

    How about this:

    • New optional field in dagger.json: views.
    • A view of a directory is simply an include/exclude filter applied to that directory
    • A view has a name. It can be reference from the CLI with a simple syntax: PATH:[VIEW]
    • If there is only one view, it is used by default

    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:

    $ 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):

    $ dagger -m github.com/kpendgound/golang --project .:default

    Things I like about this option:

    • @sagikazarmark doesn't have to type super-long commands, and rely on wrappers
    • No new file
    • We keep the existing include/exclude logic. Less new code to implement, document and debug
    • We still add special syntax to the CLI argument, but it's much simpler.

    Thoughts?

  19. sagikazarmark commented on Feb 26, 2024

    @sagikazarmark
    Contributor

    Works for me. UX is good enough for me.

  20. modified the milestones: v0.10.0, v0.10.x on Feb 26, 2024
  21. sipsma commented on Mar 1, 2024

    @sipsma
    Contributor

    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.

  22. jedevc commented on Mar 5, 2024

    @jedevc
    Contributor

    I think views makes 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.

  23. sipsma commented on Mar 8, 2024

    @sipsma
    Contributor

    Draft PR for the basic idea here: #6857

  24. sagikazarmark commented on Mar 19, 2024

    @sagikazarmark
    Contributor

    YES! 🎉

  25. modified the milestones: v0.10.x, v0.10.3 on Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/cli

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions