Skip to content

Unconstructable optional types provided to the container should not silently fail #269

@dnathe4th

Description

@dnathe4th

Take the following minimal example:

package main

import (
	"fmt"
	"go.uber.org/dig"
)

type A struct {
	b *B
	c *C
}
type B struct{}
type C struct{}
type D struct{}

type paramsA struct {
	dig.In

	B *B
	C *C `optional:"true"`
}

func main() {
	c := dig.New()

	c.Provide(func(p paramsA) *A {
		return &A{
			b: p.B,
			c: p.C,
		}
	})
	c.Provide(func() *B { return &B{} })
	c.Provide(func(d *D) *C { return &C{} })

	if err := c.Invoke(func(a *A) {
		fmt.Printf("a.c == nil is %t \n", a.c == nil)
	}); err != nil {
		panic(err)
	}
}

Running this will return the string "a.c == nil is true". Despite the fact that a constructor was Provided to the container for *C, there is no feedback to the user that a.c == nil because *C's dependencies were not fulfilled. The only way to arrive at this conclusion without tracing back every optional dependency to all of their dependencies is to remove the optional:"true" tag temporarily and see what error dig returns when the container is resolved.

Instead, I contend that when an optional dependency does have a constructor Provided to the container, either (1) an error should always be returned if the optional dependency's dependencies cannot be met, or (2) a flag should able to be provided to the container (maybe some "strict" mode?) for cause optional dependency errors to be returned for easy debuggability.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions