#Running codegen programmatically

1 messages · Page 1 of 1 (latest)

zenith cedar
#

I am trying to get dagger to do codegen programmatically for CI of my "daggerverse". I am getting an error failed to import local module source: context cancelled . Here's the gist of what I'm trying to do

package main

import (
    "context"
)

type Ci struct {
    //+private
    Modules []string
}

func New() *Ci {
    modules := []string{
        "oauth",
        "docker",
        "ci",
        "maven",
        "sonar",
    }

    return &Ci{Modules: modules}
}

func (ci *Ci) Lint(ctx context.Context, source *Directory) (string, error) {
    gen, _ := dag.Docker().From("alpine").
        WithWorkdir("/src").
        WithMountedDirectory("/src", source).
        WithExec([]string{"apk", "add", "curl"}).
        WithExec([]string{"sh", "-c", "curl -L https://dl.dagger.io/dagger/install.sh | sh"}).Sync(ctx)

    return gen.WithWorkdir(ci.Modules[0]).
        WithExec([]string{"../bin/dagger", "develop"}, ContainerWithExecOpts{
            ExperimentalPrivilegedNesting: true,
        }).
        Stdout(ctx)
}

"Docker" is a custom module where I prefix my corp Artifactory url. Which shouldn't affect this. I have stripped the part where I'm trying to do golanglint-ci because the code doesn't even reach it.

#

I am not sure what I'm doing wrong.

#

The above code should be reproducible if you create a couple of modules and change the modules slice to match.

humble tide
#

The way we do that in our CI:

func daggerDevelop(source *Directory) *Directory {
 return source.AsModule().GeneratedContextDirectory()
}

Right @whole hound ?

#

(you don't need to exec the dagger CLI at all, just call the underlying feature in the API)

#

All module loading, codegen etc is accessible in the API btw

#

even introspection of the module's functions, types etc

whole hound
zenith cedar
#

Ooh that's really handy! Let me test that. Off for the weekend, so happy weekend folks! ☺️

zenith cedar
#

Hey folks. Happy Monday! I tested this and it works great. I have a couple of pieces of feedback.

  1. It was confusing to me that AsModule(sourceRootPath: "./mypath").GeneratedContextDirectory() returned the entire parent context directory and not just ./mypath. I created an empty dag.Directory() and individually added the results of the modules but all of my parent context folder files were still available in the resulting folder (with the correctly generated module folders). IDK if that's a feature or a bug. It may be doing unnecessary writes back.
  2. When using GeneratedContextDirectory() the --progress=plain output writes a whole bunch of mkdir and mkfile to the log which adds visual clutter for not much gain. Would be great if that can be cleaned up if it's even possible. Attaching a pic of how it looks.