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
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@BlackINT3
Copy link

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 katiehockman added OS-Windows WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Mar 19, 2019
@katiehockman
Copy link
Contributor

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

/cc @ianlancetaylor @bcmills

@BlackINT3
Copy link
Author

@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? :)

@katiehockman katiehockman removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 21, 2019
@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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 28, 2019
@bcmills bcmills added this to the Unplanned milestone Mar 28, 2019
@bcmills
Copy link
Contributor

bcmills 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
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

3 participants