status
stringclasses
1 value
repo_name
stringclasses
31 values
repo_url
stringclasses
31 values
issue_id
int64
1
104k
title
stringlengths
4
233
body
stringlengths
0
186k
issue_url
stringlengths
38
56
pull_url
stringlengths
37
54
before_fix_sha
stringlengths
40
40
after_fix_sha
stringlengths
40
40
report_datetime
timestamp[us, tz=UTC]
language
stringclasses
5 values
commit_datetime
timestamp[us, tz=UTC]
updated_file
stringlengths
7
188
chunk_content
stringlengths
1
1.03M
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/container_test.go
c, ctx := connect(t) defer c.Close() ctx, cancel := context.WithCancel(ctx) defer cancel() dockerd := c.Container().From("docker:23.0.1-dind"). WithMountedCache("/var/lib/docker", c.CacheVolume("docker-lib"), dagger.ContainerWithMountedCacheOpts{
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/container_test.go
Sharing: dagger.Private, }). WithMountedCache("/tmp", c.CacheVolume("share-tmp")). WithExposedPort(2375). WithExec([]string{"dockerd", "--host=tcp://0.0.0.0:2375", "--tls=false", }, dagger.ContainerWithExecOpts{ InsecureRootCapabilities: true, }) dockerHost, err := dockerd.Endpoint(ctx, dagger.ContainerEndpointOpts{ Scheme: "tcp", }) require.NoError(t, err) randID := identity.NewID() out, err := c.Container().From("docker:23.0.1-cli"). WithMountedCache("/tmp", c.CacheVolume("share-tmp")). WithServiceBinding("docker", dockerd). WithEnvVariable("DOCKER_HOST", dockerHost). WithExec([]string{"sh", "-e", "-c", strings.Join([]string{ fmt.Sprintf("echo %s-from-outside > /tmp/from-outside", randID), "docker run --rm -v /tmp:/tmp alpine cat /tmp/from-outside", fmt.Sprintf("docker run --rm -v /tmp:/tmp alpine sh -c 'echo %s-from-inside > /tmp/from-inside'", randID), "cat /tmp/from-inside", }, "\n")}). Stdout(ctx) require.NoError(t, err) require.Equal(t, fmt.Sprintf("%s-from-outside\n%s-from-inside\n", randID, randID), out) }
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
package core import ( "context" _ "embed" "fmt" "os" "path/filepath" "strconv" "testing" "time" "dagger.io/dagger" "github.com/dagger/dagger/internal/engine" "github.com/dagger/dagger/internal/testutil" "github.com/moby/buildkit/identity" "github.com/stretchr/testify/require" "golang.org/x/sync/errgroup" ) func TestServiceHostnamesAreStable(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() www := c.Directory().WithNewFile("index.html", "Hello, world!") srv := c.Container(). From("python"). WithMountedDirectory("/srv/www", www). WithWorkdir("/srv/www"). WithExec([]string{"echo", "first"}). WithExec([]string{"echo", "second"}). WithExec([]string{"echo", "third"}). WithEnvVariable("FOO", "123"). WithEnvVariable("BAR", "456"). WithExposedPort(8000). WithExec([]string{"python", "-m", "http.server"}) hosts := map[string]int{} for i := 0; i < 10; i++ { hostname, err := srv.Hostname(ctx) require.NoError(t, err) hosts[hostname]++ } require.Len(t, hosts, 1) } func TestContainerHostnameEndpoint(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() t.Run("hostname is independent of exposed ports", func(t *testing.T) { a, err := c.Container(). From("python"). WithExposedPort(8000). WithExec([]string{"python", "-m", "http.server"}). Hostname(ctx) require.NoError(t, err) b, err := c.Container(). From("python"). WithExec([]string{"python", "-m", "http.server"}). Hostname(ctx) require.NoError(t, err) require.Equal(t, a, b) }) t.Run("hostname is same as endpoint", func(t *testing.T) { srv := c.Container().
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
From("python"). WithExposedPort(8000). WithExec([]string{"python", "-m", "http.server"}) hn, err := srv.Hostname(ctx) require.NoError(t, err) ep, err := srv.Endpoint(ctx) require.NoError(t, err) require.Equal(t, hn+":8000", ep) }) t.Run("endpoint can specify arbitrary port", func(t *testing.T) { srv := c.Container(). From("python"). WithExec([]string{"python", "-m", "http.server"}) hn, err := srv.Hostname(ctx) require.NoError(t, err) ep, err := srv.Endpoint(ctx, dagger.ContainerEndpointOpts{ Port: 1234, }) require.NoError(t, err) require.Equal(t, hn+":1234", ep) }) t.Run("endpoint with no port errors if no exposed port", func(t *testing.T) { srv := c.Container(). From("python"). WithExec([]string{"python", "-m", "http.server"}) _, err := srv.Endpoint(ctx) require.Error(t, err) }) } func TestContainerPortLifecycle(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() withPorts := c.Container(). From("python"). WithExposedPort(8000, dagger.ContainerWithExposedPortOpts{ Description: "eight thousand tcp", }). WithExposedPort(8000, dagger.ContainerWithExposedPortOpts{ Protocol: dagger.Udp, Description: "eight thousand udp", }). WithExposedPort(5432) cid, err := withPorts.ID(ctx) require.NoError(t, err) res := struct { Container struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
ExposedPorts []struct { Port int Protocol dagger.NetworkProtocol Description *string } } }{} getPorts := `query Test($id: ContainerID!) { container(id: $id) { exposedPorts { port protocol description } } }` err = testutil.Query(getPorts, &res, &testutil.QueryOptions{ Variables: map[string]interface{}{ "id": cid, }, }) require.NoError(t, err) require.Len(t, res.Container.ExposedPorts, 3) require.Equal(t, 8000, res.Container.ExposedPorts[0].Port) require.Equal(t, dagger.Tcp, res.Container.ExposedPorts[0].Protocol) require.Equal(t, "eight thousand tcp", *res.Container.ExposedPorts[0].Description) require.Equal(t, 8000, res.Container.ExposedPorts[1].Port) require.Equal(t, dagger.Udp, res.Container.ExposedPorts[1].Protocol) require.Equal(t, "eight thousand udp", *res.Container.ExposedPorts[1].Description) require.Equal(t, 5432, res.Container.ExposedPorts[2].Port)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
require.Equal(t, dagger.Tcp, res.Container.ExposedPorts[2].Protocol) require.Nil(t, res.Container.ExposedPorts[2].Description) withoutTCP := withPorts.WithoutExposedPort(8000) cid, err = withoutTCP.ID(ctx) require.NoError(t, err) err = testutil.Query(getPorts, &res, &testutil.QueryOptions{ Variables: map[string]interface{}{ "id": cid, }, }) require.NoError(t, err) require.Len(t, res.Container.ExposedPorts, 2) require.Equal(t, 8000, res.Container.ExposedPorts[0].Port) require.Equal(t, dagger.Udp, res.Container.ExposedPorts[0].Protocol) require.Equal(t, "eight thousand udp", *res.Container.ExposedPorts[0].Description) require.Equal(t, 5432, res.Container.ExposedPorts[1].Port) require.Equal(t, dagger.Tcp, res.Container.ExposedPorts[1].Protocol) require.Nil(t, res.Container.ExposedPorts[1].Description) withoutUDP := withPorts.WithoutExposedPort(8000, dagger.ContainerWithoutExposedPortOpts{ Protocol: dagger.Udp, }) cid, err = withoutUDP.ID(ctx) require.NoError(t, err) err = testutil.Query(getPorts, &res, &testutil.QueryOptions{ Variables: map[string]interface{}{ "id": cid, }, }) require.NoError(t, err) require.Len(t, res.Container.ExposedPorts, 2)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
require.Equal(t, 8000, res.Container.ExposedPorts[0].Port) require.Equal(t, dagger.Tcp, res.Container.ExposedPorts[0].Protocol) require.Equal(t, "eight thousand tcp", *res.Container.ExposedPorts[0].Description) require.Equal(t, 5432, res.Container.ExposedPorts[1].Port) require.Equal(t, dagger.Tcp, res.Container.ExposedPorts[1].Protocol) require.Nil(t, res.Container.ExposedPorts[1].Description) } func TestContainerExecServices(t *testing.T) { checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() srv, url := httpService(ctx, t, c, "Hello, world!") hostname, err := srv.Hostname(ctx) require.NoError(t, err) client := c.Container(). From("alpine:3.16.2"). WithServiceBinding("www", srv). WithExec([]string{"apk", "add", "curl"}). WithExec([]string{"curl", "-v", url}) code, err := client.ExitCode(ctx) require.NoError(t, err) require.Equal(t, 0, code) stdout, err := client.Stdout(ctx) require.NoError(t, err) require.Equal(t, "Hello, world!", stdout) stderr, err := client.Stderr(ctx) require.NoError(t, err) require.Contains(t, stderr, "Host: "+hostname+":8000") } func TestContainerExecServicesError(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() srv := c.Container(). From("alpine:3.16.2"). WithExposedPort(8080). WithExec([]string{"sh", "-c", "echo nope; exit 42"}) host, err := srv.Hostname(ctx) require.NoError(t, err) client := c.Container(). From("alpine:3.16.2"). WithServiceBinding("www", srv). WithExec([]string{"wget", "http:www:8080"}) _, err = client.ExitCode(ctx) require.Error(t, err) require.Contains(t, err.Error(), "start "+host+" (aliased as www): exited:") } var udpSrc string func TestContainerExecUDPServices(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() srv := c.Container(). From("golang:1.18.2-alpine"). WithMountedFile("/src/main.go", c.Directory().WithNewFile("main.go", udpSrc).File("main.go")). WithExposedPort(4321, dagger.ContainerWithExposedPortOpts{ Protocol: dagger.Udp, }). WithExec([]string{"go", "run", "/src/main.go"}) client := c.Container(). From("alpine:3.16.2"). WithExec([]string{"apk", "add", "socat"}). WithServiceBinding("echo", srv). WithExec([]string{"socat", "-", "udp:echo:4321"}, dagger.ContainerWithExecOpts{ Stdin: "Hello, world!", }) code, err := client.ExitCode(ctx) require.NoError(t, err) require.Equal(t, 0, code) stdout, err := client.Stdout(ctx) require.NoError(t, err) require.Equal(t, "Hello, world!", stdout) } func TestContainerExecServiceAlias(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() srv, _ := httpService(ctx, t, c, "Hello, world!") client := c.Container(). From("alpine:3.16.2"). WithServiceBinding("hello", srv). WithExec([]string{"apk", "add", "curl"}). WithExec([]string{"curl", "-v", "http:hello:8000"}) code, err := client.ExitCode(ctx) require.NoError(t, err) require.Equal(t, 0, code) stdout, err := client.Stdout(ctx) require.NoError(t, err) require.Equal(t, "Hello, world!", stdout) stderr, err := client.Stderr(ctx) require.NoError(t, err) require.Contains(t, stderr, "Host: hello:8000") } var pipeSrc string func TestContainerExecServicesDeduping(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() srv := c.Container(). From("golang:1.18.2-alpine"). WithMountedFile("/src/main.go", c.Directory().WithNewFile("main.go", pipeSrc).File("main.go")). WithExposedPort(8080). WithExec([]string{"go", "run", "/src/main.go"}) client := c.Container(). From("alpine:3.16.2"). WithExec([]string{"apk", "add", "curl"}). WithServiceBinding("www", srv). WithEnvVariable("CACHEBUST", identity.NewID()) eg := new(errgroup.Group) eg.Go(func() error { _, err := client. WithExec([]string{"curl", "-s", "-X", "POST", "http:www:8080/write", "-d", "hello"}). ExitCode(ctx) return err }) msg, err := client. WithExec([]string{"curl", "-s", "http:www:8080/read"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "hello", msg) require.NoError(t, eg.Wait()) } func TestContainerExecServicesChained(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() srv, _ := httpService(ctx, t, c, "0\n") for i := 1; i < 10; i++ { httpURL, err := srv.Endpoint(ctx, dagger.ContainerEndpointOpts{ Scheme: "http", }) require.NoError(t, err) srv = c.Container(). From("python"). WithFile( "/srv/www/index.html", c.HTTP(httpURL, dagger.HTTPOpts{ ExperimentalServiceHost: srv, }), ). WithExec([]string{"sh", "-c", "echo $0 >> /srv/www/index.html", strconv.Itoa(i)}). WithWorkdir("/srv/www"). WithExposedPort(8000). WithExec([]string{"python", "-m", "http.server"}) } fileContent, err := c.Container(). From("alpine:3.16.2").
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
WithServiceBinding("www", srv). WithExec([]string{"wget", "http:www:8000"}). WithExec([]string{"cat", "index.html"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n", fileContent) } func TestContainerBuildService(t *testing.T) { checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() t.Run("building with service dependency", func(t *testing.T) { content := identity.NewID() srv, httpURL := httpService(ctx, t, c, content) src := c.Directory(). WithNewFile("Dockerfile", `FROM alpine:3.16.2 WORKDIR /src RUN wget `+httpURL+` CMD cat index.html `) fileContent, err := c.Container(). WithServiceBinding("www", srv). Build(src). WithExec(nil). Stdout(ctx) require.NoError(t, err) require.Equal(t, content, fileContent) }) t.Run("building a directory that depends on a service (Container.Build)", func(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
content := identity.NewID() srv, httpURL := httpService(ctx, t, c, content) src := c.Directory(). WithNewFile("Dockerfile", `FROM alpine:3.16.2 WORKDIR /src RUN wget `+httpURL+` CMD cat index.html `) gitDaemon, repoURL := gitService(ctx, t, c, src) gitDir := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitDaemon}). Branch("main"). Tree() fileContent, err := c.Container(). WithServiceBinding("www", srv). Build(gitDir). WithExec(nil). Stdout(ctx) require.NoError(t, err) require.Equal(t, content, fileContent) }) t.Run("building a directory that depends on a service (Directory.DockerBuild)", func(t *testing.T) { content := identity.NewID() srv, httpURL := httpService(ctx, t, c, content) src := c.Directory(). WithNewFile("Dockerfile", `FROM alpine:3.16.2 WORKDIR /src RUN wget `+httpURL+` CMD cat index.html
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
`) gitDaemon, repoURL := gitService(ctx, t, c, src) gitDir := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitDaemon}). Branch("main"). Tree() fileContent, err := gitDir. DockerBuild(). WithServiceBinding("www", srv). WithExec(nil). Stdout(ctx) require.NoError(t, err) require.Equal(t, content, fileContent) }) } func TestContainerExportServices(t *testing.T) { checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() srv, httpURL := httpService(ctx, t, c, content) client := c.Container(). From("alpine:3.16.2"). WithServiceBinding("www", srv). WithExec([]string{"wget", httpURL}) filePath := filepath.Join(t.TempDir(), "image.tar") ok, err := client.Export(ctx, filePath) require.NoError(t, err) require.True(t, ok) } func TestContainerMultiPlatformExportServices(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() variants := make([]*dagger.Container, 0, len(platformToUname)) for platform := range platformToUname { srv, url := httpService(ctx, t, c, string(platform)) ctr := c.Container(dagger.ContainerOpts{Platform: platform}). From("alpine:3.16.2"). WithServiceBinding("www", srv). WithExec([]string{"wget", url}). WithExec([]string{"uname", "-m"}) variants = append(variants, ctr) } dest := filepath.Join(t.TempDir(), "image.tar") ok, err := c.Container().Export(ctx, dest, dagger.ContainerExportOpts{ PlatformVariants: variants, }) require.NoError(t, err) require.True(t, ok) } func TestServicesContainerPublish(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() srv, url := httpService(ctx, t, c, content) testRef := registryRef("services-container-publish") pushedRef, err := c.Container(). From("alpine:3.16.2"). WithServiceBinding("www", srv). WithExec([]string{"wget", url}). Publish(ctx, testRef) require.NoError(t, err) require.NotEqual(t, testRef, pushedRef) require.Contains(t, pushedRef, "@sha256:") fileContent, err := c.Container(). From(pushedRef).Rootfs().File("/index.html").Contents(ctx) require.NoError(t, err) require.Equal(t, fileContent, content) } func TestContainerRootFSServices(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() srv, url := httpService(ctx, t, c, content) fileContent, err := c.Container(). From("alpine:3.16.2"). WithServiceBinding("www", srv). WithWorkdir("/sub/out"). WithExec([]string{"wget", url}). Rootfs(). File("/sub/out/index.html"). Contents(ctx) require.NoError(t, err) require.Equal(t, content, fileContent) } func TestContainerWithRootFSServices(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() srv, url := httpService(ctx, t, c, content) gitDaemon, repoURL := gitService(ctx, t, c, c.Container(). From("alpine:3.16.2"). WithServiceBinding("www", srv). WithWorkdir("/sub/out"). WithExec([]string{"wget", url}). Rootfs()) gitDir := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitDaemon}). Branch("main"). Tree() fileContent, err := c.Container(). WithRootfs(gitDir). WithExec([]string{"cat", "/sub/out/index.html"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, content, fileContent) } func TestContainerDirectoryServices(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() srv, url := httpService(ctx, t, c, content) wget := c.Container(). From("alpine:3.16.2"). WithServiceBinding("www", srv). WithWorkdir("/sub/out"). WithExec([]string{"wget", url}) t.Run("runs services for Container.Directory.Entries", func(t *testing.T) { entries, err := wget.Directory(".").Entries(ctx) require.NoError(t, err) require.Equal(t, []string{"index.html"}, entries) }) t.Run("runs services for Container.Directory.Directory.Entries", func(t *testing.T) { entries, err := wget.Directory("/sub").Directory("out").Entries(ctx) require.NoError(t, err) require.Equal(t, []string{"index.html"}, entries) }) t.Run("runs services for Container.Directory.File.Contents", func(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
fileContent, err := wget.Directory(".").File("index.html").Contents(ctx) require.NoError(t, err) require.Equal(t, content, fileContent) }) t.Run("runs services for Container.Directory.Export", func(t *testing.T) { dest := t.TempDir() ok, err := wget.Directory(".").Export(ctx, dest) require.NoError(t, err) require.True(t, ok) fileContent, err := os.ReadFile(filepath.Join(dest, "index.html")) require.NoError(t, err) require.Equal(t, content, string(fileContent)) }) } func TestContainerFileServices(t *testing.T) { checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() srv, url := httpService(ctx, t, c, content) client := c.Container(). From("alpine:3.16.2"). WithServiceBinding("www", srv). WithWorkdir("/out"). WithExec([]string{"wget", url}) fileContent, err := client.File("index.html").Contents(ctx) require.NoError(t, err) require.Equal(t, content, fileContent) } func TestContainerWithServiceFileDirectory(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() response := identity.NewID() srv, httpURL := httpService(ctx, t, c, response) httpFile := c.HTTP(httpURL, dagger.HTTPOpts{ ExperimentalServiceHost: srv, }) gitDaemon, repoURL := gitService(ctx, t, c, c.Directory().WithNewFile("README.md", response)) gitDir := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitDaemon}). Branch("main"). Tree() t.Run("mounting", func(t *testing.T) { useBoth := c.Container(). From("alpine:3.16.2"). WithMountedDirectory("/mnt/repo", gitDir). WithMountedFile("/mnt/index.html", httpFile) httpContent, err := useBoth.File("/mnt/index.html").Contents(ctx) require.NoError(t, err) require.Equal(t, response, httpContent) gitContent, err := useBoth.File("/mnt/repo/README.md").Contents(ctx)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
require.NoError(t, err) require.Equal(t, response, gitContent) }) t.Run("copying", func(t *testing.T) { useBoth := c.Container(). From("alpine:3.16.2"). WithDirectory("/mnt/repo", gitDir). WithFile("/mnt/index.html", httpFile) httpContent, err := useBoth.File("/mnt/index.html").Contents(ctx) require.NoError(t, err) require.Equal(t, response, httpContent) gitContent, err := useBoth.File("/mnt/repo/README.md").Contents(ctx) require.NoError(t, err) require.Equal(t, response, gitContent) }) } func TestDirectoryServiceEntries(t *testing.T) { checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() gitDaemon, repoURL := gitService(ctx, t, c, c.Directory().WithNewFile("README.md", content)) entries, err := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitDaemon}). Branch("main"). Tree(). Entries(ctx) require.NoError(t, err) require.Equal(t, []string{"README.md"}, entries) } func TestDirectoryServiceTimestamp(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() gitDaemon, repoURL := gitService(ctx, t, c, c.Directory().WithNewFile("README.md", content)) ts := time.Date(1991, 6, 3, 0, 0, 0, 0, time.UTC) stamped := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitDaemon}). Branch("main"). Tree(). WithTimestamps(int(ts.Unix())) stdout, err := c.Container().From("alpine:3.16.2"). WithDirectory("/repo", stamped). WithExec([]string{"stat", "/repo/README.md"}). Stdout(ctx) require.NoError(t, err) require.Contains(t, stdout, "1991-06-03") } func TestDirectoryWithDirectoryFileServices(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() gitSrv, repoURL := gitService(ctx, t, c, c.Directory().WithNewFile("README.md", content)) httpSrv, httpURL := httpService(ctx, t, c, content) useBoth := c.Directory(). WithDirectory("/repo", c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitSrv}).Branch("main").Tree()). WithFile("/index.html", c.HTTP(httpURL, dagger.HTTPOpts{ExperimentalServiceHost: httpSrv})) entries, err := useBoth.Directory("/repo").Entries(ctx) require.NoError(t, err) require.Equal(t, []string{"README.md"}, entries) fileContent, err := useBoth.File("/index.html").Contents(ctx) require.NoError(t, err) require.Equal(t, content, fileContent) } func TestDirectoryServiceExport(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() gitDaemon, repoURL := gitService(ctx, t, c, c.Directory().WithNewFile("README.md", content)) dest := t.TempDir() ok, err := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitDaemon}). Branch("main"). Tree(). Export(ctx, dest) require.NoError(t, err) require.True(t, ok) exportedContent, err := os.ReadFile(filepath.Join(dest, "README.md")) require.NoError(t, err) require.Equal(t, content, string(exportedContent)) } func TestFileServiceContents(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() gitDaemon, repoURL := gitService(ctx, t, c, c.Directory().WithNewFile("README.md", content)) fileContent, err := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitDaemon}). Branch("main"). Tree(). File("README.md"). Contents(ctx) require.NoError(t, err) require.Equal(t, content, fileContent) } func TestFileServiceExport(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() gitDaemon, repoURL := gitService(ctx, t, c, c.Directory().WithNewFile("README.md", content)) dest := t.TempDir() filePath := filepath.Join(dest, "README.md") ok, err := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: gitDaemon}). Branch("main"). Tree(). File("README.md"). Export(ctx, filePath) require.NoError(t, err) require.True(t, ok) exportedContent, err := os.ReadFile(filePath) require.NoError(t, err) require.Equal(t, content, string(exportedContent)) } func TestFileServiceTimestamp(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() httpSrv, httpURL := httpService(ctx, t, c, content) ts := time.Date(1991, 6, 3, 0, 0, 0, 0, time.UTC) stamped := c.HTTP(httpURL, dagger.HTTPOpts{ExperimentalServiceHost: httpSrv}). WithTimestamps(int(ts.Unix())) stdout, err := c.Container().From("alpine:3.16.2"). WithFile("/index.html", stamped). WithExec([]string{"stat", "/index.html"}). Stdout(ctx) require.NoError(t, err) require.Contains(t, stdout, "1991-06-03") } func TestFileServiceSecret(t *testing.T) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
checkNotDisabled(t, engine.ServicesDNSEnvName) c, ctx := connect(t) defer c.Close() content := identity.NewID() httpSrv, httpURL := httpService(ctx, t, c, content) secret := c.HTTP(httpURL, dagger.HTTPOpts{ ExperimentalServiceHost: httpSrv, }).Secret() t.Run("secret env", func(t *testing.T) { exitCode, err := c.Container(). From("alpine:3.16.2"). WithSecretVariable("SEKRIT", secret). WithExec([]string{"sh", "-c", fmt.Sprintf(`test "$SEKRIT" = "%s"`, content)}). ExitCode(ctx) require.NoError(t, err) require.Equal(t, 0, exitCode) }) t.Run("secret mount", func(t *testing.T) { exitCode, err := c.Container(). From("alpine:3.16.2"). WithMountedSecret("/sekrit", secret). WithExec([]string{"sh", "-c", fmt.Sprintf(`test "$(cat /sekrit)" = "%s"`, content)}). ExitCode(ctx) require.NoError(t, err) require.Equal(t, 0, exitCode) }) } func httpService(ctx context.Context, t *testing.T, c *dagger.Client, content string) (*dagger.Container, string) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
t.Helper() srv := c.Container(). From("python"). WithMountedDirectory( "/srv/www", c.Directory().WithNewFile("index.html", content), ). WithWorkdir("/srv/www"). WithExposedPort(8000). WithExec([]string{"python", "-m", "http.server"}) httpURL, err := srv.Endpoint(ctx, dagger.ContainerEndpointOpts{ Scheme: "http", }) require.NoError(t, err) return srv, httpURL } func gitService(ctx context.Context, t *testing.T, c *dagger.Client, content *dagger.Directory) (*dagger.Container, string) { t.Helper() const gitPort = 9418 gitDaemon := c.Container(). From("alpine:3.16.2").
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/integration/services_test.go
WithExec([]string{"apk", "add", "git", "git-daemon"}). WithDirectory("/root/repo", content). WithMountedFile("/root/start.sh", c.Directory(). WithNewFile("start.sh", `#!/bin/sh set -e -u -x cd /root git config --global user.email "root@localhost" git config --global user.name "Test User" mkdir srv cd repo git init git branch -m main git add * git commit -m "init" cd .. cd srv git clone --bare ../repo repo.git cd .. git daemon --verbose --export-all --base-path=/root/srv `). File("start.sh")). WithExposedPort(gitPort). WithExec([]string{"sh", "/root/start.sh"}) gitHost, err := gitDaemon.Hostname(ctx) require.NoError(t, err) repoURL := fmt.Sprintf("git:%s/repo.git", gitHost) return gitDaemon, repoURL }
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
package schema import ( "fmt" "path" "strings" "github.com/dagger/dagger/core" "github.com/dagger/dagger/core/pipeline" "github.com/dagger/dagger/router" specs "github.com/opencontainers/image-spec/specs-go/v1" ) type containerSchema struct { *baseSchema host *core.Host } var _ router.ExecutableSchema = &containerSchema{} func (s *containerSchema) Name() string { return "container" } func (s *containerSchema) Schema() string { return Container } func (s *containerSchema) Resolvers() router.Resolvers { return router.Resolvers{ "ContainerID": stringResolver(core.ContainerID("")), "Query": router.ObjectResolver{ "container": router.ToResolver(s.container), }, "Container": router.ObjectResolver{ "from": router.ToResolver(s.from), "build": router.ToResolver(s.build),
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
"rootfs": router.ToResolver(s.rootfs), "pipeline": router.ToResolver(s.pipeline), "fs": router.ToResolver(s.rootfs), "withRootfs": router.ToResolver(s.withRootfs), "withFS": router.ToResolver(s.withRootfs), "file": router.ToResolver(s.file), "directory": router.ToResolver(s.directory), "user": router.ToResolver(s.user), "withUser": router.ToResolver(s.withUser), "workdir": router.ToResolver(s.workdir), "withWorkdir": router.ToResolver(s.withWorkdir), "envVariables": router.ToResolver(s.envVariables), "envVariable": router.ToResolver(s.envVariable), "withEnvVariable": router.ToResolver(s.withEnvVariable), "withSecretVariable": router.ToResolver(s.withSecretVariable), "withoutEnvVariable": router.ToResolver(s.withoutEnvVariable), "withLabel": router.ToResolver(s.withLabel), "label": router.ToResolver(s.label), "labels": router.ToResolver(s.labels), "withoutLabel": router.ToResolver(s.withoutLabel), "entrypoint": router.ToResolver(s.entrypoint), "withEntrypoint": router.ToResolver(s.withEntrypoint), "defaultArgs": router.ToResolver(s.defaultArgs), "withDefaultArgs": router.ToResolver(s.withDefaultArgs), "mounts": router.ToResolver(s.mounts), "withMountedDirectory": router.ToResolver(s.withMountedDirectory), "withMountedFile": router.ToResolver(s.withMountedFile), "withMountedTemp": router.ToResolver(s.withMountedTemp), "withMountedCache": router.ToResolver(s.withMountedCache), "withMountedSecret": router.ToResolver(s.withMountedSecret),
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
"withUnixSocket": router.ToResolver(s.withUnixSocket), "withoutUnixSocket": router.ToResolver(s.withoutUnixSocket), "withoutMount": router.ToResolver(s.withoutMount), "withFile": router.ToResolver(s.withFile), "withNewFile": router.ToResolver(s.withNewFile), "withDirectory": router.ToResolver(s.withDirectory), "withExec": router.ToResolver(s.withExec), "exec": router.ToResolver(s.withExec), "exitCode": router.ToResolver(s.exitCode), "stdout": router.ToResolver(s.stdout), "stderr": router.ToResolver(s.stderr), "publish": router.ToResolver(s.publish), "platform": router.ToResolver(s.platform), "export": router.ToResolver(s.export), "withRegistryAuth": router.ToResolver(s.withRegistryAuth), "withoutRegistryAuth": router.ToResolver(s.withoutRegistryAuth), "imageRef": router.ToResolver(s.imageRef), "withExposedPort": router.ToResolver(s.withExposedPort), "withoutExposedPort": router.ToResolver(s.withoutExposedPort), "exposedPorts": router.ToResolver(s.exposedPorts), "hostname": router.ToResolver(s.hostname), "endpoint": router.ToResolver(s.endpoint), "withServiceBinding": router.ToResolver(s.withServiceBinding), }, } } func (s *containerSchema) Dependencies() []router.ExecutableSchema { return nil } type containerArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
ID core.ContainerID Platform *specs.Platform } func (s *containerSchema) container(ctx *router.Context, parent *core.Query, args containerArgs) (*core.Container, error) { platform := s.baseSchema.platform if args.Platform != nil { if args.ID != "" { return nil, fmt.Errorf("cannot specify both existing container ID and platform") } platform = *args.Platform } ctr, err := core.NewContainer(args.ID, parent.PipelinePath(), platform) if err != nil { return nil, err } return ctr, err } type containerFromArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Address string } func (s *containerSchema) from(ctx *router.Context, parent *core.Container, args containerFromArgs) (*core.Container, error) { return parent.From(ctx, s.gw, args.Address) } type containerBuildArgs struct { Context core.DirectoryID Dockerfile string BuildArgs []core.BuildArg Target string } func (s *containerSchema) build(ctx *router.Context, parent *core.Container, args containerBuildArgs) (*core.Container, error) { return parent.Build(ctx, s.gw, &core.Directory{ID: args.Context}, args.Dockerfile, args.BuildArgs, args.Target) } func (s *containerSchema) withRootfs(ctx *router.Context, parent *core.Container, arg core.Directory) (*core.Container, error) { return parent.WithRootFS(ctx, &arg) } type containerPipelineArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string Description string Labels []pipeline.Label } func (s *containerSchema) pipeline(ctx *router.Context, parent *core.Container, args containerPipelineArgs) (*core.Container, error) { return parent.Pipeline(ctx, args.Name, args.Description, args.Labels) } func (s *containerSchema) rootfs(ctx *router.Context, parent *core.Container, args any) (*core.Directory, error) { return parent.RootFS(ctx) } type containerExecArgs struct { core.ContainerExecOpts } func (s *containerSchema) withExec(ctx *router.Context, parent *core.Container, args containerExecArgs) (*core.Container, error) { return parent.WithExec(ctx, s.gw, s.baseSchema.platform, args.ContainerExecOpts) } func (s *containerSchema) exitCode(ctx *router.Context, parent *core.Container, args any) (*int, error) { return parent.ExitCode(ctx, s.gw) } func (s *containerSchema) stdout(ctx *router.Context, parent *core.Container, args any) (*string, error) { return parent.MetaFileContents(ctx, s.gw, "stdout") } func (s *containerSchema) stderr(ctx *router.Context, parent *core.Container, args any) (*string, error) { return parent.MetaFileContents(ctx, s.gw, "stderr") } type containerWithEntrypointArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Args []string } func (s *containerSchema) withEntrypoint(ctx *router.Context, parent *core.Container, args containerWithEntrypointArgs) (*core.Container, error) { return parent.UpdateImageConfig(ctx, func(cfg specs.ImageConfig) specs.ImageConfig { cfg.Entrypoint = args.Args return cfg }) } func (s *containerSchema) entrypoint(ctx *router.Context, parent *core.Container, args containerWithVariableArgs) ([]string, error) { cfg, err := parent.ImageConfig(ctx) if err != nil { return nil, err } return cfg.Entrypoint, nil } type containerWithDefaultArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Args *[]string } func (s *containerSchema) withDefaultArgs(ctx *router.Context, parent *core.Container, args containerWithDefaultArgs) (*core.Container, error) { return parent.UpdateImageConfig(ctx, func(cfg specs.ImageConfig) specs.ImageConfig { if args.Args == nil { cfg.Cmd = []string{} return cfg } cfg.Cmd = *args.Args return cfg }) } func (s *containerSchema) defaultArgs(ctx *router.Context, parent *core.Container, args any) ([]string, error) { cfg, err := parent.ImageConfig(ctx) if err != nil { return nil, err } return cfg.Cmd, nil } type containerWithUserArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string } func (s *containerSchema) withUser(ctx *router.Context, parent *core.Container, args containerWithUserArgs) (*core.Container, error) { return parent.UpdateImageConfig(ctx, func(cfg specs.ImageConfig) specs.ImageConfig { cfg.User = args.Name return cfg }) } func (s *containerSchema) user(ctx *router.Context, parent *core.Container, args containerWithVariableArgs) (string, error) { cfg, err := parent.ImageConfig(ctx) if err != nil { return "", err } return cfg.User, nil } type containerWithWorkdirArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Path string } func (s *containerSchema) withWorkdir(ctx *router.Context, parent *core.Container, args containerWithWorkdirArgs) (*core.Container, error) { return parent.UpdateImageConfig(ctx, func(cfg specs.ImageConfig) specs.ImageConfig { cfg.WorkingDir = absPath(cfg.WorkingDir, args.Path) return cfg }) } func (s *containerSchema) workdir(ctx *router.Context, parent *core.Container, args containerWithVariableArgs) (string, error) { cfg, err := parent.ImageConfig(ctx) if err != nil { return "", err } return cfg.WorkingDir, nil } type containerWithVariableArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string Value string } func (s *containerSchema) withEnvVariable(ctx *router.Context, parent *core.Container, args containerWithVariableArgs) (*core.Container, error) { return parent.UpdateImageConfig(ctx, func(cfg specs.ImageConfig) specs.ImageConfig { newEnv := []string{} prefix := args.Name + "=" for _, env := range cfg.Env { if !strings.HasPrefix(env, prefix) { newEnv = append(newEnv, env) } } newEnv = append(newEnv, fmt.Sprintf("%s=%s", args.Name, args.Value)) cfg.Env = newEnv return cfg }) } type containerWithoutVariableArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string } func (s *containerSchema) withoutEnvVariable(ctx *router.Context, parent *core.Container, args containerWithoutVariableArgs) (*core.Container, error) { return parent.UpdateImageConfig(ctx, func(cfg specs.ImageConfig) specs.ImageConfig { removedEnv := []string{} prefix := args.Name + "=" for _, env := range cfg.Env { if !strings.HasPrefix(env, prefix) { removedEnv = append(removedEnv, env) } } cfg.Env = removedEnv return cfg }) } type EnvVariable struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string `json:"name"` Value string `json:"value"` } func (s *containerSchema) envVariables(ctx *router.Context, parent *core.Container, args any) ([]EnvVariable, error) { cfg, err := parent.ImageConfig(ctx) if err != nil { return nil, err } vars := make([]EnvVariable, 0, len(cfg.Env)) for _, v := range cfg.Env { name, value, _ := strings.Cut(v, "=") e := EnvVariable{ Name: name, Value: value, } vars = append(vars, e) } return vars, nil } type containerVariableArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string } func (s *containerSchema) envVariable(ctx *router.Context, parent *core.Container, args containerVariableArgs) (*string, error) { cfg, err := parent.ImageConfig(ctx) if err != nil { return nil, err } for _, env := range cfg.Env { name, val, ok := strings.Cut(env, "=") if ok && name == args.Name { return &val, nil } } return nil, nil } type Label struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string `json:"name"` Value string `json:"value"` } func (s *containerSchema) labels(ctx *router.Context, parent *core.Container, args any) ([]Label, error) { cfg, err := parent.ImageConfig(ctx) if err != nil { return nil, err } labels := make([]Label, 0, len(cfg.Labels)) for name, value := range cfg.Labels { label := Label{ Name: name, Value: value, } labels = append(labels, label) } return labels, nil } type containerLabelArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string } func (s *containerSchema) label(ctx *router.Context, parent *core.Container, args containerLabelArgs) (*string, error) { cfg, err := parent.ImageConfig(ctx) if err != nil { return nil, err } if val, ok := cfg.Labels[args.Name]; ok { return &val, nil } return nil, nil } type containerWithMountedDirectoryArgs struct { Path string Source core.DirectoryID } func (s *containerSchema) withMountedDirectory(ctx *router.Context, parent *core.Container, args containerWithMountedDirectoryArgs) (*core.Container, error) { return parent.WithMountedDirectory(ctx, args.Path, &core.Directory{ID: args.Source}) } type containerPublishArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Address string PlatformVariants []core.ContainerID } func (s *containerSchema) publish(ctx *router.Context, parent *core.Container, args containerPublishArgs) (string, error) { return parent.Publish(ctx, args.Address, args.PlatformVariants, s.bkClient, s.solveOpts, s.solveCh) } type containerWithMountedFileArgs struct { Path string Source core.FileID } func (s *containerSchema) withMountedFile(ctx *router.Context, parent *core.Container, args containerWithMountedFileArgs) (*core.Container, error) { return parent.WithMountedFile(ctx, args.Path, &core.File{ID: args.Source}) } type containerWithMountedCacheArgs struct { Path string `json:"path"` Cache core.CacheID `json:"cache"` Source core.DirectoryID `json:"source"` Concurrency core.CacheSharingMode `json:"sharing"` } func (s *containerSchema) withMountedCache(ctx *router.Context, parent *core.Container, args containerWithMountedCacheArgs) (*core.Container, error) { var dir *core.Directory if args.Source != "" { dir = &core.Directory{ID: args.Source} } return parent.WithMountedCache(ctx, args.Path, args.Cache, dir, args.Concurrency) } type containerWithMountedTempArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Path string } func (s *containerSchema) withMountedTemp(ctx *router.Context, parent *core.Container, args containerWithMountedTempArgs) (*core.Container, error) { return parent.WithMountedTemp(ctx, args.Path) } type containerWithoutMountArgs struct { Path string } func (s *containerSchema) withoutMount(ctx *router.Context, parent *core.Container, args containerWithoutMountArgs) (*core.Container, error) { return parent.WithoutMount(ctx, args.Path) } func (s *containerSchema) mounts(ctx *router.Context, parent *core.Container, _ any) ([]string, error) { return parent.Mounts(ctx) } type containerWithLabelArgs struct { Name string Value string } func (s *containerSchema) withLabel(ctx *router.Context, parent *core.Container, args containerWithLabelArgs) (*core.Container, error) { return parent.UpdateImageConfig(ctx, func(cfg specs.ImageConfig) specs.ImageConfig { if cfg.Labels == nil { cfg.Labels = make(map[string]string) } cfg.Labels[args.Name] = args.Value return cfg }) } type containerWithoutLabelArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string } func (s *containerSchema) withoutLabel(ctx *router.Context, parent *core.Container, args containerWithoutLabelArgs) (*core.Container, error) { return parent.UpdateImageConfig(ctx, func(cfg specs.ImageConfig) specs.ImageConfig { delete(cfg.Labels, args.Name) return cfg }) } type containerDirectoryArgs struct { Path string } func (s *containerSchema) directory(ctx *router.Context, parent *core.Container, args containerDirectoryArgs) (*core.Directory, error) { return parent.Directory(ctx, s.gw, args.Path) } type containerFileArgs struct { Path string } func (s *containerSchema) file(ctx *router.Context, parent *core.Container, args containerFileArgs) (*core.File, error) { return parent.File(ctx, s.gw, args.Path) } func absPath(workDir string, containerPath string) string { if path.IsAbs(containerPath) { return containerPath } if workDir == "" { workDir = "/" } return path.Join(workDir, containerPath) } type containerWithSecretVariableArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Name string Secret core.SecretID } func (s *containerSchema) withSecretVariable(ctx *router.Context, parent *core.Container, args containerWithSecretVariableArgs) (*core.Container, error) { return parent.WithSecretVariable(ctx, args.Name, &core.Secret{ID: args.Secret}) } type containerWithMountedSecretArgs struct { Path string Source core.SecretID } func (s *containerSchema) withMountedSecret(ctx *router.Context, parent *core.Container, args containerWithMountedSecretArgs) (*core.Container, error) { return parent.WithMountedSecret(ctx, args.Path, core.NewSecret(args.Source)) } func (s *containerSchema) withDirectory(ctx *router.Context, parent *core.Container, args withDirectoryArgs) (*core.Container, error) { return parent.WithDirectory(ctx, s.gw, args.Path, &core.Directory{ID: args.Directory}, args.CopyFilter) } func (s *containerSchema) withFile(ctx *router.Context, parent *core.Container, args withFileArgs) (*core.Container, error) { return parent.WithFile(ctx, s.gw, args.Path, &core.File{ID: args.Source}, args.Permissions) } func (s *containerSchema) withNewFile(ctx *router.Context, parent *core.Container, args withNewFileArgs) (*core.Container, error) { return parent.WithNewFile(ctx, s.gw, args.Path, []byte(args.Contents), args.Permissions) } type containerWithUnixSocketArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Path string Source core.SocketID } func (s *containerSchema) withUnixSocket(ctx *router.Context, parent *core.Container, args containerWithUnixSocketArgs) (*core.Container, error) { return parent.WithUnixSocket(ctx, args.Path, core.NewSocket(args.Source)) } type containerWithoutUnixSocketArgs struct { Path string } func (s *containerSchema) withoutUnixSocket(ctx *router.Context, parent *core.Container, args containerWithoutUnixSocketArgs) (*core.Container, error) { return parent.WithoutUnixSocket(ctx, args.Path) } func (s *containerSchema) platform(ctx *router.Context, parent *core.Container, args any) (specs.Platform, error) { return parent.Platform() } type containerExportArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Path string PlatformVariants []core.ContainerID } func (s *containerSchema) export(ctx *router.Context, parent *core.Container, args containerExportArgs) (bool, error) { if err := parent.Export(ctx, s.host, args.Path, args.PlatformVariants, s.bkClient, s.solveOpts, s.solveCh); err != nil { return false, err } return true, nil } type containerWithRegistryAuthArgs struct { Address string `json:"address"` Username string `json:"username"` Secret core.SecretID `json:"secret"` } func (s *containerSchema) withRegistryAuth(ctx *router.Context, parents *core.Container, args containerWithRegistryAuthArgs) (*core.Container, error) { secret, err := core.NewSecret(args.Secret).Plaintext(ctx, s.gw) if err != nil { return nil, err } if err := s.auth.AddCredential(args.Address, args.Username, string(secret)); err != nil { return nil, err } return parents, nil } type containerWithoutRegistryAuthArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Address string } func (s *containerSchema) withoutRegistryAuth(_ *router.Context, parents *core.Container, args containerWithoutRegistryAuthArgs) (*core.Container, error) { if err := s.auth.RemoveCredential(args.Address); err != nil { return nil, err } return parents, nil } func (s *containerSchema) imageRef(ctx *router.Context, parent *core.Container, args containerWithVariableArgs) (string, error) { return parent.ImageRef(ctx, s.gw) } func (s *containerSchema) hostname(ctx *router.Context, parent *core.Container, args any) (string, error) { if !s.servicesEnabled { return "", ErrServicesDisabled } return parent.Hostname() } type containerEndpointArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Port int Scheme string } func (s *containerSchema) endpoint(ctx *router.Context, parent *core.Container, args containerEndpointArgs) (string, error) { if !s.servicesEnabled { return "", ErrServicesDisabled } return parent.Endpoint(args.Port, args.Scheme) } type containerWithServiceDependencyArgs struct { Service core.ContainerID Alias string } func (s *containerSchema) withServiceBinding(ctx *router.Context, parent *core.Container, args containerWithServiceDependencyArgs) (*core.Container, error) { if !s.servicesEnabled { return nil, ErrServicesDisabled } return parent.WithServiceDependency(&core.Container{ID: args.Service}, args.Alias) } type containerWithExposedPortArgs struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Protocol core.NetworkProtocol Port int Description *string } func (s *containerSchema) withExposedPort(ctx *router.Context, parent *core.Container, args containerWithExposedPortArgs) (*core.Container, error) { if !s.servicesEnabled { return nil, ErrServicesDisabled } return parent.WithExposedPort(core.ContainerPort{ Protocol: args.Protocol, Port: args.Port, Description: args.Description, }) } type containerWithoutExposedPortArgs struct { Protocol core.NetworkProtocol Port int } func (s *containerSchema) withoutExposedPort(ctx *router.Context, parent *core.Container, args containerWithoutExposedPortArgs) (*core.Container, error) { if !s.servicesEnabled { return nil, ErrServicesDisabled } return parent.WithoutExposedPort(args.Port, args.Protocol) } type ExposedPort struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
core/schema/container.go
Port int `json:"port"` Protocol string `json:"protocol"` Description *string `json:"description,omitempty"` } func (s *containerSchema) exposedPorts(ctx *router.Context, parent *core.Container, args any) ([]ExposedPort, error) { if !s.servicesEnabled { return nil, ErrServicesDisabled } ports, err := parent.ExposedPorts() if err != nil { return nil, err } exposedPorts := []ExposedPort{} for _, p := range ports { exposedPorts = append(exposedPorts, ExposedPort{ Port: p.Port, Protocol: string(p.Protocol), Description: p.Description, }) } return exposedPorts, nil }
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
package dagger import ( "context" "dagger.io/dagger/internal/querybuilder" "github.com/Khan/genqlient/graphql" ) type CacheID string type ContainerID string type DirectoryID string type FileID string type Platform string type SecretID string type SocketID string type BuildArg struct { Name string `json:"name"` Value string `json:"value"` } type PipelineLabel struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Name string `json:"name"` Value string `json:"value"` } type CacheVolume struct { q *querybuilder.Selection c graphql.Client } func (r *CacheVolume) ID(ctx context.Context) (CacheID, error) { q := r.q.Select("id") var response CacheID q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *CacheVolume) XXX_GraphQLType() string { return "CacheVolume" } func (r *CacheVolume) XXX_GraphQLID(ctx context.Context) (string, error) { id, err := r.ID(ctx) if err != nil { return "", err } return string(id), nil } type Container struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
q *querybuilder.Selection c graphql.Client } type ContainerBuildOpts struct { Dockerfile string BuildArgs []BuildArg Target string } func (r *Container) Build(context *Directory, opts ...ContainerBuildOpts) *Container { q := r.q.Select("build") q = q.Arg("context", context) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Dockerfile) { q = q.Arg("dockerfile", opts[i].Dockerfile) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].BuildArgs) { q = q.Arg("buildArgs", opts[i].BuildArgs)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Target) { q = q.Arg("target", opts[i].Target) break } } return &Container{ q: q, c: r.c, } } func (r *Container) DefaultArgs(ctx context.Context) ([]string, error) { q := r.q.Select("defaultArgs") var response []string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) Directory(path string) *Directory { q := r.q.Select("directory") q = q.Arg("path", path) return &Directory{ q: q, c: r.c, } } type ContainerEndpointOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Port int Scheme string } func (r *Container) Endpoint(ctx context.Context, opts ...ContainerEndpointOpts) (string, error) { q := r.q.Select("endpoint") for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Port) { q = q.Arg("port", opts[i].Port) break } }
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Scheme) { q = q.Arg("scheme", opts[i].Scheme) break } } var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) Entrypoint(ctx context.Context) ([]string, error) { q := r.q.Select("entrypoint") var response []string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) EnvVariable(ctx context.Context, name string) (string, error) { q := r.q.Select("envVariable") q = q.Arg("name", name) var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) EnvVariables(ctx context.Context) ([]EnvVariable, error) { q := r.q.Select("envVariables") var response []EnvVariable q = q.Bind(&response) return response, q.Execute(ctx, r.c) } type ContainerExecOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Args []string Stdin string RedirectStdout string RedirectStderr string ExperimentalPrivilegedNesting bool } func (r *Container) Exec(opts ...ContainerExecOpts) *Container { q := r.q.Select("exec") for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Args) { q = q.Arg("args", opts[i].Args) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Stdin) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
q = q.Arg("stdin", opts[i].Stdin) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].RedirectStdout) { q = q.Arg("redirectStdout", opts[i].RedirectStdout) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].RedirectStderr) { q = q.Arg("redirectStderr", opts[i].RedirectStderr) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].ExperimentalPrivilegedNesting) { q = q.Arg("experimentalPrivilegedNesting", opts[i].ExperimentalPrivilegedNesting) break } } return &Container{ q: q, c: r.c, } }
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
func (r *Container) ExitCode(ctx context.Context) (int, error) { q := r.q.Select("exitCode") var response int q = q.Bind(&response) return response, q.Execute(ctx, r.c) } type ContainerExportOpts struct { PlatformVariants []*Container } func (r *Container) Export(ctx context.Context, path string, opts ...ContainerExportOpts) (bool, error) { q := r.q.Select("export") q = q.Arg("path", path) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].PlatformVariants) { q = q.Arg("platformVariants", opts[i].PlatformVariants) break } } var response bool q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) ExposedPorts(ctx context.Context) ([]Port, error) { q := r.q.Select("exposedPorts") var response []Port q = q.Bind(&response) return response, q.Execute(ctx, r.c)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
} func (r *Container) File(path string) *File { q := r.q.Select("file") q = q.Arg("path", path) return &File{ q: q, c: r.c, } } func (r *Container) From(address string) *Container { q := r.q.Select("from") q = q.Arg("address", address) return &Container{ q: q, c: r.c, } } func (r *Container) FS() *Directory { q := r.q.Select("fs") return &Directory{ q: q, c: r.c, } } func (r *Container) Hostname(ctx context.Context) (string, error) { q := r.q.Select("hostname") var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) }
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
func (r *Container) ID(ctx context.Context) (ContainerID, error) { q := r.q.Select("id") var response ContainerID q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) XXX_GraphQLType() string { return "Container" } func (r *Container) XXX_GraphQLID(ctx context.Context) (string, error) { id, err := r.ID(ctx) if err != nil { return "", err } return string(id), nil } func (r *Container) ImageRef(ctx context.Context) (string, error) { q := r.q.Select("imageRef") var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) Label(ctx context.Context, name string) (string, error) { q := r.q.Select("label") q = q.Arg("name", name) var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) Labels(ctx context.Context) ([]Label, error) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
q := r.q.Select("labels") var response []Label q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) Mounts(ctx context.Context) ([]string, error) { q := r.q.Select("mounts") var response []string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } type ContainerPipelineOpts struct { Description string Labels []PipelineLabel } func (r *Container) Pipeline(name string, opts ...ContainerPipelineOpts) *Container { q := r.q.Select("pipeline") q = q.Arg("name", name) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Description) { q = q.Arg("description", opts[i].Description) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Labels) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
q = q.Arg("labels", opts[i].Labels) break } } return &Container{ q: q, c: r.c, } } func (r *Container) Platform(ctx context.Context) (Platform, error) { q := r.q.Select("platform") var response Platform q = q.Bind(&response) return response, q.Execute(ctx, r.c) } type ContainerPublishOpts struct { PlatformVariants []*Container } func (r *Container) Publish(ctx context.Context, address string, opts ...ContainerPublishOpts) (string, error) { q := r.q.Select("publish") q = q.Arg("address", address) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].PlatformVariants) { q = q.Arg("platformVariants", opts[i].PlatformVariants) break } }
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) Rootfs() *Directory { q := r.q.Select("rootfs") return &Directory{ q: q, c: r.c, } } func (r *Container) Stderr(ctx context.Context) (string, error) { q := r.q.Select("stderr") var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) Stdout(ctx context.Context) (string, error) { q := r.q.Select("stdout") var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Container) User(ctx context.Context) (string, error) { q := r.q.Select("user") var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } type ContainerWithDefaultArgsOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Args []string } func (r *Container) WithDefaultArgs(opts ...ContainerWithDefaultArgsOpts) *Container { q := r.q.Select("withDefaultArgs") for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Args) { q = q.Arg("args", opts[i].Args) break } } return &Container{ q: q, c: r.c, } } type ContainerWithDirectoryOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Exclude []string Include []string } func (r *Container) WithDirectory(path string, directory *Directory, opts ...ContainerWithDirectoryOpts) *Container { q := r.q.Select("withDirectory") q = q.Arg("path", path) q = q.Arg("directory", directory) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Exclude) { q = q.Arg("exclude", opts[i].Exclude) break } }
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Include) { q = q.Arg("include", opts[i].Include) break } } return &Container{ q: q, c: r.c, } } func (r *Container) WithEntrypoint(args []string) *Container { q := r.q.Select("withEntrypoint") q = q.Arg("args", args) return &Container{ q: q, c: r.c, } } func (r *Container) WithEnvVariable(name string, value string) *Container { q := r.q.Select("withEnvVariable") q = q.Arg("name", name) q = q.Arg("value", value) return &Container{ q: q, c: r.c, } } type ContainerWithExecOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Stdin string RedirectStdout string RedirectStderr string ExperimentalPrivilegedNesting bool InsecureRootCapabilities bool } func (r *Container) WithExec(args []string, opts ...ContainerWithExecOpts) *Container { q := r.q.Select("withExec") q = q.Arg("args", args)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Stdin) { q = q.Arg("stdin", opts[i].Stdin) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].RedirectStdout) { q = q.Arg("redirectStdout", opts[i].RedirectStdout) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].RedirectStderr) { q = q.Arg("redirectStderr", opts[i].RedirectStderr) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].ExperimentalPrivilegedNesting) { q = q.Arg("experimentalPrivilegedNesting", opts[i].ExperimentalPrivilegedNesting) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].InsecureRootCapabilities) {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
q = q.Arg("insecureRootCapabilities", opts[i].InsecureRootCapabilities) break } } return &Container{ q: q, c: r.c, } } type ContainerWithExposedPortOpts struct { Protocol NetworkProtocol Description string } func (r *Container) WithExposedPort(port int, opts ...ContainerWithExposedPortOpts) *Container { q := r.q.Select("withExposedPort") q = q.Arg("port", port) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Protocol) { q = q.Arg("protocol", opts[i].Protocol) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Description) { q = q.Arg("description", opts[i].Description) break
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
} } return &Container{ q: q, c: r.c, } } func (r *Container) WithFS(id *Directory) *Container { q := r.q.Select("withFS") q = q.Arg("id", id) return &Container{ q: q, c: r.c, } } type ContainerWithFileOpts struct { Permissions int } func (r *Container) WithFile(path string, source *File, opts ...ContainerWithFileOpts) *Container { q := r.q.Select("withFile") q = q.Arg("path", path) q = q.Arg("source", source) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Permissions) { q = q.Arg("permissions", opts[i].Permissions) break
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
} } return &Container{ q: q, c: r.c, } } func (r *Container) WithLabel(name string, value string) *Container { q := r.q.Select("withLabel") q = q.Arg("name", name) q = q.Arg("value", value) return &Container{ q: q, c: r.c, } } type ContainerWithMountedCacheOpts struct { Source *Directory Sharing CacheSharingMode } func (r *Container) WithMountedCache(path string, cache *CacheVolume, opts ...ContainerWithMountedCacheOpts) *Container { q := r.q.Select("withMountedCache") q = q.Arg("path", path) q = q.Arg("cache", cache) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Source) { q = q.Arg("source", opts[i].Source)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Sharing) { q = q.Arg("sharing", opts[i].Sharing) break } } return &Container{ q: q, c: r.c, } } func (r *Container) WithMountedDirectory(path string, source *Directory) *Container { q := r.q.Select("withMountedDirectory") q = q.Arg("path", path) q = q.Arg("source", source) return &Container{ q: q, c: r.c, } } func (r *Container) WithMountedFile(path string, source *File) *Container { q := r.q.Select("withMountedFile") q = q.Arg("path", path) q = q.Arg("source", source) return &Container{ q: q,
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
c: r.c, } } func (r *Container) WithMountedSecret(path string, source *Secret) *Container { q := r.q.Select("withMountedSecret") q = q.Arg("path", path) q = q.Arg("source", source) return &Container{ q: q, c: r.c, } } func (r *Container) WithMountedTemp(path string) *Container { q := r.q.Select("withMountedTemp") q = q.Arg("path", path) return &Container{ q: q, c: r.c, } } type ContainerWithNewFileOpts struct { Contents string Permissions int } func (r *Container) WithNewFile(path string, opts ...ContainerWithNewFileOpts) *Container { q := r.q.Select("withNewFile")
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
q = q.Arg("path", path) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Contents) { q = q.Arg("contents", opts[i].Contents) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Permissions) { q = q.Arg("permissions", opts[i].Permissions) break } } return &Container{ q: q, c: r.c, } } func (r *Container) WithRegistryAuth(address string, username string, secret *Secret) *Container { q := r.q.Select("withRegistryAuth") q = q.Arg("address", address) q = q.Arg("username", username) q = q.Arg("secret", secret) return &Container{ q: q, c: r.c, } }
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
func (r *Container) WithRootfs(id *Directory) *Container { q := r.q.Select("withRootfs") q = q.Arg("id", id) return &Container{ q: q, c: r.c, } } func (r *Container) WithSecretVariable(name string, secret *Secret) *Container { q := r.q.Select("withSecretVariable") q = q.Arg("name", name) q = q.Arg("secret", secret) return &Container{ q: q, c: r.c, } } func (r *Container) WithServiceBinding(alias string, service *Container) *Container { q := r.q.Select("withServiceBinding") q = q.Arg("alias", alias) q = q.Arg("service", service) return &Container{ q: q, c: r.c, } } func (r *Container) WithUnixSocket(path string, source *Socket) *Container { q := r.q.Select("withUnixSocket") q = q.Arg("path", path) q = q.Arg("source", source)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
return &Container{ q: q, c: r.c, } } func (r *Container) WithUser(name string) *Container { q := r.q.Select("withUser") q = q.Arg("name", name) return &Container{ q: q, c: r.c, } } func (r *Container) WithWorkdir(path string) *Container { q := r.q.Select("withWorkdir") q = q.Arg("path", path) return &Container{ q: q, c: r.c, } } func (r *Container) WithoutEnvVariable(name string) *Container { q := r.q.Select("withoutEnvVariable") q = q.Arg("name", name) return &Container{ q: q, c: r.c, } } type ContainerWithoutExposedPortOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Protocol NetworkProtocol } func (r *Container) WithoutExposedPort(port int, opts ...ContainerWithoutExposedPortOpts) *Container { q := r.q.Select("withoutExposedPort") q = q.Arg("port", port) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Protocol) { q = q.Arg("protocol", opts[i].Protocol) break } } return &Container{ q: q, c: r.c, } } func (r *Container) WithoutLabel(name string) *Container { q := r.q.Select("withoutLabel") q = q.Arg("name", name) return &Container{ q: q, c: r.c, } } func (r *Container) WithoutMount(path string) *Container {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
q := r.q.Select("withoutMount") q = q.Arg("path", path) return &Container{ q: q, c: r.c, } } func (r *Container) WithoutRegistryAuth(address string) *Container { q := r.q.Select("withoutRegistryAuth") q = q.Arg("address", address) return &Container{ q: q, c: r.c, } } func (r *Container) WithoutUnixSocket(path string) *Container { q := r.q.Select("withoutUnixSocket") q = q.Arg("path", path) return &Container{ q: q, c: r.c, } } func (r *Container) Workdir(ctx context.Context) (string, error) { q := r.q.Select("workdir") var response string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } type Directory struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
q *querybuilder.Selection c graphql.Client } func (r *Directory) Diff(other *Directory) *Directory { q := r.q.Select("diff") q = q.Arg("other", other) return &Directory{ q: q, c: r.c, } } func (r *Directory) Directory(path string) *Directory { q := r.q.Select("directory") q = q.Arg("path", path) return &Directory{ q: q, c: r.c, } } type DirectoryDockerBuildOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Dockerfile string Platform Platform BuildArgs []BuildArg Target string } func (r *Directory) DockerBuild(opts ...DirectoryDockerBuildOpts) *Container { q := r.q.Select("dockerBuild") for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Dockerfile) { q = q.Arg("dockerfile", opts[i].Dockerfile)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Platform) { q = q.Arg("platform", opts[i].Platform) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].BuildArgs) { q = q.Arg("buildArgs", opts[i].BuildArgs) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Target) { q = q.Arg("target", opts[i].Target) break } } return &Container{ q: q, c: r.c, } } type DirectoryEntriesOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Path string } func (r *Directory) Entries(ctx context.Context, opts ...DirectoryEntriesOpts) ([]string, error) { q := r.q.Select("entries") for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Path) { q = q.Arg("path", opts[i].Path) break } } var response []string q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Directory) Export(ctx context.Context, path string) (bool, error) { q := r.q.Select("export") q = q.Arg("path", path) var response bool q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Directory) File(path string) *File { q := r.q.Select("file") q = q.Arg("path", path)
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
return &File{ q: q, c: r.c, } } func (r *Directory) ID(ctx context.Context) (DirectoryID, error) { q := r.q.Select("id") var response DirectoryID q = q.Bind(&response) return response, q.Execute(ctx, r.c) } func (r *Directory) XXX_GraphQLType() string { return "Directory" } func (r *Directory) XXX_GraphQLID(ctx context.Context) (string, error) { id, err := r.ID(ctx) if err != nil { return "", err } return string(id), nil } func (r *Directory) LoadProject(configPath string) *Project { q := r.q.Select("loadProject") q = q.Arg("configPath", configPath) return &Project{ q: q, c: r.c, } } type DirectoryPipelineOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Description string Labels []PipelineLabel } func (r *Directory) Pipeline(name string, opts ...DirectoryPipelineOpts) *Directory { q := r.q.Select("pipeline") q = q.Arg("name", name) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Description) { q = q.Arg("description", opts[i].Description) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Labels) { q = q.Arg("labels", opts[i].Labels) break } } return &Directory{ q: q, c: r.c, } } type DirectoryWithDirectoryOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Exclude []string Include []string } func (r *Directory) WithDirectory(path string, directory *Directory, opts ...DirectoryWithDirectoryOpts) *Directory { q := r.q.Select("withDirectory") q = q.Arg("path", path) q = q.Arg("directory", directory) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Exclude) { q = q.Arg("exclude", opts[i].Exclude) break } } for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Include) { q = q.Arg("include", opts[i].Include) break } } return &Directory{ q: q, c: r.c, } } type DirectoryWithFileOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Permissions int } func (r *Directory) WithFile(path string, source *File, opts ...DirectoryWithFileOpts) *Directory { q := r.q.Select("withFile") q = q.Arg("path", path) q = q.Arg("source", source) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Permissions) { q = q.Arg("permissions", opts[i].Permissions) break } } return &Directory{ q: q, c: r.c, } } type DirectoryWithNewDirectoryOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Permissions int } func (r *Directory) WithNewDirectory(path string, opts ...DirectoryWithNewDirectoryOpts) *Directory { q := r.q.Select("withNewDirectory") q = q.Arg("path", path) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Permissions) { q = q.Arg("permissions", opts[i].Permissions) break } } return &Directory{ q: q, c: r.c, } } type DirectoryWithNewFileOpts struct {
closed
dagger/dagger
https://github.com/dagger/dagger
4,668
Lazy executions are confusing to understand and sometimes don't work as expected
## Summary Developers are often confused by a property of the Dagger engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance. There are several dimensions to this problem; below is an overview of different dimensions, and status of possible solutions. | Issues | Proposals | | ------------------------------------------- | ------------------------ | | [No `withExec`](#with-exec) | <li>#4833</li> | | [`Dockerfile` build (without exec)](#build) | <li>#5065</li> | | [Implicit query execution](#implicit) | <li>#5065</li> | | [Multiple ways to execute](#multiple) | “Pipeline builder” model | | [Documentation](#docs) | <li>#3617</li> | ## <a name="with-exec"></a>Issue: no `withExec` We had some users report that part of their pipeline wasn't being executed, for which they had to add a `WithExec(nil)` statement for it to work: ```go _, err := c.Container().Build(src).ExitCode(ctx) // doesn't work _, err := c.Container().From("alpine").ExitCode(ctx) // same thing ``` ### Explanation Users may assume that since they know there’s an `Entrypoint`/`Cmd` in the docker image it should work, but it’s just updating the dagger container metadata. There’s nothing to run, it’s equivalent to the following: ```go _, err := ctr. WithEntrypoint([]string{}). WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{ Args: []string{"/bin/sh"}, }) ExitCode(ctx) // nothing to execute! ``` `ExitCode` and `Stdout` only return something for the **last executed command**. That means the equivalent of a `RUN` instruction in a `Dockerfile` or running a container with `docker run`. ### Workaround Add a `WithExec()` to tell dagger to execute the container: ```diff _, err := client.Container(). Build(src). + WithExec(nil). ExitCode(ctx) ``` The empty (`nil`) argument to `WithExec` will execute the **entrypoint and default args** configured in the dagger container. > **Note** > If you replace the `.ExitCode()` with a `Publish()`, you see that `Build()` is called and the image is published, because `Publish` doesn’t depend on execution but `Build` is still a dependency. The same is true for a bound service: ```diff db := client.Container().From("postgres"). WithExposedPort(5432). + WithExec(nil) ctr := app.WithServiceBinding("db", db) ``` Here, `WithServiceBinding` clearly needs to execute/run the *postgres* container so that *app* can connect to it, so we need the `WithExec` here too (with `nil` for default entrypoint and arguments). ### Proposals To avoid astonishment, a fix was added (#4716) to raise an error when fields like `.ExitCode` or `.WithServiceBinding` (that depend on `WithExec`) are used on a container that hasn’t been executed. However, perhaps a better solution is to implicitly execute the entrypoint and default arguments because if you’re using a field that depends on an execution, we can assume that you mean to execute the container. This is what #4833 proposes, meaning the following would now work as expected by users: ```go // ExitCode → needs execution so use default exec _, err := c.Container().From("alpine").ExitCode(ctx) // WithServiceBinding → needs execution so use default exec db := client.Container().From("postgres").WithExposedPort(5432) ctr := app.WithServiceBinding("db", db) ``` ```[tasklist] ### No `withExec` - [x] #4716 - [ ] #4833 ``` ## <a name="build"></a>Issue: `Dockerfile` build (without exec) Some users just want to test if a `Dockerfile` build succeeds or not, and **don’t want to execute the entrypoint** (e.g., long running executable): ```go _, err = client.Container().Build(src).ExitCode(ctx) ``` In this case users are just using `ExitCode` as a way to trigger the build when they also don’t want to `Publish`. It’s the same problem as above, but the intent is different. ### Workarounds With #4919, you’ll be able to skip the entrypoint: ```go _, err = client.Container(). Build(src). WithExec([]string{"/bin/true"}, dagger.ContainerWithExecOpts{ SkipEntrypoint: true, }). ExitCode(ctx) ``` But executing the container isn’t even needed to build, so `ExitCode` isn’t a good choice here. It’s just simpler to use another field such as: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Rootfs().Entries(ctx) ``` However this isn’t intuitive and is clearly a workaround (not meant for this). ### Proposal Perhaps the best solution is to use a general synchronization primitive (#5065) that simply forces resolving the laziness in the pipeline, especially since the result is discarded in the above workarounds: ```diff - _, err = client.Container().Build(src).ExitCode(ctx) + _, err = client.Container().Build(src).Sync(ctx) ``` ```[tasklist] ### `Dockerfile` build (without exec) - [x] #4919 - [ ] #5065 ``` ## <a name="implicit"></a>Issue: Implicit query execution Some functions are “lazy” and don’t result in a [query execution](http://spec.graphql.org/October2021/#sec-Execution) (e.g., `From`, `Build`, `WithXXX`), while others execute (e.g., `ExitCode`, `Stdout`, `Publish`). It’s not clear to some users which is which. ### Explanation The model is implicit, with a “rule of thumb” in each language to hint which ones execute: - **Go:** functions taking a context and returning an error - **Python** and **Node.js:** `async` functions that need an `await` Essentially, each SDK’s codegen (the feature that introspects the API and builds a dagger client that is idiomatic in each language) **transforms [leaf fields](http://spec.graphql.org/October2021/#sec-Leaf-Field-Selections) into an implicit API request** when called, and return the **value from the response**. So the “rule of thumb” is based on the need to make a request to the GraphQL server, the problem is that it may not be immediately clear and the syntax can vary depending on the language so there’s different “rules” to understand. This was discussed in: - #3555 - #3558 ### Proposal The same [Pipeline Synchronization](https://github.com/dagger/dagger/issues/5065) proposal from the previous issue helps make this a bit more explicit: ```go _, err := ctr.Sync(ctx) ``` ```[tasklist] ### Implicit query execution - [x] #3555 - [x] #3558 - [ ] #5065 ``` ## <a name="multiple"></a>Issue: Multiple ways to execute “Execution” sometimes mean different things: - **Container execution** (i.e., `Container.withExec`) - **[Query execution](http://spec.graphql.org/October2021/#sec-Execution)** (i.e., making a request to the GraphQL API) - **”Engine” execution** (i.e., doing actual work in BuildKit) The *ID* fields like `Container.ID` for example, make a request to the API, but don’t do any actual work building the container. We reduced the scope of the issue in the SDKs by avoiding passing IDs around (#3558), and keeping the pipeline as lazy as possible until an output is needed (see [Implicit query execution](#implicit) above). More importantly, users have been using `.ExitCode(ctx)` as the goto solution to “synchronize” the laziness, but as we’ve seen in the above issues, it triggers the container to execute and there’s cases where you don’t want to do that. However, adding the general `.Sync()` (#4205) to fix that may make people shift to using it as the goto solution to “resolve” the laziness instead (“synchronize”), which actually makes sense. The problem is that we now go back to needing `WithExec(nil)` because `.Sync()` can’t assume you want to execute the container. That’s a catch 22 situation! **There’s no single execute function** to “rule them all”. It requires the user to have a good enough grasp on these concepts and the Dagger model to chose the right function for each purpose: ```go // exec the container (build since it's a dependency) c.Container().Build(src).ExitCode(ctx) // just build (don't exec) c.Container().Build(src).Sync(ctx) ``` ### Proposal During the “implicit vs explicit” discussions, the proposal for the most explicit solution was for a “pipeline builder” model (https://github.com/dagger/dagger/issues/3555#issuecomment-1301327344). The idea was to make a clear separation between **building** the lazy pipeline and **executing** the query: ```go // ExitCode doesn't implicitly execute query here! Still lazy. // Just setting expected output, and adding exec as a dependency. // Build is a dependency for exec so it also runs. q := c.Container().Build(src).ExitCode() // Same as above but don't care about output, just exec. q := c.Container().Build(src).WithExec(nil) // Same as above but don't want to exec, just build. q := c.Container().Build(src) // Only one way to execute query! client.Query(q) ``` ### Downsides - It’s a big **breaking change** so it’s not seen as a viable solution now - No great solution to grab output values - More boilerplate for simple things ### Solution Embrace the laziness! ## Issue: <a name="docs"></a>Documentation We have a guide on [Lazy Evaluation](https://docs.dagger.io/api/975146/concepts#lazy-evaluation) but it’s focused on the GraphQL API and isn’t enough to explain the above issues. We need better documentation to help users understand the “lazy DAG” model (https://github.com/dagger/dagger/issues/3617). It’s even more important if the “pipeline builder” model above isn’t viable. ```[tasklist] ### Documentation - [x] #3622 - [ ] #3617 ``` ## Affected users These are only some examples of users that were affected by this: - from @RonanQuigley > DX or a Bug: In order to have a dockerfile's entrypoint executed, why did we need to use a dummy withExec? There was a unamious 😩 in our team call after we figured this out. - https://discord.com/channels/707636530424053791/708371226174685314/1079926439064911972 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - https://discord.com/channels/707636530424053791/1080160708123185264/1080174051965812766 - #5010
https://github.com/dagger/dagger/issues/4668
https://github.com/dagger/dagger/pull/4716
f2a62f276d36918b0e453389dc7c63cad195da59
ad722627391f3e7d5bf51534f846913afc95d555
2023-02-28T17:37:30Z
go
2023-03-07T23:54:56Z
sdk/go/api.gen.go
Permissions int } func (r *Directory) WithNewFile(path string, contents string, opts ...DirectoryWithNewFileOpts) *Directory { q := r.q.Select("withNewFile") q = q.Arg("path", path) q = q.Arg("contents", contents) for i := len(opts) - 1; i >= 0; i-- { if !querybuilder.IsZeroValue(opts[i].Permissions) { q = q.Arg("permissions", opts[i].Permissions) break } }