clojure-ts-mode icon indicating copy to clipboard operation
clojure-ts-mode copied to clipboard

Valid clojure code causes emacs hang

Open kthu opened this issue 1 month ago • 2 comments

Opening a clojure file with the following function in clojure-ts-mode:

(defn unescape
  [s]
  (-> s
      (string/replace "&"  "&")
      (string/replace "*"  "*")
      (string/replace "^"  "^")
      (string/replace "_"  "_")
      (string/replace "~" "~")
      (string/replace "&lt;"   "<")
      (string/replace "&gt;"   ">")
      (string/replace "&#91;"  "[")
      (string/replace "&#93;"  "]")
      (string/replace "&#40;"  "(")
      (string/replace "&#41;"  ")")
      (string/replace "&quot;" "\"")))

..causes emacs to hang for over 10 minutes. Any attempt to edit the file once emacs has sprung back to life will cause another 10+ minutes of waiting. During these 10 minutes I can see emacs saturating a CPU core.

I have reproduced this with a minimal project containing just one file with just this one function, and with a stripped down emacs init.el just containing:

(package-install 'clojure-ts-mode)
(use-package clojure-ts-mode)

Interestingly clojure-ts-mode seems to be perfectly ok with the following rewrite:

(defn unescape
  [s]
  (string/replace s #"&amp;|&#42;|&#94;|&#95;|&#126;|&lt;|&gt;|&#91;|&#93;|&#40;|&#41;|&quot;"
                  {"&amp;"  "&"
                   "&#42;"  "*"
                   "&#94;"  "^"
                   "&#95;"  "_"
                   "&#126;" "~"
                   "&lt;"   "<"
                   "&gt;"   ">"
                   "&#91;"  "["
                   "&#93;"  "]"
                   "&#40;"  "("
                   "&#41;"  ")"
                   "&quot;" "\""}))

Expected behavior

File opens fine, like it does in clojure-mode

Actual behavior

Emacs hangs

Steps to reproduce the problem

Put above function into a file and open it in clojure-ts-mode

clojure-ts-mode version

0.5.1

tree-sitter-clojure grammar version

unstable-20250526 (the version emacs installs when starting clojure-ts-mode)

Emacs version

30.2

Operating system

Arch Linux

kthu avatar Nov 12 '25 09:11 kthu

I'm not entirely sure, but it looks like an Emacs bug to me:

Image

The more expressions I uncomment the less responsive it becomes. The profiler points at treesit-font-lock-fontify-region, but I couldn't find anything suspicious in this function's code. I tried to disable fontification of strings, but it didn't change anything.

I'll try to report it to Emacs' bug tracker and get some help.

rrudakov avatar Nov 12 '25 12:11 rrudakov

https://debbugs.gnu.org/cgi/bugreport.cgi?bug=79823

rrudakov avatar Nov 12 '25 14:11 rrudakov

@kthu the issue should be fixed in the next MELPA version.

rrudakov avatar Dec 02 '25 12:12 rrudakov