Skip to content

Use .gitignore by default when loading modules #6627

Closed

Description

@helderco

Motivation

I have to constantly add "exclude": ["sdk", ".venv"] to dagger.json after dagger init.

I don't expect these files to be uploaded. They're not completely replaced in codegen. During local dev, some files can be created in there and they will stay after codegen in the container. It affects the Source dir cache and slows down module loading.

Solution

The engine already loads dagger.json first. Load .gitignore too. Prepend the .gitignore patterns to the exclude patterns from dagger.json when collecting the patterns for loading the module’s files.

As an escape hatch, if the user needs to include something in .gitignore they can add the same pattern manually to include in dagger.json.

Rationale

This seems like the easiest solution, with less work for the user but keeping control.

Doesn’t require extra work. Most modules in the Daggerverse have the automatically generated .gitignore but no patterns in "exclude". By using .gitignore, those modules would benefit automatically. Of course, this is only necessary when dagger developing modules locally.

When a module is loaded via a git reference, those patterns are already applied because the git clone won’t have those files. So using .gitignore is actually more consistent because it avoids differences in dirty working copies.

It’s still possible to have dirty woking copies because global ignores won’t be picked up, but rather than adding some of those to exclude, users might as well just add them to .gitignore which also helps other users contributing to a module that don’t have the same global ignores.

It’s also possible for files in a module to be ignored by some other .gitignore up the file tree until the git repo root, but we could automatically find those and add their patterns, prefixed with their file path relative to the context directory, to the module’s excludes.

Activity

  1. changed the title [-]Zenith: GeneratedCode.VcsGeneratedPaths should be added do `exclude` in `dagger.json` by default[/-] [+]`GeneratedCode.VcsGeneratedPaths` should be added do `exclude` in `dagger.json` by default[/+] on Jul 3, 2024
  2. changed the title [-]`GeneratedCode.VcsGeneratedPaths` should be added do `exclude` in `dagger.json` by default[/-] [+]Runtime modules should be able to configure default excludes in `dagger.json`[/+] on Jul 5, 2024
  3. helderco commented on Jul 5, 2024

    @helderco
    ContributorAuthor

    @sipsma, I updated the description. Previously, my thinking was just to add the ignore patterns to the execlude in dagger.json in dagger { init | develop } --sdk=, but wdyt about loading from .gitignore?

  4. shykes commented on Jul 25, 2024

    @shykes
    Contributor

    This seems reasonable to me but I may not be aware of all the subtleties of the design.

    In general I'm in favor of adopting the standard gitignore format whenever we need to express filters on directories (see for example the ignore part of #7647 ). This is one step further in the same general direction.

    How would the default behavior be disabled or customized?

  5. jpadams commented on Jul 25, 2024

    @jpadams
    Contributor

    @sipsma, I updated the description. Previously, my thinking was just to add the ignore patterns to the execlude in dagger.json in dagger { init | develop } --sdk=, but wdyt about loading from .gitignore?

    @drmikecrowe love to get your thoughts ☝️

  6. drmikecrowe commented on Jul 26, 2024

    @drmikecrowe

    I guess my base question would be: Why wouldn't you adopt .gitignore as the standard? In my case, I'm controlling the release target contents via .gitignore. There's no situation where I want to use local files in dagger that are outside .gitignore because they will never be in my target environment (unless I'm missing something).

    As background, here was my new user experience:

    • Install dagger
    • Run dagger init --sdk=typescript and wait 6 minutes
    • Start researching how to exclude (and you can't on dagger init)

    In my local directory, I have .yarn/cache which is 6G. Yes, it probably shouldn't be there, but in this case we were experimenting with yarn's pnp mode, which includes the cache in the git lfs

  7. helderco commented on Jul 26, 2024

    @helderco
    ContributorAuthor

    I guess my base question would be: Why wouldn't you adopt .gitignore as the standard? In my case, I'm controlling the release target contents via .gitignore. There's no situation where I want to use local files in dagger that are outside .gitignore because they will never be in my target environment (unless I'm missing something).

    Yes, that's what I'm suggesting. I'll reformulate the description when I can to provide more details on how I see this working, including picking up on global git excludes and parent .gitignore files too.

    Also, notice this part of the rationale:

    When a module is loaded via a git reference, those patterns are already applied because the git clone won’t have those files. So using .gitignore is actually more consistent because it avoids differences in dirty working copies.

    So in the same line of thought, when we support other VCSs, I'd do the equivalent to .gitignore in those cases if possible.

  8. helderco commented on Jul 26, 2024

    @helderco
    ContributorAuthor

    How would the default behavior be disabled or customized?

    A few ideas:

    • You could use dagger.json's own include/exclude patterns to negate or override a .gitignore pattern.
    • Disable with a --no-ignore flag that a lot of CLI's use (e.g., rg).
    • Disable with a ignore-vcs option in dagger.json (e.g., see hatch).
  9. drmikecrowe commented on Jul 26, 2024

    @drmikecrowe

    Remember, dagger init will not run if there is a dagger.json present -- options 1 and 3 don't seem possible. I think the second option is the only choice of those 3.

    Alternatively, scope out a .daggerignore?

    Also, does dagger respect .dockerignore?

  10. helderco commented on Jul 26, 2024

    @helderco
    ContributorAuthor

    Remember, dagger init will not run if there is a dagger.json present -- options 1 and 3 don't seem possible. I think the second option is the only choice of those 3.

    Those options aren't targetting dagger init, it's mostly dagger call. Remember, those options are for disabling the default use of .gitignore files, but your use case doesn't call for disabling. If you did want to disable, you could do it after dagger init as I can't think why a dagger init would fail based on something you have on .gitignore. Even if that's desirable, a CLI flag can be added there as well.

    Also, does dagger respect .dockerignore?

    No, it doesn't. I don't see a need for it other than possibly on getting a directory for a docker build, which is different use case, however we use a common Directory for that, uncoupled to the docker build.

  11. drmikecrowe commented on Jul 26, 2024

    @drmikecrowe

    Those options aren't targetting dagger init, it's mostly dagger call.

    Help me understand please:

    • When would you want to include files in dagger init and then exclude them in dagger call?
    • Conversely, when would you need to include files in dagger call that you previously excluded in dagger init?

    I'm trying to understand the different use-cases (and to date, my angst has been with excluding and dagger init, so I haven't had time to experience the need in dagger call yet)

  12. helderco commented on Jul 26, 2024

    @helderco
    ContributorAuthor

    I'm trying to understand the different use-cases (and to date, my angst has been with excluding and dagger init, so I haven't had time to experience the need in dagger call yet)

    Well, today, if your dagger init takes 6m and you don't proceed to adding excludes in dagger.json, depending on the changes you make triggering cache invalidation, can take you the same 6m or more in a dagger call.

    dagger init creates initial files based on a template. That's controlled by the SDK and fixed for each release. You can add any number of stuff afterwards depending on your needs. Add dependencies, add files to the source directory, run local tools that create new stuff that's not generated in dagger init but that needs excluding for subsequent dagger call commands.

    A simple yarn install in your host is a common example of this. It'll create a node_modules directory not generated in dagger init that you don't want to upload when loading the module on a dagger call. The engine, by default, adds a <source dir>/**/* include. That’s today.

    Let's say you already have a .gitignore in your repo (or even a global git exclude if this is possible for Dagger to use), with the patterns .yarn, node_modules, etc. Then with this proposal, dagger init would pick up on that automatically, thus solving the problem that you had.

    The same would be kept for dagger call without having to manually add "exclude" patterns to dagger.json. And if you happen to use direnv during development, and add .direnv to .gitignore because you don't want to commit that, then there's nothing else you need to do to exclude it in Dagger too.

  13. 4 remaining items

  14. self-assigned this
    on Aug 1, 2024
  15. helderco commented on Sep 20, 2024

    @helderco
    ContributorAuthor

    There's a POC here:

    I still need to update this issue's description with the proposal, add details and examples for design review. It's not forgotten, I'm just backlogged.

  16. fcbry commented on Sep 26, 2024

    @fcbry

    There's a POC here:

    I still need to update this issue's description with the proposal, add details and examples for design review. It's not forgotten, I'm just backlogged.

    @helderco This idea sounds good but I just thought of a potential failure case - how will Github large file support be handled? We have a bunch of repositories which have an included gitversion.exe for devops purposes, it's 160mb, and it's not the only example.. we also have repositories with stub databases and suchlike.
    Github encourage this approach (vendor lock in) so it's impossible to prevent developers doing so. How would one handle this? These artifacts won't be git excluded but we definitely don't want that junk sent to docker on every dagger ... invocation.

  17. helderco commented on Sep 26, 2024

    @helderco
    ContributorAuthor

    How would one handle this? These artifacts won't be git excluded but we definitely don't want that junk sent to docker on every dagger ... invocation.

    The idea is that the .gitignore files contribute to the final exclude patterns for loading a module. You still have dagger.json exclude to ignore more stuff that's not in any .gitignore, or even to override a pattern that was added by .gitignore.

    For example, let's say you have a .gitignore with this:

    .idea

    And in your my-module/dagger.json:

    {
      "name": "my-module",
      "sdk": "go",
      "exclude": [
        "gitversion.exe"
      ]
    }

    Then the final exclude patterns for loading your module looks like this:

    excludes := []string{
         "**/.git",                   // always added by the engine
         "**/.idea",                  // from .gitignore
         "my-module/dagger.gen.go",   // from my-module/.gitignore
         "my-module/internal/...",    // from my-module/.gitignore
         "my-module/gitversion.exe",  // from my-module/dagger.json
    }
  18. fcbry commented on Sep 27, 2024

    @fcbry

    @helderco that's great - thanks for clarifying, still familiarizing myself with dagger.

  19. sipsma commented on Oct 28, 2024

    @sipsma
    Contributor

    I'm currently working on some performance improvements to module loading that will result in enough changes/simplifications to the relevant code that I'm just going to rope this in. Re-assigning to me (cc @helderco)

  20. assigned and unassigned on Oct 28, 2024
  21. Vanuan commented on Jun 27, 2025

    @Vanuan

    Stumbled across this issue after constantly hitting no space left on device. Apparently, dagger sends my whole project to the system volume. Apparently including all the unnecessary files such as .git.
    Is it a sane default behavior?

  22. Vanuan commented on Jun 27, 2025

    @Vanuan

    For now, I'm using this:

      source: Directory;
    
      constructor(
        @argument({
          defaultPath: '.',
          ignore: [
            'dist/**',
            'node_modules/**',
            // .....
          ],
        })
        source: Directory,
      ) {
        this.source = source;
      }
    
      @func()
      buildBaseImage(): Container {
        return dag
          .container()
          .build(this.source, { dockerfile: '.dagger/Dockerfile.base' });
      }
    
    

    But surely there must be a better way. The @argument decorator appears to be analyzed statically rather than in runtime, so can't load from .gitignore there.

  23. TomChv commented on Jul 23, 2025

    @TomChv
    Member

    I'm currently working on some performance improvements to module loading that will result in enough changes/simplifications to the relevant code that I'm just going to rope this in. Re-assigning to me (cc @helderco)

    Following those improvements, #10736 should close that issue by applying .gitignore on contextual arguments.
    Module loading already only include module sources so it's not a problem anymore

  24. helderco commented on Jul 25, 2025

    @helderco
    ContributorAuthor

    Module loading already only include module sources so it's not a problem anymore

    Module loading includes everything inside the source directory, even node_modules and .yarn/cache, etc... So it can't be solved with just with the contextual directory argument of a function, this issue is specifically about loading the module itself.

  25. TomChv commented on Aug 8, 2025

    @TomChv
    Member

    Fixed by #10736, please reopen if there's anything missing to solve that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/enginekind/proposal

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions