Skip to content

CLI: don't run a container's default args when printing it #7821

Closed

Description

@shykes

Context

Since #6515, the CLI prints objects returned by a pipeline. To do this, it calls certain functions in the returned object, specifically functions which 1) return a printable value, and 2) have no required arguments.

Problem

When a pipeline returns a Container object, the CLI calls Container.stdout() and Container.stderr(). In some cases1, this will unexpectedly execute a long-running service specified in the container's defaultArgs.

[1] normally, stdout and stderr return the result of the last withExec operation. But if there is no withExec in the container state, defaultArgs are executed as a fallback. That is what causes the unexpected behavior.

For example:

dagger call -m github.com/dagger/dagger/dev@3767c8f0b3076a41c218c545c96641080b0e819b \
 docs server
  • Expected behavior: print container metadata, and return control to the user
  • Actual behavior: execute the defaultArgs (in this case a long-running server), and hang forever

Solution

Manually exclude Container.stdout and Container.stderr from the list of functions to call when printing an object. If any other functions in Container cause it to be executed, exclude that too.

Change the behavior of Container.stdout and Container.stderr so that it doesn't fallback to executing defaultArgs. Also make sure its behavior no longer depends on how many withExecs are in the container spec.

Activity

  1. shykes commented on Jul 3, 2024

    @shykes
    ContributorAuthor
  2. helderco commented on Jul 4, 2024

    @helderco
    Contributor

    I guess that answers this, right? 🙂

    On the question of "expensive scalar functions: for example, Container.stdout() is "expensive" because it forces actually executing a container, in order to retrieve its standard output. Therefore, it is argued, the CLI should not blindly call all scalar functions of an object, because it may cause expensive computation which the user does not want. This is premature optimization. Let's ship the feature in a naive implementation; then see how important it actually is to avoid expensive computation. My guess is that, in most cases, it won't matter. For example, if I query a container, I don't care that it's actually executed. Or, if it turns out it's a problem, then we can implement a special case in the core type. I certainly won't care if my own custom types don't benefit from such a special case.

    - Originally posted by @shykes in #6515

  3. shykes commented on Jul 4, 2024

    @shykes
    ContributorAuthor

    @helderco yeah I didn't think about the case where executing the container runs a server and never returns...

    So it looks like I got this part wrong:

    For example, if I query a container, I don't care that it's actually executed.

    And it's time to do this part:

    Or, if it turns out it's a problem, then we can implement a special case in the core type. I certainly won't care if my own custom types don't benefit from such a special case.

    I still believe that last part is correct.

  4. sipsma commented on Jul 4, 2024

    @sipsma
    Contributor

    One note: we don't necessarily have to hardcode stdout/stderr as exceptions and keep playing whackamole forever. TypeDef distinguishes between Fields and Functions, where Fields are already calculated values that require no work to retrieve and functions require at least some work to calculate.

    It seems very reasonable to me to just print Fields and skip Functions. And specifically to only print Fields that have scalar values.

    The only mini-problem is that we don't distinguish between Fields and Functions in TypeDefs for core types yet, but that's a superficial problem (the underlying distinction does exist for core types, it's just not exposed here).

    If we fix that then the CLI doesn't need any hardcoding, it will all be controlled from the server-side schemas (including benefiting user module objects).

  5. helderco commented on Jul 4, 2024

    @helderco
    Contributor

    TypeDef distinguishes between Fields and Functions, where Fields are already calculated values that require no work to retrieve and functions require at least some work to calculate.

    It seems very reasonable to me to just print Fields and skip Functions. And specifically to only print Fields that have scalar values.

    That's what I did initially, but we decided to try it out without that distinction. See this comment, especially last paragraph:

  6. shykes commented on Jul 4, 2024

    @shykes
    ContributorAuthor

    @sipsma I feel pretty strongly that we should avoid leaking the field/not field distinction into the CLI behavior, so that module devs remain free to change a field to an accessor and vice versa, with the confidence that they're not affecting their callers in any way. Since graphql doesn't make the distinction, neither should the CLI in my opinion.

  7. shykes commented on Jul 4, 2024

    @shykes
    ContributorAuthor

    I wonder if future annotations related to cacheability, purity, etc. could be used as hints for this in the future somehow?

  8. sipsma commented on Jul 4, 2024

    @sipsma
    Contributor

    That's what I did initially, but we decided to try it out without that distinction. See:

    Right, but was there an actual issue with the Fields approach or did you just want to start w/ the "select all scalars" instead? Couldn't glean that from the comment.

    I feel pretty strongly that we should avoid leaking the field/not field distinction into the CLI behavior, so that module devs remain free to change a field to an accessor and vice versa, with the confidence that they're not affecting their callers in any way. Since graphql doesn't make the distinction, neither should the CLI in my opinion.

    I don't follow. The behavior we went with is to only select scalars that have no args. If a module author makes changes to a signature, they will be affecting whether or not the function gets autoselected. That seems perfectly reasonable to me.

    In the same way, fields and functions are quite different from one another from an author's perspective. So if we switch to the field approach to determine autoselection, then if they make a change that switches a field to a function or vice versa, they will be affecting whether or not the value gets autoselected. That also feels perfectly reasonable to me.

    That's on top of the fact that in the current approach, a user really needs to make sure they never write a Function that takes no args, returns a scalar and does something side-effectful like a deployment, which could result in that Function unexpectedly getting called implicitly when the CLI autoselects.

    • From that perspective, the field-only approach feels much safer actually.

    I wonder if future annotations related to cacheability, purity, etc. could be used as hints for this in the future somehow?

    We don't need future annotations; we already have it in the annotations on whether it's a field or a function (which gets wrapped up in TypeDef from the CLI's perspective).

    I also don't understand how that meshes with "graphql doesn't make the distinction, neither should the CLI in my opinion". Graphql doesn't make any distinctions around cacheability, purity, etc.

    But more than that, I don't see why users should care about whether some internal detail is an explicit part of the graphql spec or not. We are't violating the graphql spec in any way, we just have the TypeDef system layered on top and using gql annotations to provide extra metadata.

  9. helderco commented on Jul 4, 2024

    @helderco
    Contributor

    That's what I did initially, but we decided to try it out without that distinction. See:

    Right, but was there an actual issue with the Fields approach or did you just want to start w/ the "select all scalars" instead? Couldn't glean that from the comment.

    No problems with the Fields approach. When I said "I have a few issues with printing the fields", I meant "fields" here in reference to the issue's title ("print module object fields with dagger call"), so I was balancing option 1 with option 2.

    I actually implemented it at first with only selecting Fields. Then switched to the other option of just listing the Fields, and per Solomon's feedback returned to printing the values. That comment was made at this stage.

    As he said, he feels strongly about not making any distinction between Field and Function. My main problem, even to this day, is inadvertently triggering expensive computation. Particularly nervous about selecting a function like Deploy, without arguments, that just returns an error. This is evidenced in the last paragraph (but also earlier comments):

    So... my gut wants to limit selecting only state functions (fields) rather than all functions, and maybe hardcode which ones in a few core types.

    But... we had a chat and I agreed to give it a chance and see what the feedback is. That's what I meant by "but we won’t really know if we don’t put it out there and get feedback."

  10. shykes commented on Jul 4, 2024

    @shykes
    ContributorAuthor

    What I mean is that changing a field to a getter function doesn't change the schema, and that's a good thing. It allows me to keep the distinction between field and getter function hidden from my callers.

    I understand how using field/not-field as a print heuristic is appealing, because a scalar field is always safe to print. But some scalar getter functions are safe to print too, and I worry that if I refactor a field into a getter function (which I do often) I will be "penalized" by my getter function disappearing from the CLI output.

  11. helderco commented on Jul 4, 2024

    @helderco
    Contributor

    Particularly nervous about selecting a function like Deploy, without arguments, that just returns an error.

    Well, I actually forgot I exclude Void (wouldn't make sense to print a void anyway):

    dagger/cmd/dagger/module.go

    Lines 1121 to 1122 in b856383

    case dagger.ObjectKind, dagger.InterfaceKind, dagger.VoidKind:
    continue

    But the worry is still there if the Deploy returns a string like the published URL or something.

  12. sipsma commented on Jul 4, 2024

    @sipsma
    Contributor

    But... we had a chat and I agreed to give it a chance and see what the feedback is. That's what I meant by "but we won’t really know if we don’t put it out there and get feedback."

    I see, and yeah to be clear I don't think it was wrong to start there or obvious either way. I am just advocating for switching this because:

    1. It avoids the whack-a-mole of hardcoding skips of core APIs that trigger execution (which goes well beyond stdout/stderr, both now and in the future) in a consistent way. That's a meaningful maintenance burden otherwise.
    2. It avoids the problem of users writing side-effectful functions that get implicitly and unobviously triggered when their function happens to have a signature that matches the current autoselection logic. Again in a consistent and obvious way for module authors (it's very obvious in the current DX whether something is a Field or a Function).

    But some scalar getter functions are safe to print too, and I worry that if I refactor a field into a getter function (which I do often) I will be "penalized" by my getter function disappearing from the CLI output.

    Yeah that makes sense, I agree it would be nice if users had even more fine-grain control here (and that is possible to add too).

    But just speaking in terms of the current state of things, we do need a heuristic here, which always means tradeoffs. And for me personally the potential surprise of a side-effectful Function getting triggered by autoselection if it happens to match our current logic outweighs the cost of not seeing it get autoselected. That's obviously just IMO though

    The whole Field/Function difference exists for essentially all of the reasons we're talking about here. Some values are pre-calculated and can be returned without any work (or even an "actual" function invocation) and some require work and an actual function call. So it seems like a very safe behavior to start with in terms of our autoselection heuristics (besides avoiding the aforementioned whackamole) and can be improved iteratively by giving users more fine grain control.


    Either way, that's just my 2c, I totally agree there's tradeoffs either way. The only thing I'd be firmly adamant about is that if we keep the current behavior and do the whackamole, we need to document and/or otherwise make it very very clear that Functions with no required args that return a scalar will get selected, and thus side-effectful user Functions should not have that signature.

  13. helderco commented on Jul 4, 2024

    @helderco
    Contributor

    I understand how using field/not-field as a print heuristic is appealing, because a scalar field is always safe to print. But some scalar getter functions are safe to print too, and I worry that if I refactor a field into a getter function (which I do often) I will be "penalized" by my getter function disappearing from the CLI output.

    That's a good point. In Python we could use a property for that:

    @object_type
    class Test:
        @function
        @property  # allows calling this as an attribute (aka field): `self.simple_scalar`
        def simple_scalar(self) -> str:
            return "not too bad computation"

    in TypeScript you have getters:

    @object()
    class Test {
      @func
      get simpleScalar() string {  // notice the `get` before the function name
        return "not too bad computation"
      }
    }
  14. sipsma commented on Jul 4, 2024

    @sipsma
    Contributor

    I understand how using field/not-field as a print heuristic is appealing, because a scalar field is always safe to print. But some scalar getter functions are safe to print too, and I worry that if I refactor a field into a getter function (which I do often) I will be "penalized" by my getter function disappearing from the CLI output.

    That's a good point. In Python we could use a property for that:

    @object_type
    class Test:
        @function
        @property  # allows calling this as an attribute (aka field): `self.simple_scalar`
        def simple_scalar(self) -> str:
            return "not too bad computation"

    in TypeScript you have getters:

    @object()
    class Test {
      @func
      get simpleScalar() string {  // notice the `get` before the function name
        return "not too bad computation"
      }
    }

    I'd be all for that sort of approach, provided the default is to not select Functions unless they have that explicit annotation as a getter. Fields are essentially our "getters" today in my mind, but it makes sense to be less rigid there, especially in languages that already have these notions like you listed.

  15. helderco commented on Jul 4, 2024

    @helderco
    Contributor

    I'd be all for that sort of approach, provided the default is to not select Functions unless they have that explicit annotation as a getter.

    Yes, that's why I brought it up. Go doesn't have this concept though.

    Fields are essentially our "getters" today in my mind, but it makes sense to be less rigid there, especially in languages that already have these notions like you listed.

    There's already a disconnect in other places. Some parameters are optional to Dagger but required in the language for example. I don't see a disconnect here however. The @property descriptor is very well used in Python to make a simple function (bad practice to put expensive operations there) be used like an object attribute. So Dagger would see this function as if it were an attribute like the other "fields", just like end users of the class.

  16. 19 remaining items

  17. self-assigned this
    on Jul 9, 2024
  18. shykes commented on Jul 9, 2024

    @shykes
    ContributorAuthor

    I understand how using field/not-field as a print heuristic is appealing, because a scalar field is always safe to print. But some scalar getter functions are safe to print too, and I worry that if I refactor a field into a getter function (which I do often) I will be "penalized" by my getter function disappearing from the CLI output.

    That's a good point. In Python we could use a property for that:

    @object_type
    class Test:
        @function
        @property  # allows calling this as an attribute (aka field): `self.simple_scalar`
        def simple_scalar(self) -> str:
            return "not too bad computation"

    in TypeScript you have getters:

    @object()
    class Test {
      @func
      get simpleScalar() string {  // notice the `get` before the function name
        return "not too bad computation"
      }
    }

    I'd be all for that sort of approach, provided the default is to not select Functions unless they have that explicit annotation as a getter. Fields are essentially our "getters" today in my mind, but it makes sense to be less rigid there, especially in languages that already have these notions like you listed.

    Do we really need another annotation? We're already planning to make functions cacheable by default, with a special annotation for them to not be cached. That no-cache annotation already covers all the functions you may want to exclude from an auto-print: deploy, publish, everything with a side effect basically.

    Restricting to fields only, without any dynamic functions allowed, is super restrictive and immediately limits how I can structure my modules. Here is an example of a module that would not work with that restriction: https://github.com/dagger/dagger/pull/7803/files#diff-ede2977d7a59efd13edd531b5bc04e014d632dcaa127606df3284d1b678c5cb6

  19. helderco commented on Jul 9, 2024

    @helderco
    Contributor

    Do we really need another annotation? We're already planning to make functions cacheable by default, with a special annotation for them to not be cached. That no-cache annotation already covers all the functions you may want to exclude from an auto-print: deploy, publish, everything with a side effect basically.

    To be fair, it's not another Dagger annotation. It's applying the same @dagger.function to another native feature of the language.

    But I agree that the cache control can solve your issue and address Erik's concerns. However, until we have that cache control, users won't be able to "opt-out" of certain functions being printed undesirably.

    What's better?

    • Unrestricted functions now, possibly frustrating some users, promising to give cache control soon?
    • Restricted to state now and expand later with cache control?

    Restricting to fields only, without any dynamic functions allowed, is super restrictive and immediately limits how I can structure my modules. Here is an example of a module that would not work with that restriction: https://github.com/dagger/dagger/pull/7803/files#diff-ede2977d7a59efd13edd531b5bc04e014d632dcaa127606df3284d1b678c5cb6

    That's interesting. I can think of a way to restructure so that you end up with the things you want to print as primitives in an object, but it feels very restraining to work around this limitation.

    I'm actually ok trying out unrestricted functions and escalating function cache. I can see how it can be an issue for some but I don't know enough about users' use cases. What if we make a poll on #General in Discord?

  20. shykes commented on Jul 9, 2024

    @shykes
    ContributorAuthor

    Do we really need another annotation?
    To be fair, it's not another Dagger annotation. It's applying the same @dagger.function to another native feature of the language.

    In Go, there is no annotation for Dagger Functions: an object's method is automatically registered as a dagger function.

    And in Typescript I'm not sure how it would go.

    Overall it just feels like a lot of concepts to explain, when we already have quite a lot.

    But I agree that the cache control can solve your issue and address Erik's concerns. However, until we have that cache control, users won't be able to "opt-out" of certain functions being printed undesirably.

    Yes that's true.

    What's better?

    • Unrestricted functions now, possibly frustrating some users, promising to give cache control soon?

    • Restricted to state now and expand later with cache control?

    I think unrestricting functions is better. The frustration is only hypothetical: today we don't print anything when your pipeline returns an object. So nobody relies on this new behavior yet. Worst case, some users can't adopt the new behavior for some of their types. They have the choice between waiting for cache control, or tweaking their API. Either way there is no breaking change.

    Restricting to fields only, without any dynamic functions allowed, is super restrictive and immediately limits how I can structure my modules. Here is an example of a module that would not work with that restriction: https://github.com/dagger/dagger/pull/7803/files#diff-ede2977d7a59efd13edd531b5bc04e014d632dcaa127606df3284d1b678c5cb6

    That's interesting. I can think of a way to restructure so that you end up with the things you want to print as primitives in an object, but it feels very restraining to around around this limitation.

    Yeah it's definitely possible, just unreasonably restrictive.

    I'm actually ok trying out unrestricted functions and escalating function cache. I can see how it can be an issue for some but I don't know enough about users' use cases. What if we make a poll on #General in Discord?

    I don't think we need a poll for this, seems like overkill.

    By the way, we could introduce the nocache annotation before actually implementing function caching. So from a caching perspective it would be a noop initially, but it could be used as a stopgap to mean "no print".

  21. helderco commented on Jul 9, 2024

    @helderco
    Contributor

    @shykes, the default args exec on stdout was added because of:

    Can you confirm if the reason for adding it in the first place no longer applies?

    It was to resolve a source of confusion in users:

    The typical workaround is to add an explicit withExec(nil), which is very confusing.

    However, if the container has either an entrypoint or defaultArgs defined - that should be used as a default command. The fact that it is not, is counter-intuitive and confusing.

  22. shykes commented on Jul 9, 2024

    @shykes
    ContributorAuthor

    @shykes, the default args exec on stdout was added because of:

    😭

  23. shykes commented on Jul 9, 2024

    @shykes
    ContributorAuthor

    I updated the issue description to be up-to-date with the last discussions.

  24. helderco commented on Jul 9, 2024

    @helderco
    Contributor

    I updated the issue description to be up-to-date with the last discussions.

    Need another update? We left the discussion on the other solution, right? Manually excluding stdout and stdout for now rather than change the behavior in the API with the implicit exec?

  25. shykes commented on Jul 9, 2024

    @shykes
    ContributorAuthor

    Need another update? We left the discussion on the other solution, right? Manually excluding stdout and stdout for now rather than change the behavior in the API with the implicit exec?

    I was wondering if I should create a separate issue - but you're right, I'll leave everything here for simplicity.

    My preference would be to do both:

    1. Remove stdout and stderr from autoprint
    2. Also remove the reliance on implicit exec. Even know we found a way to not step on the landmine this time, we should still remove it now that we know it's there. No?
  26. helderco commented on Jul 9, 2024

    @helderco
    Contributor

    Yeah, I agree. On both counts.

  27. changed the title [-]CLI: don't trigger container execution when printing a Container object[/-] [+]CLI: don't run a container's default args when printing it[/+] on Jul 9, 2024
  28. sipsma commented on Jul 9, 2024

    @sipsma
    Contributor

    That no-cache annotation already covers all the functions you may want to exclude from an auto-print: deploy, publish, everything with a side effect basically.

    I don't think this is going to be the case in general. We also want to add cache TTLs which, once added, would likely be used for side-effectful functions. We could make the rule that we only autoselect functions that are cached with no TTL, but at that point the connection seems extremely unobvious and like we are conflating two unrelated concepts.

    Separately, there's pretty easy to imagine scenarios that A) have nothing to do with caching and B) are cases where you wouldn't want functions to be autoselected, e.g.

    type TestSuites struct {
      FooSetting bool
      BarSetting string
    }
    
    // Run all of the tests using settings in TestSuites
    func (t TestSuites) AllTests() (string, error) {
       // ...
    }
    
    // Run some subset of the tests using settings in TestSuites
    func (t TestSuites) SomeSubsetOfTests() (string, error) {
       // ...
    }

    In that case, caching is not a consideration (I'd be happy with default caching once that's enabled), but I would not want to run both AllTests and SomeSubsetOfTests automatically (there would be overlapping test execution, which would get worse as more methods are added). I'd be perfectly happy however if autoselection on TestSuites just printed the settings for the test suites and tests only actually ran if I explicitly selected the function.

    • And I'd be even happier if I'm a more advanced user who wanted fine grain control and had an annotation I could put on one of the Functions that resulted in it getting autoselected and thus executed.

    I like the separate annotation for enabling a function to be autoselected because:

    1. In the "simple"/"default" case where users aren't thinking about any of this, the behavior is safe (only print values on the object that are already calculated anyways, i.e. fields)
    2. In more advanced cases, where the user wants fine grain control, you have to add an annotation (just a single line of code on a Function

    The general principle being "default case is safe+obvious + easy knobs for more advanced cases"

    I think unrestricting functions is better. The frustration is only hypothetical: today we don't print anything when your pipeline returns an object. So nobody relies on this new behavior yet. Worst case, some users can't adopt the new behavior for some of their types. They have the choice between waiting for cache control, or tweaking their API. Either way there is no breaking change.

    Let me double check I'm fully following everything. If we did a release today, there would functions that previously did not run unless explicitly selected and now will run automatically in some cases (when they have the right signature and end up getting autoselected). That would be a breaking change right?

    I realize that the number of users practically impacted by this is likely to be small, but it's plausibly non-zero and since it involves avoiding surprising/potentially-unsafe behavior it feels worth it to me to go out of our way to avoid it (even if that means adding more annotations or other potential solutions).

  29. helderco commented on Jul 10, 2024

    @helderco
    Contributor

    @sipsma, do you mind creating a new issue re: limiting the functions selection to state?

  30. removed this from the v0.12.0 milestone on Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions