Skip to content

All SDKs upload all go files in the repo when loading a module #8496

Closed

Description

@helderco

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:

dagger/core/schema/sdk.go

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 ModuleSource arg and used it to determine very precisely what's needed (i.e. right now we require **/go.mod but 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

  1. Dagger Functions were officially released in v0.10.0. ↩

Activity

  1. helderco commented on Sep 19, 2024

    @helderco
    ContributorAuthor

    This is what I was talking about yesterday @sipsma, @jedevc, @vito, @TomChv.

  2. sipsma commented on Sep 19, 2024

    @sipsma
    Contributor

    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.

  3. helderco commented on Sep 19, 2024

    @helderco
    ContributorAuthor

    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 in sdk/php/vendor, and it's like 33MB. Even though there's no .go files 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.

  4. helderco commented on Sep 19, 2024

    @helderco
    ContributorAuthor

    This is an example of the files uploaded when I run dagger functions -m modules/wolfi:

    Screenshot 2024-09-19 at 15 56 59

    Most of what's in sdk is from sdk/php:

    Screenshot 2024-09-19 at 15 57 54
  5. sipsma commented on Sep 19, 2024

    @sipsma
    Contributor

    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?

    I had a memory that resolveFromCaller was 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 codegen and go build to 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.

  6. helderco commented on Sep 19, 2024

    @helderco
    ContributorAuthor

    So I still think something deeper is just wrong and we'll have to get to the bottom of it separate from this issue.

    Oh, of course! This is just one of several. I think the lack of excludes has a much bigger impact on performance for users.

    I had a memory that resolveFromCaller was where we saw the biggest overhead atm, but that could be outdated or I could be misremembering.

    Yes that's still true, and I have another issue related to that:

    But even if there are irrelevant files causing codegen and go build to 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.

    This I'm not sure. Where's what I did:

    See the effect on those two dagger functions calls:

    Screenshot 2024-09-19 at 16 50 22 Screenshot 2024-09-19 at 16 50 35

    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 RequiredPaths may trigger those things more often and with bigger impact than expected.

  7. sipsma commented on Sep 19, 2024

    @sipsma
    Contributor

    This I'm not sure. Where's what I did:

    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:

    • Go mod cache + go build cache should be re-used still and save the vast majority of the work even if a build needs to run again. Especially in the case where the only difference are files that don't even end up as input into the build.
    • If we loaded files in the context dir that weren't used in the go build, we should ideally get a bit-for-bit identical output even if those unneeded files haven't changed and no further cache invalidation should happen

    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.

  8. changed the title [-]Deprecate `RequiredPaths` in favor of user control[/-] [+]Go SDK uploads all go files in the repo when loading a module[/+] on Sep 19, 2024
  9. changed the title [-]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[/+] on Sep 19, 2024
  10. shykes commented on Sep 19, 2024

    @shykes
    Contributor

    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 :

    However, I think the problem is the potential conflict of multiple modules requiring different patterns from the repo. For example, our own repo is daggerized with "source": ".dagger" at the root
    [...]
    So if you have a sdk/go/dev module, for example, that needs the go.mod file at the repo root, it’ll have to load all of these dependencies: https://github.com/dagger/dagger/blob/7ea7254fea1886831d3e3f211875cb86232a5945/dagger.json

    IMO this is a contrived example. Here's a more realistic example:

    • Several of our modules import ./engine/distconsts to use shared constants across the engine code, and pipeline code.
    • If we stop uploading all go files repo-wide into the Go SDK, this will break
    • To fix it, all I need to do is dagger init ./engine/distconsts then 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.

  11. helderco commented on Sep 20, 2024

    @helderco
    ContributorAuthor

    The occasional inconvenience of doing that IMO, is nothing compared to everyone having to understand and manage yet another include/exclude system.

    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, just dagger config include ../engine/distconsts/ or similar, without the possible conflicts, or without bringing in more than you need.

  12. helderco commented on Sep 20, 2024

    @helderco
    ContributorAuthor

    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.

  13. helderco commented on Sep 20, 2024

    @helderco
    ContributorAuthor

    There's another reason to want dagger.json excludes to reach the context directory. It's because it allows overriding/unsetting an exclude added by a parent .gitignore (if needed) from:

  14. helderco commented on Sep 22, 2024

    @helderco
    ContributorAuthor

    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 RequiredPaths list, which should work today, and created a new issue to propose the other approach independently:

  15. helderco commented on Sep 23, 2024

    @helderco
    ContributorAuthor

    This is actually doable with include/excludes, assuming we remove RequiredPaths.

    But until then, I think there's an easy change we can make to allow users to override unwanted includes from RequiredPaths:

  16. freeformz commented on Nov 8, 2024

    @freeformz

    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.

  17. idlsoft commented on Nov 13, 2024

    @idlsoft
    Contributor

    Couldn't dagger.json just have a

    "disable-find-up": true

    setting to instruct dagger not to look for .git?

    Or maybe look for .git and .dagger-root.

  18. helderco commented on Mar 13, 2025

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions