1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 const fileScheme = "file"
20 // URI represents the full URI for a file.
23 func (uri URI) IsFile() bool {
24 return strings.HasPrefix(string(uri), "file://")
27 // Filename returns the file path for the given URI.
28 // It is an error to call this on a URI that is not a valid filename.
29 func (uri URI) Filename() string {
30 filename, err := filename(uri)
34 return filepath.FromSlash(filename)
37 func filename(uri URI) (string, error) {
41 u, err := url.ParseRequestURI(string(uri))
45 if u.Scheme != fileScheme {
46 return "", fmt.Errorf("only file URIs are supported, got %q from %q", u.Scheme, uri)
48 // If the URI is a Windows URI, we trim the leading "/" and lowercase
49 // the drive letter, which will never be case sensitive.
50 if isWindowsDriveURIPath(u.Path) {
51 u.Path = strings.ToUpper(string(u.Path[1])) + u.Path[2:]
56 func URIFromURI(s string) URI {
57 if !strings.HasPrefix(s, "file://") {
61 if !strings.HasPrefix(s, "file:///") {
62 // VS Code sends URLs with only two slashes, which are invalid. golang/go#39789.
63 s = "file:///" + s[len("file://"):]
65 // Even though the input is a URI, it may not be in canonical form. VS Code
66 // in particular over-escapes :, @, etc. Unescape and re-encode to canonicalize.
67 path, err := url.PathUnescape(s[len("file://"):])
72 // File URIs from Windows may have lowercase drive letters.
73 // Since drive letters are guaranteed to be case insensitive,
74 // we change them to uppercase to remain consistent.
75 // For example, file:///c:/x/y/z becomes file:///C:/x/y/z.
76 if isWindowsDriveURIPath(path) {
77 path = path[:1] + strings.ToUpper(string(path[1])) + path[2:]
79 u := url.URL{Scheme: fileScheme, Path: path}
80 return URI(u.String())
83 func CompareURI(a, b URI) int {
93 func equalURI(a, b URI) bool {
97 // If we have the same URI basename, we may still have the same file URIs.
98 if !strings.EqualFold(path.Base(string(a)), path.Base(string(b))) {
101 fa, err := filename(a)
105 fb, err := filename(b)
109 // Stat the files to check if they are equal.
110 infoa, err := os.Stat(filepath.FromSlash(fa))
114 infob, err := os.Stat(filepath.FromSlash(fb))
118 return os.SameFile(infoa, infob)
121 // URIFromPath returns a span URI for the supplied file path.
122 // It will always have the file scheme.
123 func URIFromPath(path string) URI {
127 // Handle standard library paths that contain the literal "$GOROOT".
128 // TODO(rstambler): The go/packages API should allow one to determine a user's $GOROOT.
129 const prefix = "$GOROOT"
130 if len(path) >= len(prefix) && strings.EqualFold(prefix, path[:len(prefix)]) {
131 suffix := path[len(prefix):]
132 path = runtime.GOROOT() + suffix
134 if !isWindowsDrivePath(path) {
135 if abs, err := filepath.Abs(path); err == nil {
139 // Check the file path again, in case it became absolute.
140 if isWindowsDrivePath(path) {
141 path = "/" + strings.ToUpper(string(path[0])) + path[1:]
143 path = filepath.ToSlash(path)
148 return URI(u.String())
151 // isWindowsDrivePath returns true if the file path is of the form used by
152 // Windows. We check if the path begins with a drive letter, followed by a ":".
153 // For example: C:/x/y/z.
154 func isWindowsDrivePath(path string) bool {
158 return unicode.IsLetter(rune(path[0])) && path[1] == ':'
161 // isWindowsDriveURI returns true if the file URI is of the format used by
162 // Windows URIs. The url.Parse package does not specially handle Windows paths
163 // (see golang/go#6027), so we check if the URI path has a drive prefix (e.g. "/C:").
164 func isWindowsDriveURIPath(uri string) bool {
168 return uri[0] == '/' && unicode.IsLetter(rune(uri[1])) && uri[2] == ':'