dagger/dagger · error
unexpected context type in inline field %s
Error message
unexpected context type in inline field %s
What it means
When a Dagger function takes a single inline-struct argument, each field of that struct becomes its own exposed argument. A field whose type is context.Context cannot be exposed as an argument, so codegen rejects it with this error naming the offending field.
Source
Thrown at cmd/codegen/generator/go/templates/module_funcs.go:376
if ok {
stype, ok := asInlineStructAst(fnDecl.Type.Params.List[i].Type)
if !ok {
return nil, fmt.Errorf("expected struct type for %s", param.Name())
}
parent := ¶mSpec{
name: params.At(i).Name(),
paramType: param.Type(),
}
paramFields := unpackASTFields(stype.Fields)
for f := range paramType.NumFields() {
spec, err := ps.parseParamSpecVar(paramType.Field(f), paramFields[f], paramFields[f].Doc.Text(), paramFields[f].Comment.Text())
if err != nil {
return nil, err
}
if spec.isContext {
return nil, fmt.Errorf("unexpected context type in inline field %s", spec.name)
}
spec.parent = parent
specs = append(specs, spec)
}
return specs, nil
}
}
// if other parameter passing schemes fail, just treat each remaining arg
// as a top-level param
paramFields := unpackASTFields(fnDecl.Type.Params)
for ; i < params.Len(); i++ {
docComment, lineComment := ps.commentForFuncField(fnDecl, paramFields, i)
spec, err := ps.parseParamSpecVar(params.At(i), paramFields[i], docComment.Text(), lineComment.Text())
if err != nil {
return nil, err
}
if spec.isContext {View on GitHub (pinned to 82ba2681db)
Solutions
- Remove the context.Context field from the inline struct
- Keep ctx as the first parameter of the function itself, not inside the struct
- Use a different type for the field if state was intended
Example fix
// before
func (m *Mod) Foo(ctx context.Context, args struct {
Ctx context.Context
Name string
}) error
// after
func (m *Mod) Foo(ctx context.Context, args struct {
Name string
}) error Defensive patterns
Strategy: validation
Validate before calling
// ensure no context.Context fields inside inline-struct args
for _, f := range reflect.TypeOf(args).FieldList() /* compile-time: just don't declare it */ {} Try / catch
if err := runCodegen(); err != nil {
if strings.Contains(err.Error(), "unexpected context type in inline field") {
// remove the ctx field from the struct
}
} Prevention
- Only the function's first parameter may be context.Context
- Never add ctx fields inside argument structs
- Lint module functions for ctx placement
When it happens
Trigger: Declaring a context.Context field inside the anonymous struct used as a function's single argument object.
Common situations: Copy-pasting a normal function signature's parameters into a struct; misunderstanding that only the top-level function may take ctx.
Related errors
- expected struct type for %s
- could not encode default value %q: %w
- unexpected context type for arg %s
- nested structs are not supported
- defaultPath pragma %q, must be a valid string
AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05).
Data as JSON: /api/errors/f6af0f0bd59a1536.
Report an issue: GitHub.