#Are workspace APIs expensive?

1 messages ยท Page 1 of 1 (latest)

solid narwhal
#

I'm trying to cleanup my build by using Workspace APIs from 0.20.5.
We use asdf and mise to control versions of our tools, and I'd like the build to reuse the same metadata.
The code looks something like:

func VersionOf(toolName string, defaultVersion string, ctx context.Context, workspace *dagger.Workspace) (string, error) {
    wsPath, err := workspace.Path(ctx)
    if err != nil {
        return "", err
    }
    fnames := []string{".tool-versions", ".mise.local.toml", ".mise.toml"}
    curDir := wsPath
    for {
        for _, fname := range fnames {
            ffname := filepath.Join(curDir, fname)
            fileContents, err := workspace.File("/" + ffname).Contents(ctx)
            if err == nil {
              ...
            }
        }

        if curDir == "" || curDir == "." {
            break
        }
        curDir = filepath.Dir(curDir)
    }
    if defaultVersion == "" {
        return defaultVersion, fmt.Errorf("no %s version found in %s", toolName, wsPath)
    }
    return defaultVersion, nil
}

It may be just my imagination but the call workspace.File("/" + ffname).Contents(ctx) seems to be trying to sync the entire directory of the file so it's prohibitely expensive.
All these calls are meant to be done during module initialization, is this a valid use-case for the API?

fresh jackal
#

I can't vouch for that for loop of yours though ๐Ÿ™‚

solid narwhal
#

wdym? It's a findUpMany loop, best loop ever ๐Ÿ™‚

fresh jackal
#

Not saying it's bad, just can't guarantee its' not the source of your perceived slowdown

#

Also you shouldn't need workspace.Path anymore, I think I forgot to deprecate that.

#

Absolute paths are rooted in the git boundary

solid narwhal
#

the TUI was showing workspace.Directory calls which is why I asked

#

I look for files from current path and up

fresh jackal
solid narwhal
#

no it was showing workspace.Directory under workspace.File, which spooked me

fresh jackal
#

Any chance you could share a trace? That would be super super helpful

solid narwhal
#

how do I do that?

fresh jackal
#

Press w from the TUI. It will either open the web version of the trace, if already setup. Or, send you to the setup page

solid narwhal
#

before I read that I tried --progress=plain, and this is what I see:

...
13  : โ”† Workspace.file(path: "/src/galaxy/tools/fleet-cli/.tool-versions"): File!
14  : โ”† โ”† Directory.file(path: ".tool-versions"): File!
14  : โ”† โ”† Directory.file ERROR [0.0s]
14  : โ”† โ”† ! stat /.tool-versions: no such file or directory
#

notice that second line.
I'll try to do a proper trace now

#

there is a chance that call is being made if the file is missing

#

not sure I can do a dagger cloud account from work, the trace requires it