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

通过gccgo编译出的go程序不能正确的获取go版本 #335

Closed
WhereIsOops opened this issue Mar 22, 2023 · 3 comments · Fixed by #342
Closed

通过gccgo编译出的go程序不能正确的获取go版本 #335

WhereIsOops opened this issue Mar 22, 2023 · 3 comments · Fixed by #342
Labels
🐞 bug Something isn't working

Comments

@WhereIsOops
Copy link

WhereIsOops commented Mar 22, 2023

Describe the bug
目前在pkg/proc/proc.go中是使用"debug/buildinfo"的ReadFile方法获取go版本信息,但是程序是用gccgo编译出来的,不能读取到任何信息

@cfc4n cfc4n added the 🐞 bug Something isn't working label Mar 22, 2023
@cfc4n
Copy link
Member

cfc4n commented Mar 23, 2023

应该存在这个问题,之前没考虑到这个场景,空了我测下,感谢反馈。

@cfc4n cfc4n linked a pull request Apr 5, 2023 that will close this issue
@cfc4n
Copy link
Member

cfc4n commented Apr 5, 2023

我测试了一下,好像没问题,可以正常工作,你看下PR #342

@cfc4n
Copy link
Member

cfc4n commented Apr 7, 2023

我又确认了一下,golang官方也是这么写的。

https://github.com/golang/go/blob/39986d28e435d23e1d1dc41b8f16c0cf30181208/src/cmd/go/internal/version/version.go#L143-L167

	bi, err := buildinfo.ReadFile(file)
	if err != nil {
		if mustPrint {
			if pathErr := (*os.PathError)(nil); errors.As(err, &pathErr) && filepath.Clean(pathErr.Path) == filepath.Clean(file) {
				fmt.Fprintf(os.Stderr, "%v\n", file)
			} else {

				// Skip errors for non-Go binaries.
				// buildinfo.ReadFile errors are not fine-grained enough
				// to know if the file is a Go binary or not,
				// so try to infer it from the file mode and extension.
				if isGoBinaryCandidate(file, info) {
					fmt.Fprintf(os.Stderr, "%s: %v\n", file, err)
				}
			}
		}
		return
	}

	fmt.Printf("%s: %s\n", file, bi.GoVersion)
	bi.GoVersion = "" // suppress printing go version again
	mod := bi.String()
	if *versionM && len(mod) > 0 {
		fmt.Printf("\t%s\n", strings.ReplaceAll(mod[:len(mod)-1], "\n", "\n\t"))
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants