Skip to content

Commit 8299f11

Browse files
authored
fix: Check for a bogus root folder in workspace/didChangeWorkspaceFolders request (#380)
Sometimes, LSP clients can send bogus folders (CWD, or homedir) as part of added folders in didChangeWorkspaceFolders. Whenever this happens marksman treats the root as the real root of the project folders and recursively scans/indexes mardown documents underneath.This is problematic when such root is a homedir because it makes marksman scan too much and potentially fail. Fixes #377 Potentially, addresses #373 too
1 parent b9ec789 commit 8299f11

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

Marksman/Folder.fs

+31
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,37 @@ module Folder =
249249

250250
let private ignoreFiles = [ ".ignore"; ".gitignore"; ".hgignore" ]
251251

252+
let private isRealWorkspaceFolder (root: RootPath) : bool =
253+
let root = RootPath.toSystem root
254+
255+
if Directory.Exists(root) then
256+
let markerFiles = [| ".marksman.toml" |]
257+
let markerDirs = [| ".git"; ".hg"; ".svn" |]
258+
259+
let hasMarkerFile () =
260+
Array.exists (fun marker -> File.Exists(Path.Join(root, marker))) markerFiles
261+
262+
let hasMarkerDir () =
263+
Array.exists (fun marker -> Directory.Exists(Path.Join(root, marker))) markerDirs
264+
265+
hasMarkerDir () || hasMarkerFile ()
266+
else
267+
false
268+
269+
// Context is in
270+
// * https://github.com/helix-editor/helix/issues/4436
271+
// * https://github.com/artempyanykh/marksman/discussions/377
272+
let checkWorkspaceFolderWithWarn (folderId: FolderId) : bool =
273+
if isRealWorkspaceFolder folderId.data then
274+
true
275+
else
276+
logger.warn (
277+
Log.setMessage "Workspace folder is bogus"
278+
>> Log.addContext "root" folderId.data
279+
)
280+
281+
false
282+
252283
let isSingleFile folder =
253284
match folder.data with
254285
| SingleFile _ -> true

Marksman/Folder.fsi

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Folder =
2424
val docs: Folder -> seq<Doc>
2525
val docCount: Folder -> int
2626

27+
val checkWorkspaceFolderWithWarn: FolderId -> bool
2728
val tryLoad: userConfig: option<Config> -> name: string -> FolderId -> option<Folder>
2829

2930
val singleFile: doc: Doc -> config: option<Config> -> Folder

Marksman/Server.fs

+2-31
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,12 @@ open Newtonsoft.Json.Linq
2828
module ServerUtil =
2929
let logger = LogProvider.getLoggerByName "ServerUtil"
3030

31-
let private isRealWorkspaceFolder (root: RootPath) : bool =
32-
let root = RootPath.toSystem root
33-
34-
if Directory.Exists(root) then
35-
let markerFiles = [| ".marksman.toml" |]
36-
let markerDirs = [| ".git"; ".hg"; ".svn" |]
37-
38-
let hasMarkerFile () =
39-
Array.exists (fun marker -> File.Exists(Path.Join(root, marker))) markerFiles
40-
41-
let hasMarkerDir () =
42-
Array.exists (fun marker -> Directory.Exists(Path.Join(root, marker))) markerDirs
43-
44-
hasMarkerDir () || hasMarkerFile ()
45-
else
46-
false
47-
48-
// Remove this and related logic when https://github.com/helix-editor/helix/issues/4436 is resolved.
49-
let checkWorkspaceFolderWithWarn (folderId: FolderId) : bool =
50-
if isRealWorkspaceFolder folderId.data then
51-
true
52-
else
53-
logger.warn (
54-
Log.setMessage "Workspace folder is bogus"
55-
>> Log.addContext "root" folderId.data
56-
)
57-
58-
false
59-
6031
let extractWorkspaceFolders (par: InitializeParams) : Map<string, FolderId> =
6132
match par.WorkspaceFolders with
6233
| Some folders ->
6334
folders
6435
|> Array.map (fun { Name = name; Uri = uri } -> name, UriWith.mkRoot uri)
65-
|> Array.filter (fun (_, folderId) -> checkWorkspaceFolderWithWarn folderId)
36+
|> Array.filter (fun (_, folderId) -> Folder.checkWorkspaceFolderWithWarn folderId)
6637
|> Map.ofArray
6738
| _ ->
6839
let rootUri =
@@ -75,7 +46,7 @@ module ServerUtil =
7546
// No folders are configured. The client can still add folders later using a notification.
7647
Map.empty
7748
| Some rootUri ->
78-
if checkWorkspaceFolderWithWarn rootUri then
49+
if Folder.checkWorkspaceFolderWithWarn rootUri then
7950
let rootName = RootPath.filename rootUri.data
8051

8152
Map.ofList [ rootName, rootUri ]

Marksman/State.fs

+5-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ module State =
180180
for f in added do
181181
let rootUri = UriWith.mkRoot f.Uri
182182

183-
let folder = Folder.tryLoad userConfig f.Name rootUri
183+
let folder =
184+
if Folder.checkWorkspaceFolderWithWarn rootUri then
185+
Folder.tryLoad userConfig f.Name rootUri
186+
else
187+
None
184188

185189
match folder with
186190
| Some folder -> yield folder

0 commit comments

Comments
 (0)