Skip to content

feat: add native gitignore in fsutil - #10883

Merged
jedevc merged 1 commit into
dagger:mainfrom
jedevc:gitignore-in-fsutil
Sep 4, 2025
Merged

jedevc merged 1 commit into
dagger:mainfrom
jedevc:gitignore-in-fsutil

Conversation

@jedevc

@jedevc jedevc commented Aug 15, 2025

Copy link
Copy Markdown
Contributor

Implements my suggestion in #10868 (comment).

We remove the original approach from #10736, which attempts to convert gitignore-style patterns into dockerignore-style patterns (supported by buildkit, which uses mobypatternmatcher). Instead, we add a gitignoreFS, which creates a filtered FS in the style of filterFS, but that automatically extracts gitignore filters from .gitignore files in the tree, and applies them using go-git.

This has a couple of advantages:

  • Instead of converting, we directly process gitignore patterns. No weird issues like chore: fix incorrect behaviour for dir only pattern on .gitignore #10870 can ever occur again. Using a mature implementation like go-git ensures we're as close to git as possible.
  • Fewer file transfers to-and-from the host filesystem - we don't need to load gitignores to process patterns, the file-transfer protocol simply understands them natively.
  • Simpler implementation - all the gitignore code is completely isolated to one component in the file transfer stack, instead of being spread out through gitignores and the like.

@jedevc
jedevc force-pushed the gitignore-in-fsutil branch from ed8ad36 to 2a10ffd Compare August 15, 2025 15:01
@jedevc
jedevc force-pushed the gitignore-in-fsutil branch 4 times, most recently from 0d564a3 to 88f9423 Compare August 22, 2025 09:57
@jedevc jedevc changed the title wip: chore: use native gitignore in fsutil chore: use native gitignore in fsutil Aug 22, 2025
Comment thread core/integration/host_test.go Resolved
@jedevc

jedevc commented Aug 22, 2025

Copy link
Copy Markdown
Contributor Author

There's one failing check, that I'm unsure how to resolve.

t.Run("ignore dir but keep file in subdir", func(ctx context.Context, t *testctx.T) {
out, err := modGen.With(daggerCall("ignore-dir-but-keep-file-in-subdir", "directory", "--path", "internal/telemetry", "entries")).Stdout(ctx)
require.NoError(t, err)
require.Equal(t, "proxy.go\n", out)
})

internal/telemetry is ignored - if I pass this as an argument... what should happen?

  • Should it error out? (currently implemented)
  • Should it error out with a specific "this file/directory is ignored, and cannot be loaded"?
  • Should it be loaded, but with none of it's contents, since they're ignored?
  • Should we discard the gitignore pattern that would directly ignore internal/telemetry, but continue to apply all the other patterns inside it?
  • Should we just load internal/telemetry as-is, no changes? (but then, what's the point of the gitignore at all?)

@jedevc
jedevc requested review from TomChv and helderco August 22, 2025 10:04
@TomChv

TomChv commented Aug 25, 2025

Copy link
Copy Markdown
Member

internal/telemetry is ignored - if I pass this as an argument... what should happen?

  • Should it error out? (currently implemented)
  • Should it error out with a specific "this file/directory is ignored, and cannot be loaded"?
  • Should it be loaded, but with none of it's contents, since they're ignored?
  • Should we discard the gitignore pattern that would directly ignore internal/telemetry, but continue to apply all the other patterns inside it?
  • Should we just load internal/telemetry as-is, no changes? (but then, what's the point of the gitignore at all?)

If you try to access a directory that doesn't exist with our current API, it will error out, I think we should keep that DX to avoid inconsistency but maybe as you mention in option 2, with a specific error so the user can have a better understanding of why this dir/file is missing.

Comment thread core/schema/host.go
dagql.Arg("exclude").Doc(`Exclude artifacts that match the given pattern (e.g., ["node_modules/", ".git*"]).`),
dagql.Arg("include").Doc(`Include only artifacts that match the given pattern (e.g., ["app/", "package.*"]).`),
dagql.Arg("noCache").Doc(`If true, the directory will always be reloaded from the host.`),
dagql.Arg("noGitAutoIgnore").Doc(`Don't apply .gitignore filter rules inside the directory`),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering about this.

I think this makes sense as the default, but it is a bit weird to me that users can't disable it when passing directories from the CLI.

Do we need a way to override it?

@TomChv TomChv Aug 27, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a way but how?

It may be simpler as a pragma so you can target a specific argument, because from the CLI it would be global to the dagger call cmd

func (e *Example) A(
  //+noGitAutoIgnore
  dir *dagger.Directory
) { ... } 

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mm, yeah, but what about if it's not a contextual arg?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that option can apply to any directory, even if it's not a contextual directory.

Because when we do dagger call a --dir /foo/bar, we look for the closest .git to see if we can apply git ignore, it's totally decouple from contextual argument in this case.

You may have //+noGitAutoIgnore but no //+defaultPath

@jedevc jedevc Aug 28, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my bad, I should have been clearer - what if it's not an arg at all - what if it's not a user function?

Like, what if it's a core function, or a function in a module you don't control?

We'd have to have weird flag syntax, something like --dir file:///foo/bar?noGitAutoIgnore=true.

I don't really love it, kind of why I'm suggesting #10883 (comment) in the meantime.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess then no support for Host.directory except for module argument may be the solution.

I wonder what's @helderco opinion

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the problem with dagger call, but with shell there's no issue:

dagger -c 'a $(host | directory --no-git-auto-ignore /foo/bar)'

We could disable just for call, but then how would you enable it?

If it were up to me we'd deprecate dagger call in favor of shell instead of having this split.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify I think only in rare use cases people will want it off so my preference is to have it enabled by default as much as possible.

Also, dagger call is already more limited than shell in a lot of ways so it's not a deal breaker this is yet another thing, although I do understand if someone that needs it off already uses dagger call a lot it can be frustrating being forced to switch to shell syntax. That's why I'd rather not have this split in general. It creates friction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, okay, I'm happy with that answer 🎉

I'd consider this resolved!

@jedevc

jedevc commented Aug 27, 2025

Copy link
Copy Markdown
Contributor Author

Hm, mildly tempted to just have the default for Host.directory ... not have gitignore support by default.

Just do it for modules? That would resolve both the above issues for now.

@helderco

Copy link
Copy Markdown
Contributor

There's one failing check, that I'm unsure how to resolve.

I think this should be split into two tests.

In order to match .gitignore, since the intent of the test is to negate the ignore on a specific file, the first pattern should be internal/telemetry/**:

func (t *Test) IgnoreDirButKeepFileInSubdir(
-  // +ignore=["internal/telemetry", "!internal/telemetry/proxy.go"]
+  // +ignore=["internal/telemetry/**", "!internal/telemetry/proxy.go"]
  // +defaultPath="./dagger"
  dir *dagger.Directory,
) *dagger.Directory {
  return dir
}

But you raise an interesting question. What happens when you do have these patterns?

internal/telemetry
!internal/telemetry/proxy.go

I think like git, the second pattern should have no effect (basically internal/telemetry shouldn't exist) and if you try to select an ignored subdir from it it should be like any other directory that doesn't have the target path:

❯ dagger -M -c 'directory | directory internal/telemetry | entries'
! /internal/telemetry: no such file or directory

@jedevc

jedevc commented Aug 29, 2025

Copy link
Copy Markdown
Contributor Author

Still struggling a bit here. I think I have two questions:

  1. If I do dagger -c 'host | directory internal/telemetry/ | entries' where internal/telemetry/ is in the .gitignore, what should happen? Currently this errors out in host.directory, because internal/telemetry/ gets filtered out by gitignoreFS.

    Is that reasonable? Or does one of the alternative bullet points from feat: add native gitignore in fsutil #10883 (comment) sound better?

  2. Does that failing test even make sense now? internal/telemetry is part of the gititgnore, so you can't unignore it using an include pattern - they're two separate layers. I think this makes sense, if it's in the gitignore it's not gonna be in a modulesource that's from git (and not local) - so you get a more consistent behavior.

    This is a change from the original impl, I explained some of the logic in feat: apply git ignore pattern on contextual directory argument #10736 (comment).

    It wouldn't be impossible to have that behavior (so that you could do !internal/telemetry if you really wanted to override the git ignore) - but it would definitely require a big fork/change of fsutil, so wanna confirm, since I actually think we shouldn't allow it.

@jedevc
jedevc force-pushed the gitignore-in-fsutil branch from 6df8034 to cdc5f49 Compare August 29, 2025 10:37
@jedevc
jedevc marked this pull request as ready for review August 29, 2025 13:52
@jedevc
jedevc requested review from a team as code owners August 29, 2025 13:52
@jedevc jedevc changed the title chore: use native gitignore in fsutil feat: add native gitignore in fsutil Aug 29, 2025
@jedevc jedevc added the needs/changelog Pull requests that require a changelog entry label Aug 29, 2025
@helderco

helderco commented Sep 1, 2025 •

Copy link
Copy Markdown
Contributor
  1. If I do dagger -c 'host | directory internal/telemetry/ | entries' where internal/telemetry/ is in the .gitignore, what should happen? Currently this errors out in host.directory, because internal/telemetry/ gets filtered out by gitignoreFS.
    Is that reasonable? Or does one of the alternative bullet points from feat: add native gitignore in fsutil #10883 (comment) sound better?

I think it’s reasonable. If it’s trivial it can be interesting to give a more specific error message but could be confusing if that doesn’t happen everywhere there’s exclude patterns filtering a directory (otherwise users may think that’s not the problem if the error doesn’t specify any exclude patterns).

  1. Does that failing test even make sense now? internal/telemetry is part of the gititgnore, so you can't unignore it using an include pattern - they're two separate layers. I think this makes sense, if it's in the gitignore it's not gonna be in a modulesource that's from git (and not local) - so you get a more consistent behavior.

Oh yeah, that’s a good point i.e., getting consistent behavior with git. How do you disallow overrides in Host.directory, for example? Do you have to apply all the .gitignore patterns first and only filter the user’s patterns on the resulting directory? Does it take a non-trivial performance hit to do that?

[…] wanna confirm, since I actually think we shouldn't allow it.

Agreed. I didn’t previously consider consistency with git here as far as the overrides are concerned, was just concerned about an escape hatch out of extreme caution. In that case, if an escape hatch ends up being necessary, I can support doing that through simply disabling the auto gitignore feature, as we already have (except on the CLI’s directory flag).

@TomChv TomChv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left smalls comments, overall code is nice!

The tests part on fsxutil is 🔥

Comment thread core/integration/host_test.go Resolved
Comment thread core/integration/host_test.go Resolved
Comment thread core/integration/host_test.go Resolved
Comment thread core/integration/module_test.go Resolved
Comment thread core/schema/host.go Resolved
Comment thread engine/sources/local/source.go Resolved
@jedevc
jedevc force-pushed the gitignore-in-fsutil branch from cdc5f49 to bdb2f9b Compare September 3, 2025 11:51
@jedevc

jedevc commented Sep 3, 2025

Copy link
Copy Markdown
Contributor Author

Working on the failures, realized I had a misunderstanding of how the local/remote file interactions work

@jedevc
jedevc force-pushed the gitignore-in-fsutil branch from bdb2f9b to b6f41e3 Compare September 3, 2025 15:02
@jedevc

jedevc commented Sep 3, 2025 •

Copy link
Copy Markdown
Contributor Author

There we go 😅 It needed the corresponding changes to Copy.

Otherwise, we were syncing the remote -> local (using gitignore), but then filling up the snapshot using the entire contents of the local snapshot (not using the gitignore).

Just required a little bit of hackery, but it works now!

@TomChv TomChv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, CI is green, I just left a question but I think we can merge that :D

I'll follow this up with a PR to dagOpFi host.Directory so we have better control on the option.

Comment thread core/schema/host.go

includePatterns := make([]string, 0, 2+len(args.Include))
if relPath != "." {
includePatterns = append(includePatterns, "!*", relPath)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while to understand how you could give the host path (context dir) but still make it work if we're important a children dir of that ctx dir but I get it with that line.

So just to be sure I understand, let's say we have a repo at /work and you import /work/bar/foo.

hostPath will be: /work
relpath will be : bar/foo

Include: ["!*", "bar/foo"] -> Everything is ignored except bar/foo

Then you do the rest of the logic with ignores etc...
Am I right or I misunderstood something?

@jedevc jedevc Sep 4, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, completely correct.

We also move the include/exclude paths to be "rebased".

I'll add a test for this, since it's a bit of a complex interaction. (edit: actually not needed, since all the existing include/exclude tests should cover this)

@jedevc
jedevc merged commit a4c780b into dagger:main Sep 4, 2025
@jedevc
jedevc deleted the gitignore-in-fsutil branch September 4, 2025 09:43
@babo

babo commented Sep 5, 2025 •

Copy link
Copy Markdown

This change just broke my module constructor which refers to a file with a default name .args which is in the .gitignore to prevent it to accidentally checked in to the repository. How to resolve this?

`"failed to get value for argument "args": select: failed to stat file'

    // Constructor
    func New(ctx context.Context,
        // environment variables file
        // +defaultPath=".args"
        // +optional
        args *dagger.File) {...}

@danielgafni

Copy link
Copy Markdown

#10994

@nathan-joslin

nathan-joslin commented Sep 5, 2025 •

Copy link
Copy Markdown

@jedevc Does this handle .git/info/exclude? I do not see this mentioned in threads or the changes.

As a side note, git config's core.excludesfile is a third gitignore location. There likely is an argument that a global gitignore file is outside of dagger's sandbox and as such shouldn't be handled implicitly/by default (but potentially explicitly through flags, env vars, etc.)

@jedevc

jedevc commented Sep 8, 2025

Copy link
Copy Markdown
Contributor Author

Adding another note (for me, most likely) - I don't think this works as expected if we have .gitignore in the exclude patterns. Since it never makes it to the local clone, then the copy to the snapshot won't be done correctly.

@staticaland

Copy link
Copy Markdown

Pardon my ignorance, but how do I go from:

// Sync files to S3 bucket
func (m *Aws) SyncToBucket(
	ctx context.Context,
	bucket string,
	src *dagger.Directory,
	
...

	ctr = ctr.WithMountedDirectory("/work", src)
	
...

To this:

Add NoGitAutoIgnore argument to Host.Directory to disable this behavior.

I don't know enough about Dagger to understand that yet 😅

Context

MkDocs static site, where the output dir is in .gitignore. Pretty common pattern, that is for example included in GitHub's Python .gitignore file.

I've worked around this by using a separate directory when running in Dagger locally vs fully automated, where I don't expect any accidental commits of the built files to happen.

@nathan-joslin

nathan-joslin commented Sep 8, 2025 •

Copy link
Copy Markdown

@staticaland You may be interested in #10994.

You may be able to use the host functions directly to ensure the gitignore'd dir is available when needed. See func (*Host) Directory.

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

Labels

needs/changelog Pull requests that require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants