dagger icon indicating copy to clipboard operation
dagger copied to clipboard

🐞 Error when using a `context.Context` field in the module struct

Open dirodriguezm opened this issue 1 year ago • 1 comments

What is the issue?

I found that this error happens when adding a context.Context field in the module's struct.

Like this:

type DaggerBugMinimal struct {
	Context context.Context
}

Running any function while having a Context field results in an error:

Error: generate code: template: module.go.tmpl:85:3: executing "_dagger.gen.go/module.go.tmpl" at <ModuleMainSrc>: error calling ModuleMainSrc: cannot code-generate for foreign type Context

Dagger version

dagger v0.12.7 (registry.dagger.io/engine:v0.12.7) darwin/arm64

Steps to reproduce

  1. mkdir dagger-bug-minimal
  2. dagger init --sdk=go --source=.
  3. edit the type ModuleName struct struct to have a context.Context field
  4. run dagger call container-echo --string-arg=isthisabug

The default generated main.go file with just the added context.Context field.

package main

import (
    "context"
    "dagger/dagger-bug-minimal/internal/dagger"
)

type DaggerBugMinimal struct {
    Context context.Context
}

// Returns a container that echoes whatever string argument is provided
func (m *DaggerBugMinimal) ContainerEcho(stringArg string) *dagger.Container {
    return dag.Container().From("alpine:latest").WithExec([]string{"echo", stringArg})
}

// Returns lines that match a pattern in the files of the provided Directory
func (m *DaggerBugMinimal) GrepDir(ctx context.Context, directoryArg *dagger.Directory, pattern string) (string, error) {
    return dag.Container().
        From("alpine:latest").
        WithMountedDirectory("/mnt", directoryArg).
        WithWorkdir("/mnt").
        WithExec([]string{"grep", "-R", pattern, "."}).
        Stdout(ctx)
}

Log output

✔ connect 0.9s
✘ initialize 0.5s
! input: moduleSource.withContextDirectory.asModule resolve: failed to create module: select: failed to update codegen and runtime: failed to generate code: failed to get modified source directory for go module sdk codegen: select: process "codegen --output /src --module-context-path /src --module-name dagger-bug-minimal --introspection-json-path /schema.json" did not complete successfully: exit code: 1
  ✔ resolving module ref 0.1s
  ✘ installing module 0.4s
  ! input: moduleSource.withContextDirectory.asModule resolve: failed to create module: select: failed to update codegen and runtime: failed to generate code: failed to get modified source directory for go module sdk codegen: select: process "codegen --output /src --module-context-path /src --module-name dagger-bug-minimal --introspection-json-path /schema.json" did not complete successfully: exit code: 1
    ✘ ModuleSource.asModule: Module! 0.3s
    ! failed to create module: select: failed to update codegen and runtime: failed to generate code: failed to get modified source directory for go module sdk codegen: select: process "codegen --output /src --module-context-path /src --module-name dagger-bug-minimal --introspection-json-path /schema.json" did not complete successfully: exit code: 1

Error: input: moduleSource.withContextDirectory.asModule resolve: failed to create module: select: failed to update codegen and runtime: failed to generate code: failed to get modified source directory for go module sdk codegen: select: process "codegen --output /src --module-context-path /src --module-name dagger-bug-minimal --introspection-json-path /schema.json" did not complete successfully: exit code: 1

Stdout:
generating go module: dagger-bug-minimal
creating directory . [skipped]
writing dagger.gen.go
writing go.mod
writing go.sum
creating directory internal [skipped]
creating directory internal/dagger [skipped]
writing internal/dagger/dagger.gen.go [skipped]
creating directory internal/querybuilder [skipped]
writing internal/querybuilder/marshal.go [skipped]
writing internal/querybuilder/querybuilder.go [skipped]
creating directory internal/telemetry [skipped]
writing internal/telemetry/attrs.go [skipped]
writing internal/telemetry/env.go [skipped]
writing internal/telemetry/exporters.go [skipped]
writing internal/telemetry/init.go [skipped]
writing internal/telemetry/live.go [skipped]
writing internal/telemetry/logging.go [skipped]
writing internal/telemetry/proxy.go [skipped]
writing internal/telemetry/span.go [skipped]
writing internal/telemetry/transform.go [skipped]
running post-command: go mod tidy
needs another pass...
Stderr:
Error: generate code: template: module.go.tmpl:85:3: executing "_dagger.gen.go/module.go.tmpl" at <ModuleMainSrc>: error calling ModuleMainSrc: cannot code-generate for foreign type Context

dirodriguezm avatar Sep 07 '24 13:09 dirodriguezm

Public fields will be exposed to Dagger by default and Context isn't a supported Dagger type, so you need to either add the // +private pragma to not expose (but allow serialization), or make it private (lowercased).

helderco avatar Sep 10 '24 21:09 helderco