dagger/dagger · error
failed to generate type def code for %s: %w
Error message
failed to generate type def code for %s: %w
What it means
Wrapping error from the StructVisitor in moduleMainSrc: after successfully generating implementation code, producing the object's TypeDefCode (the dag.TypeDef().WithObject(...) registration statement) failed. The object name is included; the true cause is wrapped with %w and comes from the type's TypeDef generation.
Source
Thrown at cmd/codegen/generator/go/templates/modules.go:163
}
return nil
},
StructVisitor: func(ps *parseState, named *types.Named, obj *types.TypeName, objTypeSpec *parsedObjectType, strct *types.Struct) error {
if err := ps.fillObjectFunctionCases(named, objFunctionCases); err != nil {
// errors indicate an internal problem rather than something w/ user code, so error instead
return fmt.Errorf("failed to generate function cases for %s: %w", obj.Name(), err)
}
// Add the object to the module
implCode, err := objTypeSpec.ImplementationCode()
if err != nil {
return fmt.Errorf("failed to generate json method code for %s: %w", obj.Name(), err)
}
implementationCode.Add(implCode).Line()
objTypeDefCode, err := objTypeSpec.TypeDefCode()
if err != nil {
return fmt.Errorf("failed to generate type def code for %s: %w", obj.Name(), err)
}
createMod = dotLine(createMod, "WithObject").Call(Add(Line(), objTypeDefCode))
return nil
},
IfaceVisitor: func(ps *parseState, named *types.Named, obj *types.TypeName, ifaceTypeSpec *parsedIfaceType, iface *types.Interface) error {
// Add the iface to the module
implCode, err := ifaceTypeSpec.ImplementationCode()
if err != nil {
return fmt.Errorf("failed to generate concrete struct code for %s: %w", obj.Name(), err)
}
implementationCode.Add(implCode).Line()
ifaceTypeDefCode, err := ifaceTypeSpec.TypeDefCode()
if err != nil {
return fmt.Errorf("failed to generate type def code for %s: %w", obj.Name(), err)
}
createMod = dotLine(createMod, "WithInterface").Call(Add(Line(), ifaceTypeDefCode))View on GitHub (pinned to 82ba2681db)
Solutions
- Read the wrapped cause to identify the failing field or sub-type.
- Change the offending field to a supported Dagger type (named struct, primitive, slice, enum, scalar).
- Regenerate dagger.gen.go via `dagger develop` and confirm the module compiles; upgrade CLI/SDK if this persists on valid code.
Example fix
// before
type Cache struct {
Entries map[string]Entry
}
// after
type Cache struct {
Entries []Entry // or []EntryKeyVal with Key/Value fields
} Defensive patterns
Strategy: try-catch
Validate before calling
// Pre-validate that all object field types map to Dagger TypeDef kinds.
func validateTypeDefFields(t reflect.Type) error {
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i).Type
if ft.Kind() == reflect.Map || ft.Kind() == reflect.Chan || ft.Kind() == reflect.Func {
return fmt.Errorf("%s.%s (%v) has no TypeDef mapping", t.Name(), t.Field(i).Name, ft)
}
}
return nil
} Try / catch
if err := codegen(); err != nil && strings.Contains(err.Error(), "failed to generate type def code") {
log.Fatalf("typedef generation failed: %v", errors.Unwrap(err))
} Prevention
- Replace maps and other unmappable composites with slices of key/value structs.
- Keep referenced dependency-module types loadable and up to date.
- Unwrap the %w chain to identify the exact failing field before editing.
When it happens
Trigger: objTypeSpec.TypeDefCode() errors while building the TypeDef for a module object — usually a field whose type reference cannot be converted to a TypeDef (unsupported basic type, anonymous composite, nested failure via slices).
Common situations: Objects containing fields with exotic Go types, deeply nested unsupported composites, or broken references to types from dependent modules.
Related errors
- could not encode default value %q: %w
- expected struct type for %s
- unexpected context type in inline field %s
- unexpected context type for arg %s
- nested structs are not supported
AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05).
Data as JSON: /api/errors/1d2f862163c14bc7.
Report an issue: GitHub.