Repository navigation
feat: apply git ignore pattern on contextual directory argument - #10736
Conversation
| // 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 { |
There was a problem hiding this comment.
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.
- Then it's reusable, external clients can use it.
- 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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Make sense! alright! I'll push my changes so far, and continue on this
There was a problem hiding this comment.
Note from a 1:1 with Justin
- We look for .git
- If not .git -> skip
- 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
There was a problem hiding this comment.
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.
365929e to
7b78242
Compare
This comment was marked as outdated.
This comment was marked as outdated.
helderco
left a comment
There was a problem hiding this comment.
Thanks for moving this forward! 🙏
| // 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| // (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 { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
I'll do that in a follow up since it's related to optimization
f623901 to
7d88dd2
Compare
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. |
Yeah, I agree.
@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. |
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
|
Remember that you can't just make the default value for the option dagql.Arg("noGitAutoIgnore") |
|
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:
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. |
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
Again, this is the metric that I'm more interested in, i.e., a clean clone of a repo with lots of So measure against our repo, or another one that has even more For example, if processing each Another interesting metric is comparing the effect on cache before and after a |
|
FTR, this change LGTM, I just want to make sure the impact of this change is negligible before approving. |
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
…he context directory instead of the whole context dir Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
8ebc88e to
90c2ecf
Compare
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| core.CopyFilter | ||
| HostDirCacheConfig | ||
|
|
||
| ContextDirectoryPath string `internal:"true" default:""` |
There was a problem hiding this comment.
nit: call this GitIgnoreRoot or something. It's not directly tied to the idea of context directories.
| }, | ||
| }, | ||
| ) | ||
|
|
| // 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)) |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| for _, pattern := range rebasedIgnorePatterns { | ||
| if strings.HasPrefix(pattern, "!") { |
| 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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 filefoo.txt, but I override it indagger.jsonto"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.txtis 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)
| ┆ 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! |
There was a problem hiding this comment.
Hm. I don't think the internal args should be appearing in telemetry.
There was a problem hiding this comment.
I regenerate the tests and it's still displayed
Not a blocker but I'll ping @vito
Signed-off-by: Tom Chauveau <[email protected]>
|
@jedevc Comment applied, I wait for CI and I merge this PR |
Signed-off-by: Tom Chauveau <[email protected]>
…nt (dagger#10736)" This reverts commit eb70a48.
…nt (dagger#10736)" This reverts commit eb70a48. Signed-off-by: Justin Chadwell <[email protected]>
…nt (dagger#10736)" This reverts commit eb70a48. Signed-off-by: Justin Chadwell <[email protected]>
…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]>
…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]>
Automatically apply .gitignore patterns if the directory is inside a git repository.
Automatically load .gitignore patterns on contextual arguments by setting the internal argument
ContextDirectoryPathtoHost.Directory.Add
NoAutoGitIgnoreargument toHost.Directoryto 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_modulesbut 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.