Skip to content
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

Handle Invalid WSL URIs #1057

Merged
merged 5 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions internal/langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/code"
lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
Expand Down Expand Up @@ -132,10 +133,23 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams)
}
} else {
rootURI := string(params.RootURI)

invalidUriErr := jrpc2.Errorf(code.InvalidParams,
"Unsupported or invalid URI: %q "+
"This is most likely client bug, please report it.", rootURI)

if uri.IsWSLURI(rootURI) {
properties["root_uri"] = "invalid"
// For WSL URIs we return additional error data
// such that clients (e.g. VS Code) can provide better UX
// and nudge users to open in the WSL Remote Extension instead.
return serverCaps, invalidUriErr.WithData("INVALID_URI_WSL")
}

if !uri.IsURIValid(rootURI) {
properties["root_uri"] = "invalid"
return serverCaps, fmt.Errorf("Unsupported or invalid URI: %q "+
"This is most likely bug, please report it.", rootURI)

return serverCaps, invalidUriErr
}

err := svc.setupWalker(ctx, params, cfgOpts)
Expand Down
2 changes: 1 addition & 1 deletion internal/langserver/handlers/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestInitialize_withInvalidRootURI(t *testing.T) {
"capabilities": {},
"processId": 12345,
"rootUri": "meh"
}`}, code.SystemError.Err())
}`}, code.InvalidParams.Err())
}

func TestInitialize_multipleFolders(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions internal/uri/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,23 @@ func isLikelyWindowsDriveURIPath(uriPath string) bool {
}
return uriPath[0] == '/' && unicode.IsLetter(rune(uriPath[1])) && uriPath[2] == ':'
}

// IsWSLURI checks whether URI represents a WSL (Windows Subsystem for Linux)
// UNC path on Windows, such as \\wsl$\Ubuntu\path.
//
// Such a URI represents a common user error since the LS is generally
// expected to run in the same environment where files are located
// (i.e. within the Linux subsystem with Linux paths such as /Ubuntu/path).
func IsWSLURI(uri string) bool {
unescapedPath, err := url.PathUnescape(uri)
if err != nil {
return false
}

u, err := url.ParseRequestURI(unescapedPath)
if err != nil {
return false
}

return u.Scheme == "file" && u.Host == "wsl$"
}
26 changes: 26 additions & 0 deletions internal/uri/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,29 @@ func TestMustParseURI(t *testing.T) {
})
}
}

func TestIsWSLURI(t *testing.T) {
tests := []struct {
name string
uri string
want bool
}{
{
name: "UNC WSL Path should return true",
uri: `file://wsl%24/Ubuntu/home/james/some/path`,
want: true,
},
{
name: "Regular file path should return false",
uri: `file://C:/foo/james/foo`,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsWSLURI(tt.uri); got != tt.want {
t.Errorf("IsWSLURI() = %v, want %v", got, tt.want)
}
})
}
}