Skip to content

path/filepath: Rel() returns invalid path if volume case is different #10802

Closed
@pavel-main

Description

@pavel-main

Go version - go1.4.2 windows/amd64
Operating system - Windows 7 64-bit

The problem:

package main

import (
    "path/filepath"
)

func main() {
    relpath, err := filepath.Rel(`E:\Projects\`, `e:\Projects\src`)
    if err != nil {
        println(err.Error())
        return
    }
    println(relpath)
}

Output:

Rel: can't make \Projects\src relative to \Projects

Expected:

src

I've ocassionally found this when some go program's os.Getcwd() call returned me a path with lowercased volume name - not sure why this happened, can't reproduce anymore. Some users, who set their GOPATH to something like e:\Projects might have problems.

Probably, on Windows, VolumeName() should have separate implementation like this:

func VolumeName(path string) string {
    if !isUNC(path) {
        return strings.ToUpper(path[:volumeNameLen(path)])
    }
    return path[:volumeNameLen(path)]
}

Helpful link - Naming Files, Paths, and Namespaces

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions