From fc92818c941b91f470122d1b83f9fdf7c9a42a90 Mon Sep 17 00:00:00 2001 From: Charlie Egan Date: Wed, 8 May 2024 15:20:18 +0100 Subject: [PATCH] lsp: URI to path conversions respect os.Separator (#703) This was causing an issue in the loading of config files on windows https://github.com/StyraInc/regal/issues/682 Signed-off-by: Charlie Egan --- internal/lsp/uri/uri.go | 3 +++ internal/lsp/uri/uri_test.go | 21 +++++++++++---------- pkg/config/config.go | 6 ------ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/internal/lsp/uri/uri.go b/internal/lsp/uri/uri.go index aaf137dc..fb148b8f 100644 --- a/internal/lsp/uri/uri.go +++ b/internal/lsp/uri/uri.go @@ -52,5 +52,8 @@ func ToPath(client clients.Identifier, uri string) string { } } + // Convert path to use system separators + path = filepath.FromSlash(path) + return path } diff --git a/internal/lsp/uri/uri_test.go b/internal/lsp/uri/uri_test.go index 3036b77a..d2d843f6 100644 --- a/internal/lsp/uri/uri_test.go +++ b/internal/lsp/uri/uri_test.go @@ -1,6 +1,7 @@ package uri import ( + "path/filepath" "testing" "github.com/styrainc/regal/internal/lsp/clients" @@ -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"), }, } @@ -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"), }, } diff --git a/pkg/config/config.go b/pkg/config/config.go index 581721d7..7c4d0d8e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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() { @@ -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")