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

[Issue] Compile Error: misplaced compiler directive #8

Open
Min8Yue opened this issue Nov 5, 2024 · 3 comments
Open

[Issue] Compile Error: misplaced compiler directive #8

Min8Yue opened this issue Nov 5, 2024 · 3 comments

Comments

@Min8Yue
Copy link

Min8Yue commented Nov 5, 2024

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
Copy link
Owner

dengsgo commented Nov 5, 2024

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
Copy link
Author

Min8Yue commented Nov 5, 2024

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

$ 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
Copy link
Owner

dengsgo commented Nov 5, 2024

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

$ 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 编译试试看。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants