Skip to content

path/filepath: IsAbs considers \\host\share as a relative path but should be recognized as an absolute path #47123

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

Closed
MichaelEischer opened this issue Jul 10, 2021 · 9 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

@MichaelEischer
Copy link

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

$ go version
go version go1.16.5 windows/amd64

Does this issue reproduce with the latest release?

1.16.5 is the latest release at the moment.

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\Michael\AppData\Local\go-build
set GOENV=C:\Users\Michael\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Michael\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Michael\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.5
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\Michael\AppData\Local\Temp\go-build1869960865=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
        "fmt"
        "path/filepath"
)

func main() {
        fmt.Println(filepath.IsAbs(`\\host\share`))
        fmt.Println(filepath.IsAbs(`\\host\share\`))
}

What did you expect to see?

true
true

What did you see instead?

false
true

According to https://docs.microsoft.com/en-us/dotnet/standard/io/file-path-formats#unc-paths "UNC paths must always be fully qualified." which suggests that there are no relative UNC paths, and thus \\host\share should also be considered as absolute path.

@odeke-em odeke-em changed the title filepath.IsAbs considers \\host\share as relative path path/filepath: IsAbs considers \\host\share as a relative path but should be recognized as an absolute path Jul 11, 2021
@odeke-em odeke-em added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows labels Jul 11, 2021
@odeke-em
Copy link
Member

Thank you for the bug report @MichaelEischer! Kindly cc-ing @alexbrainman @zx2c4 for when y’all have some spare time/cycles.

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/333911 mentions this issue: path/filepath: fix IsAbs considers \\host\share as a relative path

@alexbrainman
Copy link
Member

Thank you for bug report @MichaelEischer .

You might have a point with your reasoning, but I am not convinced we should change current behaviour.

Consider this: \\host\share\ refers to a root directory of that share. What does \\host\share reefer to? Same root directory? I am not convinced. \\host\share is more like C: - it is more like a drive letter.

Also, path/filepath.IsAbs(path/filepath.Volume(x)) currently always returns false regardless if path is UNC or not. This symmetry will break, if we change path/filepath.IsAbs behaviour.

Also let's hear what @mattn has to say about this. He, probably, wrote original code.

Alex

@MichaelEischer
Copy link
Author

I'd say that \\host\share and \\host\share\ refer to the same directory, as there are no relative UNC paths. But I also agree that \\host\share conceptually seems to be more of a volume than a directory. Interestingly dir \\host\share\ on Windows prints that the directory listing is for volume and directory \\host\share, whereas for dir C:\ I get volume C: and directory C:\.

Doesn't the IsAbs/Volume asymmetry already apply to filepath.Rel after the changes in #41230?

filepath.Rel(`C:`, `C:\dir`)

doesn't work, whereas

filepath.Rel(`\\host\share`, `\\host\share\file.txt`)

does.

@180909
Copy link
Contributor

180909 commented Jul 15, 2021

Maybe this can give some help.

@mattn
Copy link
Member

mattn commented Jul 15, 2021

I agree with this change. \\host\share is not like C:. IsAbs have not to return result depend on last backslash for UNC paths. The syntax that can represent a relative path is only available if the path is C:.

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char* argv[]) {
  char* paths[] = {
    "C:",
    "C:\\",
    "C:temp",
    "C:\\temp",
    "\\\\127.0.0.1",
    "\\\\127.0.0.1\\",
    "\\\\127.0.0.1\\C$",
    "\\\\127.0.0.1\\C$\\",
    NULL
  };
  for (char **p = paths; *p; p++) {
    char path[_MAX_PATH];
    char* res = _fullpath(path, *p, sizeof(path));
    printf("%s is %s\n", *p, res);
  }
  return 0;
}
C: is C:\temp
C:\ is C:\
C:temp is C:\temp\temp
C:\temp is C:\temp
\\127.0.0.1 is \\127.0.0.1
\\127.0.0.1\ is \\127.0.0.1\
\\127.0.0.1\C$ is \\127.0.0.1\C$
\\127.0.0.1\C$\ is \\127.0.0.1\C$\

(This is obviously thing)

@mattn
Copy link
Member

mattn commented Jul 15, 2021

But https://golang.org/cl/333911 seems wrong code. IsAbs should be separated for Windows.

Ah, it's wrong (fixing path not path/filepath)

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/334809 mentions this issue: path/filepath: fix IsAbs considers \\host\share as a relative path

@alexbrainman
Copy link
Member

Doesn't the IsAbs/Volume asymmetry already apply to filepath.Rel after the changes in #41230?

Indeed.

Alex

@golang golang locked and limited conversation to collaborators Aug 18, 2022
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
6 participants