Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- No changes yet.

### Fixed
- Errors from building an annotated function passed to `fx.Decorate` or `fx.Replace`
are now reported instead of being silently ignored together with the decorator.

## [1.24.0](https://github.com/uber-go/fx/compare/v1.23.0...v1.24.0) - 2025-05-13

Expand Down
6 changes: 4 additions & 2 deletions decorate.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ func runDecorator(c container, d decorator, opts ...dig.DecorateOption) (err err

switch decorator := decorator.(type) {
case annotated:
if dcor, derr := decorator.Build(); derr == nil {
err = c.Decorate(dcor, opts...)
var dcor any
if dcor, err = decorator.Build(); err != nil {
return
}
err = c.Decorate(dcor, opts...)
default:
err = c.Decorate(decorator, opts...)
}
Expand Down
24 changes: 24 additions & 0 deletions decorate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package fx_test

import (
"errors"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -442,6 +443,29 @@ func TestDecorateFailure(t *testing.T) {
assert.Contains(t, err.Error(), "major sadness")
})

t.Run("annotation build error on a decorator is reported", func(t *testing.T) {
type Logger struct {
Name string
}

app := NewForTest(t,
fx.Provide(func() *Logger {
return &Logger{Name: "root"}
}),
fx.Decorate(fx.Annotate(
func(l *Logger) *Logger { return l },
// *Logger does not implement io.Reader, so building
// this annotated decorator must fail.
fx.As(new(io.Reader)),
)),
fx.Invoke(func(l *Logger) {}),
)

err := app.Err()
require.Error(t, err)
assert.Contains(t, err.Error(), "does not implement")
})

t.Run("all decorator dependencies must be provided", func(t *testing.T) {
type Logger struct {
Name string
Expand Down