Skip to content

Self invocation: generate types and allow to call the module from the module - #10475

Closed
eunomie wants to merge 8 commits into
dagger:mainfrom
eunomie:self-typedefs
Closed

eunomie wants to merge 8 commits into
dagger:mainfrom
eunomie:self-typedefs

Conversation

@eunomie

@eunomie eunomie commented May 26, 2025 •

Copy link
Copy Markdown
Member

Note

This is the first part of #10336 and #8030

TypeDefs split

To be able to generate types of the module itself, this means it requires to generate and expose (register to the engine) the types and functions exposed by the module even if the module can't be compiled. If the module is referencing its own types, there's a moment they don't exist.
This circular dependency is cut by allowing module SDKs to expose a new function moduleTypeDefs. If this function exists, it will be called to register the types instead of calling the moduleRuntime.

Previously, moduleRuntims is called in two different ways:

  • with empty values -> the typedefs are returned to the engine
  • with values specifying the function to call -> invoke what is asked, return the value to the engine

Instead of this split in the module runtime code, this is now outside:

  • moduleTypeDefs is called in cases we would have call moduleRuntime with empty values (registering typedefs)
  • moduleRuntime in all other cases: function invocations

Tip

This is backward compatible, if moduleTypeDefs is not defined, moduleRuntime will be called in both cases.


In Java this is done by splitting the previous entry point:

  • io.dagger.gen.entrypoint.Entrypoint only contains methods invocation
  • io.dagger.gen.entrypoint.TypeDefs only contains types definition

A second java project is created to handle this new TypeDefs, with its own pom.xml, a different jar is created and executed when the engine is calling for types.

In Go, the register part of the invoke method has been extracted, to roughly do the same behaviour as explained for java.

Tech details

Under the hood, when we are looking for the module dependencies, the module itself is loaded as a module so the types are made available. Then the flow continues as before.

Flow looks like:

  1. dagger develop
  2. load dependencies
  3. if moduleTypeDefs defined
    • load module asModule (introspection file used is without the module itself)
    • append types to the ones from dependencies
  4. generate codegen (introspection file contains the module itself)

And while calling a function

  1. moduleSourceAsModule
  2. load dependencies
  3. runModuleDefInSDKto get the types exposed by the module
    • if moduleTypeDefs, call it, else call moduleRuntime
  4. if moduleTypeDefs, append types to the dependencies
  5. call moduleRuntime

One temporary trick is it allows types to be installed two times. It's something that needs to be fixed, but I haven't yet figured out where is the problem.

Current limitations

The code is only defined for the Java SDK as this was easier for me to work on it.
This needs to be extended to other SDKs.
This is working for Java and Go SDKs.

The module is loaded two times, one to get the types, one to call the functions. This makes things slower and needs to be improved.

Environment/LLM types are prefixed like any dependency, no special case done here. This is something that could be improved.

Example

Once dagger is built from the branch:

Generate a new Java module

$ dagger init --sdk=../../sdk/java modules/my-module

or

$ dagger init --sdk=go my-module

Add a new echo function to the created MyModule class/struct (under src/main/java/io/dagger/modules/mymodule/MyModule.java in java)

@Function
public Container echo(String stringArg) {
  return dag().myModule().containerEcho(stringArg);
}
func (m *MyModule) Echo(stringArg string) *dagger.Container {
        return dag.MyModule().ContainerEcho(stringArg)
}

If you have an IDE with code completion, you should see the code completion for myModule/MyModule, meaning the types have been generated.

Call it

$ dagger -c 'echo "self invocation" | stdout'

You can see the expected output. This means the echo function have call the module through the engine and not directly the containerEcho from the local code.

In the traces we can see the module (in the screenshot myGoModule) calling the module itself then creating the container and running the command:
image

@wingyplus

Copy link
Copy Markdown
Contributor

Exciting feature! I got an error during follow the example:

✘ Container.withExec(args: ["javac", "-cp", "/root/.m2/repository/io/dagger/dagger-java-annotation-processor/0.18.9-self-test-module/dagger-java-annotation-processor-0.18.9-self-test-module-all.jar", "-processor", "io.dagger.annotation.processor.TypeDefs", "-proc:only", "-d", "target/generated", "src/main/java/io/dagger/modules/mymodule/package-info.java", "src/main/java/io/dagger/modules/mymodule/MyModule.java"]): Container! 0.7s
error: Annotation processor 'io.dagger.annotation.processor.TypeDefs' not found                                                                                                                                                      
warning: Annotation processing without compilation requested but no processors were found.                                                                                                                                           
1 error                                                                                                                                                                                                                              
1 warning                                                                                                                                                                                                                            
! process "javac -cp /root/.m2/repository/io/dagger/dagger-java-annotation-processor/0.18.9-self-test-module/dagger-java-annotation-processor-0.18.9-self-test-module-all.jar -processor io.dagger.annotation.processor.TypeDefs -
! proc:only -d target/generated src/main/java/io/dagger/modules/mymodule/package-info.java src/main/java/io/dagger/modules/mymodule/MyModule.java" did not complete successfully: exit code: 1
Error: failed to generate code: input: moduleSource.withName.withSDK.withSourceSubpath.withEngineVersion.generatedContextDirectory failed to run codegen: failed to transform module source into module: select: failed to get module runtime: failed to call sdk moduleRuntime: select: process "javac -cp /root/.m2/repository/io/dagger/dagger-java-annotation-processor/0.18.9-self-test-module/dagger-java-annotation-processor-0.18.9-self-test-module-all.jar -processor io.dagger.annotation.processor.TypeDefs -proc:only -d target/generated src/main/java/io/dagger/modules/mymodule/package-info.java src/main/java/io/dagger/modules/mymodule/MyModule.java" did not complete successfully: exit code: 1

@eunomie

eunomie commented May 26, 2025

Copy link
Copy Markdown
Member Author

@wingyplus At which step did this happen? During the dagger init or later?

@wingyplus

Copy link
Copy Markdown
Contributor

@eunomie i found it during the dagger init call.

@eunomie

eunomie commented May 27, 2025

Copy link
Copy Markdown
Member Author

🤔 So if you run this command on the branch self-typedefs that doesn't work?

$ ./hack/dev dagger init --sdk=../../sdk/java modules/my-self-module

I tried again and everything went fine.

Annotation processor 'io.dagger.annotation.processor.TypeDefs' not found

I can see the file in the branch. I don't understand why that doesn't work. But maybe there's something wrong that only works on my machine (like a file not properly added). That sounds strange but could be the case.

Comment on lines +17 to +20
func (sdk *runtimeModule) HasModuleTypeDefs() bool {
_, ok := sdk.mod.funcs["moduleTypeDefs"]
return ok
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You could just hide that in the TypeDefs function instead of having this checker function.

We want to keep the SDK interface and its components as close as the one implemented by users.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Currently not. Doesn't mean it can't, I'll have a second look at it.
The TydeDefs method itself is doing the check, and if the moduleTypeDefs doesn't exist it silently redirects to moduleRuntime. But it's also used outside of the call to moduleTypeDefs, to know if we must add the types to the list of dependencies.
I'll see what other solution I can find.

@helderco

Copy link
Copy Markdown
Contributor

Love the TypeDefs split! @jedevc presented this idea last year and I love that it allows registering functions directly from the SDK module instead of having to, for example, build a Go app and run it in a container in order to register type defs.

This simplifies a Docker SDK for example, but I also want to build a new SDK in a different language where the code introspection is read from a json file. In this case I want to build a simple container that’s different from what the ModuleRuntime function returns, just to run a tool that gives me that json introspection, and parse/register it from Go directly.

One thing I would add is that in case you still want to report the type defs from the same code base/runtime that invokes the functions (like Python) I’d rather have moduleTypeDefs call the container from ModuleRuntime explicitly, with possibly a different entrypoint/exec + args, instead of looking for an empty parentName. I would even deprecate the latter outside backwards compat support, except that it can simplify the implementation if it’s the same code base. I guess at least the option is there to do it either way.

@wingyplus

Copy link
Copy Markdown
Contributor

🤔 So if you run this command on the branch self-typedefs that doesn't work?

$ ./hack/dev dagger init --sdk=../../sdk/java modules/my-self-module

I tried again and everything went fine.

I share a trace that produced from init call here.

Screenshot 2568-05-27 at 22 36 44

Annotation processor 'io.dagger.annotation.processor.TypeDefs' not found

I can see the file in the branch. I don't understand why that doesn't work. But maybe there's something wrong that only works on my machine (like a file not properly added). That sounds strange but could be the case.

I also see it at dagger-java-annotation-processor/src/main/java/io/dagger/annotation/processor/TypeDefs.java on my local machine (but didn't check it inside the SDK module container).

@eunomie

eunomie commented Jun 3, 2025

Copy link
Copy Markdown
Member Author

I updated the readme with the Go implementation

eunomie added 8 commits June 12, 2025 16:22
This is an optional new entry point on the module SDK.
It's designed to perform the type definition from the module like
the actual moduleRuntime when an empty string is passed as argument.

If the function doesn't exist, the moduleRuntime is used instead.

But this allows to decouple both of them so we can registry types
of a module without the runnable module itself because to build it
needs the types defined by the module

Signed-off-by: Yves Brissaud <[email protected]>
Use `moduleTypeDefs` instead of `moduleRuntime` if available.
Cache the runtime as expected if `moduleTypeDefs` has been called.

Signed-off-by: Yves Brissaud <[email protected]>
This allows to add the module types as dependencies and thus generate
bindings for the module itself.

Signed-off-by: Yves Brissaud <[email protected]>
Generate register function for typedefs in a dedicated class so
it can be used independently.

Signed-off-by: Yves Brissaud <[email protected]>
As the module user code might contain references to the module
self types, this allows to generate the typedefs in a project
not depending on the current module. It means the typedefs can
be generated even if the code cannot compile because it requires
the typedefs.

Signed-off-by: Yves Brissaud <[email protected]>
Temporary fix to have self module working

Signed-off-by: Yves Brissaud <[email protected]>
@eunomie

eunomie commented Jun 13, 2025

Copy link
Copy Markdown
Member Author

@wingyplus after some time, I finally hit your issue 😅 I'm fixing it

@eunomie

eunomie commented Jun 13, 2025

Copy link
Copy Markdown
Member Author

I'm closing this one, splitting the changes in smaller PRs. First part is #10584

@eunomie eunomie closed this Jun 13, 2025
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