dengsgo / go-decorator

让 Go 便捷使用装饰器的工具,装饰器能够切面 (AOP)、代理 (Proxy) 任意的函数和方法,提供观察和控制函数的能力。go-decorator is a tool that allows Go to easily use decorators. The decorator can slice aspect (AOP) and proxy any function and method, providing the ability to observe and control functions.
MIT License
60 stars 3 forks source link

[Issue] Compile Error: misplaced compiler directive #8

Open Min8Yue opened 2 weeks ago

Min8Yue commented 2 weeks ago

I tried to use nested annotations, a compilation error was reported, and the error message was not enough for me to locate the problem. When I changed the location of the function in the source code, the problem disappeared. Source Code

package main

import (
    "context"
    "fmt"

    "github.com/dengsgo/go-decorator/decor"
)

func main() {
    Processor()
}

func Processor() {
    ctx := context.Background()
    DM(ctx, "a")
}

//go:decor Decor
func RpcRank(ctx context.Context, input string) (output string, err error) {
    fmt.Println("RpcRank")
    return input, nil
}

//go:decor Decor
func Rank(ctx context.Context, input string) (output string, err error) {
    fmt.Println("rank")
    RpcRank(ctx, input)
    return input, nil
}

//go:decor Decor
func DM(ctx context.Context, input string) (output string, err error) {
    fmt.Println("dm")
    Rank(ctx, input)
    return "a", err
}

func Decor(ctx *decor.Context) {
    fmt.Println("a")
    ctx.TargetDo()
}

output

x go run -toolexec decorator main.go
# command-line-arguments
./main.go:22: misplaced compiler directive
command-line-arguments: open /var/folders/3g/8n4r6n1d5b1fz6phh1rpfmx80000gp/T/go-build3187272546/b001/_pkg_.a: no such file or directory

Enviroment go version go1.22.5 darwin/arm64

dengsgo commented 2 weeks ago

x go run -toolexec decorator main.go

command-line-arguments

./main.go:22: misplaced compiler directive command-line-arguments: open /var/folders/3g/8n4r6n1d5b1fz6phh1rpfmx80000gp/T/go-build3187272546/b001/pkg.a: no such file or directory

这个错误通常是在使用 go-decorator 之前有编译过此代码,go 的旧编译缓存被引用导致的。

可以尝试使用 -a 标志强制重新编译一次来解决。在这个 case 中,可以使用命令: 

$ go run -toolexec decorator -a main.go

This error is usually caused by compiling the code before using go-decorator, and the old compilation cache of go is being referenced.

Try forcing a recompile with the -a flag. In this case, you can use the command: 

$ go run -toolexec decorator -a main.go
Min8Yue commented 2 weeks ago

相同的源码,还是无法通过编译. 你可以尝试复现吗?

$ go run -toolexec decorator -a main.go
# command-line-arguments
./main.go:22: misplaced compiler directive
command-line-arguments: open /var/folders/3g/8n4r6n1d5b1fz6phh1rpfmx80000gp/T/go-build2815434048/b001/_pkg_.a: no such file or directory
dengsgo commented 2 weeks ago

相同的源码,还是无法通过编译. 你可以尝试复现吗?

$ go run -toolexec decorator -a main.go
# command-line-arguments
./main.go:22: misplaced compiler directive
command-line-arguments: open /var/folders/3g/8n4r6n1d5b1fz6phh1rpfmx80000gp/T/go-build2815434048/b001/_pkg_.a: no such file or directory

看报错似乎不是 decorator 的问题。使用 go build -toolexec decorator 编译试试看。