Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: Add FAQ page #1160

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ module.exports = {
'modules.md',
],
},
['faq.md', 'FAQ'],
{
title: 'Community',
children: [
Expand Down
47 changes: 47 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Frequently Asked Questions

This page contains answers to common questions and issues with using Fx.

## Does the order of `fx.Option`s matter?

No, the order in which you provide various Fx options
to `fx.Options`, `fx.New`, `fx.Module`, and others does not matter.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is true for determining the order in which fx.Invoked functions get run, which was a recent point of confusion (ref: #1152). Consider exempting Invoke from this statement.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. There's a weird situation here.
On paper, there's no guarantee about the order in which invokes run.
But you're right, their order matters.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JacobOaks I realized that this was actually too simplified and lost some information.
Ordering of options relative to fx.New is irrelevant, but ordering of operations relative to each other can matter.
I've rewritten this section to better clarify this.

LMK what you think

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looks like we do document the fx.Invoke invocation order.
I'll update this entry.


Ordering is determined by dependencies,
and dependencies are determined by function parameters and return types.

If `ParseConfig` returns a `*Config`,
and `NewLogger` accepts a `*Config` parameter,
then `ParseConfig` will always run before `NewLogger`.

The following are all equivalent:

```go
fx.Options(fx.Provide(ParseConfig, NewLogger))
fx.Options(fx.Provide(NewLogger, ParseConfig))
fx.Options(fx.Provide(ParseConfig), fx.Provide(NewLogger))
fx.Options(fx.Provide(NewLogger), fx.Provide(ParseConfig))
```

## Why does `fx.Supply` not accept interfaces?

This is a technical limitation of how reflection in Go works.
Suppose you have:

```go
var redisClient ClientInterface = &redis.Client{ ... }
```

When you call `fx.Supply(redisClient)`,
the knowledge that you intended to use this as a `ClientInterface` is lost.
Fx has to use runtime reflection to inspect the type of the value,
and at that point the Go runtime only tells it that it’s a `*redis.Client`.

You can work around this with the `fx.Annotate` function
and the `fx.As` annotation.

```go
fx.Supply(
fx.Annotate(redisClient, fx.As(new(ClientInterface))),
)
```
21 changes: 21 additions & 0 deletions supply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
package fx_test

import (
"bytes"
"errors"
"io"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -110,6 +112,25 @@ func TestSupply(t *testing.T) {
require.Same(t, thirdIn, out.Third)
})

t.Run("AnnotateIsSupported", func(t *testing.T) {
t.Parallel()

var out struct {
fx.In

Got io.Writer
}

var give bytes.Buffer
app := fxtest.New(t,
fx.Supply(fx.Annotate(&give, fx.As(new(io.Writer)))),
fx.Populate(&out),
)
defer app.RequireStart().RequireStop()

require.Same(t, &give, out.Got)
})

t.Run("InvalidArgumentIsSupplied", func(t *testing.T) {
t.Parallel()

Expand Down
Loading