Repository navigation
Description
Summary
Stop including all Go packages in a repo with every Go module within it by default, in favor of transferring that decision to the module author.
Motivation
The biggest reason for doing this is performance. It’s also confusing to folks that this happens in the first place:
The RequiredPaths feature was added in v0.9.9, when Dagger Modules were still experimental1. It allows SDKs to specify files from the repo that they may need in order to load a module.
Currently, only the Go SDK uses it:
Lines 485 to 498 in fa9d3c7
| func (sdk *goSDK) RequiredPaths(_ context.Context) ([]string, error) { | |
| return []string{ | |
| "**/go.mod", | |
| "**/go.sum", | |
| "**/go.work", | |
| "**/go.work.sum", | |
| // TODO: the below could be optimized by scoping only to go modules that actually | |
| // end up being needed for the dagger module. | |
| // including vendor/ is potentially expensive, but required | |
| "**/vendor/", | |
| // needed in order to re-use go.mod from any parent dir (otherwise it's an invalid go module) | |
| "**/*.go", | |
| }, nil | |
| } |
But the problem is that those glob patterns match against the whole git repo, not just parent directories, and they're always applied. Additionally, it includes the vendor/ directory which is also present for example in PHP projects, and can be quite big. If that directory exists in a given repo, every Go module will load it.
Besides, the Go SDK is used to bootstrap every other SDK, which means the PHP SDK module includes all those files from our repo, including test cases, in every user module’s initialize phase, for example.
Solution
Just remove support for the RequiredPaths field in SDKs, and make a recommendation for users on how best to integrate with code outside the module.
By giving the control to the user, we can default to not load anything extra from outside the root directory (in all SDKs), while still allowing the use case of “importing code” from parent directories, but only on the modules that need it, and only the files that are actually needed.
It’s also more self-documenting and easier to detect if a module needs files outside of its root.
Rationale
Historical context
This issue was known from the start, although it was initially about go.mod but then had to include more files. It was necessary in order for the Go SDK to edit an existing parent dir’s go.mod on dagger init, but that no longer happens (by default) since:
In the original PR’s description it says:
This is currently a bit barebones in that it's just a flat list of glob patterns. Better would be if this method was given the
ModuleSourcearg and used it to determine very precisely what's needed (i.e. right now we require**/go.modbut really we only needs ones that are in parent dirs or are referenced by those in parent dirs). But saving that optimization for the future.
- Originally posted @sipsma in #6575 (emphasis mine)
I think it’s time to optimize this, although the initial intent may have been to limit the patterns to parent directories, I think we should just give that control to the user.
Need for control
There’s a reason I didn’t want to add it to the Python SDK early on:
To be useful, it'd have to be configurable by the modules.
- Originally posted by @helderco in #6575 (comment)
Go has standardized tooling but Python and TypeScript have multiple package managers, runtimes, lock files, and configuration files. The SDK can’t know in advance what the user will need, without having to load all the things. And we can't analyze the source directory to look for clues because this needs to happen very early on, in order to figure out the patterns needed for loading the sources in the first place.
Besides, even in Go modules, in most cases users don’t want all of those files, and those that do need some, only they know which ones.
Compatibility
This would be a breaking change for any Go module that’s currently depending on Go files outside of its root directory, but users could mitigate it early by being explicit with the patterns they need today, even with the implicit ones from the Go SDK because they’ll just be deduped.
There’s two ways to do this:
- Use a module dependency to declare the files needed
- Use the
dagger.json’s include patterns for more fine-grained control
Example
In a repo that looks like this:
.
├── .git
├── cmd
│ └── main.go
├── core
│ ├── backend.go
│ ├── consts.go
│ └── services.go
├── my-module
│ └── dagger.json
│ └── main.go
├── go.mod
└── go.sum
A: module dependency
Essentially, you’d add a dagger.json, even without any sources (no “sdk” defined), in the desired directory.
You’d dagger init in the repo root to create a dagger.json file next to .git and add the includes there:
{
"include": [
"go.mod",
"go.sum",
"cmd/*.go"
]
}And then dagger install on the module:
{
"name": "my-module",
"sdk": "go",
"source": ".",
"dependencies": [
{
"name": "parent",
"source": ".."
}
]
}This should add the dependency’s include patterns (and files), along with the module’s.
B: dagger.json include patterns
I think that two different modules could easily require different files from the same directory in the repo. Using dagger.json include patterns is simpler than using dependencies, provides more fine-grained control, and is easier to manage and audit:
{
"name": "my-module",
"sdk": "go",
"source": ".",
"include": [
"../go.mod",
"../go.sum",
"../cmd/*.go"
]
}Footnotes
-
Dagger Functions were officially released in v0.10.0. ↩
Activity
helderco commentedon Sep 19, 2024
This is what I was talking about yesterday @sipsma, @jedevc, @vito, @TomChv.
sipsma commentedon Sep 19, 2024
Yeah while I still think it would be possible for the Go SDK to automatically determine which, if any, other local packages/modules are needed based on imports/dependencies, the complexity of doing so is still today a bit overwhelming to the point it doesn't feel worth it right now. And your point about this being even more complicated in other SDKs with less standardized tooling is well taken.
So I think I'm good with changing this to be a user-facing problem for now since it does obviously hurt performance in the default case.
My only slight (non-blocking) hangup is that it just doesn't make sense the loading of these files adds so much overhead. It really shouldn't be adding enormous amounts of time (> a handful of seconds tops) to load source code files from one directory to another, especially when it's all on a local machine. Maybe if there were GBs of source code in a massive monorepo, but we are seeing this slowness on much much smaller repos.
In an ideal world, we wouldn't need to think about these sorts of tradeoffs in terms of making something a user problem for the sake of improving performance. However, this is pending our larger investigations into general performance bottlenecks though, so I don't want to block improvements based on that.
helderco commentedon Sep 19, 2024
No, loading all the go files in our repo for example isn't that slow to upload on its own, but it does invalidate cache more often if you edit one of those files, right?
Besides, one of those patterns in the Go SDK is
vendor/**, which also exists in other languages like PHP. For example, I have it insdk/php/vendor, and it's like 33MB. Even though there's no.gofiles in there, it's still including everything in there because of the**.The overhead just seems unnecessary for most use cases that don't even need it.
helderco commentedon Sep 19, 2024
This is an example of the files uploaded when I run
dagger functions -m modules/wolfi:Most of what's in
sdkis fromsdk/php:sipsma commentedon Sep 19, 2024
I had a memory that
resolveFromCallerwas where we saw the biggest overhead atm, but that could be outdated or I could be misremembering.But even if there are irrelevant files causing
codegenandgo buildto run again, those should be extremely quick in theory (due to cache mounts) and produce the same output causing the any subsequent executions to still be cached.So I still think something deeper is just wrong and we'll have to get to the bottom of it separate from this issue.
To be clear, none of this invalidates what you're saying, I'm on board with this change since I agree the challenge of trying to automatically optimize which files across the entire repo get loaded on behalf of users is not tenable for us now.
helderco commentedon Sep 19, 2024
Oh, of course! This is just one of several. I think the lack of excludes has a much bigger impact on performance for users.
Yes that's still true, and I have another issue related to that:
.gitignoreby default when loading modules #6627.gitignorepatterns on module load by default #8501This I'm not sure. Where's what I did:
sdk/php/vendor, as a throwback to this user: Use.gitignoreby default when loading modules #6627 (comment)dagger functions -m modules/wolfisdk/php/runtime/main.godagger functions -m modules/wolfia second timeSee the effect on those two
dagger functionscalls:It makes sense since your context directory has changes now, and this will invalidate the cache of any node in the DAG that depends on it, which can have a compounding effect.
Of course there's certainly issues even in the runtime modules, doing things that may be innefficient, I'm just saying that
RequiredPathsmay trigger those things more often and with bigger impact than expected.sipsma commentedon Sep 19, 2024
Oh right, sorry I wasn't clear, I meant to say that in theory that cascading invalidation shouldn't actually slow us down that much (even though it does today), for a few reasons:
Reality today completely diverges from all of that (for a number of reasons, some known, some unknown), but that's part of what we need to look at separately. In the meantime, the fix here to make these files user controlled + specified should be a good mitigation.
[-]Deprecate `RequiredPaths` in favor of user control[/-][+]Go SDK uploads all go files in the repo when loading a module[/+][-]Go SDK uploads all go files in the repo when loading a module[/-][+]All SDKs upload all go files in the repo when loading a module[/+]shykes commentedon Sep 19, 2024
Just to re-iterate my proposal: we should use Dagger dependencies for this. We have enough complicated ways to customize the project layout already. We don't need to add another dimension. Not to mention the risk of breaking an existing field by interpreting the patterns differently. Not worth the potential headache of supporting and debugging the breakage.
To address your objection @helderco :
IMO this is a contrived example. Here's a more realistic example:
./engine/distconststo use shared constants across the engine code, and pipeline code.dagger init ./engine/distconststhen in each module using it:dagger install ../engine/distconsts. Done.To generalize: this is about a Go/Python/Typescript module importing Go/Python/Typescript files from outside its source. In theory it's possible that those files happen to live in a module with tons of dependencies. I think in practice that's either not the case, or easy to fix by moving the file to a subdirectory (which is probably the cleaner layout anyway). The occasional inconvenience of doing that IMO, is nothing compared to everyone having to understand and manage yet another include/exclude system.
helderco commentedon Sep 20, 2024
It's not another include/exclude system. It's the same that's already there. The difference is today it's limited to the root directory and what I'm proposing is to allow it to match the context directory, with an absolute path.
So in your use case, and with
dagger config, you'd also be able to fix with one command, justdagger config include ../engine/distconsts/or similar, without the possible conflicts, or without bringing in more than you need.helderco commentedon Sep 20, 2024
In any case, I'm in favor of moving forward with disabling RequiredPaths and recommending using module dependencies to support that use case, until we get feedback that it's not enough and more control is needed. I do think that the patterns in dagger.json give a better control, and it's not an entirely new thing, but I'm more interested in preventing loading all the Go files (and
vendor) by default.helderco commentedon Sep 20, 2024
There's another reason to want
dagger.jsonexcludes to reach the context directory. It's because it allows overriding/unsetting an exclude added by a parent.gitignore(if needed) from:.gitignorepatterns on module load by default #8501helderco commentedon Sep 22, 2024
I've split the dependencies vs contextual excludes approaches. I updated this issue's description to suggest using dependencies as a mitigation for removing the
RequiredPathslist, which should work today, and created a new issue to propose the other approach independently:dagger.jsoninclude/exclude patterns to match context directory #8531helderco commentedon Sep 23, 2024
This is actually doable with
include/excludes, assuming we removeRequiredPaths.But until then, I think there's an easy change we can make to allow users to override unwanted includes from
RequiredPaths:freeformz commentedon Nov 8, 2024
FWIW: if the dagger cli could help out,
go list(or a vendored equivalent) could determine all of the go dependencies for a module, right down to embed files. I do something like this to determine if a set of changes requires a rebuild/deployment in Jenkins. It would need to be done at the host level though to avoid the uploads.idlsoft commentedon Nov 13, 2024
Couldn't
dagger.jsonjust have asetting to instruct dagger not to look for
.git?Or maybe look for
.gitand.dagger-root.Use .dagger-root to improve module load performance on large monorepos (
feat: use .dagger-root to improve module load performance on large mo…
helderco commentedon Mar 13, 2025
This has been resolved in: