Skip to content

python: decouple SDK library components - #9538

Merged
helderco merged 19 commits into
dagger:mainfrom
helderco:python/decouple
Apr 21, 2025
Merged

helderco merged 19 commits into
dagger:mainfrom
helderco:python/decouple

Conversation

@helderco

@helderco helderco commented Feb 8, 2025 •

Copy link
Copy Markdown
Contributor

Summary

The initial goal with this was to reduce the size of what gets generated into the user’s host when developing Dagger modules in Python. That has been expanded to also simplify the development workflow and avoid the need for dagger develop in order to have IDE completions.

However, the simplified workflow part has been put on hold but the changes kept (turned off), in order to bring it at a later time with a bit more investment.

Additionally, uv and codegen + dependencies have been bundled in the engine. This prevents issues for some users behind proxies, mirrors, or unauthenticated to ghcr.io (uv).

External client bindings to avoid dagger develop

Why?

There’s a complaint in #8689 that dagger init generates too many files to the host.

The first iteration of this PR was to just exclude some parts of the client library that have no use in modules, like the automatic provisioning feature. This would allow generating less files to user’s host on dagger init. At the time it was already on my mind because of confusion over the supported engine version included in the client library.1

At the same time, some users complained that the client library was vendored instead of pulling from the published package. This was mostly on TypeScript, but I’ve seen it being asked generally too. I felt it was doable in Python, my problem was how do you run our integration tests, with changes to the client library that haven’t been published yet?

Then the discussion on #8689 led to complaints over the need to dagger develop in the first place. I started to think that if we generate the client bindings outside of the client library, we can depend on the published library, avoid the need to vendor and patch the whole client library, and recommend to commit those client bindings to git. This way dagger develop is no longer a necessary pre-requisite on a clean clone. You just need uv sync.

So this trades 50+ uncommitted files that need to be regenerated every time, with just one committed. It’s also aligned with support for calling module functions in custom clients (with dagger run), and it simplifies the user’s workflow.

How?

I wanted to generate the client bindings to the user’s src/dagger/gen/client.py, which would sit next to src/my_module/main.py. The problem is that the clean solution for doing that would required the dagger package to not have a __init__.py (implicit Python namespace package), but we need that to expose all the public API under the top level dagger import. So I decided on a top level src/dagger_gen.py module.

The client library will try to import dagger.Client from dagger_gen.py and fallback to the default dagger/client/gen.py if it doesn’t exist. So users continue to import from dagger as normal, and the client library no longer needs to be patched with the necessary client bindings for the current module.

In the Python SDK runtime module, we check for the engine’s version from the API to always enforce the same version as a dependency in pyproject.toml, for compatibility. On an engine upgrade, that version should be bumped automatically on dagger develop.

If it’s detected that it’s a custom build of the engine, an override is added to pyproject.toml to look for the library in the sdk subdir. This config is used to actually decide to vendor or not, and where. It doesn’t matter is there’s a dependencies = ["dagger-io==v0.19.0"], it’s just replaced like a replace instruction in go.mod. This way our test suite continues to work as normal because it will continue to vendor the library and pick up changes.

On hold ⏳

I started worrying about missing details on how to manage these versions, and the need to add more test cases for this, but this PR has been in draft for a long time, and used as base for an even longer PR in suspension (interfaces support).

In order to avoid blocking this even further I made a last minute decision to keep the vendoring in all cases for now, but making it easy to turn it off later. This way there’s no expected changes for users here.

My plan is to make another PR later, specifically for turning all of this on, in concert with the client generator work that @TomChv has done, for TypeScript and Go.

Bundled codegen and uv

Since this PR had a bunch of changes to the SDK, I reused it to experiment on bundling the codegen script. Python’s codegen is a subpackage of the client library, with only one dependency. It’s been installed during module load but since it’s a different pyproject.toml from the users’s it was a problem for some users that needed to set their mirrors more globally rather than just configuring them it their pyproject.toml. Now it’s bundled in the engine as a single executable file, so it’s no longer trying to install the codegen’s dependencies during module load.

Additionally, I also decided to include the uv binaries because they’re normally pulled from ghcr.io and it causes failures for people, either from a network failure, or from not having done docker login ghcr.io, which adds friction.

I also experimented with bundling a warmed uv cache but didn’t seem to make any noticeable effect so I decided to try it again at a later time.

Footnotes

  1. It’s a chicken and egg situation because that version is only bumped right after the engine is released, before publishing to the public package registry. However the client library has already been copied into the just released engine which is what gets copied to the host on dagger init. Thus, if you open that file, it always points to the previous engine version. ↩

@helderco helderco added this to the v0.16.0 milestone Feb 12, 2025
@sipsma sipsma modified the milestones: v0.16.0, v0.16.1 Feb 19, 2025
@helderco helderco removed this from the v0.16.2 milestone Feb 27, 2025
@github-actions

This comment was marked as resolved.

@helderco
helderco force-pushed the python/decouple branch 2 times, most recently from e502fb3 to 158494b Compare April 3, 2025 13:11
@helderco
helderco marked this pull request as ready for review April 15, 2025 14:14
@helderco
helderco requested a review from a team as a code owner April 15, 2025 14:14
@helderco

Copy link
Copy Markdown
Contributor Author

I'm going to let CI run all the tests to confirm nothing has changed. I'll fix any failures but the feature is frozen and ready for review.

@helderco helderco added this to the v0.18.4 milestone Apr 15, 2025
@helderco
helderco force-pushed the python/decouple branch 8 times, most recently from dfe28e8 to 3e8115a Compare April 20, 2025 23:06
@helderco
helderco requested review from TomChv and jedevc April 21, 2025 11:15

@sipsma sipsma 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.

One comment but not sure how significant it is, otherwise LGTM

Comment thread sdk/python/runtime/image.go Outdated
return err
}
if ref == "" {
i = &Image{}

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: I think this is a no-op since it's just setting i, which is a local var. Guessing the intention is *i = Image{}? That would actually change the value i is pointing to. Need to be careful of i == nil case though

Not sure if this is actually a significant code branch ever expected to be hit though

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.

Good catch, that line can be removed.

helderco added 19 commits April 21, 2025 18:03
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
@helderco
helderco merged commit da0eb7b into dagger:main Apr 21, 2025
@helderco
helderco deleted the python/decouple branch April 21, 2025 18:22
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.

3 participants