Skip to content

sdks: Sync doesn't always work as expected with certain APIs #7788

Closed

Description

@sipsma

A tangential issue I noticed during some adventures with #7767

At least in the Go SDK, the generated Sync method will just return the query it has built so far, e.g.

dagger/sdk/go/dagger.gen.go

Lines 2102 to 2106 in adca4e6

func (r *Directory) Sync(ctx context.Context) (*Directory, error) {
q := r.query.Select("sync")
return r, q.Execute(ctx)
}

However, this does not play nicely with our core APIs that rely on returning a dagql.Instance of a different API call, e.g.

dagger/core/host.go

Lines 63 to 69 in adca4e6

func (host *Host) Directory(
ctx context.Context,
srv *dagql.Server,
dirPath string,
pipelineNamePrefix string,
filter CopyFilter,
) (dagql.Instance[*Directory], error) {

While Sync is correctly called, the SDK doesn't use the returned ID and instead uses the query it already built up.

This is especially wrong in the case of impure APIs, since it will result in the API being repeatedly called incorrectly with the "original" ID.

  • e.g. if the query is to load a local dir and sync is called, then any subsequent calls on the returned Directory will re-run the load of the local dir.

In terms of the practical impact today, not 100% sure, it did break new APIs I was adding in #7767 but:

  1. I'm not sure if it causes significant incorrect/unperformant behavior for existing core APIs. It seems possible but would require more tracing of code and testing
  2. I only looked at the Go SDK so far, not sure what others are impacted if any

cc @vito @helderco @jedevc

Activity

  1. added
    kind/bug
    area/engine
    on Jun 29, 2024
  2. added this to the next milestone on Jun 29, 2024
  3. helderco commented on Jun 29, 2024

    @helderco
    Contributor

    It was done this way because at the time we didn't have a good way to convert any ID to its corresponding object. That changed with the loadXFromID fields.

    I left a comment in codegen, but was removed in #5881 by 7393e19#diff-de9a6011ccf4884745982bf7658dae4ec2e8d2564dd5a5b1063ef7dbe5678de7L145-L153:

    // FIXME: We don't have a simple way to convert any ID to its
    // corresponding object (in codegen) so for now just return the
    // current instance. Currently, `sync` is the only field where
    // the error is what we care about but more could be added later.
    // To avoid wasting a result, we return the ID which is a leaf value
    // and triggers execution, but then convert to object in the SDK to
    // allow continued chaining. For this, we're assuming the returned
    // ID represents the exact same object but if that changes, we'll
    // need to adjust.
    
  4. helderco commented on Jun 29, 2024

    @helderco
    Contributor

    This was fixed in Python in #5353, with this change:

    f799e77#diff-456e93caaf55f83fa963bff05f16413d26c0bfc01505ee73c7753b915ff22a55L403-R450

    This was made possible by mapping out which fields would take an ID and return the corresponding object:

    # Collect fields under Query that receive an id argument and return
    # an object type that also has an id field that returns the same
    # id type as the Query field argument.
    #
    # Example:
    # `Query.directory(id: DirectoryID): Directory` matches
    # `Directory.id(): DirectoryID`
    #
    # Used to create a classmethod that returns a Directory instance
    # from a DirectoryID by telling us which field to query for.
    id_query_map: IDQueryMap = {}

    This logic was simplified later by #6184.

    So it's easy to fix in Go and TS now, because of the new convention.

  5. wingyplus commented on Jun 29, 2024

    @wingyplus
    Contributor

    I just realize that elixir return an id, not a structure to the caller. Which is definitely a bug in the codegen. I will submit a PR to fix this and make sure the api align with other sdk in 2 or 3 days.

  6. self-assigned this
    on Jul 10, 2024
  7. added
    kind/consistency
    on Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/enginearea/sdkkind/bugkind/consistency

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions