Skip to content

feat: apply git ignore pattern on contextual directory argument - #10736

Merged
TomChv merged 36 commits into
dagger:mainfrom
TomChv:feat/apply-gitignore-pattern-on-module-loading
Aug 6, 2025
Merged

TomChv merged 36 commits into
dagger:mainfrom
TomChv:feat/apply-gitignore-pattern-on-module-loading

Conversation

@TomChv

@TomChv TomChv commented Jul 16, 2025 •

Copy link
Copy Markdown
Member

Automatically apply .gitignore patterns if the directory is inside a git repository.
Automatically load .gitignore patterns on contextual arguments by setting the internal argument ContextDirectoryPath to Host.Directory.
Add NoAutoGitIgnore argument to Host.Directory to disable that behaviour.

Complete benchmarks here: #10736 (comment)

This shows the module loading time inside clean repository, we already know from #10736 (comment) that it improves directory loading when files are automatically ignored like node_modules but as asked at #10736 (comment) these metrics shows the overhead of reading .gitignore files on a clean repo where these patterns would never be applied.

Screenshot 2025-08-01 at 21 17 31 image

@TomChv
TomChv requested a review from jedevc July 16, 2025 15:25
@TomChv TomChv self-assigned this Jul 16, 2025
Comment thread core/modulesource.go Outdated
Comment on lines +336 to +343
// Rebased the ignore patterns to be relative to the mounted path from the context directory.
// - If the contextual path is /foo/bar, all /foo/bar/.gitignore patterns must become the root or they'll not be applied correctly
// - Recursive patterns are not affected
// - Patterns that are outside the contextual path are ignored.
// - If relativePathToCtx is "." (meaning the contextual path is the same as the mounted path), then all ignore
// patterns are applied.
for _, pattern := range src.Local.IgnorePatterns {

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.

As we discussed before, if we can, we should try and get this logic into Host.directory (probably through an opt-in optional arg?), even if it requires a bit more effort.

  1. Then it's reusable, external clients can use it.
  2. We can use it for all CLI directory args (not just for the default context args). Without that, we'd have to implement it twice, or we have different directories included for whether you pass it in manually, or rely on the default.

@TomChv TomChv Jul 18, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I agree with you but there's still that problem of context directory that we'll need to work around. I'm quite not sure yet to how solve it.

To load an argument we call LoadContext with the default path set in the argument.
Like +defaultPath="/foo/bar", but in the logic your pointing at, I'm looking for .gitignore on all context directory, not just the given path and its children.

// - Recursive patterns are not affected <- That means they must be applied

In Host.Directory, we could have a new argument like useGitIgnore that will apply all .gitignore existing in the given directory, that's totally possible.
But for contextual argument, it's more tricky, applying .gitignore existing in /foo/bar isn't enough, we need to get .gitignore from the context dir and anything under it.

So I think the logic will be duplicated in order to make that possible, what do you think?

up! I have an idea! I could simply just keep the recursive pattern and implement the git ignore research in Host.Directory too, we'll have a little bit of duplication but that's the best of both world imo

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.

So, is the problem that if we load foo/bar, that we won't load foo/other/.gitignore? If my understanding is right, we can handle that - we can have the gitignore loading logic in Host.directory right? We're not restricted there, we can read any files on the host if we want.

If that's not the problem, can you share an example?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can have the git ignore logic in host.directory yeah, but that means we mean to dedup the findup logic to find the parent directory (either a .git repo or the argument dir is considered the root if there's no .git?)

@TomChv TomChv Jul 21, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

My only problem is where do we stop looking for a parent .gitignore, for context dir it's easy, we already have the ContextDirectoryPath when looking for these gitignore, but if we include that in host.Directory, we don't have it so we need to figure it out inside the func, which lead to either deduplication or no-complete ignore (arg dir is considered the root dir).

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.

Ah, I see, yeah, we'll have a copy of the findup logic. However, it shouldn't be a copy, we should extract that logic to a specific function and reuse it there.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Make sense! alright! I'll push my changes so far, and continue on this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note from a 1:1 with Justin

  1. We look for .git
  2. If not .git -> skip
  3. If there's -> grab all .gitignore of that repo

Add a hidden argument so we don't duplicate findup if it's called from LoadContext, if contextDirectoryPath is set, skip find up

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in ec87d7f

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.

If this feature needs to be explicitly enabled I can see possible confusion if user puts the flag but then nothing happens. If not an error, could be just a warning. If feature is enabled by default than I think it's ok.

Comment thread core/schema/util.go Resolved
Comment thread core/schema/util.go Resolved
Comment thread core/schema/util.go Resolved
@TomChv
TomChv force-pushed the feat/apply-gitignore-pattern-on-module-loading branch 2 times, most recently from 365929e to 7b78242 Compare July 22, 2025 09:29
@TomChv

This comment was marked as outdated.

@TomChv
TomChv requested review from a team as code owners July 23, 2025 16:39
@TomChv
TomChv requested a review from jedevc July 23, 2025 16:46

@helderco helderco left a comment

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.

Thanks for moving this forward! 🙏

Comment thread core/util.go Resolved
Comment thread core/util.go Resolved
Comment thread core/util.go Resolved
Comment thread core/schema/host.go Resolved
Comment thread core/schema/host.go Resolved
Comment thread core/schema/host.go Resolved
Comment thread core/schema/host.go
// Rebased the ignore patterns to be relative to the mounted path from the context directory.
// - If the contextual path is /foo/bar, all /foo/bar/.gitignore patterns must become the root or they'll not be applied correctly
// - Recursive patterns are not affected
// - Patterns that are outside the contextual path are ignored.

@helderco helderco Jul 24, 2025 •

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.

Can you come up with an example where this could happen (as a comment)? It's good to have that guard just want to understand that use case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 36bfc50

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.

My comment was specifically on line 422:

Patterns that are outside the contextual path are ignored.

And I meant for you to just comment here, not to code.

Comment thread core/schema/host.go Resolved
Comment thread core/schema/host.go Resolved
Comment thread core/util.go Outdated
// (ends with `/`), we treat is as a regular path then readd the exclusion to make
// sure the recusive pattern is applied if needed.
// Example: !foo becomes foo then **/foo then !**/foo
func extractGitIgnorePatterns(gitIgnoreContent string, parentDir string) []string {

@helderco helderco Jul 24, 2025 •

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.

Just a thought, but there's another potential optimization that could be made to avoid loading patterns from .gitignore files inside directories that match already discovered ignore patterns. For example, in dagger's root .gitignore there's .venv and .idea, but these files will get included anyway:

▶ host | directory ~/Projects/dagger --include 'sdk/python/**/.gitignore' | glob '**/.gitignore'
sdk/python/.gitignore
sdk/python/.idea/.gitignore
sdk/python/.pytest_cache/.gitignore
sdk/python/.ruff_cache/.gitignore
sdk/python/.venv/.gitignore
sdk/python/dev/.gitignore
sdk/python/dev/.idea/.gitignore
sdk/python/dev/sdk/runtime/.gitignore
sdk/python/runtime/.gitignore

However, I believe the impact from that is trivial so I'm not suggesting to actually do it, unless there's a measurable performance benefit. And would rather leave that logic to buildkit/filesync anyway since it's still going to be just two filesync calls (barring the cost of opening each .gitignore file but in my testing I found it to be trivial, although didn't measure on a slow computer).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll do that in a follow up since it's related to optimization

@TomChv
TomChv force-pushed the feat/apply-gitignore-pattern-on-module-loading branch from f623901 to 7d88dd2 Compare July 24, 2025 17:01
@TomChv
TomChv requested a review from helderco July 24, 2025 17:58
@shykes

shykes commented Jul 25, 2025

Copy link
Copy Markdown
Contributor

@TomChv @helderco shouldn't this be enabled by default? Are you worried about breaking changes? Pesonally I'm more worried about nobody ever taking advantage of this obscure flag

@TomChv

TomChv commented Jul 25, 2025

Copy link
Copy Markdown
Member Author

@TomChv @helderco shouldn't this be enabled by default? Are you worried about breaking changes? Pesonally I'm more worried about nobody ever taking advantage of this obscure flag

I talked about it we Justin and we wanted to take it slow in case some people were sending files that are ignored by their .gitignore.
But I enabled ignoreVCS on directory sent from the CLI, we may switch it back to false if it creates any problem for our users

@helderco

Copy link
Copy Markdown
Contributor

@TomChv @helderco shouldn't this be enabled by default? Are you worried about breaking changes? Pesonally I'm more worried about nobody ever taking advantage of this obscure flag

Yeah, I agree.

we may switch it back to false if it creates any problem for our users

@TomChv, changing a default is not an easy task. It's possible we break a few users that rely on getting ignored files somehow, but that's speculative and the benefit for most users outweighs that in my mind.

@TomChv

TomChv commented Jul 25, 2025 •

Copy link
Copy Markdown
Member Author

Yeah, I agree.

Alright, I changed it to true by default

EDIT: Actually it seems that it's breaking the SDK loading because it detects a recursive call.

I'll switch it back to false for now

failed to get configured module: failed to load local module source context: select: failed to load .gitignore patterns: failed to load directory ignore patterns: select: failed to load .gitignore patterns: failed to load directory ignore patterns: select: recursive call detected
https://dagger.cloud/dagger/traces/b69b5ea8798b89848c919d069bee82ed

Comment thread core/schema/util.go Resolved
@helderco

Copy link
Copy Markdown
Contributor

Alright, I changed it to true by default

Remember that you can't just make the default value for the option true in the API, it needs to be false because of the Go SDK, so you need to rename the option as well in order for that default to be false:

dagql.Arg("noGitAutoIgnore")

@TomChv TomChv added this to the v0.18.15 milestone Jul 25, 2025
Comment thread core/schema/util.go Resolved
@helderco
helderco requested a review from sipsma July 28, 2025 11:20
@TomChv

TomChv commented Jul 29, 2025 •

Copy link
Copy Markdown
Member Author

I did some tests and benchmarks on that feature, the big difference happens on repository that has a lot of generated artifacts, builds and files.

For example, here's a benchmark on a simple Next application:

0.18.4 Cold cache 10736 Cold cache 0.18.4 Cached 10737 Cached
Module loading 0.6s 0.1 0.1s 0.1s
Host directory 28.1s 0.1s 0.5s 0.0s

We can observe that module loading is also improved a bit thanks to these changes

I also did some tests on our dagger repo, on greetings-api and openmeter but since I just cloned these repository, there's not much difference in speed because everything has already been ignored during the clone.

@helderco

helderco commented Jul 30, 2025 •

Copy link
Copy Markdown
Contributor

[...] the big difference happens on repository that has a lot of generated artifacts, builds and files.

I think that's pretty obvious or easy to understand. I'm more interested in the overhead of the implementation.

You should also measure in ms at least, and isolate the .gitignore specific part. Baseline is it takes 0 ns without it, of course.

I also did some tests on our dagger repo, on greetings-api and openmeter but since I just cloned these repository, there's not much difference in speed because everything has already been ignored during the clone.

Again, this is the metric that I'm more interested in, i.e., a clean clone of a repo with lots of .gitignore files. This tells us the overhead of reading all those files. Basically looking for what's the impact in cases where it's not needed. But the openmeter repo only has 7 .gitignore files while our own dagger/dagger repo has 243!

❯ dagger -M -c 'git https://github.com/dagger/dagger | head | tree | glob "**/.gitignore"'

So measure against our repo, or another one that has even more .gitignore files.

For example, if processing each .gitignore file takes 5ms, then reading 200 of those in sequence can take a full 1s. That's not negligible, although we can decide that it's still worth it (even on clean clones since it's not easy to figure out in advance if you'll benefit or not). If you then optimize into reading in parallel instead of in sequence and it maybe drops to 100ms instead of 1s, then that's much easier to accept.

Another interesting metric is comparing the effect on cache before and after a npm install (for example) on a clean clone. I suspect that without this it invalidates cache, while with it it doesn't.

@helderco

Copy link
Copy Markdown
Contributor

FTR, this change LGTM, I just want to make sure the impact of this change is negligible before approving.

@TomChv
TomChv force-pushed the feat/apply-gitignore-pattern-on-module-loading branch from 8ebc88e to 90c2ecf Compare August 5, 2025 09:43

@helderco helderco left a comment

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.

There's several ways to further optimize this, please follow-up when you can.

For now it seems to me the overhead is not a deal breaker, but would still like another look by @sipsma or @jedevc on the engine/cache side in case there's any red flags.

Signed-off-by: Tom Chauveau <[email protected]>
- Apply .gitignore pattenrs when a directory is sent as an argument to a module function.
- Apply .gitignore patterns when a local module is loaded.

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

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.

This is a breaking change - calling Host.directory will now give different results.

On the fence as to whether we want to do this or not. I think we definitely can break for context dirs and module loading, but we might want to preserve the behavior with Host.directory.

We'd do this by having GitAutoIgnore instead, and just setting it when loading.

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.

Ah, I see above this was discussed and agreed on.

That's fine, but since this is a breaking change, we need to make it as such in the changelog.

No compat layer required IMO, because modules can't call the Host api anyways.

Comment thread core/schema/host.go Outdated
core.CopyFilter
HostDirCacheConfig

ContextDirectoryPath string `internal:"true" default:""`

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.

nit: call this GitIgnoreRoot or something. It's not directly tied to the idea of context directories.

Comment thread core/schema/host.go Outdated
},
},
)

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.

nit: blank line

// Load .gitignore patterns before loading the module so they can be used in the `Include` argument
// instead of Exclude so we get the right behaviour.
// We only include the .gitignore from the module source path and above up to the context directory.
ignorePatterns, err := loadDirectoryGitIgnorePatterns(ctx, src.Local.ContextDirectoryPath, filepath.Join(src.Local.ContextDirectoryPath, moduleSourcePath))

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.

The bit we'd wrap in Directory.__gitignoresFor is just core.LoadGitIgnoreInDirectory 🎉

The logic already looks good, it's just a matter of making it an internal schema method.

Comment thread core/schema/modulesource.go Outdated
}

for _, pattern := range rebasedIgnorePatterns {
if strings.HasPrefix(pattern, "!") {

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.

strings.CutPrefix

Args: []dagql.NamedInput{
{Name: "path", Value: dagql.String(src.Local.ContextDirectoryPath)},
{Name: "include", Value: dagql.ArrayInput[dagql.String](dagql.NewStringArray(fullIncludePaths...))},
{Name: "noGitAutoIgnore", Value: dagql.NewBoolean(true)}, // Disable .gitignore applying since it's done above.

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.

Mild confusion here. I was thinking that we wouldn't need any logic in loadSourceModuleContext, it could all be done in host.Directory. What's the reason it can't be done there?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because when we load a module, we extract the pattern from the exclude and include field of the dagger.json then we rebase and reformat them so they are all in the include.
But in Host.Directory, we only use the exclude field so having both led to unexpected behaviour and ignore.
So by getting the .gitignore and negate them so they are in the include, we get the correct behaviour.

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.

What do you mean by unexpected behavior? Why can't we just pass the include + exclude directly in, and also have the gitignore behavior, so the merging gets done there?

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.

It's because of #10736 (comment)

Module loading starts with including dagger.json and <source_dir>/** patterns. Then every pattern from dagger.json is added. If dagger.json has any excludes, they'll be negated and moved to the includes.

If module loading added .gitignore patterns to Host.directory's excludes they would always be applied after the patterns in dagger.json. This way .gitignore patterns are applied after <source_dir>/** but before user added patterns in dagger.json, allowing users to override something specifically, if needed.

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.

Thinking about this again, in the context of #10883.

I don't think it should be possible to alter the gitignore patterns in dagger.json. I'm struggling to imagine a legitimate use case.

One issue is how this would interact with local vs git sources.

  • If I have a .gitignored file foo.txt, but I override it in dagger.json to "include": ["foo.txt"], then it could be loaded.
  • But once I push this to a remote module, and load it from a git source, then foo.txt is no longer there.

I think it makes more sense to have a consistent behavior.

(I'm a bit biased, because the implementation in #10883 doesn't do any pattern conversion - it directly adds gitignore support to the file transfer protocol. There's no extra ordering to do, it has to be applied separately. If this is a required feature, I can think about other ways to do it, but I think it's just potentially confusing to be able to override the patterns in .gitignore)

Comment thread core/util.go Resolved
┆ dir: Host.directory(path: "/app/dagql/idtui"): Directory!
┆ contextFile: Directory.file(path: "main.go"): File!
┆ contextDir: Host.directory(path: "/app/dagql/idtui/viztest", exclude: [], noCache: true): Directory!
┆ contextDir: Host.directory(path: "/app/dagql/idtui/viztest", exclude: [], noCache: true, contextDirectoryPath: "/app/dagql/idtui"): Directory!

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.

Hm. I don't think the internal args should be appearing in telemetry.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I regenerate the tests and it's still displayed
Not a blocker but I'll ping @vito

Signed-off-by: Tom Chauveau <[email protected]>
@TomChv

TomChv commented Aug 6, 2025

Copy link
Copy Markdown
Member Author

@jedevc Comment applied, I wait for CI and I merge this PR

@TomChv
TomChv merged commit eb70a48 into dagger:main Aug 6, 2025
jedevc added a commit to jedevc/dagger that referenced this pull request Aug 18, 2025
jedevc added a commit to jedevc/dagger that referenced this pull request Aug 18, 2025
jedevc added a commit to jedevc/dagger that referenced this pull request Aug 18, 2025
jedevc pushed a commit to jedevc/dagger that referenced this pull request Aug 20, 2025
…er#10736)

Automatically apply .gitignore patterns if the directory is inside a git repository.
Automatically load .gitignore patterns on contextual arguments by setting the internal argument ContextDirectoryPath to Host.Directory.
Add NoAutoGitIgnore argument to Host.Directory to disable that behaviour.

Signed-off-by: Tom Chauveau <[email protected]>
jedevc pushed a commit to jedevc/dagger that referenced this pull request Aug 21, 2025
…er#10736)

Automatically apply .gitignore patterns if the directory is inside a git repository.
Automatically load .gitignore patterns on contextual arguments by setting the internal argument ContextDirectoryPath to Host.Directory.
Add NoAutoGitIgnore argument to Host.Directory to disable that behaviour.

Signed-off-by: Tom Chauveau <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants