Skip to content

replace root w/ root-for in dagger.json - #6386

Merged
sipsma merged 17 commits into
dagger:mainfrom
sipsma:root-to-sources
Jan 30, 2024
Merged

sipsma merged 17 commits into
dagger:mainfrom
sipsma:root-to-sources

Conversation

@sipsma

@sipsma sipsma commented Jan 10, 2024 •

Copy link
Copy Markdown
Contributor

🔥 Update Instructions for Breaking Changes

Modules that use "root" in dagger.json

This update will cause existing modules that use "root" in dagger.json to fail loading.

As an example of how to update, say your module is in directory layout like:

.
└── foo
    └── bar
        ├── dagger.json
        └── main.go

And dagger.json has contents:

{
  "name": "my-mod",
  "root": "../..",
  "sdk": "go"
}

To update, you should:

  • Remove the existing dagger.json file
  • cd to the directory where root was pointing to
  • Using the example layout above, run dagger mod init -m ./foo/bar --name my-mod --sdk go
    • adjust for the paths, name and SDK your module uses

As a more concrete example, starting at this repo+commit which uses root in the testcontainers and docker modules: https://github.com/vito/daggerverse/tree/fe4bc99ba02abfb74c10ae76f1794568ece70cc3

The process of updating those modules is:

sipsma@dagger_dev:~/tmp/repro$ ls
apko  concourse  docker  gitutil-client  go  LICENSE.md  moddesc  nix  noredist  test  testcontainers
sipsma@dagger_dev:~/tmp/repro$ cat testcontainers/dagger.json 
{
  "name": "testcontainers",
  "root": "..",
  "sdk": "go",
  "dependencies": [
    "../docker"
  ]
}
sipsma@dagger_dev:~/tmp/repro$ rm testcontainers/dagger.json 
sipsma@dagger_dev:~/tmp/repro$ dagger mod init -m testcontainers/ --name testcontainers --sdk go
• Engine: 0b75435fd6ad (version v0.9.8)
⧗ 7.39s ✔ 73
sipsma@dagger_dev:~/tmp/repro$ cat docker/dagger.json 
{
  "name": "docker",
  "root": "..",
  "sdk": "go",
  "dependencies": [
    "github.com/kpenfound/dagger-modules/proxy@4974464186921b92deec3299cf3f94d3067dddca"
  ]
}
sipsma@dagger_dev:~/tmp/repro$ rm docker/dagger.json 
sipsma@dagger_dev:~/tmp/repro$ dagger mod init -m docker --name=docker --sdk=go 
• Engine: 0b75435fd6ad (version v0.9.8)
⧗ 4.07s ✔ 70 ∅ 3
sipsma@dagger_dev:~/tmp/repro$ dagger mod install -m docker github.com/kpenfound/dagger-modules/proxy@4974464186921b92deec3299cf3f94d3067dddca
• Engine: 0b75435fd6ad (version v0.9.8)
⧗ 23.17s ✔ 257 ∅ 3
sipsma@dagger_dev:~/tmp/repro$ dagger mod install -m testcontainers docker
• Engine: 0b75435fd6ad (version v0.9.8)
⧗ 6.78s ✔ 314 ∅ 27
sipsma@dagger_dev:~/tmp/repro$ cat testcontainers/dagger.json 
{
  "name": "testcontainers",
  "sdk": "go",
  "dependencies": [
    {
      "name": "docker",
      "source": "../docker"
    }
  ]
}

Modules that use the Host API to read source files

(using go syntax below, but same idea applies to all SDKs)

Previously, module containers set their working directory to the directory containing the module's source code. You could then read files from that directory with calls like dag.Host().File("./some/file.txt").

Now, module containers are set to an initially empty scratch directory, so those calls will not work anymore.

To use files/directories from the module source code, you can instead:

  1. Directories - dag.CurrentModule().Source().Directory("some/subdir")
  2. Files - dag.CurrentModule().Source().File("some/file.txt")

Additionally, it used to technically be possible to use files/directories from anywhere in the root dir loaded for a module in the case where "root" was configured in dagger.json. This is no longer possible; instead anything you need to use from outside the module's source code directory should be accepted as an argument to a function or the module constructor.

One final use case to mention: if you need to create new files/directories and then load them into the dagger API during module execution, this is still possible.

  1. Directories - dag.CurrentModule().Workdir("./some/dir")
  2. Files - dag.CurrentModule().WorkdirFile("./some/file.txt")

These paths will all be relative to the module's initial working directory (an initially empty scratch directory, as mentioned above).


Fixes #6291

Summary of changes

Have details on all this below, but to summarize

  1. dagger.json removes root in favor of source
    • As part of this, the format required significant updates. The code here handles loading+updating old dagger.jsons automatically, except for the case where root was being used, which will required manual updates from the user.
  2. Module configuration and refs are now core APIs
  3. As part of above, automatic creating of .gitignore files for module generated source has been removed.
    • .gitattributes are still created though
  4. dag.CurrentModule functionality is expanded and the official API for modules to access the source dir and live files/dirs from the module container's filesystem, replacing dag.Host() in the module API
  5. Module containers are now set to use an empty scratch directory as their workdir

dagger.json changes

Expand This change only really impacts existing advanced use cases where `root` is being used. These cases occur when a module needs something outside the directory tree under `dagger.json` in order to execute. Some examples: * When a module is depending on other modules in the same git repo but those other modules are not under the same directory tree * A module is re-using a go.mod/package.json/pyproject.toml in a repo that exists in a parent directory

The new format inverts the existing relationship between dagger.json and the source directory that is loaded into module containers:

  • Previously, each module got its own dagger.json and could set root to point to an arbitrary parent directory. When the module container was loaded, that root directory was loaded into the container while the current working directory of the module container remained the actual source directory of module (i.e. the one containing the main.go/main.py/etc.)
  • Now, root is gone. Instead, anytime a module needs a root separate from its source dir, there will be two dagger.jsons involve. There is still the same dagger.json that exists today in the source dir, but there is also a dagger.json in the root dir that specifies a "root-for" key which points to the module's source dir. The engine knows how to "find-up" the correct root of the module based on this.

Note that in the simple cases where dagger.json and the module source directory are the same (i.e. what happens today when you run dagger mod init without the --root flag), there's no behavior change. The module's root-for in dagger.json just ends up being set to ., but the user won't see any differences otherwise.

Example

For a directory tree like this:

.
├── dagger.json
├── modA
└── subdir
    └── modB

Where the mod* directories contain source code for different modules. Also say that modA has a dep on modB.

The top-level dagger.json will look like this:

{
  "root-for": [
    {
      "source": "modA"
    },
    {
      "source": "subdir/modB"
    }
  ]
}

modA's dagger.json will be:

{
  "name": "modA",
  "sdk": "go",
  "dependencies": [
    {
      "name": "modB",
      "source": "../modB"
    }
  ]
}

modB's dagger.json will be:

{
  "name": "modB",
  "sdk": "python",
  "dependencies": [
    {
      "name": "foobar",
      "source": "github.com/org/daggerverse/[email protected]"
    }
  ]
}

Important notes:

  • All paths are relative to dagger.json. Any paths that point above a given dagger.json (i.e. dependencies) cannot go past the root-for the module being configure.

Impact on CLI

Expand

Path handling

This is all ultimately internal and meant to not be thought about by users, but worth noting.

CLI commands like init/sync/install are aware of the dagger.json they are using even if it's in a parent directory and try to handle path arguments as intuitively as possible.

To initialize a module with a dagger.json in a parent directory, you now do e.g. dagger mod init -m ./subdir --name=foo --sdk=go. The module source code will go in ./subdir and a separate dagger.json with root-for will in the directory you ran the init from.

For example, if you were in the subdir/modB directory in the example above you could run this:

dagger mod install ../modA

to add a dependency on modA to modB.

The CLI+Engine do all the path manipulations to know that it should update modB (because it's in the source directory for it), where the dagger.json it should update is located and that ../modA needs to be made relative to that dagger.json.

Support for named dependencies + sdk-less dependencies

Quoting from #6291 (comment) for the rationale behind this:

We discussed the idea of "codeless modules": what if you could dagger install modules without having to write code? This would add pinned dependencies in dagger.json the usual way, but the sdk and source fields would not be set. dagger call would support looking up dependencies by short name. So dagger install github.com/shykes/daggerverse/tailscale would install a pinned version of that module to the current dagger.json (which could be in my home directory or app repo), then dagger call -m tailscale would call the exact pinned version I installed. Later, if I want to add code to my module, I add the sdk field (command TBD here), and my same dependencies are codegened the usual way. This makes for a smoother learning curve.

This all now works.

Additionally, when you install a dependency, you can now optionally include a --name flag, e.g. dagger mod install --name ts github.com/org/daggerverse/tailscale. If you don't provide it, the name defaults to the name of the dependency module as configured in its dagger.json.

This is useful for the above use case but simultaneously also provides support for avoiding name conflicts. The --name you choose there is actually applied to the gql schema + codegen for the module its installed to, essentially creating an alias that can be used in naming corner cases (see #6478 for details on some of those problems which this PR now mostly solves).

Module Config+Ref API

Expand

In the latest version of this PR, I've switched the implementation of this to be based around core APIs. Motivation being:

  1. Centralize more logic, which makes it easier for all the consumers of the functionality (the CLI, the engine internally and daggerverse) to be consistent, fewer cases to think about, less code duplication when needing to handle slightly different types between clients/engine, etc.
  2. Help out with a follow up to this PR that's going to add versioning to various configuration in dagger.json, which is a lot easier to manage internally in the engine
  3. This is something we've discussed and wanted for a while in general anyways. It's better long term for functionality to be API based since it allows any consumer in any language to use it more easily (whereas previously everything was a Go util lib)

This PR didn't manage to move everything to be API based; there's still the core/modules/ util package. But that package now only has some bare minimum config file utils the CLI still needs. It no longer does any of the ref parsing/management. And there are now paths to fully API-ifying everything left in that package (they just were becoming too much to scope into this effort).

Refs aren't externally impacted very much by this change. They still must point to a module source directory, which is the same as before.

Internally, the implementation needs to now do some "find-up" logic for a given module ref to find the directory that contains dagger.json. It's implemented such that it will always stop at the root of a git repo (whether due to being a git ref in the first place or even local refs that point to a directory in a local git checkout).

ModuleSource

This is the API object representation of what used to be called Refs in the util lib. The name Source ended up making more sense to me as the API name since refs are a way of specifying where source of the module is located, along with other metadata on where its sourced from (git url, commit, etc.).

However, you still create a ModuleSource with a refString that has the same format as refs previously, there's no change in the format of refs in their string format. They just are used to construct a ModuleSource in the API now.

ModuleSource has different subtypes for LocalModuleSource and GitModuleSource, with git specific metadata only being queryable from GitModuleSource.

Internally, the state of ModuleSource amounts to:

  • a RootDirectory, which is a directory that contains the module config + source code (possibly as subpaths)
  • a SourceSubpath, which is the subpath of the directory that contains the module source code specifically.
  • any metadata that was parseable from the original ref string like git url, commit, etc.

ModuleSource has a asModule field for upgrading it to a full blown Module. It internally uses the recently added Module.withSource field, which has been updated to accept a ModuleSource rather than a directory+subpath it accepted previously.

ModuleSource also has a dependency function for resolving a ref as a dependency relative to another one. A motivating example for its need is when you have a git module source that contains a module with dependencies on other modules locally relative to it in the git repo. In that case, those "local" deps are relative to a git source, so you want to be able to resolve them to git sources (just at a different subpath in the git repo).

Module now also includes a Source field for recovering the original Source it was created from (and associated metdata).

Config + related codegen adjustments

Obtaining the codegen for a module (i.e. for the CLI init/install/sync commands) has been adjusted in a few ways.

A Module no longer returns a GeneratedCode object, instead there's just a field on it called GeneratedSourceDirectory of type Directory.

That Directory is the directory where dagger.json is located and includes:

  1. codegen for the module from the SDK (placed in the correct subdir if needed)
  2. .gitattributes files based on the SDK
  3. any dagger.json initialization or updates

dagger.json is now created by the engine itself as part of the above, clients no longer create or update it themselves.

To support the above, Module has new fields for updating configuration: withName, withSDK and withDependencies. If you start with a Source that includes no dagger.json, you can thus still turn it into a (uninitialized) Module, configure it via withName, withSDK, etc. and then call GeneratedSourceDirectory to get all of the generated code, including the shiny new dagger.json.

Siimilarly, if you load a Module that already has a dagger.json but modify dependencies or other configuration, calling GeneratedSourceDirectory will return a directory with the updated dagger.json (and all the sdk codegen, .gitattributes, etc.).

This removes the need for clients to update dagger.json themselves (and thus handle a lot of the logic around format, versioning, dependency refs, etc.).

However, as of right now the CLI does still read dagger.json in order to find the include/exclude settings (which are pretty important for monorepo-type use cases). Those in particular are tricky because in the naive approach, if you upload a directory containing dagger.json to the engine and then ask for those settings, its already too late to apply them.

  • There is actually a viable path to addressing this. Namely, the engine has the ability to read+stat arbitrary files from the client (as part of general filesync support). So it would be possible to have the engine just accept a local path from the caller, find the dagger.json without actually loading everything (just a series of "remote stats" essentially) and then do the loading w/ include+exclude applied.
  • Surprisingly, the implementation of that is actually not even that hard (we already have a ReadCallerFile util in the engine, this would just require a very slight variant for "stat"). I only left it out because it raises questions around performance and security which were too much for this already too big PR. Open to us in the future if desired though.

Removing automatic .gitignore

We've discussed for a while about removing the automatic setup of .gitignore of generated files. The main reason being that more often than not in our own usage we have to manually remove those .gitignores because otherwise once its pushed to a remote repo, CI can't compile dagger module code, which impacts linting.

I had the choice here to either write the logic for setting up .gitignore files in the update codegen process, or just not do that since we were intending to remove this soon anyways. I went with the latter to save time.

Also note that .gitattributes is still setup the same as before. This only impacts .gitignore.

Misc technical details

  • Detection of module dependency loops no longer worked the same as before (via recursive call detection from CacheMaps wrapping api calls), so I had to implement it more "by hand".
    • This is done in Module.WithDependencies via a simple walk of the DAG of deps.
    • The hardest part here was that we needed a consistent identifier for the modules. The ID of the module from dagql didn't quite work in every case because the IDs change based on, e.g., whether initialize was called or not, among other little details that modify the ID without modifying the module's "true identity".
    • Instead, I landed on use the ModuleSource's ID to compare module identity. This makes conceptual sense since a Module is ultimately defined by its source.
    • There was still subtly here to ensure the ID was as consistent as possible, which is especially tricky for local module sources. The normalization required is implemented here: https://github.com/dagger/dagger/blob/720b5cde90dce8262f08963e7d54947c5bde34c8/core/schema/module.go#L1024

SDK + Runtime Changes

Expand

Modules run in /scratch now

We want any access to module source dirs to be through the API now (dag.CurrentModule().Source()), while still allowing use cases that call for loading live dirs/files from the module containers' filesystems.

The approach is that the workdir of module runtimes are set to an empty dir at /scratch by the engine. More details under CurrentModule API below.

This worked pretty much out of the box for Go+Python. For Typescript, it broke nodejs module (as opposed to dagger module) loading. Eventually figured out that it was because the tsconfig.json we use to specify paths mapping was no longer being used, with the fix being to just explicitly provide a --tsconfig arg to tsx: https://github.com/sipsma/dagger/blob/07d4205730941460afd26ed01174d068a5e88964/sdk/typescript/runtime/main.go#L55-L55

SDK-as-module constructor

Part of the overall changes are to make a module's root dir only strictly needed by a module's SDK (i.e. for go.mod/package.json/etc.) and otherwise hidden from the end module (in order to reduce "shadow args").

Instead, anything that previously would have been needed from a root context now needs to be an explicit argument to the module. The idea is to make this easy via support for "default-args" or "scripts" in dagger.json, but those are out of scope of this PR (too much on top of already too much).

So in the meantime, this PR does some setup for what that would look like. Namely, the Python+Typescript SDK-as-modules now have a constructor that's used to pass the full SDK source code (which previously relied on root). The engine internally already handles these built-in SDKs specially, so it just continues to special case them a bit and knows to provide these args.

The generalized idea that will come out of this in the long run will be aforementioned support for either "default-args" or "scripts" in dagger.json (where these constructor args would be encoded) and possibly a generalized idea behind "sdk config" (though that in particular is a bit vague atm admittedly).

TypeDef registration doesn't use CurrentModule anymore

SDKs registering the typedefs of their module now call dag.Module().WithObject(...).<etc> rather than dag.CurrentModule().WithObject(...).<etc.>.

This is all SDKs need to know, the end effect is the same.

The reason for this are a bunch of subtleties around dagql IDs and the fact that CurrentModule is now an object separate from Module. Previously, the implementation heavily relied on Query.currentModule returning a dagq.Instance of *core.Module. In this new approach, I couldn't find any reasonable way to return a dagql.Instance[*core.CurrentModule]. It would require that the call context have an instance of CurrentModule rather than its current instance of Module, but I couldn't find any way of doing that without hacking dagql in bizarre+probably-broken ways.

So I just made the aforementioned change to have SDKs use Module, creating an empty module and registering types on it and then the server just merges in that module definition to itself here: https://github.com/sipsma/dagger/blob/07d4205730941460afd26ed01174d068a5e88964/core/module.go#L335-L335

CurrentModule API

Expand

As discussed in #6291, we now lean on the dag.CurrentModule() API heavily:

  • dag.CurrentModule().Source() returns a *Directory for the module's source dir only. It does not include the root directory
  • dag.CurrentModule().Workdir("some/dir") - returns a *Directory loaded from the container's live filesystem, but rooted under its working directory of /scratch. This is a replacement for dag.Host().Directory() for module code.
  • dag.CurrentModule().WorkdirFile("some/file.txt") - same as above but for *File, mainly as a convenience similar to how we also have a dag.Host().File()
  • dag.CurrentModule().Name() - returns the name of the module being executed in. Only added because the python SDK needs it (here) but makes perfect sense to expose in general anyways.

@sipsma sipsma modified the milestone: v0.9.7 Jan 15, 2024
@sipsma

sipsma commented Jan 18, 2024 •

Copy link
Copy Markdown
Contributor Author

Just to update, been working on this for the last few days, with the goals of:

  1. Rebase on dagql
  2. Refactor all module config management + refs to be API based
    • This helps this PR, but is even more helpful for the follow up to this PR that will add versioning to dagger.json (including core + builtin-sdk deps), with the goal there being to give us a path to make backwards incompatible changes w/out breaking every module in existence
  3. Generally handle more cases more robustly (it turns out there's an absurd number of corner cases...)

It's been a constant battle of having all but a few tests passing, refactoring to address the test failures, getting a new set of a few test failures, repeat. There's also been a lot trickiness around the API refactor and the new dagql world, which has all sorts of new subtleties that have taken getting used to. But nearing the end now (🤞) and reasonably happy with how it's all shaping up.

But given it's taken way longer than I was expecting, it's not gonna make it for the v0.9.7 release, which is why I removed that milestone.

@shykes

shykes commented Jan 18, 2024

Copy link
Copy Markdown
Contributor

Will this break existing dagger.json files?

@shykes

shykes commented Jan 18, 2024

Copy link
Copy Markdown
Contributor

Sorry to be late to the party... And I feel bad for asking the question so far into implementation, but:

have multiple modules in a monorepo that shared a common parent directory to be loaded.

Do we really need this? What's the use case?

I feel obligated to ask, because that one requirement seems pretty expensive in complexity, both to you and to the user. I foresee edge cases where the user, or our code, get confused between "the module's directory" and "the source code's directory". Or questions like "when I type -m foo, will it load the directory at path foo, or lookup the module named foo in dagger.json? What if there is both exist, and they're not the same module?

So, we should make sure the use case is really, really worth it...

@sipsma

sipsma commented Jan 18, 2024 •

Copy link
Copy Markdown
Contributor Author

Will this break existing dagger.json files?

@shykes It will not break dagger.json files provided they weren't using root (most modules today are not using it afaik), the updates have handling of loading the old format when root isn't there.

With a significant amount more effort, it is probably possible to handle updating root too, but it gets a lot more complicated, so it's currently just going to be an error message with instructions on updating.

Obviously the backwards incompatible change sucks for the cases where root was being used, which is one of the reasons we want this pre-RC, but it felt like the reasonable tradeoff. I'm making various other tweaks to the format in dagger.json to ensure that we can more easily update the format in the future without breaking everything (i.e. making dependencies a list of objects rather than string, including version numbers, etc.).

@shykes

shykes commented Jan 18, 2024

Copy link
Copy Markdown
Contributor

It will not break dagger.json files provided they weren't using root (most modules today are not using it afaik), the updates have handling of loading the old format when root isn't there.

That seems like the right tradeoff to me.

@sipsma

sipsma commented Jan 18, 2024

Copy link
Copy Markdown
Contributor Author

have multiple modules in a monorepo that shared a common parent directory to be loaded.

Do we really need this? What's the use case?

It's more that it's a case that becomes possible with the new source approach and is not obscure enough to ignore, as opposed to being the primary goal per-se (which is the replacement of root with source).

To give a more concrete example, say you have a monorepo of modules that depends on each other and also need some common config files that are stored in separate dir, like:

.
├── dagger.json
└── common-config
    └── some-file.txt
├── some-go-sdk-module
    ├── dagger.gen.go
    └── main.go
├── some-python-sdk-module
    ├── sdk/
    └── src/main.py

It's unavoidable in these sorts of cases for there to be a single dagger.json in that parent dir that needs to manage both modules. It just sort of comes along with the switch to source.

I foresee edge cases where the user, or our code, get confused between "the module's directory" and "the source code's directory". Or questions like "when I type -m foo, will it load the directory at path foo, or lookup the module named foo in dagger.json? What if there is both exist, and they're not the same module?

Module refs (what you specify as the arg to -m) are consistent in that foo is a local module ref and thus you would be pointing to the local dir ./foo. It's not possible to specify a module via just its name like that, and if we did update the module ref format in the future I think we'd have to deal with this sort of ambiguity anyways, these changes aside.

So while I can see how that could be confusing, it feels like it would be fairly obscure; there would need to be some mismatch between the name of a module and the dir it's stored in for this question to even arise in a user in the first place.

Also worth re-emphasizing that for all the use cases that don't need a parent directory to be loaded (which is the majority I believe), none of this has an impact. dagger.json is still just in the module's source code directory. It's only advanced/monorepo use cases where users need to think about any of this.

@sipsma
sipsma force-pushed the root-to-sources branch 2 times, most recently from be266da to 0fdd994 Compare January 19, 2024 02:12
@sipsma
sipsma marked this pull request as ready for review January 19, 2024 02:12
@sipsma
sipsma requested review from a team as code owners January 19, 2024 02:12
@sipsma
sipsma requested review from helderco, jedevc and vito and removed request for a team January 19, 2024 02:12
@sipsma sipsma changed the title WIP: replace root w/ sources in dagger.json replace root w/ sources in dagger.json Jan 19, 2024
@shykes

shykes commented Jan 19, 2024

Copy link
Copy Markdown
Contributor

I continue to have a bad feeling over breaking the 1-1 mapping of dagger.json to module. I feel there will be a butterfly effect of UX paper cuts that will accumulate over time, and by the time the pain becomes a serious issue, it will be too late to fix. I realize I don't have a bullet proof case, but have a strong gut feeling about that one, sorry for being a PITA.

I think it would be cleaner to keep the 1-1 mapping, and optionally support an overlays key where a parent module can designate extra directories to be shared with sub-modules.

@sipsma

sipsma commented Jan 19, 2024

Copy link
Copy Markdown
Contributor Author

I think it would be cleaner to keep the 1-1 mapping, and optionally support an overlays key where a parent module can designate extra directories to be shared with sub-modules.

Not totally following yet, where would the configuration files for the different modules actually be in this case?


My argument for the current approach:

Right now, the rule is essentially just "the module will be loaded with the directory under the dagger.json configuring it", which is relatively simple (at least, relatively speaking for these sorts of complex use cases). I'm hesitant to moving to something more complicated to explain+understand than that. That rule just inherently implies that it's possible for a single directory to be shared by multiple modules.

But I'd also say that in practice multiple modules being configured in the same file has not in particular been the source of significant complication in the implementation at all, it's relatively straightforward and doesn't introduce tons of corner cases or anything like that.

And on the UX side, the updates to dagger.json for configuring it all automatic and in the background, so it's not something I anticipate users needing to think about. But if there's particular situations you're thinking of, let me know.

The main reason it doesn't have much of an impact in practice is that modules are still uniquely identified by the path to the directory that contains their actual source code, not by the path that contains dagger.json. So when you are using the dagger CLI you still just point to where the module actually is, no thinking about whether it has a shared dagger.json or not. The only time you really need to think about that is during dagger mod init (and even then that's only in these exceptional cases anyways). After that it's seamless.

@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.

Loving the expanded API, mostly paid attention to DagQL technicalities for this review, I think I only really spotted one suspicious thing (there's one place where we update a dagql.Instance in-place)

Comment thread core/modulesource.go Resolved
Comment thread cmd/dagger/module.go Resolved
Comment thread cmd/dagger/module.go Resolved
Comment thread core/codegen.go Resolved
Comment thread core/directory.go Resolved
Comment thread core/schema/module.go Resolved
Comment thread core/schema/module.go 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.

dang, unfortunate

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.

from, import, and with are reserved words in Python; Function is reserved in PHP; in Typescript you have import, but Function and Module are also soft keywords... Every SDKs need a way to go around reserved words in their codegen.

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 the alternative here was to update the Rust SDK to special case this, but since this is a required arg, most (if not all?) SDKs just allow clients to provide the arg value directly rather than needing to specify the name of the arg plus the value. So the overhead of using a slightly more verbose name here felt less than needing to special case an SDK.

That being said, I agree in general that we can't just always avoid reserved names, the only general solution is for SDKs to have special handling.

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.

Yeah, it should be a requirement for every SDK to have a list of reserved words and deal with them from day one like Python does, rather than having to adjust manually when they come up:

if iskeyword(s):
s += "_"

Comment thread core/schema/module.go Resolved
Comment thread core/schema/module.go Resolved
Comment thread core/schema/module.go Resolved
@sipsma

This comment was marked as outdated.

@shykes

shykes commented Jan 19, 2024 •

Copy link
Copy Markdown
Contributor

Dumping a list of use cases from today's bikeshedding session:

  • Use case 1: have module root in repo root (for easier discovery), but module source code in subdirectory (to avoid polluting the repo)
  • Use case 2: monorepo with several modules, that all need to share the same go.mod/go.sum files, for policy reasons.
  • Use case 3: several modules need to share the same root directory, and cannot be merged. For example, the root of a repo contains both 1) the main codebase, and 2) shared formatting configuration for many subcomponents of the repo. Encapsulating both in the same module might cause circular dependencies.

@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.

(submitting comments so far to maximize async feedback, will leave individual comments from here on, sorry in advance for spam)

Comment thread cmd/dagger/module.go 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.

👍 - probably want to expand this in the future, but for now we're pretty narrowly scoped to git everywhere else too, so no need yet

Comment thread docs/docs-graphql/schema.graphqls Resolved
Comment thread core/module.go
Query *Query

// The source of the module
Source dagql.Instance[*ModuleSource] `field:"true" name:"source" doc:"The source for the module."`

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.

Super nice having this available, making this the canonical ID for deduping makes sense to me

Comment thread core/module.go Resolved
Comment thread core/module.go Resolved
Comment thread core/module.go
The provided Module is not fully initialized; the Runtime field will not be set yet.
*/
Codegen(context.Context, *Module, dagql.Instance[*Directory], string) (*GeneratedCode, error)
Codegen(context.Context, *Module, dagql.Instance[*ModuleSource]) (*GeneratedCode, 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.

Always nice when two args turn into one 👨‍🍳😎🔥🤌💋 (someday that'll merge into the standard guy fieri chef kiss emoji)

Comment thread core/schema/module.go Resolved
Comment thread core/schema/module.go Resolved
Comment thread core/schema/module.go Resolved
Comment thread core/schema/module.go Resolved
Comment thread core/schema/module.go Resolved
Comment thread core/schema/module.go Resolved
Signed-off-by: Erik Sipsma <[email protected]>
Comment thread core/schema/sdk.go
},
{
Name: "introspectionJson",
Value: dagql.String(introspectionJSON),

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.

NB: this huge JSON string ends up in IDs and gets sent over telemetry, which is kind of unfortunate. Thankfully it doesn't seem to end up anywhere important like the module's initialized ID, which is what propagates to IDs produced by the module. Would be nice to find another way to do this in the future anyway (maybe something with DagQL makes it easier), but not a big deal right now.

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 agree but the immediately available alternative is to turn it into a File via withNewFile, at which point it's still going to end up in the ID. I think we could blobify it though, so then we just pass the content hash in the ID.

Will save for follow-ups though since it's more a performance optimization

@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.

Phew - think I got through it all, didn't see any blockers so approving!

The only feedback design-wise I have is that it feels a little weird to have the logical definition of a module split between two files, imagining a scenario where you remove a module entirely but accidentally leave a dangling reference to it in the root dagger.json. But I understand there are a lot of constraints being satisfied here.

Maybe we could detect that + error until you clean it up?

Comment thread core/integration/module_config_test.go Outdated
// test dagger.json source configs that aren't inherently covered in other tests

t.Parallel()
c, ctx := connect(t)

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.

Probably worth following the pattern set forth in #6521 - basically if you expect a t.Run to take a while, give it its own client, otherwise don't mark it t.Parallel and just let them all use the root-level client.

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.

Ah good catch, fixing before merge

@sipsma

sipsma commented Jan 30, 2024

Copy link
Copy Markdown
Contributor Author

The only feedback design-wise I have is that it feels a little weird to have the logical definition of a module split between two files, imagining a scenario where you remove a module entirely but accidentally leave a dangling reference to it in the root dagger.json. But I understand there are a lot of constraints being satisfied here.

Agree that the split does lead to some weird situations like that, but yeah it comes down to tradeoffs between all the different possible implementations. That at least is more of an obscure corner case that we can also handle reasonably.

Maybe we could detect that + error until you clean it up?

A stale entry technically wouldn't hurt anything, so I don't think we'd need to error out and can instead just have the engine detect it and automatically update the dagger.json to remove any stale entries it finds, which will get picked up on the next dagger mod sync/install. I'll append that to the list of follow-ups here 👍

@sipsma
sipsma merged commit 73a7394 into dagger:main Jan 30, 2024
@sipsma

sipsma commented Jan 30, 2024

Copy link
Copy Markdown
Contributor Author

Just realized I forgot to update the title of the PR before merge 🤦‍♂️... So it's confusingly committed as replace root w/ sources in dagger.json...

Just as an FYI for people in the future that might look at this and be confused.

@sipsma sipsma changed the title replace root w/ sources in dagger.json replace root w/ root-for in dagger.json Jan 30, 2024
@nipuna-perera

Copy link
Copy Markdown
Contributor

Wow this is an insane amount of work! Nice job Eric! Can't wait to test out the new stuff :)

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.

Modules: replace root in dagger.json with source

5 participants