Repository navigation
Host export support for project commands - #5157
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
|
Had to pivot to some other things for a while, but finally got back and updated this with the simpler, less ambitious approach described in the updated PR description. cc @shykes I think this approach is a decent solution to the issue you brought up around making it convenient for callers to export to the project dir. The crux is that while |
There was a problem hiding this comment.
cc @vito I added the ability for a file export path to just be a parent dir on the host, with the file name being set to whatever the file's name is in LLB. I threw it in this PR because it greatly simplified support for the new --output flag on dagger do.
I have some vague recollection of discussing this, but I don't remember the context at all. I just wanted to double check with you that it's not that there's some obscure case where this is a bad idea or something.
There was a problem hiding this comment.
Here's one dragon I found in the prior discussion: #3348 (comment)
Oh, yikes, the Buildkit exporter actually seems to do a os.RemoveAll of the destination directory in order to replace it with a file. So that actually "works" but doesn't do NEARLY what the user wanted. 😱
Is that still happening? 😄
There was a problem hiding this comment.
Oh right! I knew there was something weird around this but couldn't remember or find it, thank you for digging that up!
Interestingly, that isn't the behavior I'm seeing. I can export a single file into an existing directory and the rest of the existing contents are left alone. I'll poke around a bit to check there's not corner cases with undesirable behavior though.
There was a problem hiding this comment.
I went all over the place on what type to use here. I settled on String because it's simple and possible w/out major codegen updates. It also is logical in that if you were to use graphql introspection to get the type of the underlying resolver or to query for __typename, it would also be a string.
Looked into graphql unions+interfaces. Interfaces in particular are kind of tempting, but it's too much to handle updating each SDK w/ interface support to make sense right now.
Enums might make sense here too, but I wasn't sure how much of a difference it'd make or it it would be "non-graphql-y" to create an Enum for all the types that exist in the schema anyways. Open to this option still though.
There was a problem hiding this comment.
Seems good to me. 👍 Agree that interfaces would be nice, but no need to block.
It's almost like we want to just query like this:
{
build {
... on File {
export(path: "/foo", allowParentDirPath: true)
}
... on Directory {
export(path: "/foo")
}
}
}...but that would require build to return an interface, instead of the concrete return type that we learn from the code, which doesn't seem worthwhile for internal magic/plumbing.
There was a problem hiding this comment.
Yeah agree it's not worth it yet, but if we hit any more cases that would benefit from interface support we should definitely consider adding it. Could be pretty useful potentially.
There was a problem hiding this comment.
@vito I noticed this as I was just backfilling tests for project dirs being imported; I am setting the silent flag when invoking dagger do in the integ tests but I was still getting progress output.
Was setting LogOutput even when this is set intentional? I may have just misunderstood the docs for it of disable terminal UI and progress output
There was a problem hiding this comment.
Eh actually this broke something else, so I reverted and am leaving in those lines for now. Can address separately if updates are needed.
There was a problem hiding this comment.
Haha, I saw this a bit ago and realized it's probably a mistake. It's because I conflated "disable TUI" with --silent which feels totally wrong in hindsight. --silent should really mean silent, and we probably need some other flag to force console output. I'm working in this area already so I can fix it up along the way, but feel free to make any changes needed to unblock this PR.
This comment was marked as outdated.
This comment was marked as outdated.
3acdec7 to
041d546
Compare
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
vito
left a comment
There was a problem hiding this comment.
Cool stuff! Tried everything out locally.
| if !silent { | ||
| cmd.SetOut(vtx.Stdout()) | ||
| cmd.SetErr(vtx.Stderr()) | ||
| } |
There was a problem hiding this comment.
FYI I fixed --silent in #5297 - now it really means silent. The previous behavior is now --progress=plain instead. I think this'll be redundant once the fix is merged, but it's harmless.
| return nil, fmt.Errorf("failed to get cmd id: %w", err) | ||
| } | ||
| projCmd = *c.ProjectCommand(dagger.ProjectCommandOpts{ID: dagger.ProjectCommandID(projCmdID)}) | ||
| projCmd = *c.ProjectCommand(dagger.ProjectCommandOpts{ID: projCmdID}) |
There was a problem hiding this comment.
Curious about the * here since we don't typically do that with SDK objects. Is it needed?
There was a problem hiding this comment.
Yeah it's due to the bug described in the comment above:
// TODO: this shouldn't be needed, there is a bug in our codegen for lists of objects. It should
// internally be doing this so it's not needed explicitly
Actually, I think I forgot to make an issue for fixing this, I'll add that to the backlog
There was a problem hiding this comment.
Seems good to me. 👍 Agree that interfaces would be nice, but no need to block.
It's almost like we want to just query like this:
{
build {
... on File {
export(path: "/foo", allowParentDirPath: true)
}
... on Directory {
export(path: "/foo")
}
}
}...but that would require build to return an interface, instead of the concrete return type that we learn from the code, which doesn't seem worthwhile for internal magic/plumbing.
| ) | ||
| } | ||
|
|
||
| func TestFile(ctx dagger.Context, prefix string) (*dagger.File, error) { |
There was a problem hiding this comment.
Is it possible to return a *dagger.File with no error? I think that'll actually be somewhat common for commands that have no effects and just build up a DAG.
There was a problem hiding this comment.
Not yet, but we could support that pretty easily too. I'll make a backlog issue for that one also
| "directory": router.ToResolver(s.directory), | ||
| }, | ||
| "Directory": router.ObjectResolver{ | ||
| "Directory": router.ToIDableObjectResolver(core.DirectoryID.ToDirectory, router.ObjectResolver{ |
There was a problem hiding this comment.
TIL parentheses are optional in (core.DirectoryID).ToDirectory
There was a problem hiding this comment.
Yeah I almost didn't believe this when my IDE didn't report any errors, but it worked!
| if err != nil { | ||
| return fmt.Errorf("failed to get absolute path of output path: %w", err) | ||
| } | ||
| switch projResultType { |
There was a problem hiding this comment.
I really like that it exports for you automatically. The Bass approach to this, until recently, was for scripts to print JSON encoded File/Directory equivalents to stdout, which had to be exported like ./bass/build | bass --export. This led to a mess of tiny Bash wrapper scripts, which I cleaned up later by just adding explicit support for (write)ing to host paths in v0.12.0. It's nice that we're instead able to leverage the schema here and have typed return values lead to the behavior you'd want most of the time.
I guess the only catch will be when folks want commands to output to e.g. dist/ or out/. They can use --output obviously, but that'd probably quickly be wrapped with a Bash script. Or you can have your command return an "overlay" like ./dist/foo. Or maybe we can support explicit exports someday?
There was a problem hiding this comment.
I guess the only catch will be when folks want commands to output to e.g. dist/ or out/. They can use --output obviously, but that'd probably quickly be wrapped with a Bash script. Or you can have your command return an "overlay" like ./dist/foo. Or maybe we can support explicit exports someday?
Yeah that's true, I won't be incredibly saddened if users have to make use of the --output flag for those sorts of use cases, but we could definitely try to make it less painful. Maybe a DAGGER_DEFAULT_OUTPUT env would help sometimes? Though unless you set that in your .bashrc or equivalent it's probably more overhead than it's worth.
We definitely can support explicit exports from commands someday, that's what the older iteration of this PR did (if you look at the first edit of the PR description you can see the details). I backed off because
- It is quite tricky to get a comprehensible API for differentiating between which "host" you want to export to (the command's host? the caller's host? etc.). It's also tricky to do this in a backwards compatible way.
- The implementation is a mess since it involves interaction across sessions. The idea to move the session to a frontend would fix this, but obviously too much to bite off for the moment.
That option is still open for us though, it could easily co-exist with the approach implemented in this PR if desired.
There was a problem hiding this comment.
I guess the only catch will be when folks want commands to output to e.g. dist/ or out/. They can use --output obviously, but that'd probably quickly be wrapped with a Bash script. Or you can have your command return an "overlay" like ./dist/foo. Or maybe we can support explicit exports someday?
You can frame this problem as who and where:
-
Q. who is responsible for the logic that modifies the project directory?
-
A. The author of the command.
-
Q. where should that logic live?
-
A. In the command code and nowhere else
-
Q. I'm using a command, and want to change the logic (for example I want it to write to a
./outdirectory) -
A. Change the code of the command, or write a different command and use that
For the above to work, we want to avoid "pseudo-code" living outside the command code (for example a wrapper bash script). And for that to happen, we need a rigid convention around the command. In this case: "a command can modify the project directory any way it wants."
There was a problem hiding this comment.
In this case: "a command can modify the project directory any way it wants."
Yep, and that's the behavior now if you don't specify --output, the file/dir just gets overlayed onto your project dir. --output is really only needed for special cases. Good to have but the default of the command writing to the project dir is probably what users will do the vast majority of the time.
Also, I just realized that I misread @vito's original comment, I thought you were talking about the case where you want to write outside the project dir, but I think you're referring to writing to dirs under it.
If so, yeah if the command has a file but wants to ensure it's always written explicitly under ./out, then it can do this:
return ctx.Client().Directory().WithFile("out/foo.txt", theFile)Which I think is fine. We avoid that boilerplate when you just want to return a single File with no explicit parent dirs, but if you really want an explicit parent dir, then it feels okay to ask for this from the user.
We should optimize WithFile to use mergeop and maybe even consider some extra sugar for this pattern, but those would all be improvements to the client SDKs.
Co-authored-by: Alex Suraci <[email protected]> Signed-off-by: Erik Sipsma <[email protected]>
gerhard
left a comment
There was a problem hiding this comment.
A lot of great changes here, especially liked the integration tests.
This change alone would make for a cracking release!
Before this, commands could access the "project dir" since it was their own local working dir in the container, but they could not export anything to the host.
This adds support for project commands to return
dagger.Fileanddagger.Directoryobjects. When invoked fromdagger do, these files/dirs will be exported to the caller's host filesystem with the following behavior:dagger.Fileforfoo.txt, that will be written to<project-dir>/foo.txtdagger.Directory, it's contents will be overlayed onto<project-dir>git://), then an error will be returned. Instead, the caller will have to use the--outputflag described belowdagger doalso supports an optional--outputflag now too, which enables callers to explicitly set where the file or dir will be exported.git://projects, this flag must be setAt this time, commands still only have access to the project dir inside the container, they cannot access arbitrary directories on the host.
dagger.Directory.dagger do --project git://github.com/dagger/go --output ./bin build --src ./path/to/go/src/code, where--srcis a non-project dir loaded from the caller's host to be available to the commandExamples
Currently, the best examples are the ones used by the integration tests added in this PR here. They are pretty minimal, planning on updating with more before merge.
After pulling down this PR and running
./hack/devto build the new cli, you can run:./bin/dagger do -c ./core/integration/testdata/projects/go/basic testFilefoo.txtfile created in the root of the repo, as specified here./bin/dagger do -c ./core/integration/testdata/projects/go/basic testDirFor both of those commands, you can also specify
./bin/dagger do -c ./core/integration/testdata/projects/go/basic --output <some dir>to override the default behavior of putting files in the local project dir.Current Caveats
TODOs
Before merge
Longer-term
c.Host().Directory(".")hoop, e.g. maybectx.ProjectDir().