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: Open("/") fails on Windows #35492

Closed
apmckinlay opened this issue Nov 10, 2019 · 8 comments

Comments

@apmckinlay
Copy link

@apmckinlay apmckinlay commented Nov 10, 2019

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

$ go version
go version go1.13.4 windows/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\andrew\AppData\Local\go-build
set GOENV=C:\Users\andrew\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\andrew\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64

What did you do?

package main

import (
	"fmt"
	"os"
)

func main() {
	_, err := os.Open("/")
	fmt.Println(err)
}

What did you expect to see?

I expected it to succeed, i.e. err == nil

What did you see instead?

It failed with:

open /: The filename, directory name, or volume label syntax is incorrect.

It works (err == nil) if you use a backslash:

os.Open(`\`)

file_windows.go openDir has:

if len(path) == 2 && path[1] == ':' || 
	(len(path) > 0 && path[len(path)-1] == '\\') { // it is a drive letter, like C:
	mask = path + `*`
} else {
	mask = path + `\*`
}

Since it only checks for trailing back slash, for forward slash "/" it adds \* resulting in /\* which is invalid.

Obviously, the workaround is to use back slash instead of forward slash. But since forward slashes work in file paths in many/most other places, it might be worth checking for either slash in openDir.

@agnivade

This comment has been minimized.

Copy link
Contributor

@agnivade agnivade commented Nov 11, 2019

Is this a new thing, or forward slashes were always supported in Windows ?

@networkimprov

This comment has been minimized.

Copy link

@networkimprov networkimprov commented Nov 11, 2019

Back- and forward-slashes have long been interchangeable on Windows.

cc @alexbrainman @mattn @zx2c4
@gopherbot add OS-Windows

@agnivade agnivade changed the title os.Open("/") fails on Windows os: Open("/") fails on Windows Nov 11, 2019
@alexbrainman

This comment has been minimized.

Copy link
Member

@alexbrainman alexbrainman commented Nov 12, 2019

@apmckinlay yes both / and \ should work in this situation.

Would you like to send a fix for this? This https://golang.org/doc/contribute.html is how to contribute.

Thank you.

Alex

@apmckinlay

This comment has been minimized.

Copy link
Author

@apmckinlay apmckinlay commented Nov 12, 2019

@networkimprov

This comment has been minimized.

Copy link

@networkimprov networkimprov commented Nov 12, 2019

You could post to golang-dev asking if anyone would like to implement this; that's probably the quickest path to a patch...

@mattn

This comment has been minimized.

Copy link
Member

@mattn mattn commented Nov 13, 2019

Ah, I did not read this thread. I already have my changes to fix this on my local PC. @apmckinlay do you try this soon? (please feel free to answer).

@apmckinlay

This comment has been minimized.

Copy link
Author

@apmckinlay apmckinlay commented Nov 13, 2019

@mattn If you have a fix already prepared that's great, go ahead. (I am travelling so I would be slow)

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Nov 13, 2019

Change https://golang.org/cl/206997 mentions this issue: os: handle backslash and slash both in the path on Windows

@gopherbot gopherbot closed this in e77106c Nov 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.