Skip to content

Commit

Permalink
fix: apply patches in ascending order (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
matino authored Apr 17, 2024
1 parent ee5b439 commit 4464c70
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/testdata/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,16 @@ func Closure(ctx context.Context) (int, error) {
a := func(x int) (int, error) { return x + 1, nil }
return a(5)
}

func FunctionCallingAnonymousFunc(ctx context.Context) error {
if err := Exec(ctx, func(ctx context.Context) error {
return nil
}); err != nil {
return err
}
return nil
}

func Exec(ctx context.Context, fn func(ctx context.Context) error) error {
return fn(ctx)
}
22 changes: 22 additions & 0 deletions internal/testdata/instrumented/basic.go.exp
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,25 @@ func Closure(ctx context.Context) (int, error) {
a := func(x int) (int, error) { return x + 1, nil }
return a(5)
}

func FunctionCallingAnonymousFunc(ctx context.Context) error {
ctx, span := otel.Tracer("app").Start(ctx, "FunctionCallingAnonymousFunc")
defer span.End()

if err := Exec(ctx, func(ctx context.Context) error {
ctx, span := otel.Tracer("app").Start(ctx, "anonymous")
defer span.End()

return nil
}); err != nil {
return err
}
return nil
}

func Exec(ctx context.Context, fn func(ctx context.Context) error) error {
ctx, span := otel.Tracer("app").Start(ctx, "Exec")
defer span.End()

return fn(ctx)
}
5 changes: 5 additions & 0 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"go/ast"
"go/token"
"go/types"
"sort"

"golang.org/x/tools/go/ast/astutil"
)
Expand Down Expand Up @@ -185,6 +186,10 @@ func (p *Processor) Process(fset *token.FileSet, file *ast.File) error {
})

if len(patches) > 0 {
// Patches must be applied in the ascending order, otherwise the
// modified source file will become corrupted.
sort.Slice(patches, func(i, j int) bool { return patches[i].pos < patches[j].pos })

if err := patchFile(fset, file, patches...); err != nil {
return err
}
Expand Down

0 comments on commit 4464c70

Please sign in to comment.