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
Container struct { ID string Fs struct { Entries []string } } }{} err := testutil.Query( `{ container { id fs { entries } } }`, &res, nil) require.NoError(t, err) require.Empty(t, res.Container.Fs.Entries) } func TestContainerFrom(t *testing.T) { t.Parallel() res := 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/container_test.go
Container struct { From struct { Fs struct { File struct { Contents string } } } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { fs { file(path: "/etc/alpine-release") { contents } } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.Fs.File.Contents, "3.16.2\n") } func TestContainerBuild(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/container_test.go
ctx := context.Background() c, err := dagger.Connect(ctx) require.NoError(t, err) defer c.Close() contextDir := c.Directory(). WithNewFile("main.go", `package main import "fmt" import "os" func main() { for _, env := range os.Environ() { fmt.Println(env) } }`) t.Run("default Dockerfile location", func(t *testing.T) { src := contextDir. WithNewFile("Dockerfile", `FROM golang:1.18.2-alpine WORKDIR /src COPY main.go . RUN go mod init hello RUN go build -o /usr/bin/goenv main.go ENV FOO=bar CMD goenv `) env, err := c.Container().Build(src).WithExec([]string{}).Stdout(ctx) require.NoError(t, err) require.Contains(t, env, "FOO=bar\n") })
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
t.Run("custom Dockerfile location", func(t *testing.T) { src := contextDir. WithNewFile("subdir/Dockerfile.whee", `FROM golang:1.18.2-alpine WORKDIR /src COPY main.go . RUN go mod init hello RUN go build -o /usr/bin/goenv main.go ENV FOO=bar CMD goenv `) env, err := c.Container().Build(src, dagger.ContainerBuildOpts{ Dockerfile: "subdir/Dockerfile.whee", }).WithExec([]string{}).Stdout(ctx) require.NoError(t, err) require.Contains(t, env, "FOO=bar\n") }) t.Run("subdirectory with default Dockerfile location", func(t *testing.T) { src := contextDir. WithNewFile("Dockerfile", `FROM golang:1.18.2-alpine WORKDIR /src COPY main.go . RUN go mod init hello RUN go build -o /usr/bin/goenv main.go ENV FOO=bar CMD goenv `) sub := c.Directory().WithDirectory("subcontext", src).Directory("subcontext") env, err := c.Container().Build(sub).WithExec([]string{}).Stdout(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/container_test.go
require.NoError(t, err) require.Contains(t, env, "FOO=bar\n") }) t.Run("subdirectory with custom Dockerfile location", func(t *testing.T) { src := contextDir. WithNewFile("subdir/Dockerfile.whee", `FROM golang:1.18.2-alpine WORKDIR /src COPY main.go . RUN go mod init hello RUN go build -o /usr/bin/goenv main.go ENV FOO=bar CMD goenv `) sub := c.Directory().WithDirectory("subcontext", src).Directory("subcontext") env, err := c.Container().Build(sub, dagger.ContainerBuildOpts{ Dockerfile: "subdir/Dockerfile.whee", }).WithExec([]string{}).Stdout(ctx) require.NoError(t, err) require.Contains(t, env, "FOO=bar\n") }) t.Run("with build args", func(t *testing.T) { src := contextDir. WithNewFile("Dockerfile", `FROM golang:1.18.2-alpine ARG FOOARG=bar WORKDIR /src COPY main.go . RUN go mod init hello RUN go build -o /usr/bin/goenv main.go
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
ENV FOO=$FOOARG CMD goenv `) env, err := c.Container().Build(src).WithExec([]string{}).Stdout(ctx) require.NoError(t, err) require.Contains(t, env, "FOO=bar\n") env, err = c.Container().Build(src, dagger.ContainerBuildOpts{BuildArgs: []dagger.BuildArg{{Name: "FOOARG", Value: "barbar"}}}).WithExec([]string{}).Stdout(ctx) require.NoError(t, err) require.Contains(t, env, "FOO=barbar\n") }) t.Run("with target", func(t *testing.T) { src := contextDir. WithNewFile("Dockerfile", `FROM golang:1.18.2-alpine AS base CMD echo "base" FROM base AS stage1 CMD echo "stage1" FROM base AS stage2 CMD echo "stage2" `) output, err := c.Container().Build(src).WithExec([]string{}).Stdout(ctx) require.NoError(t, err) require.Contains(t, output, "stage2\n") output, err = c.Container().Build(src, dagger.ContainerBuildOpts{Target: "stage1"}).WithExec([]string{}).Stdout(ctx) require.NoError(t, err) require.Contains(t, output, "stage1\n") require.NotContains(t, output, "stage2\n") }) } func TestContainerWithRootFS(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/container_test.go
t.Parallel() ctx := context.Background() c, err := dagger.Connect(ctx) require.NoError(t, err) defer c.Close() alpine316 := c.Container().From("alpine:3.16.2") alpine316ReleaseStr, err := alpine316.File("/etc/alpine-release").Contents(ctx) require.NoError(t, err) alpine316ReleaseStr = strings.TrimSpace(alpine316ReleaseStr) dir := alpine316.Rootfs() exitCode, err := c.Container().WithEnvVariable("ALPINE_RELEASE", alpine316ReleaseStr).WithRootfs(dir).WithExec([]string{ "/bin/sh", "-c", "test -f /etc/alpine-release && test \"$(head -n 1 /etc/alpine-release)\" = \"$ALPINE_RELEASE\"", }).ExitCode(ctx) require.NoError(t, err) require.Equal(t, exitCode, 0) alpine315 := c.Container().From("alpine:3.15.6") varVal := "testing123" alpine315WithVar := alpine315.WithEnvVariable("DAGGER_TEST", varVal) varValResp, err := alpine315WithVar.EnvVariable(ctx, "DAGGER_TEST") require.NoError(t, err)
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
require.Equal(t, varVal, varValResp) alpine315ReplacedFS := alpine315WithVar.WithRootfs(dir) varValResp, err = alpine315ReplacedFS.EnvVariable(ctx, "DAGGER_TEST") require.NoError(t, err) require.Equal(t, varVal, varValResp) releaseStr, err := alpine315ReplacedFS.File("/etc/alpine-release").Contents(ctx) require.NoError(t, err) require.Equal(t, "3.16.2\n", releaseStr) } func TestContainerExecExitCode(t *testing.T) { t.Parallel() res := struct { Container struct { From struct { WithExec struct { ExitCode *int } } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { withExec(args: ["true"]) { exitCode } } } }`, &res, 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
core/integration/container_test.go
require.NoError(t, err) require.NotNil(t, res.Container.From.WithExec.ExitCode) require.Equal(t, 0, *res.Container.From.WithExec.ExitCode) /* It's not currently possible to get a nonzero exit code back because Buildkit raises an error. We could perhaps have the shim mask the exit status and always exit 0, but we would have to be careful not to let that happen in a big chained LLB since it would prevent short-circuiting. We could only do it when the user requests the exitCode, but then we would actually need to run the command _again_ since we'd need some way to tell the shim what to do. Hmm... err = testutil.Query( `{ container { from(address: "alpine:3.16.2") { withExec(args: ["false"]) { exitCode } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.WithExec.ExitCode, 1) */ } func TestContainerExecStdoutStderr(t *testing.T) { t.Parallel() res := 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/container_test.go
Container struct { From struct { WithExec struct { Stdout string Stderr string } } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { withExec(args: ["sh", "-c", "echo hello; echo goodbye >/dev/stderr"]) { stdout stderr } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.WithExec.Stdout, "hello\n") require.Equal(t, res.Container.From.WithExec.Stderr, "goodbye\n") } func TestContainerExecStdin(t *testing.T) { t.Parallel() res := struct { Container struct { From 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/container_test.go
WithExec struct { Stdout string } } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { withExec(args: ["cat"], stdin: "hello") { stdout } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.WithExec.Stdout, "hello") } func TestContainerExecRedirectStdoutStderr(t *testing.T) { t.Parallel() res := struct { Container struct { From struct { WithExec struct { Out, Err struct { Contents 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/container_test.go
} }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { withExec( args: ["sh", "-c", "echo hello; echo goodbye >/dev/stderr"], redirectStdout: "out", redirectStderr: "err" ) { out: file(path: "out") { contents } err: file(path: "err") { contents } } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.WithExec.Out.Contents, "hello\n") require.Equal(t, res.Container.From.WithExec.Err.Contents, "goodbye\n") c, ctx := connect(t) defer c.Close() execWithMount := c.Container().From("alpine:3.16.2"). WithMountedDirectory("/mnt", c.Directory()). WithExec([]string{"sh", "-c", "echo hello; echo goodbye >/dev/stderr"}, dagger.ContainerWithExecOpts{ RedirectStdout: "/mnt/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/container_test.go
RedirectStderr: "/mnt/err", }) stdout, err := execWithMount.File("/mnt/out").Contents(ctx) require.NoError(t, err) require.Equal(t, "hello\n", stdout) stderr, err := execWithMount.File("/mnt/err").Contents(ctx) require.NoError(t, err) require.Equal(t, "goodbye\n", stderr) err = testutil.Query( `{ container { from(address: "alpine:3.16.2") { exec( args: ["sh", "-c", "echo hello; echo goodbye >/dev/stderr"], redirectStdout: "out", redirectStderr: "err" ) { stdout stderr } } } }`, &res, nil) require.Error(t, err) require.Contains(t, err.Error(), "stdout: no such file or directory") require.Contains(t, err.Error(), "stderr: no such file or directory") } func TestContainerNullStdoutStderr(t *testing.T) { t.Parallel() res := 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/container_test.go
Container struct { From struct { Stdout *string Stderr *string } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { stdout stderr } } }`, &res, nil) require.NoError(t, err) require.Nil(t, res.Container.From.Stdout) require.Nil(t, res.Container.From.Stderr) } func TestContainerExecWithWorkdir(t *testing.T) { t.Parallel() res := struct { Container struct { From struct { WithWorkdir struct { WithExec 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/container_test.go
Stdout string } } } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { withWorkdir(path: "/usr") { withExec(args: ["pwd"]) { stdout } } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.WithWorkdir.WithExec.Stdout, "/usr\n") } func TestContainerExecWithUser(t *testing.T) { t.Parallel() res := struct { Container struct { From struct { User string WithUser struct { User string WithExec 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/container_test.go
Stdout string } } } } }{} t.Run("user name", func(t *testing.T) { err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { user withUser(name: "daemon") { user withExec(args: ["whoami"]) { stdout } } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, "", res.Container.From.User) require.Equal(t, "daemon", res.Container.From.WithUser.User) require.Equal(t, "daemon\n", res.Container.From.WithUser.WithExec.Stdout) }) t.Run("user and group name", func(t *testing.T) { err := testutil.Query( `{ 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/container_test.go
from(address: "alpine:3.16.2") { user withUser(name: "daemon:floppy") { user withExec(args: ["sh", "-c", "whoami; groups"]) { stdout } } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, "", res.Container.From.User) require.Equal(t, "daemon:floppy", res.Container.From.WithUser.User) require.Equal(t, "daemon\nfloppy\n", res.Container.From.WithUser.WithExec.Stdout) }) t.Run("user ID", func(t *testing.T) { err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { user withUser(name: "2") { user withExec(args: ["whoami"]) { stdout } } } }
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
}`, &res, nil) require.NoError(t, err) require.Equal(t, "", res.Container.From.User) require.Equal(t, "2", res.Container.From.WithUser.User) require.Equal(t, "daemon\n", res.Container.From.WithUser.WithExec.Stdout) }) t.Run("user and group ID", func(t *testing.T) { err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { user withUser(name: "2:11") { user withExec(args: ["sh", "-c", "whoami; groups"]) { stdout } } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, "", res.Container.From.User) require.Equal(t, "2:11", res.Container.From.WithUser.User) require.Equal(t, "daemon\nfloppy\n", res.Container.From.WithUser.WithExec.Stdout) }) } func TestContainerExecWithEntrypoint(t *testing.T) { t.Parallel() res := 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/container_test.go
Container struct { From struct { Entrypoint []string WithEntrypoint struct { Entrypoint []string WithExec struct { Stdout string } WithEntrypoint 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/container_test.go
Entrypoint []string } } } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { entrypoint withEntrypoint(args: ["sh", "-c"]) { entrypoint withExec(args: ["echo $HOME"]) { stdout } withEntrypoint(args: []) { entrypoint } } } } }`, &res, nil) require.NoError(t, err) require.Empty(t, res.Container.From.Entrypoint) require.Equal(t, []string{"sh", "-c"}, res.Container.From.WithEntrypoint.Entrypoint) require.Equal(t, "/root\n", res.Container.From.WithEntrypoint.WithExec.Stdout) require.Empty(t, res.Container.From.WithEntrypoint.WithEntrypoint.Entrypoint) } func TestContainerWithDefaultArgs(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/container_test.go
t.Parallel() res := struct { Container struct { From struct { Entrypoint []string DefaultArgs []string WithExec struct { Stdout string } WithDefaultArgs struct { Entrypoint []string DefaultArgs []string } WithEntrypoint struct { Entrypoint []string DefaultArgs []string WithExec struct { Stdout string } WithDefaultArgs struct { Entrypoint []string DefaultArgs []string WithExec 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/container_test.go
Stdout string } } } } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { entrypoint defaultArgs withDefaultArgs { entrypoint defaultArgs } withEntrypoint(args: ["sh", "-c"]) { entrypoint defaultArgs withExec(args: ["echo $HOME"]) { stdout } withDefaultArgs(args: ["id"]) { entrypoint defaultArgs withExec(args: []) { stdout }
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
} } } } }`, &res, nil) t.Run("default alpine (no entrypoint)", func(t *testing.T) { require.NoError(t, err) require.Empty(t, res.Container.From.Entrypoint) require.Equal(t, []string{"/bin/sh"}, res.Container.From.DefaultArgs) }) t.Run("with nil default args", func(t *testing.T) { require.Empty(t, res.Container.From.WithDefaultArgs.Entrypoint) require.Empty(t, res.Container.From.WithDefaultArgs.DefaultArgs) }) t.Run("with entrypoint set", func(t *testing.T) { require.Equal(t, []string{"sh", "-c"}, res.Container.From.WithEntrypoint.Entrypoint) require.Equal(t, []string{"/bin/sh"}, res.Container.From.WithEntrypoint.DefaultArgs) }) t.Run("with exec args", func(t *testing.T) { require.Equal(t, "/root\n", res.Container.From.WithEntrypoint.WithExec.Stdout) }) t.Run("with default args set", func(t *testing.T) { require.Equal(t, []string{"sh", "-c"}, res.Container.From.WithEntrypoint.WithDefaultArgs.Entrypoint) require.Equal(t, []string{"id"}, res.Container.From.WithEntrypoint.WithDefaultArgs.DefaultArgs) require.Equal(t, "uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)\n", res.Container.From.WithEntrypoint.WithDefaultArgs.WithExec.Stdout) }) } func TestContainerExecWithEnvVariable(t *testing.T) { t.Parallel() res := 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/container_test.go
Container struct { From 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/container_test.go
WithEnvVariable struct { WithExec struct { Stdout string } } } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { withEnvVariable(name: "FOO", value: "bar") { withExec(args: ["env"]) { stdout } } } } }`, &res, nil) require.NoError(t, err) require.Contains(t, res.Container.From.WithEnvVariable.WithExec.Stdout, "FOO=bar\n") } func TestContainerVariables(t *testing.T) { t.Parallel() res := struct { Container struct { From struct { EnvVariables []schema.EnvVariable WithExec 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/container_test.go
Stdout string } } } }{} err := testutil.Query( `{ container { from(address: "golang:1.18.2-alpine") { envVariables { name value } withExec(args: ["env"]) { stdout } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, []schema.EnvVariable{ {Name: "PATH", Value: "/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, {Name: "GOLANG_VERSION", Value: "1.18.2"}, {Name: "GOPATH", Value: "/go"}, }, res.Container.From.EnvVariables) require.Contains(t, res.Container.From.WithExec.Stdout, "GOPATH=/go\n") } func TestContainerVariable(t *testing.T) { t.Parallel() res := 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/container_test.go
Container struct { From struct { EnvVariable *string } } }{} err := testutil.Query( `{ container { from(address: "golang:1.18.2-alpine") { envVariable(name: "GOLANG_VERSION") } } }`, &res, nil) require.NoError(t, err) require.NotNil(t, res.Container.From.EnvVariable) require.Equal(t, "1.18.2", *res.Container.From.EnvVariable) err = testutil.Query( `{ container { from(address: "golang:1.18.2-alpine") { envVariable(name: "UNKNOWN") } } }`, &res, nil) require.NoError(t, err) require.Nil(t, res.Container.From.EnvVariable) } func TestContainerWithoutVariable(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/container_test.go
t.Parallel() res := struct { Container struct { From struct { WithoutEnvVariable struct { EnvVariables []schema.EnvVariable WithExec 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/container_test.go
Stdout string } } } } }{} err := testutil.Query( `{ container { from(address: "golang:1.18.2-alpine") { withoutEnvVariable(name: "GOLANG_VERSION") { envVariables { name value } withExec(args: ["env"]) { stdout } } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.WithoutEnvVariable.EnvVariables, []schema.EnvVariable{ {Name: "PATH", Value: "/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, {Name: "GOPATH", Value: "/go"}, }) require.NotContains(t, res.Container.From.WithoutEnvVariable.WithExec.Stdout, "GOLANG_VERSION") } func TestContainerEnvVariablesReplace(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/container_test.go
t.Parallel() res := struct { Container struct { From struct { WithEnvVariable struct { EnvVariables []schema.EnvVariable WithExec struct { Stdout string } } } } }{} err := testutil.Query( `{ container { from(address: "golang:1.18.2-alpine") { withEnvVariable(name: "GOPATH", value: "/gone") { envVariables { name value
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
} withExec(args: ["env"]) { stdout } } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.WithEnvVariable.EnvVariables, []schema.EnvVariable{ {Name: "PATH", Value: "/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, {Name: "GOLANG_VERSION", Value: "1.18.2"}, {Name: "GOPATH", Value: "/gone"}, }) require.Contains(t, res.Container.From.WithEnvVariable.WithExec.Stdout, "GOPATH=/gone\n") } func TestContainerLabel(t *testing.T) { ctx := context.Background() c, err := dagger.Connect(ctx) require.NoError(t, err) defer c.Close() t.Run("container with new label", func(t *testing.T) { label, err := c.Container().From("alpine:3.16.2").WithLabel("FOO", "BAR").Label(ctx, "FOO") require.NoError(t, err) require.Contains(t, label, "BAR") }) t.Run("container labels", func(t *testing.T) { res := 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/container_test.go
Container struct { From struct { Labels []schema.Label } } }{} err := testutil.Query( `{ container { from(address: "nginx") { labels { name value } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, []schema.Label{ {Name: "maintainer", Value: "NGINX Docker Maintainers <[email protected]>"}, }, res.Container.From.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
core/integration/container_test.go
}) t.Run("container without label", func(t *testing.T) { label, err := c.Container().From("nginx").WithoutLabel("maintainer").Label(ctx, "maintainer") require.NoError(t, err) require.Empty(t, label) }) t.Run("container replace label", func(t *testing.T) { label, err := c.Container().From("nginx").WithLabel("maintainer", "bar").Label(ctx, "maintainer") require.NoError(t, err) require.Contains(t, label, "bar") }) t.Run("container with new label - nil panics", func(t *testing.T) { label, err := c.Container().WithLabel("FOO", "BAR").Label(ctx, "FOO") require.NoError(t, err) require.Contains(t, label, "BAR") }) t.Run("container label - nil panics", func(t *testing.T) { label, err := c.Container().Label(ctx, "FOO") require.NoError(t, err) require.Empty(t, label) }) t.Run("container without label - nil panics", func(t *testing.T) { label, err := c.Container().WithoutLabel("maintainer").Label(ctx, "maintainer") require.NoError(t, err) require.Empty(t, label) }) t.Run("container labels - nil panics", func(t *testing.T) { res := 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/container_test.go
Container struct { From struct { Labels []schema.Label } } }{} err := testutil.Query( `{ container { labels { name value } } }`, &res, nil) require.NoError(t, err) require.Empty(t, res.Container.From.Labels) }) } func TestContainerWorkdir(t *testing.T) { t.Parallel() res := 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/container_test.go
Container struct { From struct { Workdir string WithExec struct { Stdout string } } } }{} err := testutil.Query( `{ container { from(address: "golang:1.18.2-alpine") { workdir withExec(args: ["pwd"]) { stdout } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.Workdir, "/go") require.Equal(t, res.Container.From.WithExec.Stdout, "/go\n") } func TestContainerWithWorkdir(t *testing.T) { t.Parallel() 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/container_test.go
From struct { WithWorkdir struct { Workdir string WithExec struct { Stdout string } } } } }{} err := testutil.Query( `{ container { from(address: "golang:1.18.2-alpine") { withWorkdir(path: "/usr") { workdir withExec(args: ["pwd"]) { stdout } } } } }`, &res, nil) require.NoError(t, err) require.Equal(t, res.Container.From.WithWorkdir.Workdir, "/usr") require.Equal(t, res.Container.From.WithWorkdir.WithExec.Stdout, "/usr\n") } func TestContainerWithMountedDirectory(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Directory struct { WithNewFile struct { WithNewFile struct { ID string } } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-file", contents: "some-content") { withNewFile(path: "some-dir/sub-file", contents: "sub-content") { id } } } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.WithNewFile.WithNewFile.ID execRes := struct { Container struct { From struct { WithMountedDirectory struct { WithExec struct { Stdout string WithExec 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/container_test.go
Stdout string } } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt", source: $id) { withExec(args: ["cat", "/mnt/some-file"]) { stdout withExec(args: ["cat", "/mnt/some-dir/sub-file"]) { stdout } } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.NoError(t, err) require.Equal(t, "some-content", execRes.Container.From.WithMountedDirectory.WithExec.Stdout) require.Equal(t, "sub-content", execRes.Container.From.WithMountedDirectory.WithExec.WithExec.Stdout) } func TestContainerWithMountedDirectorySourcePath(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/container_test.go
t.Parallel() dirRes := 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/container_test.go
Directory struct { WithNewFile struct { WithNewFile struct { Directory struct { ID string } } } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-file", contents: "some-content") { withNewFile(path: "some-dir/sub-file", contents: "sub-content") { directory(path: "some-dir") { id } } } } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.WithNewFile.WithNewFile.Directory.ID execRes := struct { Container struct { From struct { WithMountedDirectory struct { WithExec 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/container_test.go
WithExec struct { Stdout string } } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt", source: $id) { withExec(args: ["sh", "-c", "echo >> /mnt/sub-file; echo -n more-content >> /mnt/sub-file"]) { withExec(args: ["cat", "/mnt/sub-file"]) { stdout } } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.NoError(t, err) require.Equal(t, "sub-content\nmore-content", execRes.Container.From.WithMountedDirectory.WithExec.WithExec.Stdout) } func TestContainerWithMountedDirectoryPropagation(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Directory struct { WithNewFile struct { ID core.DirectoryID } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-file", contents: "some-content") { id } } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.WithNewFile.ID execRes := struct { Container struct { From struct { WithMountedDirectory struct { WithExec struct { Stdout string WithExec struct { WithExec struct { Stdout string WithMountedDirectory struct { WithExec struct { Stdout string WithExec 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/container_test.go
Stdout string } } } } } } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt", source: $id) { withExec(args: ["cat", "/mnt/some-file"]) { # original content stdout withExec(args: ["sh", "-c", "echo >> /mnt/some-file; echo -n more-content >> /mnt/some-file"]) { withExec(args: ["cat", "/mnt/some-file"]) { # modified content should propagate stdout withMountedDirectory(path: "/mnt", source: $id) { withExec(args: ["cat", "/mnt/some-file"]) { # should be back to the original content stdout withExec(args: ["cat", "/mnt/some-file"]) { # original content override should propagate
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
stdout } } } } } } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.NoError(t, err) require.Equal(t, "some-content", execRes.Container.From.WithMountedDirectory.WithExec.Stdout) require.Equal(t, "some-content\nmore-content", execRes.Container.From.WithMountedDirectory.WithExec.WithExec.WithExec.Stdout) require.Equal(t, "some-content", execRes.Container.From.WithMountedDirectory.WithExec.WithExec.WithExec.WithMountedDirectory.WithExec.Stdout) require.Equal(t, "some-content", execRes.Container.From.WithMountedDirectory.WithExec.WithExec.WithExec.WithMountedDirectory.WithExec.WithExec.Stdout) } func TestContainerWithMountedFile(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Directory struct { WithNewFile struct { File struct { ID core.FileID } } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-dir/sub-file", contents: "sub-content") { file(path: "some-dir/sub-file") { id } } } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.WithNewFile.File.ID execRes := 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/container_test.go
From struct { WithMountedFile struct { WithExec struct { Stdout string } } } } }{} err = testutil.Query( `query Test($id: FileID!) { container { from(address: "alpine:3.16.2") { withMountedFile(path: "/mnt/file", source: $id) { withExec(args: ["cat", "/mnt/file"]) { stdout } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.NoError(t, err) require.Equal(t, "sub-content", execRes.Container.From.WithMountedFile.WithExec.Stdout) } func TestContainerWithMountedCache(t *testing.T) { t.Parallel() cacheID := newCache(t) execRes := 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/container_test.go
Container struct { From struct { WithEnvVariable struct { WithMountedCache struct { WithExec 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/container_test.go
Stdout string } } } } } }{} query := `query Test($cache: CacheID!, $rand: String!) { container { from(address: "alpine:3.16.2") { withEnvVariable(name: "RAND", value: $rand) { withMountedCache(path: "/mnt/cache", cache: $cache) { withExec(args: ["sh", "-c", "echo $RAND >> /mnt/cache/file; cat /mnt/cache/file"]) { stdout } } } } } }` rand1 := identity.NewID() err := testutil.Query(query, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "cache": cacheID, "rand": rand1, }}) require.NoError(t, err) require.Equal(t, rand1+"\n", execRes.Container.From.WithEnvVariable.WithMountedCache.WithExec.Stdout) rand2 := identity.NewID() err = testutil.Query(query, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "cache": cacheID,
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
"rand": rand2, }}) require.NoError(t, err) require.Equal(t, rand1+"\n"+rand2+"\n", execRes.Container.From.WithEnvVariable.WithMountedCache.WithExec.Stdout) } func TestContainerWithMountedCacheFromDirectory(t *testing.T) { t.Parallel() dirRes := struct { Directory struct { WithNewFile struct { Directory struct { ID core.FileID } } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-dir/sub-file", contents: "initial-content\n") { directory(path: "some-dir") { id } } } }`, &dirRes, nil) require.NoError(t, err) initialID := dirRes.Directory.WithNewFile.Directory.ID cacheID := newCache(t) execRes := 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/container_test.go
Container struct { From struct { WithEnvVariable struct { WithMountedCache struct { WithExec struct { Stdout string } } } } } }{} query := `query Test($cache: CacheID!, $rand: String!, $init: DirectoryID!) { container { from(address: "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/container_test.go
withEnvVariable(name: "RAND", value: $rand) { withMountedCache(path: "/mnt/cache", cache: $cache, source: $init) { withExec(args: ["sh", "-c", "echo $RAND >> /mnt/cache/sub-file; cat /mnt/cache/sub-file"]) { stdout } } } } } }` rand1 := identity.NewID() err = testutil.Query(query, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "init": initialID, "rand": rand1, "cache": cacheID, }}) require.NoError(t, err) require.Equal(t, "initial-content\n"+rand1+"\n", execRes.Container.From.WithEnvVariable.WithMountedCache.WithExec.Stdout) rand2 := identity.NewID() err = testutil.Query(query, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "init": initialID, "rand": rand2, "cache": cacheID, }}) require.NoError(t, err) require.Equal(t, "initial-content\n"+rand1+"\n"+rand2+"\n", execRes.Container.From.WithEnvVariable.WithMountedCache.WithExec.Stdout) } func TestContainerWithMountedTemp(t *testing.T) { t.Parallel() execRes := 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/container_test.go
Container struct { From struct { WithMountedTemp struct { WithExec struct { Stdout string } } } } }{} err := testutil.Query(`{ container { from(address: "alpine:3.16.2") { withMountedTemp(path: "/mnt/tmp") { withExec(args: ["grep", "/mnt/tmp", "/proc/mounts"]) { stdout } } } } }`, &execRes, nil) require.NoError(t, err) require.Contains(t, execRes.Container.From.WithMountedTemp.WithExec.Stdout, "tmpfs /mnt/tmp tmpfs") } func TestContainerWithDirectory(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/container_test.go
t.Parallel() c, ctx := connect(t) defer c.Close() dir := c.Directory(). WithNewFile("some-file", "some-content"). WithNewFile("some-dir/sub-file", "sub-content"). Directory("some-dir") ctr := c.Container(). From("alpine:3.16.2"). WithWorkdir("/workdir"). WithDirectory("with-dir", dir) contents, err := ctr.WithExec([]string{"cat", "with-dir/sub-file"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "sub-content", contents) contents, err = ctr.WithExec([]string{"cat", "/workdir/with-dir/sub-file"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "sub-content", contents)
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
mount := c.Directory(). WithNewFile("mounted-file", "mounted-content") ctr = c.Container(). From("alpine:3.16.2"). WithWorkdir("/workdir"). WithMountedDirectory("mnt/mount", mount). WithDirectory("mnt/mount/dst/with-dir", dir) contents, err = ctr.WithExec([]string{"cat", "mnt/mount/mounted-file"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "mounted-content", contents) contents, err = ctr.WithExec([]string{"cat", "mnt/mount/dst/with-dir/sub-file"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "sub-content", contents) mnt := c.Directory().WithNewDirectory("/a/b/c") ctr = c.Container(). From("alpine:3.16.2"). WithMountedDirectory("/mnt", mnt) dir = c.Directory(). WithNewDirectory("/foo"). WithNewFile("/foo/some-file", "some-content") ctr = ctr.WithDirectory("/mnt/a/b/foo", dir) contents, err = ctr.WithExec([]string{"cat", "/mnt/a/b/foo/foo/some-file"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "some-content", contents) } func TestContainerWithFile(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/container_test.go
t.Parallel() c, ctx := connect(t) defer c.Close() file := c.Directory(). WithNewFile("some-file", "some-content"). File("some-file") ctr := c.Container(). From("alpine:3.16.2"). WithWorkdir("/workdir"). WithFile("target-file", file) contents, err := ctr.WithExec([]string{"cat", "target-file"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "some-content", contents) contents, err = ctr.WithExec([]string{"cat", "/workdir/target-file"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "some-content", contents) } func TestContainerWithNewFile(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/container_test.go
t.Parallel() c, ctx := connect(t) defer c.Close() ctr := c.Container(). From("alpine:3.16.2"). WithWorkdir("/workdir"). WithNewFile("some-file", dagger.ContainerWithNewFileOpts{ Contents: "some-content", }) contents, err := ctr.WithExec([]string{"cat", "some-file"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "some-content", contents) contents, err = ctr.WithExec([]string{"cat", "/workdir/some-file"}). Stdout(ctx) require.NoError(t, err) require.Equal(t, "some-content", contents) } func TestContainerMountsWithoutMount(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Directory struct { WithNewFile struct { WithNewFile struct { ID string } } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-file", contents: "some-content") { withNewFile(path: "some-dir/sub-file", contents: "sub-content") { id } } } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.WithNewFile.WithNewFile.ID execRes := 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/container_test.go
Container struct { From struct { WithDirectory struct { WithMountedTemp struct { Mounts []string WithMountedDirectory struct { Mounts []string WithExec struct { Stdout string WithoutMount struct { Mounts []string WithExec struct { Stdout string } } } } } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "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/container_test.go
withDirectory(path: "/mnt/dir", directory: "") { withMountedTemp(path: "/mnt/tmp") { mounts withMountedDirectory(path: "/mnt/dir", source: $id) { mounts withExec(args: ["ls", "/mnt/dir"]) { stdout withoutMount(path: "/mnt/dir") { mounts withExec(args: ["ls", "/mnt/dir"]) { stdout } } } } } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.NoError(t, err) require.Equal(t, []string{"/mnt/tmp"}, execRes.Container.From.WithDirectory.WithMountedTemp.Mounts) require.Equal(t, []string{"/mnt/tmp", "/mnt/dir"}, execRes.Container.From.WithDirectory.WithMountedTemp.WithMountedDirectory.Mounts) require.Equal(t, "some-dir\nsome-file\n", execRes.Container.From.WithDirectory.WithMountedTemp.WithMountedDirectory.WithExec.Stdout) require.Equal(t, "", execRes.Container.From.WithDirectory.WithMountedTemp.WithMountedDirectory.WithExec.WithoutMount.WithExec.Stdout) require.Equal(t, []string{"/mnt/tmp"}, execRes.Container.From.WithDirectory.WithMountedTemp.WithMountedDirectory.WithExec.WithoutMount.Mounts) } func TestContainerReplacedMounts(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/container_test.go
t.Parallel() c, ctx := connect(t) defer c.Close() lower := c.Directory().WithNewFile("some-file", "lower-content") upper := c.Directory().WithNewFile("some-file", "upper-content") ctr := c.Container(). From("alpine:3.16.2"). WithMountedDirectory("/mnt/dir", lower) t.Run("initial content is lower", func(t *testing.T) { mnts, err := ctr.Mounts(ctx) require.NoError(t, err) require.Equal(t, []string{"/mnt/dir"}, mnts) out, err := ctr.WithExec([]string{"cat", "/mnt/dir/some-file"}).Stdout(ctx) require.NoError(t, err) require.Equal(t, "lower-content", out) }) replaced := ctr.WithMountedDirectory("/mnt/dir", upper) t.Run("mounts of same path are replaced", func(t *testing.T) { mnts, err := replaced.Mounts(ctx) require.NoError(t, err) require.Equal(t, []string{"/mnt/dir"}, mnts) out, err := replaced.WithExec([]string{"cat", "/mnt/dir/some-file"}).Stdout(ctx) require.NoError(t, err) require.Equal(t, "upper-content", 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/container_test.go
t.Run("removing a replaced mount does not reveal previous mount", func(t *testing.T) { removed := replaced.WithoutMount("/mnt/dir") mnts, err := removed.Mounts(ctx) require.NoError(t, err) require.Empty(t, mnts) }) clobberedDir := c.Directory().WithNewFile("some-file", "clobbered-content") clobbered := replaced.WithMountedDirectory("/mnt", clobberedDir) t.Run("replacing parent of a mount clobbers child", func(t *testing.T) { mnts, err := clobbered.Mounts(ctx) require.NoError(t, err) require.Equal(t, []string{"/mnt"}, mnts) out, err := clobbered.WithExec([]string{"cat", "/mnt/some-file"}).Stdout(ctx) require.NoError(t, err) require.Equal(t, "clobbered-content", out) }) clobberedSubDir := c.Directory().WithNewFile("some-file", "clobbered-sub-content") clobberedSub := clobbered.WithMountedDirectory("/mnt/dir", clobberedSubDir) t.Run("restoring mount under clobbered mount", func(t *testing.T) { mnts, err := clobberedSub.Mounts(ctx) require.NoError(t, err) require.Equal(t, []string{"/mnt", "/mnt/dir"}, mnts) out, err := clobberedSub.WithExec([]string{"cat", "/mnt/dir/some-file"}).Stdout(ctx) require.NoError(t, err) require.Equal(t, "clobbered-sub-content", out) }) } func TestContainerDirectory(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Directory struct { WithNewFile struct { WithNewFile struct { ID core.DirectoryID } } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-file", contents: "some-content") { withNewFile(path: "some-dir/sub-file", contents: "sub-content") { id } } } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.WithNewFile.WithNewFile.ID writeRes := struct { Container struct { From struct { WithMountedDirectory struct { WithMountedDirectory struct { WithExec 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/container_test.go
Directory struct { ID core.DirectoryID } } } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { withMountedDirectory(path: "/mnt/dir/overlap", source: $id) { withExec(args: ["sh", "-c", "echo hello >> /mnt/dir/overlap/another-file"]) { directory(path: "/mnt/dir/overlap") { id } } } } } } }`, &writeRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.NoError(t, err) writtenID := writeRes.Container.From.WithMountedDirectory.WithMountedDirectory.WithExec.Directory.ID execRes := 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/container_test.go
Container struct { From struct { WithMountedDirectory struct { WithExec struct { Stdout string } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { withExec(args: ["cat", "/mnt/dir/another-file"]) { stdout } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": writtenID, }}) require.NoError(t, err) require.Equal(t, "hello\n", execRes.Container.From.WithMountedDirectory.WithExec.Stdout) } func TestContainerDirectoryErrors(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Directory struct { WithNewFile struct { WithNewFile struct { ID core.DirectoryID } } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-file", contents: "some-content") { withNewFile(path: "some-dir/sub-file", contents: "sub-content") { id } } } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.WithNewFile.WithNewFile.ID err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { directory(path: "/mnt/dir/some-file") { id
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
} } } } }`, nil, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.Error(t, err) require.Contains(t, err.Error(), "path /mnt/dir/some-file is a file, not a directory") err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { directory(path: "/mnt/dir/bogus") { id } } } } }`, nil, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.Error(t, err) require.Contains(t, err.Error(), "bogus: no such file or directory") err = testutil.Query( `{ container { from(address: "alpine:3.16.2") { withMountedTemp(path: "/mnt/tmp") {
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
directory(path: "/mnt/tmp/bogus") { id } } } } }`, nil, nil) require.Error(t, err) require.Contains(t, err.Error(), "bogus: cannot retrieve path from tmpfs") cacheID := newCache(t) err = testutil.Query( `query Test($cache: CacheID!) { container { from(address: "alpine:3.16.2") { withMountedCache(path: "/mnt/cache", cache: $cache) { directory(path: "/mnt/cache/bogus") { id } } } } }`, nil, &testutil.QueryOptions{Variables: map[string]any{ "cache": cacheID, }}) require.Error(t, err) require.Contains(t, err.Error(), "bogus: cannot retrieve path from cache") } func TestContainerDirectorySourcePath(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Directory struct { WithNewFile struct { Directory struct { ID core.DirectoryID } } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-dir/sub-dir/sub-file", contents: "sub-content\n") { directory(path: "some-dir") { id } } } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.WithNewFile.Directory.ID writeRes := struct { Container struct { From 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/container_test.go
WithMountedDirectory struct { WithExec struct { Directory struct { ID core.DirectoryID } } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { withExec(args: ["sh", "-c", "echo more-content >> /mnt/dir/sub-dir/sub-file"]) { directory(path: "/mnt/dir/sub-dir") { id } } } } } }`, &writeRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.NoError(t, err) writtenID := writeRes.Container.From.WithMountedDirectory.WithExec.Directory.ID execRes := 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/container_test.go
From struct { WithMountedDirectory struct { WithExec struct { Stdout string } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { withExec(args: ["cat", "/mnt/dir/sub-file"]) { stdout } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": writtenID, }}) require.NoError(t, err) require.Equal(t, "sub-content\nmore-content\n", execRes.Container.From.WithMountedDirectory.WithExec.Stdout) } func TestContainerFile(t *testing.T) { t.Parallel() id := newDirWithFile(t, "some-file", "some-content-") writeRes := 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/container_test.go
Container struct { From struct { WithMountedDirectory struct { WithMountedDirectory struct { WithExec 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/container_test.go
File struct { ID core.FileID } } } } } } }{} err := testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { withMountedDirectory(path: "/mnt/dir/overlap", source: $id) { withExec(args: ["sh", "-c", "echo -n appended >> /mnt/dir/overlap/some-file"]) { file(path: "/mnt/dir/overlap/some-file") { id } } } } } } }`, &writeRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.NoError(t, err) writtenID := writeRes.Container.From.WithMountedDirectory.WithMountedDirectory.WithExec.File.ID execRes := 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/container_test.go
Container struct { From struct { WithMountedFile struct { WithExec struct { Stdout string } } } } }{} err = testutil.Query( `query Test($id: FileID!) { container { from(address: "alpine:3.16.2") { withMountedFile(path: "/mnt/file", source: $id) { withExec(args: ["cat", "/mnt/file"]) { stdout } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": writtenID, }}) require.NoError(t, err) require.Equal(t, "some-content-appended", execRes.Container.From.WithMountedFile.WithExec.Stdout) } func TestContainerFileErrors(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/container_test.go
t.Parallel() id := newDirWithFile(t, "some-file", "some-content") err := testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { file(path: "/mnt/dir/bogus") { id } } } } }`, nil, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.Error(t, err) require.Contains(t, err.Error(), "bogus: no such file or directory") err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { file(path: "/mnt/dir") { id }
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
} } } }`, nil, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.Error(t, err) require.Contains(t, err.Error(), "path /mnt/dir is a directory, not a file") err = testutil.Query( `{ container { from(address: "alpine:3.16.2") { withMountedTemp(path: "/mnt/tmp") { file(path: "/mnt/tmp/bogus") { id } } } } }`, nil, nil) require.Error(t, err) require.Contains(t, err.Error(), "bogus: cannot retrieve path from tmpfs") cacheID := newCache(t) err = testutil.Query( `query Test($cache: CacheID!) { container { from(address: "alpine:3.16.2") { withMountedCache(path: "/mnt/cache", cache: $cache) { file(path: "/mnt/cache/bogus") { id
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
} } } } }`, nil, &testutil.QueryOptions{Variables: map[string]any{ "cache": cacheID, }}) require.Error(t, err) require.Contains(t, err.Error(), "bogus: cannot retrieve path from cache") secretID := newSecret(t, "some-secret") err = testutil.Query( `query Test($secret: SecretID!) { container { from(address: "alpine:3.16.2") { withMountedSecret(path: "/sekret", source: $secret) { file(path: "/sekret") { contents } } } } }`, nil, &testutil.QueryOptions{Variables: map[string]any{ "secret": secretID, }}) require.Error(t, err) require.Contains(t, err.Error(), "sekret: no such file or directory") } func TestContainerFSDirectory(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Container struct { From struct { Directory struct { ID core.DirectoryID } } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { directory(path: "/etc") { id } } } }`, &dirRes, nil) require.NoError(t, err) etcID := dirRes.Container.From.Directory.ID execRes := 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/container_test.go
Container struct { From struct { WithMountedDirectory struct { WithExec struct { Stdout string } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/etc", source: $id) { withExec(args: ["cat", "/mnt/etc/alpine-release"]) { stdout } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": etcID, }}) require.NoError(t, err) require.Equal(t, "3.16.2\n", execRes.Container.From.WithMountedDirectory.WithExec.Stdout) } func TestContainerRelativePaths(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Directory struct { WithNewFile struct { ID core.DirectoryID } } }{} err := testutil.Query( `{ directory { withNewFile(path: "some-file", contents: "some-content") { id } } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.WithNewFile.ID writeRes := struct { Container struct { From struct { WithExec struct { WithWorkdir struct { WithWorkdir struct { Workdir string WithMountedDirectory struct { WithMountedTemp struct { WithMountedCache 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/container_test.go
Mounts []string WithExec struct { Directory struct { ID core.DirectoryID } } WithoutMount struct { Mounts []string } } } } } } } } } }{} cacheID := newCache(t) err = testutil.Query( `query Test($id: DirectoryID!, $cache: CacheID!) { container { from(address: "alpine:3.16.2") { withExec(args: ["mkdir", "-p", "/mnt/sub"]) { withWorkdir(path: "/mnt") { withWorkdir(path: "sub") { workdir withMountedDirectory(path: "dir", source: $id) { withMountedTemp(path: "tmp") { withMountedCache(path: "cache", cache: $cache) {
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
mounts withExec(args: ["touch", "dir/another-file"]) { directory(path: "dir") { id } } withoutMount(path: "cache") { mounts } } } } } } } } } }`, &writeRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, "cache": cacheID, }}) require.NoError(t, err) require.Equal(t, []string{"/mnt/sub/dir", "/mnt/sub/tmp", "/mnt/sub/cache"}, writeRes.Container.From.WithExec.WithWorkdir.WithWorkdir.WithMountedDirectory.WithMountedTemp.WithMountedCache.Mounts) require.Equal(t, []string{"/mnt/sub/dir", "/mnt/sub/tmp"}, writeRes.Container.From.WithExec.WithWorkdir.WithWorkdir.WithMountedDirectory.WithMountedTemp.WithMountedCache.WithoutMount.Mounts) writtenID := writeRes.Container.From.WithExec.WithWorkdir.WithWorkdir.WithMountedDirectory.WithMountedTemp.WithMountedCache.WithExec.Directory.ID execRes := 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/container_test.go
Container struct { From struct { WithMountedDirectory struct { WithExec struct { Stdout string } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "alpine:3.16.2") { withMountedDirectory(path: "/mnt/dir", source: $id) { withExec(args: ["ls", "/mnt/dir"]) { stdout } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": writtenID, }}) require.NoError(t, err) require.Equal(t, "another-file\nsome-file\n", execRes.Container.From.WithMountedDirectory.WithExec.Stdout) } func TestContainerMultiFrom(t *testing.T) { t.Parallel() dirRes := 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/container_test.go
Directory struct { ID core.DirectoryID } }{} err := testutil.Query( `{ directory { id } }`, &dirRes, nil) require.NoError(t, err) id := dirRes.Directory.ID execRes := 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/container_test.go
Container struct { From struct { WithMountedDirectory struct { WithExec struct { From struct { WithExec struct { WithExec struct { Stdout string } } } } } } } }{} err = testutil.Query( `query Test($id: DirectoryID!) { container { from(address: "node:18.10.0-alpine") { withMountedDirectory(path: "/mnt", source: $id) { withExec(args: ["sh", "-c", "node --version >> /mnt/versions"]) { from(address: "golang:1.18.2-alpine") { withExec(args: ["sh", "-c", "go version >> /mnt/versions"]) { withExec(args: ["cat", "/mnt/versions"]) { stdout
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
} } } } } } } }`, &execRes, &testutil.QueryOptions{Variables: map[string]any{ "id": id, }}) require.NoError(t, err) require.Contains(t, execRes.Container.From.WithMountedDirectory.WithExec.From.WithExec.WithExec.Stdout, "v18.10.0\n") require.Contains(t, execRes.Container.From.WithMountedDirectory.WithExec.From.WithExec.WithExec.Stdout, "go version go1.18.2") } func TestContainerPublish(t *testing.T) { c, ctx := connect(t) defer c.Close() testRef := registryRef("container-publish") pushedRef, err := c.Container(). From("alpine:3.16.2"). Publish(ctx, testRef) require.NoError(t, err) require.NotEqual(t, testRef, pushedRef) require.Contains(t, pushedRef, "@sha256:") contents, err := c.Container(). From(pushedRef).Rootfs().File("/etc/alpine-release").Contents(ctx) require.NoError(t, err) require.Equal(t, contents, "3.16.2\n") } func TestExecFromScratch(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/container_test.go
ctx, cancel := context.WithCancel(context.Background()) defer cancel() c, err := dagger.Connect(ctx) require.NoError(t, err) defer c.Close() execBusybox := c.Container(). WithMountedFile("/busybox", c.Container().From("busybox:musl").File("/bin/busybox")). WithExec([]string{"/busybox"}) _, err = execBusybox.Stdout(ctx) require.NoError(t, err) _, err = execBusybox.Publish(ctx, registryRef("from-scratch")) require.NoError(t, err) } func TestContainerMultipleMounts(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/container_test.go
c, ctx := connect(t) defer c.Close() dir := t.TempDir() require.NoError(t, os.WriteFile(filepath.Join(dir, "one"), []byte("1"), 0o600)) require.NoError(t, os.WriteFile(filepath.Join(dir, "two"), []byte("2"), 0o600)) require.NoError(t, os.WriteFile(filepath.Join(dir, "three"), []byte("3"), 0o600)) one := c.Host().Directory(dir).File("one") two := c.Host().Directory(dir).File("two") three := c.Host().Directory(dir).File("three") build := c.Container().From("alpine:3.16.2"). WithMountedFile("/example/one", one). WithMountedFile("/example/two", two). WithMountedFile("/example/three", three) build = build.WithExec([]string{"ls", "/example/one", "/example/two", "/example/three"}) build = build.WithExec([]string{"cat", "/example/one", "/example/two", "/example/three"}) out, err := build.Stdout(ctx) require.NoError(t, err) require.Equal(t, "123", out) } func TestContainerExport(t *testing.T) { t.Parallel() ctx := context.Background() wd := t.TempDir() dest := t.TempDir() c, err := dagger.Connect(ctx, dagger.WithWorkdir(wd)) require.NoError(t, err)
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
defer c.Close() ctr := c.Container().From("alpine:3.16.2") t.Run("to absolute dir", func(t *testing.T) { imagePath := filepath.Join(dest, "image.tar") ok, err := ctr.Export(ctx, imagePath) require.NoError(t, err) require.True(t, ok) entries := tarEntries(t, imagePath) require.Contains(t, entries, "oci-layout") require.Contains(t, entries, "index.json") require.Contains(t, entries, "manifest.json") }) t.Run("to workdir", func(t *testing.T) { ok, err := ctr.Export(ctx, "./image.tar") require.NoError(t, err) require.True(t, ok) entries := tarEntries(t, filepath.Join(wd, "image.tar")) require.Contains(t, entries, "oci-layout") require.Contains(t, entries, "index.json") require.Contains(t, entries, "manifest.json") }) t.Run("to outer dir", func(t *testing.T) { ok, err := ctr.Export(ctx, "../") require.Error(t, err) require.False(t, ok) }) } func TestContainerMultiPlatformExport(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/container_test.go
ctx, cancel := context.WithCancel(context.Background()) defer cancel() c, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) require.NoError(t, err) defer c.Close() variants := make([]*dagger.Container, 0, len(platformToUname)) for platform := range platformToUname { ctr := c.Container(dagger.ContainerOpts{Platform: platform}). From("alpine:3.16.2"). 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) entries := tarEntries(t, dest) require.Contains(t, entries, "oci-layout") require.Contains(t, entries, "index.json") require.NotContains(t, entries, "manifest.json") } func TestContainerWithDirectoryToMount(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/container_test.go
t.Parallel() ctx := context.Background() c, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) require.NoError(t, err) defer c.Close() mnt := c.Directory(). WithNewDirectory("/top/sub-dir/sub-file"). Directory("/top") ctr := c.Container(). From("alpine:3.16.2"). WithMountedDirectory("/mnt", mnt) dir := c.Directory(). WithNewFile("/copied-file", "some-content") ctr = ctr.WithDirectory("/mnt/sub-dir/copied-dir", dir) contents, err := ctr.WithExec([]string{"find", "/mnt"}).Stdout(ctx) require.NoError(t, err) require.Equal(t, []string{ "/mnt", "/mnt/sub-dir", "/mnt/sub-dir/sub-file", "/mnt/sub-dir/copied-dir", "/mnt/sub-dir/copied-dir/copied-file", }, strings.Split(strings.Trim(contents, "\n"), "\n")) } var echoSocketSrc string func TestContainerWithUnixSocket(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/container_test.go
ctx := context.Background() c, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) require.NoError(t, err) tmp := t.TempDir() sock := filepath.Join(tmp, "test.sock") l, err := net.Listen("unix", sock) require.NoError(t, err) defer l.Close() go func() { for { c, err := l.Accept() if err != nil { if !errors.Is(err, net.ErrClosed) { t.Logf("accept: %s", err) panic(err) } return } n, err := io.Copy(c, c) if err != nil { t.Logf("hello: %s", err) panic(err) } t.Logf("copied %d bytes", n) err = c.Close() if err != nil { t.Logf("close: %s", err) panic(err) } }
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
}() echo := c.Directory().WithNewFile("main.go", echoSocketSrc).File("main.go") ctr := c.Container(). From("golang:1.20.0-alpine"). WithMountedFile("/src/main.go", echo). WithUnixSocket("/tmp/test.sock", c.Host().UnixSocket(sock)). WithExec([]string{"go", "run", "/src/main.go", "/tmp/test.sock", "hello"}) stdout, err := ctr.Stdout(ctx) require.NoError(t, err) require.Equal(t, "hello\n", stdout) t.Run("socket can be removed", func(t *testing.T) { without := ctr.WithoutUnixSocket("/tmp/test.sock"). WithExec([]string{"ls", "/tmp"}) stdout, err = without.Stdout(ctx) require.NoError(t, err) require.Empty(t, stdout) }) t.Run("replaces existing socket at same path", func(t *testing.T) { repeated := ctr.WithUnixSocket("/tmp/test.sock", c.Host().UnixSocket(sock)) stdout, err := repeated.Stdout(ctx) require.NoError(t, err) require.Equal(t, "hello\n", stdout) without := repeated.WithoutUnixSocket("/tmp/test.sock"). WithExec([]string{"ls", "/tmp"}) stdout, err = without.Stdout(ctx) require.NoError(t, err) require.Empty(t, stdout) }) } func TestContainerExecError(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/container_test.go
t.Parallel() ctx := context.Background() c, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) require.NoError(t, err) outMsg := "THIS SHOULD GO TO STDOUT" encodedOutMsg := base64.StdEncoding.EncodeToString([]byte(outMsg)) errMsg := "THIS SHOULD GO TO STDERR" encodedErrMsg := base64.StdEncoding.EncodeToString([]byte(errMsg))
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
t.Run("includes output of failed exec in error", func(t *testing.T) { _, err = c.Container(). From("alpine:3.16.2"). WithExec([]string{"sh", "-c", fmt.Sprintf( `echo %s | base64 -d >&1; echo %s | base64 -d >&2; exit 1`, encodedOutMsg, encodedErrMsg, )}). ExitCode(ctx) require.Error(t, err) require.Contains(t, err.Error(), outMsg) require.Contains(t, err.Error(), errMsg) }) t.Run("includes output of failed exec in error when redirects are enabled", func(t *testing.T) { _, err = c.Container(). From("alpine:3.16.2"). WithExec( []string{"sh", "-c", fmt.Sprintf( `echo %s | base64 -d >&1; echo %s | base64 -d >&2; exit 1`, encodedOutMsg, encodedErrMsg, )}, dagger.ContainerWithExecOpts{ RedirectStdout: "/out", RedirectStderr: "/err", }, ). ExitCode(ctx) require.Error(t, err) require.Contains(t, err.Error(), outMsg) require.Contains(t, err.Error(), errMsg) }) } func TestContainerWithRegistryAuth(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/container_test.go
t.Parallel() ctx := context.Background() c, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) require.NoError(t, err) defer c.Close() testRef := privateRegistryRef("container-with-registry-auth") container := c.Container().From("alpine:3.16.2") _, err = container.Publish(ctx, testRef) require.Error(t, err) pushedRef, err := container. WithRegistryAuth( privateRegistryHost, "john", c.Container(). WithNewFile("secret.txt", dagger.ContainerWithNewFileOpts{Contents: "xFlejaPdjrt25Dvr"}). File("secret.txt"). Secret(), ). Publish(ctx, testRef) require.NoError(t, err) require.NotEqual(t, testRef, pushedRef) require.Contains(t, pushedRef, "@sha256:") } func TestContainerImageRef(t *testing.T) { t.Parallel() t.Run("should test query returning imageRef", func(t *testing.T) { res := 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/container_test.go
Container struct { From struct { ImageRef string } } }{} err := testutil.Query( `{ container { from(address: "alpine:3.16.2") { imageRef } } }`, &res, nil) require.NoError(t, err) require.Contains(t, res.Container.From.ImageRef, "docker.io/library/alpine:3.16.2@sha256:") }) t.Run("should throw error after the container image modification with exec", func(t *testing.T) { res := 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/container_test.go
Container struct { From struct { ImageRef string } } }{} err := testutil.Query( `{ container { from(address:"hello-world") { exec(args:["/hello"]) { imageRef } } } }`, &res, nil) require.Error(t, err) require.Contains(t, err.Error(), "Image reference can only be retrieved immediately after the 'Container.From' call. Error in fetching imageRef as the container image is changed") }) t.Run("should throw error after the container image modification with exec", func(t *testing.T) { res := 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/container_test.go
Container struct { From struct { ImageRef string } } }{} err := testutil.Query( `{ container { from(address:"hello-world") { withExec(args:["/hello"]) { imageRef } } } }`, &res, nil) require.Error(t, err) require.Contains(t, err.Error(), "Image reference can only be retrieved immediately after the 'Container.From' call. Error in fetching imageRef as the container image is changed") }) t.Run("should throw error after the container image modification with directory", func(t *testing.T) { c, ctx := connect(t) defer c.Close() dir := c.Directory().
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
WithNewFile("some-file", "some-content"). WithNewFile("some-dir/sub-file", "sub-content"). Directory("some-dir") ctr := c.Container(). From("alpine:3.16.2"). WithWorkdir("/workdir"). WithDirectory("with-dir", dir) _, err := ctr.ImageRef(ctx) require.Error(t, err) require.Contains(t, err.Error(), "Image reference can only be retrieved immediately after the 'Container.From' call. Error in fetching imageRef as the container image is changed") }) } func TestContainerBuildNilContextError(t *testing.T) { t.Parallel() ctx := context.Background() c, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) require.NoError(t, err) defer c.Close() err = testutil.Query( `{ container { build(context: "") { id } } }`, &map[any]any{}, nil) require.ErrorContains(t, err, "invalid nil input definition to definition op") } func TestContainerInsecureRootCapabilites(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/container_test.go
c, ctx := connect(t) defer c.Close() ctx, cancel := context.WithCancel(ctx) defer cancel() out, err := c.Container().From("alpine:3.16.2"). WithExec([]string{"cat", "/proc/self/status"}). Stdout(ctx) require.NoError(t, err) require.Contains(t, out, "CapInh:\t0000000000000000") require.Contains(t, out, "CapPrm:\t00000000a80425fb") require.Contains(t, out, "CapEff:\t00000000a80425fb") require.Contains(t, out, "CapBnd:\t00000000a80425fb") require.Contains(t, out, "CapAmb:\t0000000000000000") out, err = c.Container().From("alpine:3.16.2"). WithExec([]string{"cat", "/proc/self/status"}, dagger.ContainerWithExecOpts{ InsecureRootCapabilities: true, }). Stdout(ctx) require.NoError(t, err) require.Contains(t, out, "CapInh:\t0000003fffffffff") require.Contains(t, out, "CapPrm:\t0000003fffffffff") require.Contains(t, out, "CapEff:\t0000003fffffffff") require.Contains(t, out, "CapBnd:\t0000003fffffffff") require.Contains(t, out, "CapAmb:\t0000003fffffffff") } func TestContainerInsecureRootCapabilitesWithService(t *testing.T) {