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
Closed

os: Open("/") fails on Windows #35492

apmckinlay opened this issue Nov 10, 2019 · 8 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows

Comments

@apmckinlay
Copy link

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
Copy link
Contributor

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

@networkimprov
Copy link

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
@agnivade agnivade added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 11, 2019
@alexbrainman
Copy link
Member

@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
Copy link
Author

apmckinlay commented Nov 12, 2019 via email

@networkimprov
Copy link

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

@mattn
Copy link
Member

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
Copy link
Author

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

@gopherbot
Copy link
Contributor

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

@golang golang locked and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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

6 participants