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

fix: command gf build does not accept file path from command line #3417

Closed
wants to merge 7 commits into from

Conversation

clh021
Copy link

@clh021 clh021 commented Mar 22, 2024

问题描述:

gf build -mod vendor \
	-v 0.0.1 \
	-n detect_hardware_os \
	-a amd64,arm64 -s linux \
	-p ./bin \
	-e "-trimpath -ldflags '\
	-X \"github.com/clh021/detect_hardware_os/service/cmd/version.BuildTime=${buildTime}\" \
	-X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitTime=${gitTime}\" \
	-X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitHash=${gitHash}\" \
	-X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitCount=${gitCount}\" \
	'" 
	cmd/v3/main.go

经多次测试只要添加 -X 参数就会出现这个问题。

所以看了下源码:

// cmd/gf/internal/cmd/cmd_build.go:L158-L166
	if file == "" {
		file = parser.GetArg(2).String()
		// Check and use the main.go file.
		if gfile.Exists(cBuildDefaultFile) {
			file = cBuildDefaultFile
		} else {
			mlog.Fatal("build file path is empty or main.go not found in current working directory")
		}
	}

源码中只判断了 cBuildDefaultFile 是否存在,存在就用默认的,不存在就退出了。
这样前面 file = parser.GetArg(2).String() 这一行代码就失去了存在的意义。

本次 PR 适应没有 -X 参数和 有 -X 参数的情况。

@houseme houseme requested review from gqcn and hailaz March 23, 2024 14:06
} else {
mlog.Fatal("build file path is empty or main.go not found in current working directory")
// Check and use Arg(2) as the file path.
if !gfile.Exists(file) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我想是否判断一下file为空,如果为空的话直接赋值cBuildDefaultFile,后续流程直接使用变量file就好?

if file == "" {
    file = cBuildDefaultFile
}

Copy link
Author

@clh021 clh021 Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按照您的设想,我调整代码进行测试

	fmt.Println("------------  in --------------------")
	g.Dump(in)
	fmt.Println("------------  parser --------------------")
	g.Dump(parser.GetArgAll())
	if file == "" {
		// file = parser.GetArg(2).String()
		// // Check and use the main.go file.
		// if gfile.Exists(cBuildDefaultFile) {
			file = cBuildDefaultFile
		// } else {
		// 	mlog.Fatal("build file path is empty or main.go not found in current working directory")
		// }
	}

测试结果如下:

gf build -mod vendor \
-v 0.0.19 \
-n detect_hardware_os \
-a amd64,arm64 -s linux \
-p ./bin \
-e "-trimpath -ldflags '\
-X \"github.com/clh021/detect_hardware_os/service/cmd/version.BuildTime=2024-03-26 09:51:48\" \
-X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitTime=2024-03-22 15:19:40\" \
-X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitHash=8ae7ca71106991a4268\" \
-X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitCount=19\" \
'" \
cmd/v3/main.go
// --out
------------  in --------------------
{
    File:          "",
    Name:          "detect_hardware_os",
    Version:       "0.0.19",
    Arch:          "amd64,arm64",
    System:        "linux",
    Output:        "",
    Path:          "./bin",
    Extra:         "-trimpath -ldflags '-X \"github.com/clh021/detect_hardware_os/service/cmd/version.BuildTime=2024-03-26 09:51:48\" -X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitTime=2024-03-22 15:19:40\" -X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitHash=8ae7ca71106991a4268\" -X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitCount=19\" '",
    Mod:           "vendor",
    Cgo:           false,
    VarMap:        {},
    PackSrc:       "",
    PackDst:       "internal/packed/build_pack_data.go",
    ExitWhenError: false,
    DumpENV:       false,
}
------------  parser --------------------
[
    "/usr/local/hostbin/gf",
    "build",
    "cmd/v3/main.go",
]

再次尝试去除参数的测试

$ gf build -mod vendor \
-v 0.0.19 \
-n detect_hardware_os \
-a amd64,arm64 -s linux \
-p ./bin \
-e "-trimpath" \         
cmd/v3/main.go  
------------  in --------------------
{
    File:          "",
    Name:          "detect_hardware_os",
    Version:       "0.0.19",
    Arch:          "amd64,arm64",
    System:        "linux",
    Output:        "",
    Path:          "./bin",
    Extra:         "-trimpath",
    Mod:           "vendor",
    Cgo:           false,
    VarMap:        {},
    PackSrc:       "",
    PackDst:       "internal/packed/build_pack_data.go",
    ExitWhenError: false,
    DumpENV:       false,
}
------------  parser --------------------
[
    "gf",
    "build",
    "cmd/v3/main.go",
]
2024-03-26 09:57:09.283 start building...
2024-03-26 09:57:09.283 go build -o ./bin/0.0.19/linux_amd64/detect_hardware_os -mod=vendor -trimpath main.go
2024-03-26 09:57:09.288 failed to build, os:linux, arch:amd64, error:
cannot find module providing package main.go: import lookup disabled by -mod=vendor

you may use command option "--debug" to enable debug info and check the details

去除所有 -e 参数的测试

$ gf build -mod vendor \
-v 0.0.19 \
-n detect_hardware_os \
-a amd64,arm64 -s linux \
-p ./bin \
cmd/v3/main.go
------------  in --------------------
{
    File:          "cmd/v3/main.go",
    Name:          "detect_hardware_os",
    Version:       "0.0.19",
    Arch:          "amd64,arm64",
    System:        "linux",
    Output:        "",
    Path:          "./bin",
    Extra:         "",
    Mod:           "vendor",
    Cgo:           false,
    VarMap:        {},
    PackSrc:       "",
    PackDst:       "internal/packed/build_pack_data.go",
    ExitWhenError: false,
    DumpENV:       false,
}
------------  parser --------------------
[
    "gf",
    "build",
    "cmd/v3/main.go",
]
2024-03-26 09:57:21.770 start building...

经测试,前面的 parser.GetArg(2).String() 目前来说是必要的。
parser 的参数更为可靠。只要出现了 -e 参数就会丢失 File 参数。(这让我想到了我此前发现的另一个问题,命令行参数解析的部分太过复杂,我没能找出具体原因)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我多测试了一下,发现只要把 cmd/v3/main.go 的参数放在所有选项的前面 in.File 就可以接收到参数。
换句话说,要求参数必须在所有选项的前面。这和我平时用 go build 的习惯略有差异。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,感谢你的验证,我来看看呢。

@gqcn gqcn changed the title fix: cmd/gf cmd_build.go use arg(2) like ./cmd/v1/main.go as build file fix: command gf build does not accept file path from command line Mar 25, 2024
gqcn added a commit that referenced this pull request Mar 27, 2024
@gqcn
Copy link
Member

gqcn commented Mar 27, 2024

@clh021 Hello, this issue is also be fixed in #3429

@gqcn gqcn closed this Mar 27, 2024
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

Successfully merging this pull request may close these issues.

None yet

2 participants