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

os: investigate why Stat(non-existent-file) on Windows with builders returns error prefixed with "CreateFile" #38841

Closed
odeke-em opened this issue May 4, 2020 · 5 comments

Comments

@odeke-em
Copy link
Member

@odeke-em odeke-em commented May 4, 2020

Coming here from an unexpected trybot failure in https://storage.googleapis.com/go-build-log/ee4c7a10/windows-amd64-2016_e7a90c70.log
and CL 230941

where the relevant code is https://play.golang.org/p/El2eXIywXlu or inlined below

package main

import (
	"log"
	"os"
	"strings"
)

func main() {
	got := "open x.go: no such file or directory\n"
	_, sysNotExistErr := os.Stat("x.go")
	if sysNotExistErr == nil {
		// Oddly the file might exist, do nothing and just report the original failure.
		log.Fatalf("Expected an error %q\n", got)
	}

	want := strings.Replace(sysNotExistErr.Error(), "stat x.go:", "open x.go:", -1) + "\n"
	if got != want {
		log.Fatalf("Expected an error, but got:\n\t%q\nwant:\n\t%q", got, want)
	}
}

but oddly on the Windows builder it returned

# go run run.go -- fixedbugs/issue36437.go
incorrect output
Expected an error, but got:
	"open x.go: The system cannot find the file specified.\n"
want:
	"CreateFile x.go: The system cannot find the file specified."

The "CreateFile x.go" is quite unexpected. Perhaps we should investigate as to how "CreateFile" got invoked?

Kindly cc-ing @alexbrainman.

@muhammadharis
Copy link

@muhammadharis muhammadharis commented May 4, 2020

It seems that the error that is being returned is from this line: https://github.com/golang/go/blob/master/src/os/stat_windows.go#L97

@networkimprov
Copy link

@networkimprov networkimprov commented May 4, 2020

Hi Emmanuel, CreateFile is the WinApi name for Posix open(2). It only creates filesystem entries in certain cases.

Why is the fix for #36437 examining the error string instead of if !os.IsNotExist(err)?

EDIT: oh, it doesn't call os.Stat() directly. I imagine it needs a runtime.GOOS=="windows" variant.

@iWdGo
Copy link
Contributor

@iWdGo iWdGo commented May 6, 2020

On Windows, os.Stat attempts several calls to cope with various file types. Some details on the rationale are here.
The following could help to check error message content.

if !os.IsNotExist(sysNotExistErr) {
	log.Fatalf("Expected a not exist error")
}

s := "x.go:0"
if n := strings.LastIndex(sysNotExistErr.Error(), s); n != -1 {
	log.Fatalf("Line number is unexpected: %s", sysNotExistErr.Error()[n:len(s)])
}

For the records, no index number shows on Windows 10 and go version go1.14.2 windows/amd64 as go run x.go and os.Stat both return CreateFile x.go: The system cannot find the file specified.

@alexbrainman
Copy link
Member

@alexbrainman alexbrainman commented May 9, 2020

CreateFile is the WinApi name for Posix open(2). It only creates filesystem entries in certain cases.

Correct. CreateFile Windows API is generally used to open files

https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea

There is no open syscall on Windows. It will be confusing, if we just blindly replace CreateFile with open string. For example, the errors you see relate directly to CreateFile API, and have different meaning when returned for different APIs.

Alex

@odeke-em
Copy link
Member Author

@odeke-em odeke-em commented May 9, 2020

Got it, thank you @muhammadharis @networkimprov @iWdGo @alexbrainman for the answers! SGTM.

@odeke-em odeke-em closed this May 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.