Skip to content

Decorate does not work if inside fx.Module and Populate is called from outside Module #1222

Description

@CTrando

Describe the bug

First, we can:

  • create an fx.Module
  • provide a type A inside the module
  • decorate the type A inside the module
  • outside the fx.Module, populate type A

The result is that the decorate function would not have been applied.

To Reproduce

I have a testcase to reproduce this:

func TestReproPopulate(t *testing.T) {
	t.Parallel()

	var something string
	app := fxtest.New(
		t,
		fx.Module("something",
			fx.Provide(func() string {
				return "something"
			}),
			fx.Decorate(func(h string) string {
				return "else"
			}),
		),
		fx.Populate(&something),
	)
	app.RequireStart()
	app.RequireStop()

	require.Equal(t, "else", something)
}

I expect that the decorate statement would have run to replace the string to else, but it stays as something. As a result, this testcase fails. I'm on the latest version of fx (v1.22.1).

However, this passes:

func TestReproPopulate(t *testing.T) {
	t.Parallel()

	var something string
	app := fxtest.New(
		t,
		fx.Module("something",
			fx.Provide(func() string {
				return "something"
			}),
		),
		fx.Decorate(func(h string) string {
			return "else"
		}),
		fx.Populate(&something),
	)
	app.RequireStart()
	app.RequireStop()

	require.Equal(t, "else", something)
}

Expected behavior

I expect that the decorate inside the module still runs, and we populate the type after the decorate executes. The testcase above should pass.

Additional context

I believe this is because fx.Populate runs as an fx.Invoke, which runs at the top level scope and doesn't look at decorators in the child scope.

Activity

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

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