Skip to content

path/filepath: Rel returns error in Windows if no Separator after drive #13208

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
amkgo opened this issue Nov 11, 2015 · 6 comments
Closed

path/filepath: Rel returns error in Windows if no Separator after drive #13208

amkgo opened this issue Nov 11, 2015 · 6 comments

Comments

@amkgo
Copy link

amkgo commented Nov 11, 2015

Windowns 8.1
Go 1.5.1

filepath.Rel() will return error if there is no Separator at the end of disk letter:

fmt.Prinln(filepath.Rel(`C:`, `C:\Windows`) )
// output: Rel: can't make \Windows relative to

It's OK only if there is a Separator at the end:

fmt.Prinln(filepath.Rel(`C:\`, `C:\Windows`) )
// output: Windows <nil>

Is this behavior as expected or an issue?

Andrew

@ianlancetaylor ianlancetaylor changed the title filepath.Rel() returns error in Windows path/filepath: Rel returns error in Windows if no Separator after drive Nov 11, 2015
@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Nov 11, 2015
@ianlancetaylor
Copy link
Member

CC @alexbrainman

@alexbrainman
Copy link
Member

I think the failure of filepath.Rel(C:, C:\Windows) is intentional. godoc for Rel says:

An error is returned if targpath can't be made relative to basepath or if knowing the current working directory would be necessary to compute it.

C: is relative basepath - you would need to know the current directory to decide how to walk from it into C:\Windows.

The error message needs work (filled issue #13259 for that).

Please reopen, if you think I missed something.

Alex

@AngangGuo
Copy link

I think C: means C:\ in Windows system. If the base directory is C:\Go then it must be written as filepath.Rel("C:\Go", "C:\Windows").

Like the example in filepath.Rel() that filepath.Rel("/a","/a/b/c") is b/c. I'll get the same result when I run it no matter where my current directory is. The "/a" means the current directory of "/a", that is "/a/."(dot) or simply "/a/". Likewise, C: should means the current directory of C: drive, that should be "C:."(dot) or simply "C:".

That said, the basepath in filepath.Rel() means the current folder(path) of the basepath. Rel() should add a Separator to the basepath if there is none.

Andrew

@alexbrainman
Copy link
Member

I think C: means C:\ in Windows system. ...

cmd.exe thinks differently:

C:\a>dir
 Volume in drive C has no label.
 Volume Serial Number is ABCD-EFGH

 Directory of C:\a

20/11/2015  04:30 PM    <DIR>          .
20/11/2015  04:30 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)          305,408 bytes free

C:\a>dir c:
 Volume in drive C has no label.
 Volume Serial Number is ABCD-EFGH

 Directory of C:\a

20/11/2015  04:30 PM    <DIR>          .
20/11/2015  04:30 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)          305,408 bytes free

C:\a>u:

U:\>dir c:
 Volume in drive C has no label.
 Volume Serial Number is ABCD-EFGH

 Directory of C:\a

20/11/2015  04:30 PM    <DIR>          .
20/11/2015  04:30 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)          305,408 bytes free

U:\>c:

C:\a>dir c:
 Volume in drive C has no label.
 Volume Serial Number is ABCD-EFGH

 Directory of C:\a

20/11/2015  04:30 PM    <DIR>          .
20/11/2015  04:30 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)          305,408 bytes free

C:\a>

Likewise, "C:" should means the current directory of "C:" drive, that should be "C:." or simply "C:".

This is wrong too. "C:" means "C:.".

Alex

@AngangGuo
Copy link

I got it. You are right.

Maybe we need to note this special case in the Rel() document and let the user add the Separator themselves if they intend to the root of the VolumnName. Or make it default to root if it's just VolumnName.

Andrew

@alexbrainman
Copy link
Member

C: has no meaning unless you know what the "C: current directory" is. This is a general filepath concept. I don't see how it is useful to mention this is Rel documentation.

Alex

@golang golang locked and limited conversation to collaborators Nov 27, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants