Skip to content

Commit 7e2701d

Browse files
authored
Fix infinite recursion in all-watchable-directories (#4598)
Fixes #4597
1 parent a195d47 commit 7e2701d

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

lsp-mode.el

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,21 +1986,29 @@ want to watch."
19861986
(not (lsp--string-match-any ignored-directories full-path)))))
19871987

19881988

1989-
(defun lsp--all-watchable-directories (dir ignored-directories)
1989+
(defun lsp--all-watchable-directories (dir ignored-directories &optional visited)
19901990
"Traverse DIR recursively returning a list of paths that should have watchers.
1991-
IGNORED-DIRECTORIES will be used for exclusions"
1991+
IGNORED-DIRECTORIES will be used for exclusions.
1992+
VISITED is used to track already-visited directories to avoid infinite loops."
19921993
(let* ((dir (if (f-symlink? dir)
19931994
(file-truename dir)
1994-
dir)))
1995-
(apply #'nconc
1996-
;; the directory itself is assumed to be part of the set
1997-
(list dir)
1998-
;; collect all subdirectories that are watchable
1999-
(-map
2000-
(lambda (path) (lsp--all-watchable-directories (f-join dir path) ignored-directories))
2001-
;; but only look at subdirectories that are watchable
2002-
(-filter (lambda (path) (lsp--path-is-watchable-directory path dir ignored-directories))
2003-
(directory-files dir))))))
1995+
dir))
1996+
;; Initialize visited directories if not provided
1997+
(visited (or visited (make-hash-table :test 'equal))))
1998+
(if (gethash dir visited)
1999+
;; If the directory has already been visited, skip it
2000+
nil
2001+
;; Mark the current directory as visited
2002+
(puthash dir t visited)
2003+
(apply #'nconc
2004+
;; the directory itself is assumed to be part of the set
2005+
(list dir)
2006+
;; collect all subdirectories that are watchable
2007+
(-map
2008+
(lambda (path) (lsp--all-watchable-directories (f-join dir path) ignored-directories visited))
2009+
;; but only look at subdirectories that are watchable
2010+
(-filter (lambda (path) (lsp--path-is-watchable-directory path dir ignored-directories))
2011+
(directory-files dir)))))))
20042012

20052013
(defun lsp-watch-root-folder (dir callback ignored-files ignored-directories &optional watch warn-big-repo?)
20062014
"Create recursive file notification watch in DIR.

0 commit comments

Comments
 (0)