Skip to content

Host export support for project commands - #5157

Merged
sipsma merged 12 commits into
dagger:mainfrom
sipsma:host
Jun 15, 2023
Merged

sipsma merged 12 commits into
dagger:mainfrom
sipsma:host

Conversation

@sipsma

@sipsma sipsma commented May 20, 2023 •

Copy link
Copy Markdown
Contributor

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.File and dagger.Directory objects. When invoked from dagger do, these files/dirs will be exported to the caller's host filesystem with the following behavior:

  • By default, files+dirs will be written under the local project directory
    • So if a command returns a dagger.File for foo.txt, that will be written to <project-dir>/foo.txt
    • If a command returns a dagger.Directory, it's contents will be overlayed onto <project-dir>
    • If the project was not loaded from a local directory (i.e. it was specified using git://), then an error will be returned. Instead, the caller will have to use the --output flag described below
  • dagger do also supports an optional --output flag now too, which enables callers to explicitly set where the file or dir will be exported.
    • And in the case of git:// projects, this flag must be set

At this time, commands still only have access to the project dir inside the container, they cannot access arbitrary directories on the host.

  • However, we can pretty easily add support for this either in this PR or in a follow up by enabling commands to accept args of type dagger.Directory.
  • This would be conceptually similar to the previous iteration of this PR, but in a way that's not a huge breaking API change.
  • The most compelling use case for this is with git projects, e.g.
    • dagger do --project git://github.com/dagger/go --output ./bin build --src ./path/to/go/src/code, where --src is a non-project dir loaded from the caller's host to be available to the command

Examples

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/dev to build the new cli, you can run:

./bin/dagger do -c ./core/integration/testdata/projects/go/basic testFile

  • you'll see a foo.txt file created in the root of the repo, as specified here

./bin/dagger do -c ./core/integration/testdata/projects/go/basic testDir

  • you'll see a few subdirs/files created in the root of the repo, as specified here

For 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

  1. As mentioned above, no ability for callers to pass in arbitrary directories from their host yet. Command code can only access the project dir loaded into their container. But we have multiple options for enabling this if/when desired.
    • As a result, the use cases described in this comment won't be supported yet. But based on conversations w/ Joe after that comment this may be okay as his use cases may be better addressed by other longer term features.
  2. Due to the fact that local dirs are tied to a session and command containers are short-lived, if a command loads a file/dir from its host (i.e. in the container) and tries to return those, an error will result.

TODOs

Before merge

  • Add tests for git projects, including error case where --output is not explicitly set
  • Add tests that verify the project dir is imported to the command implementation (very simple, but may as well have test coverage)
  • Update the internal dev docs on projects for the new requirements that commands should support exporting Files/Directories
  • Fix the various implementation kludge noted w/ TODOs in the code and graphql docs. Main thing is it would be nice to avoid hardcoding type names all over the place and instead use a more central/authoritative source, but strictly speaking this is more just nice-to-have.

Longer-term

  • Consider support for callers passing in arbitrary host dirs to commands
  • Add ability for command code to access its own project dir without having to jump through the whole c.Host().Directory(".") hoop, e.g. maybe ctx.ProjectDir().
    • This is both for convenience, better performance, and to enable partial workarounds for caveat 2 mentioned above.
    • I originally wanted to do this in this PR, but then remembered it requires all the craziness present in the previous iteration of this PR to enable local dirs to be seamlessly passed from host->project container. We can absolutely do that, but I'd prefer to defer since it could also be addressed by the below bullet point
  • Consider the rearchitecture proposed here to fix corner cases involving file/dir export

@sipsma

This comment was marked as outdated.

@sipsma

This comment was marked as outdated.

@sipsma

sipsma commented Jun 7, 2023

Copy link
Copy Markdown
Contributor Author

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 dagger do callers can explicitly set an --output dir, if they don't then it defaults to the project dir on the host. And for the corner case of git:// projects, they just get an error telling them they have to explicitly set --output.

Comment thread core/schema/file.graphqls Outdated

@sipsma sipsma Jun 7, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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? 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread core/schema/project.graphqls Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sipsma
sipsma marked this pull request as ready for review June 9, 2023 17:21
@sipsma sipsma changed the title WIP host support for project commands Host export support for project commands Jun 9, 2023
Comment thread cmd/dagger/engine.go Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh actually this broke something else, so I reverted and am leaving in those lines for now. Can address separately if updates are needed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sipsma

This comment was marked as outdated.

@sipsma
sipsma force-pushed the host branch 2 times, most recently from 3acdec7 to 041d546 Compare June 13, 2023 20:29
@sipsma sipsma mentioned this pull request Jun 13, 2023
5 tasks done

@vito vito left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool stuff! Tried everything out locally.

Comment thread cmd/dagger/do.go Resolved
Comment thread cmd/dagger/do.go
if !silent {
cmd.SetOut(vtx.Stdout())
cmd.SetErr(vtx.Stderr())
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/dagger/do.go
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})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious about the * here since we don't typically do that with SDK objects. Is it needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread core/schema/project.graphqls Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, but we could support that pretty easily too. I'll make a backlog issue for that one also

Comment thread core/schema/directory.go
"directory": router.ToResolver(s.directory),
},
"Directory": router.ObjectResolver{
"Directory": router.ToIDableObjectResolver(core.DirectoryID.ToDirectory, router.ObjectResolver{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL parentheses are optional in (core.DirectoryID).ToDirectory

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I almost didn't believe this when my IDE didn't report any errors, but it worked!

Comment thread cmd/dagger/do.go
if err != nil {
return fmt.Errorf("failed to get absolute path of output path: %w", err)
}
switch projResultType {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@sipsma sipsma Jun 15, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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.
  2. 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ./out directory)

  • 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."

@sipsma sipsma Jun 15, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed 👍

Co-authored-by: Alex Suraci <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>

@gerhard gerhard left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of great changes here, especially liked the integration tests.

This change alone would make for a cracking release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants