Skip to content

Root container provided function may look provided parameter in child scope too to resolve it's params #374

Description

@alexisvisco

Describe the bug
When a invoker need a parameter that is provided by the root container, and the provided function need a param that is provided by the child scope : the parameter needed for invoking the function will not be resolved

To Reproduce

package main

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

type config struct {
	str               string
	boolPassedInScope bool
}

func main() {

	mainContainer := dig.New()

	mainContainer.Provide(func() string { return "Hello" })

	// boolPassedInScope will be passed in the child scope
	mainContainer.Provide(func(helloStr string, boolPassedInScope bool) config {
		return config{str: helloStr, boolPassedInScope: boolPassedInScope}
	})

	scope := mainContainer.Scope("newscope")

	scope.Provide(func() bool { return true })

	// this will fail because boolPassedInScope is not provided in the main scope
	// and the config cannot be resolved due to the fact that in the mainContainer there
	// is no boolean provided
	err := scope.Invoke(func(c config) {
		fmt.Println(c.str)
		fmt.Println(c.boolPassedInScope)
	})

	if err != nil {
		panic(err)
	}

}

Expected behavior
I don't know if it's possible but without adding dig.Export to the line scope.Provide(func() bool { return true }) the provided function to create the config should look in the child scope to find the boolPassedInScope.

Additional context
Maybe my approach is wrong but in my application framework I need to pass context, logger that is scoped because I launch multiple invoke in parallel so I can't just set them in the root container (because the logger and ctx is customized for the invoker).

Also I can't use named dig parameter because the invoker signature must not change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions