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

cmd/go: 'build -o' overwrite behavior differs between Linux and Windows #30837

Open
BlackINT3 opened this issue Mar 14, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@BlackINT3
Copy link

commented Mar 14, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12 windows/amd64
go version go1.12 linux/amd64

Does this issue reproduce with the latest release?

yes

What did you do?

When exec "build -o dst_file" on windows casually, i got an error:
go build go-test: build output "dst_file" already exists and is not an object file.
I found dst_file allready exists and is an empty file,but it works on linux in the same case.

After reviewed code related build, I found the code snippet:
b.moveOrCopyFile(a.Target, a1.built, perm, false)
force is false, and moveOrCopyFile rename src to dst on linux, and not check the param force.

What did you expect to see?

It should be the same on different os, I think param force should be retain semantically, eg:

			
if err := os.Chmod(src, mode); err == nil {
+	// Be careful about removing/overwriting dst.
+	// Do not remove/overwrite if dst exists and is a directory
+	// or a non-object file.
+	if fi, err := os.Stat(dst); err == nil {
+		if fi.IsDir() {
+			return fmt.Errorf("build output %q already exists and is a directory", dst)
+		}
+		if !force && fi.Mode().IsRegular() && !isObject(dst) {
+			return fmt.Errorf("build output %q already exists and is not an object file", dst)
+		}
+	}
	if err := os.Rename(src, dst); err == nil {
		if cfg.BuildX {
			b.Showcmd("", "mv %s %s", src, dst)
		}
		return nil
	}
}

Can someone give me some advice?
:) thanks.

@katiehockman

This comment has been minimized.

Copy link
Contributor

commented Mar 19, 2019

@BlackINT3 can you provide an example program that can reproduce this error?

/cc @ianlancetaylor @bcmills

@BlackINT3

This comment has been minimized.

Copy link
Author

commented Mar 21, 2019

@katiehockman
umm... If target file allready exsits and not a object file, build will be failed. But it works on linux.
Whether shoud check it on linux? :)

@bcmills bcmills changed the title cmd/go: build moveOrCopyFile dst forcely on linux cmd/go: 'build -o' overwrite behavior differs between Linux and Windows Mar 28, 2019

@bcmills bcmills added this to the Unplanned milestone Mar 28, 2019

@bcmills

This comment has been minimized.

Copy link
Member

commented Mar 28, 2019

A lot of filesystem behavior differs across platforms, and statting the output file has a (small but nonzero) latency cost.

Could you describe some situations where this particular difference matters in practice?

CC @jayconrod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.