this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

golps/protocol: introduce DocumentURI.Path method

This differs from the FilePath method because it returns a cleaned
/-separated path.

This is then used to make manipulation of paths safe within the LSP.

In https://cue.gerrithub.io/c/cue-lang/cue/+/1230705 we found and
corrected the use of a DocumentURI that was being directly cast to a
string. This meant that the URI encoding was not being removed.

However it turns out there are a couple of other locations in the LSP
where casting of URIs to strings were unsafe, and we want paths, not
filepaths. This new DocumentURI.Path method provides this functionality.

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: Ic97ec0e337f9555888ea551805fe3996a6a6a27d
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1231004
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+23 -2
+21
internal/golangorgx/gopls/protocol/uri.go
··· 12 12 import ( 13 13 "fmt" 14 14 "net/url" 15 + "path" 15 16 "path/filepath" 16 17 "strings" 17 18 "unicode" ··· 85 86 panic(err) 86 87 } 87 88 return filepath.FromSlash(filename) 89 + } 90 + 91 + // Path returns the /-separated path for the given URI. The returned 92 + // path has been cleaned. 93 + // 94 + // DocumentURI("").Path() returns the empty string. 95 + // 96 + // Path panics if called on a URI that is not a valid filename. 97 + func (uri DocumentURI) Path() string { 98 + filename, err := filename(uri) 99 + if err != nil { 100 + // e.g. ParseRequestURI failed. 101 + // 102 + // This can only affect DocumentURIs created by 103 + // direct string manipulation; all DocumentURIs 104 + // received from the client pass through 105 + // ParseRequestURI, which ensures validity. 106 + panic(err) 107 + } 108 + return path.Clean(filename) 88 109 } 89 110 90 111 // Dir returns the URI for the directory containing the receiver.
+1 -1
internal/lsp/cache/module.go
··· 213 213 214 214 dirUri := file.Dir() 215 215 // NB pkgPath will have a '/' at [0] because m.rootURI will not have a trailing '/' 216 - pkgPath := strings.TrimPrefix(string(dirUri), string(m.rootURI)) 216 + pkgPath := strings.TrimPrefix(dirUri.Path(), m.rootURI.Path()) 217 217 218 218 isOldMod := false 219 219 ip := ast.ImportPath{Qualifier: pkgName}
+1 -1
internal/lsp/cache/package.go
··· 172 172 // time after this pkg is loaded, new file is created which also 173 173 // matches the glob, then this will method would return true. 174 174 func (pkg *Package) matchesUnknownEmbedding(fileUri protocol.DocumentURI) bool { 175 - filepath, wasCut := strings.CutPrefix(string(fileUri), string(pkg.module.rootURI)+"/") 175 + filepath, wasCut := strings.CutPrefix(fileUri.Path(), pkg.module.rootURI.Path()+"/") 176 176 if !wasCut { 177 177 return false 178 178 }