Skip to content

Commit

Permalink
Fix scheme stripping of file URI
Browse files Browse the repository at this point in the history
This is in response to issue at: #50

A panic would be raised on Windows computers, where removing the
file:// scheme would still have an invalid leading slash
(e.g. '/C:/Users/...').

Fixes #50
  • Loading branch information
AubreySLavigne committed Feb 22, 2020
1 parent 8a4b03e commit ef4774b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/creack/pty v1.1.9 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-language-server/uri v0.2.0 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/gruntwork-io/terragrunt v0.21.13
github.com/hashicorp/go-hclog v0.9.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-language-server/uri v0.2.0 h1:zkFlKR0EYmCg3076hwVbM9/+/Ugr8vMQvQQgoS2JpTo=
github.com/go-language-server/uri v0.2.0/go.mod h1:1wEq7lT5PmX5uljJuQJeRxdW06jr5VfA1aR1FKe2/gc=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
Expand Down Expand Up @@ -137,6 +139,8 @@ github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.2-0.20190829225427-b1c9c4891a65 h1:B3yqxlLHBEoav+FDQM8ph7IIRA6AhQ70w119k3hoT2Y=
github.com/google/go-cmp v0.3.2-0.20190829225427-b1c9c4891a65/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
Expand Down Expand Up @@ -473,6 +477,7 @@ golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgw
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522 h1:bhOzK9QyoD0ogCnFro1m2mz41+Ib0oOhfJnBp5MR4K4=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
Expand Down
6 changes: 5 additions & 1 deletion langserver/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ func TextDocumentComplete(ctx context.Context, vs lsp.CompletionParams) (lsp.Com
//log.Println(tfstructs.Clients)
parser := configs.NewParser(memfs.MemFs)

fileURL := strings.Replace(string(vs.TextDocument.URI), "file://", "", 1)
uri, err := absolutePath(string(vs.TextDocument.URI))
if err != nil {
return lsp.CompletionList{}, err
}
fileURL := uri.Filename()

decodedFileURL, _ := url.QueryUnescape(fileURL)
fileDir := filepath.Dir(decodedFileURL)
Expand Down
8 changes: 6 additions & 2 deletions langserver/did_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import (
"context"
"github.com/juliosueiras/terraform-lsp/tfstructs"
lsp "github.com/sourcegraph/go-lsp"
"strings"
)

func TextDocumentDidChange(ctx context.Context, vs lsp.DidChangeTextDocumentParams) error {
tempFile.Truncate(0)
tempFile.Seek(0, 0)
tempFile.Write([]byte(vs.ContentChanges[0].Text))

fileURL := strings.Replace(string(vs.TextDocument.URI), "file://", "", 1)
uri, err := absolutePath(string(vs.TextDocument.URI))
if err != nil {
return err
}
fileURL := uri.Filename()

DiagsFiles[fileURL] = tfstructs.GetDiagnostics(tempFile.Name(), fileURL)

TextDocumentPublishDiagnostics(Server, ctx, lsp.PublishDiagnosticsParams{
Expand Down
8 changes: 6 additions & 2 deletions langserver/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"context"
"github.com/juliosueiras/terraform-lsp/tfstructs"
lsp "github.com/sourcegraph/go-lsp"
"strings"
)

func TextDocumentDidOpen(ctx context.Context, vs lsp.DidOpenTextDocumentParams) error {
fileURL := strings.Replace(string(vs.TextDocument.URI), "file://", "", 1)
uri, err := absolutePath(string(vs.TextDocument.URI))
if err != nil {
return err
}
fileURL := uri.Filename()

DiagsFiles[fileURL] = tfstructs.GetDiagnostics(fileURL, fileURL)

TextDocumentPublishDiagnostics(Server, ctx, lsp.PublishDiagnosticsParams{
Expand Down
9 changes: 9 additions & 0 deletions langserver/uri.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package langserver

import (
"github.com/go-language-server/uri"
)

func absolutePath(in string) (uri.URI, error) {
return uri.Parse(string(in))
}
33 changes: 33 additions & 0 deletions langserver/uri_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package langserver

import (
"testing"
)

func TestAbsolutePath(t *testing.T) {
tests := []struct {
path string
expected string
}{
{
path: "file:///C:/Users/acmeuser/git/tf-test/main.tf",
expected: "C:/Users/acmeuser/git/tf-test/main.tf",
},
{
path: "file:///etc/fstab",
expected: "/etc/fstab",
},
}

for _, test := range tests {
uri, err := absolutePath(test.path)
if err != nil {
t.Errorf("Parsing path '%s' resulted in error: %s", test.path, err)
}
res := uri.Filename()

if res != test.expected {
t.Errorf("Unexpected result. Got '%s', Expected '%s'", res, test.expected)
}
}
}

0 comments on commit ef4774b

Please sign in to comment.