Skip to content

simplify module config UX via automatic sparse loading of required files - #6575

Merged
sipsma merged 17 commits into
dagger:mainfrom
sipsma:always-include-config
Feb 8, 2024
Merged

sipsma merged 17 commits into
dagger:mainfrom
sipsma:always-include-config

Conversation

@sipsma

@sipsma sipsma commented Feb 2, 2024 •

Copy link
Copy Markdown
Contributor

Goals

  • Simplify module loading further by just always defaulting the "context" to the root of the git repo
    • for the local case, we find-up a .git and otherwise default to the source root path specified so everything stays sandboxed
    • users no longer need to think about running dagger init in the right place, there's no root/root-for/etc. settings, etc.
    • the tradeoff is we need to be much more intelligent about only sparsely loading exactly the files/dirs we need based on local deps, local custom sdks, files that are required by the SDKs in use and custom include/exclude settings provided by the user. This also needs to apply recursively to any deps/sdks/files needed in the whole context
  • Add support for dagger init --source <path> and dagger develop --source <path> for customizing the subdirs at which actual module source code is located
    • if not specified --source defaults to dagger/. See this discord thread for discussion on the default value.

Example Usage

--source usage

One of the primary use cases --source as an arg to init/develop enables is putting dagger.json in the root of a repo without necessarily polluting the root with SDK source files, e.g.

  1. from root of local git repo clone, run dagger init --name foo
    • dagger.json is created in your cwd by default, so in this case it's in the root of the repo
  2. run dagger install to add some deps
  3. decide you want to start writing some code, so run dagger develop --sdk=go --source=some/subdir
    • you'll now see main.go and other go sdk files at under some/subdir in the repo
    • dagger.json is also updated to set "source": "some/subdir"
    • If --source hadn't been specified, it would have defaulted dagger/go (with the last element always being set to the name of the sdk)
    • --source=. is valid too still, just not the default
  4. Your module is still the default one when in the root of repo, so you can do e.g. dagger call foofunc ... from the root
    • This also applies to anyone else git clone'ing your daggerized repo for the first time
    • It also applies to anyone invoking/installing your module from its remote git ref, e.g. dagger install github./com/org/repo is enough since they just need to point to the root of the repo

This is just one use case, generally speaking the goal here is to be flexible while retaining sane defaults and sandboxing everything to the .git context.

Terminology

  • Context
    • this is the dir that is loaded into the engine and provided to SDKs for codegen. End modules don't get implicit access to the whole context; it's only used to support modules that use go.mod/package.json/etc. from parent dirs and to load any deps/custom sdks/etc. local to the module
    • For local modules, this is a sparsely loaded directory rooted at the nearest parent dir containing .git (or the dir containing dagger.json as a fallback)
    • For git modules, this is just the git repo. As noted in the code, support for sparsely loading from git remotes is a TODO and will require Cache efficient checkouts of subdirectories of large git repos #6292)
  • Source Root Path
    • this is the dir under Context (or the same as it) that contains dagger.json for a module.
    • it's important in that if you specify a module ref, it should point to this dir. e.g. dagger call -m github.com/foo/bar/the/sourceroot ...
  • Source Path
    • this is the dir that contains the actual module source implementation (i.e. main.go, src/index.ts, src/main.py, etc.)
    • It can be set via --source and defaults to dagger/ (see above sections for more usage details)

Implementation notes

  • SDKs now have an additional method RequireFiles that allows them to specify which files they need in order to load (i.e. go.mod is one required by the go SDK)
    • This is currently a bit barebones in that it's just a flat list of glob patterns. Better would be if this method was given the ModuleSource arg and used it to determine very precisely what's needed (i.e. right now we require **/go.mod but really we only needs ones that are in parent dirs or are referenced by those in parent dirs). But saving that optimization for the future.
  • The sparse loading of local dirs uses the filesync API to first walk the whole DAG of local deps and read dagger.json from each one. Code
    • Once that's done, we collect all the include/exclude values and invoke all the SDKs encountered to determine which files they strictly require and finally do a single local import of everything we need. Code
    • This required adding an extra method for just stat'ing paths, which enabled the find-up of .git from the engine code. Code
  • This effort required another iteration on the Module/ModuleSource API, though it amounted to mostly re-arranging rather than anything new besides the sparse loading logic
    • The biggest rearrangement is the WithName/WithSDK/WithDependencies moved from Module to ModuleSource. This was required because those apis can actually change what is required from the context and trying to later re-load those files from their original source while also keeping source IDs pure was practically speaking not really possible.
    • The idea is essentially that all the configuration happens on ModuleSource and once AsModule is called it's immutable
    • I actually think all the code around this ended up even cleaner than before somehow even though it was only changed to get things working for this feature 🤷‍♂️🎉

@sipsma sipsma added this to the v0.9.9 milestone Feb 2, 2024
@sipsma
sipsma requested review from jedevc and vito February 2, 2024 07:38
@sipsma
sipsma force-pushed the always-include-config branch from 2604b93 to 65151c0 Compare February 5, 2024 17:27
@sipsma
sipsma marked this pull request as draft February 5, 2024 17:28
@sipsma sipsma changed the title fix module include/exclude applying to essential files simplify module config UX via automatic sparse loading of required files Feb 5, 2024
@sipsma
sipsma force-pushed the always-include-config branch 6 times, most recently from e643298 to eb960ee Compare February 7, 2024 14:16
@sipsma
sipsma marked this pull request as ready for review February 7, 2024 14:16
@sipsma
sipsma requested review from a team as code owners February 7, 2024 14:16
@sipsma
sipsma requested review from helderco, jedevc and vito and removed request for jedevc and vito February 7, 2024 14:16
@helderco

helderco commented Feb 7, 2024

Copy link
Copy Markdown
Contributor
  • It can be set via --source and defaults to dagger/<sdk name>

What's the arguments/use cases for this default?

@sipsma

sipsma commented Feb 7, 2024 •

Copy link
Copy Markdown
Contributor Author
  • It can be set via --source and defaults to dagger/<sdk name>

What's the arguments/use cases for this default?

@helderco mentioned in the PR description briefly but just updated that with more details, let me know if it makes sense EDIT: I missed the key words of "this default" in your message when I replied 😅 Now I specifically updated the description with details around that default value in particular too

@jedevc

jedevc commented Feb 7, 2024

Copy link
Copy Markdown
Contributor

Hm, I have to agree with @helderco - I think dagger/ by itself is a reasonable default. I don't think it's particularly common to have lots of different sdks in one repo. And even if there are, having them all named just by their sdk isn't great - I'm going to want to rename these to dagger/main, dagger/helper, etc, and actually describe their purpose, not the language they're written in.

Actually, the most "expected" option for me as a user would be to default to . - but error out if the current directory has contents, and require the user to use --source. This is familiar to a lot of init tools, like git, or hugo.

That said, I'm also happy with dagger or ci or any variation of the above honestly - as long as we don't have the SDK in there, I can't see why I'd ever want to use the default in that case (I'd just always specify --source for my own use).

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

Mostly improvements to --help for now, so people don't depend on docs to learn. It took quite a bit of experimenting (dagger init still) to understand how it all plays together.

It takes some getting used to the behavior of using from "root", since I was used to have modules all over, but once you do I think it makes sense.

However, the default of dagger/<sdk> for --source doesn't sit well. Prefer just dagger or .dagger. I don't think changing the SDK would be something common by any stretch. I'd just create a new one.

Comment thread cmd/dagger/module.go Resolved
Comment thread sdk/python/runtime/main.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.

Won't this include sdk/pyproject.toml? Only need the one at the root of source dir.

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.

It will yeah, but I think what we'd optimally want is the "nearest" one in any parent dir in the context directory (I'm assuming pyproject.toml has similar-ish behavior to go.mod/package.json/etc.). To do that we'll need even more intelligence.

My first thought was something like actually giving this SDK function ModuleSource so the SDK can inspect it. The problem there is we are lazily loading these dirs, so we actually couldn't give SDKs plain old Directory, we'd need to give them some object that lets them stat + read files from the caller's host. Which is do-able technically but too much for here.

There's probably other ways of approaching this like allowing the SDK to return something that conveys i.e. "give me any file with this name from the nearest parent dir above the source dir" and then let the engine do the rest.

Either way, all that felt like excessive optimization for now, so I just went with this approach. Can optimize more in the future as 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.

Since all files from source dir are already included, I think I'll remove this until a use case presents itself. To be useful, it'd have to be configurable by the modules.

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.

Cool sounds good, I do see now that there's no subtest under TestModulePythonInit like respects parent pyproject.toml, so I guess it's different than go/ts right now

Comment thread cmd/dagger/module.go Resolved
Comment thread cmd/dagger/module.go Resolved
Comment thread cmd/dagger/module.go Resolved
Comment thread cmd/dagger/module.go Resolved
@sipsma

sipsma commented Feb 7, 2024

Copy link
Copy Markdown
Contributor Author

It takes some getting used to the behavior of using from "root", since I was used to have modules all over, but once you do I think it makes sense.

One thing to clarify is that it's still completely valid to put modules all over the place as it was previously. It's actually easier than ever to do that because you don't have to contend with calling init in the right place to get the root-for you need. E.g. you can dagger install any other local module in the git repo and it will just work.

Source just makes it possible to also separate out dagger.json's location from the implementation source, but it's not a strictly enforced or required pattern. It just makes sense for a lot of use cases we think.

@helderco

helderco commented Feb 7, 2024

Copy link
Copy Markdown
Contributor

One thing to clarify is that it's still completely valid to put modules all over the place as it was previously.

Yes, I said it took "getting used to" because of the change in defaults. Felt more "distributed" and now it feels more "centralized". 🙂

I'll still want to add --source=. on one hand, but no longer need --name on the other (at least in most cases). 🙂

Being able to set the target dir is great! 🙌 Really disliked all the mkdir && cd && init.

But if --source becomes relative to the workdir for consistency, and I provide a target path, as long as I still prefer putting the sources in ".", I'll have to repeat it:

dagger init --sdk=python --source=some/sub/dir some/sub/dir

@sipsma

sipsma commented Feb 7, 2024

Copy link
Copy Markdown
Contributor Author

But if --source becomes relative to the workdir for consistency, and I provide a target path, as long as I still prefer putting the sources in ".", I'll have to repeat it:

This is true, but I still feel pretty strongly that it's worth being consistent here even if it can result in more verbosity for situations like that. All this path arithmetic is a huge headache to think about and easy to mess up so I'd rather we just have a consistent model of "paths are relative to your cwd". Also works better with tab shell completion since that also has the same assumption.

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

I like the described changes, and nothing stands out immediately as weird in the code 🎉

That said, this is a several thousand line diff - so don't really have the time to dig through all of this line-by-line, but... LGTM!

@helderco

helderco commented Feb 7, 2024

Copy link
Copy Markdown
Contributor

This is true, but I still feel pretty strongly that it's worth being consistent here even if it can result in more verbosity for situations like that.

Of course, I agree.

Comment thread cmd/dagger/module.go Outdated
Comment on lines 426 to 428

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.

Since dagger init defaults name to the parent dir, when would it be empty by dagger develop time?

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 suppose that first check for "" is probably redundant other than obscure cases like hand-edited dagger.json. It's also not hurting anything either and makes this slightly more resilient to those obscure cases, so I'm inclined to leave it as is.

@sipsma sipsma Feb 8, 2024 •

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.

Actually while I was updating from your other comments I realized this is totally dumb and we probably just shouldn't even have --name in the first place on develop, at least until we actually support renaming a module that already has one set. Any obscure benefits are greatly outweighed by giving users the wrong impression they can actually use develop --name its current state. Gonna switch to that instead.

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.

Suggested change
Use: "develop [--name string --sdk string] [--source string]",
Use: "develop",

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.

Maybe require --sdk if there's no source/code.

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.

--source default here is diff from init.

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.

Maybe require --sdk if there's no source/code.

Yeah that makes sense, would be confusing to silently do nothing. Fixing.

--source default here is diff from init.

Good catch, cleaned up all this logic a bit more and fixing that.

Comment thread sdk/python/runtime/main.go Outdated
Comment on lines 16 to 18

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.

Could maybe use the opposite of this, i.e., exclude sets of files. Each language as their own common .gitignore globs.

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.

The behavior of include/exclude (as we inherit from buildkit) is actually that if anything is provided in include, that results in everything else being discluded. So when we specify include we're saying only include those files. Which actually is exactly what we want here; ignore everything unless told not to 🙂

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

Looks good overall 🙂 My comments amount to mostly polish. In the interest of unblocking this, I'll approve now. We can polish later if you're out of time.

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

Mostly reviewed DagQL related changes, been following the discussion and I'm in agreement with the current state of things - LGTM!

Comment thread core/modulesource.go Outdated
Comment on lines 55 to 58

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.

The With prefixes here feel a little confusing, since they'd clash with the methods I imagine you would use to set them. I guess these are distinctly named to indicate they're overrides to apply to the original module?

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 these are distinctly named to indicate they're overrides to apply to the original module?

Yeah we do want to track them separately from the original values for various reasons:

  • knowing the original name is important because it should always be used for codegen, with the override name only impacting the schema of those depending on it
  • knowing the original dependencies is important since we de-dupe deps by symbolic ref with a preference for the ones specified in WithDependencies
  • etc.

I'm trying to think of a better name. OverrideName/etc. feels wrong too these also are used when there wasn't an original value in the first place. If I think of something I'll update but otherwise leave it for now. There's a comment right above these lines explaining them if nothing else 😅

Comment thread core/schema/modulesource.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.

Ah so this is the tricky part. Definitely a bit awkward, but this is the price you pay for reproducible pipelines, I guess.

Does this only affect local module sources? For example, if I ran with -m pointing to a Git ref, will the resulting ID's module still reference git like normal and keep all the nice theoretical portability properties? (I think the answer is yes, but just making sure.)

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.

Does this only affect local module sources? For example, if I ran with -m pointing to a Git ref, will the resulting ID's module still reference git like normal and keep all the nice theoretical portability properties? (I think the answer is yes, but just making sure.)

Yep only local sources right now. Git sources are inherently normalized like you said, much easier to deal with 😄

@sipsma

sipsma commented Feb 7, 2024

Copy link
Copy Markdown
Contributor Author

Thank you everyone for the reviews! I am making updates discussed above about having --source always be relative cwd (lots of fun corner cases), but very small change. After that I'll update based on the rest of the comments and merge 🚢

sipsma and others added 16 commits February 7, 2024 20:06
Signed-off-by: Erik Sipsma <[email protected]>
Co-authored-by: Alex Suraci <[email protected]>
Co-authored-by: Erik Sipsma <[email protected]>
This started as a fix to a problem where syncing a module using
"include" in dagger.json was resulting in the dagger.json being reset to
an empty module. The root cause was that the include did not have
dagger.json itself in it, so when the module was loaded into the engine
it looked like an uninitialized module that needed to be initialized.

That bug in particular was a regression caused by the recent changes
around module loading and configuration.

This change fixes that bug, but along the way I realized there's quite a
few pre-existing issues that have existed since forever around
include/exclude too. It's too much to squash all at once, but I went with
an approach that will set us up to fix those in further iterations and
should make it possible to implement some loading optimizations around
huge monorepos we've discussed previously.

The other pre-existing issues are:
* Include/exclude settings apply to source files like main.go,
  src/main.py, src/index.ts, etc.
  * This one in particular is fixed in this change. A new Function,
    `requiredPaths` is added to the SDK interface that lets SDKs tell
    the engine which files should always be loaded.
* Include/exclude settings apply to local dependencies (and local custom
  SDKs), which I'd consider surprising enough to be a bug. It would be
  expected that if you configure a local dependency, that acts as an
  implicit include of it.
* Include/exclude settings of local dependencies are ignored, only the
  top level module's include/excludes are actually impactful in terms
  of what's loaded.
* Exclude in particular overrides any include settings, including all
  essential files (still the case after this change).

The approach I went with here centralizes even more loading logic in the
engine. There's a new field on LocalModuleSource called
resolveFromCaller that uses the filesync API to do all of the find-up
logic, config reading/parsing and directory loading for the caller from
the engine. The CLI (or any other SDK user) can now load modules from
their local filesystem with just that API rather than doing the whole
dance themselves.

This approach is beneficial for a few reasons:
1. There's less logic to keep in sync between the engine and the CLI.
   Similarly, local module loading is re-usable beyond just the CLI now
2. Fixing the problem around files like main.go, src/main.py, etc. not
   being loaded required adding a new function to the SDK interface. The
   CLI could have been exposed to that, but it would have continued to
   make its logic even more complicated, especially around the case of
   custom local SDKs.
3. In the future, fixing the rest of the pre-existing issues listed
   above would also have deepened the complexity of the CLI's logic
   much, much further (while also requiring the engine-side loading of
   dependencies to have its own version of the same logic).
4. This sets us up for optimizations we've discusssed previously around
   loading large sets of dependencies from monorepos, which again
   requires complex logic that's better centralized in the engine.

It's still entirely possible for SDKs to just load up a local directory
as a module if they want; that API is not removed. It's just not
required they implement complex custom loading logic that is required to
get everything working correctly+optimally.

Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
This is a bit of hack atm but enough to unblock the only use case for
it. Can be generalized as needed.

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]>
@sipsma
sipsma force-pushed the always-include-config branch from 34ef45f to 4e876e0 Compare February 8, 2024 06:07
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