Skip to content

Commit

Permalink
lsp: URI to path conversions respect os.Separator
Browse files Browse the repository at this point in the history
This was causing an issue in the loading of config files on windows #682

Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 committed May 8, 2024
1 parent f4eb0f8 commit f20d4a3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
3 changes: 3 additions & 0 deletions internal/lsp/uri/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ func ToPath(client clients.Identifier, uri string) string {
}
}

// Convert path to use system separators
path = filepath.FromSlash(path)

return path
}
21 changes: 11 additions & 10 deletions internal/lsp/uri/uri_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uri

import (
"path/filepath"
"testing"

"github.com/styrainc/regal/internal/lsp/clients"
Expand Down Expand Up @@ -91,15 +92,15 @@ func TestURIToPath(t *testing.T) {
}{
"unix unprefixed": {
uri: "/foo/bar",
want: "/foo/bar",
want: filepath.FromSlash("/foo/bar"),
},
"unix simple": {
uri: "file:///foo/bar",
want: "/foo/bar",
want: filepath.FromSlash("/foo/bar"),
},
"windows not encoded": {
uri: "file://c:/foo/bar",
want: "c:/foo/bar",
want: filepath.FromSlash("c:/foo/bar"),
},
}

Expand All @@ -126,32 +127,32 @@ func TestURIToPath_VSCode(t *testing.T) {
}{
"unix unprefixed": {
uri: "/foo/bar",
want: "/foo/bar",
want: filepath.FromSlash("/foo/bar"),
},
"unix simple": {
uri: "file:///foo/bar",
want: "/foo/bar",
want: filepath.FromSlash("/foo/bar"),
},
"windows encoded": {
uri: "file:///c%3A/foo/bar",
want: "c:/foo/bar",
want: filepath.FromSlash("c:/foo/bar"),
},
"unix encoded with space in path": {
uri: "file:///Users/foo/bar%20baz",
want: "/Users/foo/bar baz",
want: filepath.FromSlash("/Users/foo/bar baz"),
},
// these other examples shouldn't happen, but we should handle them
"windows not encoded": {
uri: "file://c:/foo/bar",
want: "c:/foo/bar",
want: filepath.FromSlash("c:/foo/bar"),
},
"windows not prefixed": {
uri: "c:/foo/bar",
want: "c:/foo/bar",
want: filepath.FromSlash("c:/foo/bar"),
},
"windows not prefixed, but encoded": {
uri: "c%3A/foo/bar",
want: "c:/foo/bar",
want: filepath.FromSlash("c:/foo/bar"),
},
}

Expand Down
6 changes: 0 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func FindRegalDirectory(path string) (*os.File, error) {
}

regalDir, err := os.Open(searchPath)

if err == nil {
rdInfo, err := regalDir.Stat()
if err == nil && rdInfo.IsDir() {
Expand All @@ -133,11 +132,6 @@ func FindRegalDirectory(path string) (*os.File, error) {

// Move up one level in the directory tree
parts := strings.Split(dir, rio.PathSeparator)
if len(parts) == 0 {
// See https://github.com/StyraInc/regal/issues/682
// Not sure how we could get here, but we need to stop if we do
return nil, errors.New("stopping as dir is empty string")
}

if len(parts) < 2 {
return nil, errors.New("stopping as dir is root directory")
Expand Down

0 comments on commit f20d4a3

Please sign in to comment.