diff --git a/internal/document/dir_handle.go b/internal/document/dir_handle.go index 4e2548502..46c738df8 100644 --- a/internal/document/dir_handle.go +++ b/internal/document/dir_handle.go @@ -1,10 +1,6 @@ package document import ( - "fmt" - "os" - "strings" - "github.com/hashicorp/terraform-ls/internal/uri" ) @@ -26,8 +22,6 @@ func (dh DirHandle) Path() string { // It is however outside the scope of the function to verify // this is actually the case or whether the directory exists. func DirHandleFromPath(dirPath string) DirHandle { - dirPath = strings.TrimSuffix(dirPath, fmt.Sprintf("%c", os.PathSeparator)) - return DirHandle{ URI: uri.FromPath(dirPath), } @@ -39,10 +33,8 @@ func DirHandleFromPath(dirPath string) DirHandle { // It is however outside the scope of the function to verify // this is actually the case or whether the directory exists. func DirHandleFromURI(dirUri string) DirHandle { - // Dir URIs are usually without trailing separator already - // but we double check anyway, so we deal with the same URI - // regardless of language client differences - dirUri = strings.TrimSuffix(string(dirUri), "/") + // Normalize the raw URI to account for any escaping differences + dirUri = uri.MustParseURI(dirUri) return DirHandle{ URI: dirUri, diff --git a/internal/document/dir_handle_test.go b/internal/document/dir_handle_test.go index baba0ce7d..77480193e 100644 --- a/internal/document/dir_handle_test.go +++ b/internal/document/dir_handle_test.go @@ -24,6 +24,12 @@ func TestDirHandleFromURI(t *testing.T) { URI: "file:///C:/random/path", }, }, + { + RawURI: "file:///C%3A/random/path", + ExpectedHandle: DirHandle{ + URI: "file:///C:/random/path", + }, + }, } for i, tc := range testCases { diff --git a/internal/document/handle.go b/internal/document/handle.go index dfdb40de1..5623b5331 100644 --- a/internal/document/handle.go +++ b/internal/document/handle.go @@ -1,10 +1,11 @@ package document import ( + "fmt" + "os" + "path" "path/filepath" "strings" - - "github.com/hashicorp/terraform-ls/internal/uri" ) // Handle represents a document location @@ -22,13 +23,11 @@ type Handle struct { // It is however outside the scope of the function to verify // this is actually the case or whether the file exists. func HandleFromURI(docUri string) Handle { - path := uri.MustPathFromURI(docUri) - - filename := filepath.Base(path) + filename := path.Base(docUri) dirUri := strings.TrimSuffix(docUri, "/"+filename) return Handle{ - Dir: DirHandle{URI: dirUri}, + Dir: DirHandleFromURI(dirUri), Filename: filename, } } @@ -39,13 +38,11 @@ func HandleFromURI(docUri string) Handle { // It is however outside the scope of the function to verify // this is actually the case or whether the file exists. func HandleFromPath(docPath string) Handle { - docUri := uri.FromPath(docPath) - filename := filepath.Base(docPath) - dirUri := strings.TrimSuffix(docUri, "/"+filename) + dirPath := strings.TrimSuffix(docPath, fmt.Sprintf("%c%s", os.PathSeparator, filename)) return Handle{ - Dir: DirHandle{URI: dirUri}, + Dir: DirHandleFromPath(dirPath), Filename: filename, } } diff --git a/internal/document/handle_test.go b/internal/document/handle_test.go index 9612c8501..0115c39a0 100644 --- a/internal/document/handle_test.go +++ b/internal/document/handle_test.go @@ -25,6 +25,13 @@ func TestHandleFromURI(t *testing.T) { Filename: "config.tf", }, }, + { + RawURI: "file:///C%3A/random/path/to/config.tf", + ExpectedHandle: Handle{ + Dir: DirHandle{URI: "file:///C:/random/path/to"}, + Filename: "config.tf", + }, + }, } for i, tc := range testCases {