]> code.delx.au - gnu-emacs-elpa/blob - packages/bug-hunter/bug-hunter.el
multishell - Merge edge-case but significant fixes
[gnu-emacs-elpa] / packages / bug-hunter / bug-hunter.el
1 ;;; bug-hunter.el --- Hunt down errors by bisecting elisp files -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: https://github.com/Malabarba/elisp-bug-hunter
7 ;; Version: 1.0.1
8 ;; Keywords: lisp
9 ;; Package-Requires: ((seq "1.3") (cl-lib "0.5"))
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15 ;;
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;; An Emacs library that finds the source of an error or unexpected
26 ;; behavior inside an elisp configuration file (typically `init.el' or
27 ;; `.emacs').
28 ;;
29 ;; Usage Examples
30 ;; ==============
31 ;;
32 ;; Automated error hunting
33 ;; ~~~~~~~~~~~~~~~~~~~~~~~
34 ;;
35 ;; If your Emacs init file signals an error during startup, but you don’t
36 ;; know why, simply issue
37 ;; ,----
38 ;; | M-x bug-hunter-init-file RET e
39 ;; `----
40 ;; and The Bug Hunter will find it for you. Note that your `init.el' (or
41 ;; `.emacs') must be idempotent for this to work.
42 ;;
43 ;;
44 ;; Interactive hunt
45 ;; ~~~~~~~~~~~~~~~~
46 ;;
47 ;; If Emacs starts up without errors but something is not working as it
48 ;; should, invoke the same command, but choose the interactive option:
49 ;; ,----
50 ;; | M-x bug-hunter-init-file RET i
51 ;; `----
52 ;; The Bug Hunter will start a separate Emacs instance several times, and
53 ;; then it will ask you each time whether that instance presented the
54 ;; problem you have. After doing this about 5--12 times, you’ll be given
55 ;; the results.
56 ;;
57 ;;
58 ;; Assertion hunt
59 ;; ~~~~~~~~~~~~~~
60 ;;
61 ;; The Bug Hunter can also find your issue based on an assertion.
62 ;; Essentially, if you can write a code snippet that returns non-nil when
63 ;; it detects the issue, just provide this snippet as the assertion and
64 ;; the Bug Hunter will do the rest.
65 ;;
66 ;; For example, let’s say there’s something in your init file that’s
67 ;; loading the `cl' library, and you don’t want that. You /know/ you’re
68 ;; not loading it yourself, but how can you figure out which external
69 ;; package is responsible for this outrage?
70 ;;
71 ;; ,----
72 ;; | M-x bug-hunter-init-file RET a (featurep 'cl) RET
73 ;; `----
74 ;;
75 ;; *That’s it!* You’ll be given a nice buffer reporting the results:
76 ;;
77 ;; - Are you getting obscure errors when trying to open /".tex"/ files?
78 ;; - Don’t despair! Just use `(find-file "dummy.tex")' as the
79 ;; assertion.
80 ;; - Did `ox-html' stop working due to some arcane misconfiguration?
81 ;; - Just write an assertion that does an export and checks the result.
82 ;; - Does some random command suddenly bind itself to `C-j' and you can’t
83 ;; figure out why?
84 ;; - `(eq (key-binding "\n") 'unwanted-command)' is the assertion for
85 ;; you!
86 ;;
87 ;; Finally, you can also use `bug-hunter-file' to hunt in other files.
88 ;;
89 ;;
90 ;; init.org and other literate-style configs
91 ;; =========================================
92 ;;
93 ;; Please see the full Readme on http://github.com/Malabarba/elisp-bug-hunter
94 \f
95 ;;; Code:
96 (require 'seq)
97 (require 'cl-lib)
98
99 (defconst bug-hunter--interactive-explanation
100 "You have asked to do an interactive hunt, here's how it goes.
101 1) I will start a new Emacs instance, which opens a new frame.
102 2) You will try to reproduce your problem on the new frame.
103 3) When you’re done, close that frame.
104 4) I will ask you if you managed to reproduce the problem.
105 5) We will repeat steps up to %s times, so hang tight!")
106
107 (defconst bug-hunter--assertion-reminder
108 "Remember, the assertion must be an expression that returns
109 non-nil in your current (problematic) Emacs state, AND that
110 returns nil on a clean Emacs instance.
111 If you're unsure how to write an assertion, you can try the interactive
112 hunt instead, or see some examples in the Readme:
113 https://github.com/Malabarba/elisp-bug-hunter"
114 "Printed to the user if they provide a bad assertion.")
115
116 (defvar bug-hunter--current-head nil
117 "Current list of expressions under scrutiny. Used for user feedback.
118 Used if the user aborts before bisection ends.")
119
120 (defvar bug-hunter--i 0
121 "Current step of the bisection. Used for user feedback.")
122 (defvar bug-hunter--estimate 0
123 "Estimate on how many steps the bisection can take. Used for user feedback.
124 This is the base 2 log of the number of expressions in the
125 file.")
126
127 (defvar bug-hunter--current-file nil
128 "File currently being debugged.")
129
130 (defun bug-hunter--read-buffer ()
131 "Return all sexps after point as a list."
132 (let (out line col)
133 (or (condition-case er
134 ;; Looks hacky, but comes from `byte-compile-from-buffer'.
135 (while (progn (while (progn (skip-chars-forward " \t\n\^l")
136 (looking-at ";"))
137 (forward-line 1))
138 (not (eobp)))
139 (setq line (line-number-at-pos (point)))
140 (setq col (current-column))
141 (push (list (read (current-buffer)) line col)
142 out)
143 nil)
144 (end-of-file `(bug-caught (end-of-file) ,line ,col))
145 (invalid-read-syntax `(bug-caught ,er ,line ,col))
146 (error (error "Ran into an error we don't understand, please file a bug report: %S" er)))
147 (nreverse out))))
148
149 (defun bug-hunter--read-contents (file)
150 "Return all sexps in FILE as a list."
151 (with-temp-buffer
152 (insert-file-contents file)
153 (goto-char (point-min))
154 (bug-hunter--read-buffer)))
155
156 \f
157 ;;; Reporting functions
158 (defun bug-hunter--report-print (&rest r)
159 "Print information on the \"*Bug-Hunter Report*\" buffer.
160 R is passed to `format' and inserted."
161 (with-current-buffer (get-buffer-create "*Bug-Hunter Report*")
162 (goto-char (point-max))
163 (let ((inhibit-read-only t))
164 (insert "\n" (apply #'format r)))))
165
166 (defun bug-hunter--report (&rest r)
167 "Report arbitrary information.
168 R is passed to `bug-hunter--report-print'."
169 (declare (indent 1))
170 (apply #'bug-hunter--report-print r)
171 (redisplay)
172 (apply #'message r))
173
174 (defun bug-hunter--report-user-error (&rest r)
175 "Report the user has done something wrong.
176 R is passed to `bug-hunter--report-print'."
177 (declare (indent 1))
178 (apply #'bug-hunter--report-print r)
179 (bug-hunter--report-print "\xc\n")
180 (apply #'user-error r))
181
182 (defvar compilation-error-regexp-alist)
183 (defun bug-hunter--init-report-buffer (assertion steps)
184 "Create and prepare the \"*Bug-Hunter Report*\" buffer.
185 Also add some descriptions depending on ASSERTION."
186 (with-current-buffer (get-buffer-create "*Bug-Hunter Report*")
187 (let ((inhibit-read-only t))
188 (erase-buffer)
189 (compilation-mode "Bug Hunt")
190 (set (make-local-variable 'compilation-error-regexp-alist)
191 '(comma))
192 (pcase assertion
193 (`interactive (insert (format bug-hunter--interactive-explanation (+ 2 steps))))))
194 (current-buffer)))
195
196 (defun bug-hunter--pretty-format (value padding)
197 "Return a VALUE as a string with PADDING spaces on the left."
198 (with-temp-buffer
199 (pp value (current-buffer))
200 (goto-char (point-min))
201 (let ((pad (make-string padding ?\s)))
202 (while (not (eobp))
203 (insert pad)
204 (forward-line 1)))
205 (buffer-string)))
206
207 (defun bug-hunter--report-error (error line column &optional expression)
208 "Print on report buffer information about ERROR.
209 LINE and COLUMN are the coordinates where EXPRESSION started in
210 the file."
211 (when line
212 (bug-hunter--report "%S, line %s pos %s:"
213 bug-hunter--current-file line column))
214 (bug-hunter--report " %s"
215 (cl-case (car error)
216 (end-of-file
217 "There's a missing closing parenthesis, the expression on this line never ends.")
218 (invalid-read-syntax
219 (let ((char (cadr error)))
220 (if (member char '("]" ")"))
221 (concat "There's an extra " char
222 " on this position. There's probably a missing "
223 (if (string= char ")") "(" "[")
224 " before that.")
225 (concat "There's a " char
226 " on this position, and that is not valid elisp syntax."))))
227 (user-aborted
228 (let* ((print-level 2)
229 (print-length 15)
230 (forms (cadr error))
231 (size (length forms)))
232 (concat "User aborted while testing the following expressions:\n"
233 (mapconcat (lambda (x) (bug-hunter--pretty-format x 4))
234 (if (< size 16) forms (seq-take forms 7))
235 "")
236 (when (> size 16)
237 (format "\n ... %s omitted expressions ...\n\n"
238 (- size 14)))
239 (when (> size 16)
240 (mapconcat (lambda (x) (bug-hunter--pretty-format x 4))
241 (seq-drop forms (- size 7)) "")))))
242 (assertion-triggered
243 (concat "The assertion returned the following value here:\n"
244 (bug-hunter--pretty-format (cadr error) 4)))
245 (t (format "The following error was signaled here:\n %S"
246 error))))
247 (when expression
248 (bug-hunter--report " Caused by the following expression:\n%s"
249 (bug-hunter--pretty-format expression 4)))
250 (bug-hunter--report "\xc\n")
251 `[,error ,line ,column ,expression])
252
253 \f
254 ;;; Execution functions
255 (defun bug-hunter--print-to-temp (sexp)
256 "Print SEXP to a temp file and return the file name."
257 (let ((print-length nil)
258 (print-level nil)
259 (file (make-temp-file "bug-hunter")))
260 (with-temp-file file
261 (print sexp (current-buffer)))
262 file))
263
264 (defun bug-hunter--run-emacs (file &rest args)
265 "Start an Emacs process to run FILE and return the output buffer.
266 ARGS are passed before \"-l FILE\"."
267 (let ((out-buf (generate-new-buffer "*Bug-Hunter Command*"))
268 (exec (file-truename (expand-file-name invocation-name
269 invocation-directory))))
270 (apply #'call-process exec nil out-buf nil
271 (append args (list "-l" file)))
272 out-buf))
273
274 (defun bug-hunter--run-form (form)
275 "Run FORM with \"emacs -Q\" and return the result."
276 (let ((file-name (bug-hunter--print-to-temp (list 'prin1 form))))
277 (unwind-protect
278 (with-current-buffer (bug-hunter--run-emacs file-name "-Q" "--batch")
279 (goto-char (point-max))
280 (forward-sexp -1)
281 (prog1 (read (current-buffer))
282 (kill-buffer (current-buffer))))
283 (delete-file file-name))))
284
285 (defun bug-hunter--run-form-interactively (form)
286 "Run FORM in a graphical instance and ask user about the outcome."
287 (let ((file-name (bug-hunter--print-to-temp (list 'prin1 form))))
288 (unwind-protect
289 (bug-hunter--run-emacs file-name "-Q")
290 (delete-file file-name))
291 (y-or-n-p "Did you find the problem/bug in this instance? ")))
292
293 (defun bug-hunter--wrap-forms-for-eval (forms)
294 "Return FORMS wrapped in initialization code."
295 `(let ((server-name (make-temp-file "bug-hunter-temp-server-file")))
296 (delete-file server-name)
297 (if site-run-file (load site-run-file t t))
298 (run-hooks 'before-init-hook)
299 ,@forms
300 (package-initialize)
301 (run-hooks 'after-init-hook)))
302
303 (defun bug-hunter--run-and-test (forms assertion)
304 "Execute FORMS in the background and test ASSERTION.
305 See `bug-hunter' for a description on the ASSERTION.
306
307 If ASSERTION is 'interactive, the form is run through
308 `bug-hunter--run-form-interactively'. Otherwise, a slightly
309 modified version of the form combined with ASSERTION is run
310 through `bug-hunter--run-form'."
311 (if (eq assertion 'interactive)
312 (bug-hunter--run-form-interactively
313 (bug-hunter--wrap-forms-for-eval forms))
314 (bug-hunter--run-form
315 `(condition-case er
316 ,(append (bug-hunter--wrap-forms-for-eval forms)
317 (list assertion))
318 (error (cons 'bug-caught er))))))
319
320
321 \f
322 ;;; The actual bisection
323 (defun bug-hunter--split (l)
324 "Split list L in two lists of same size."
325 (seq-partition l (ceiling (/ (length l) 2.0))))
326
327 (defun bug-hunter--bisect (assertion safe head &optional tail)
328 "Implementation used by `bug-hunter--bisect-start'.
329 ASSERTION is received by `bug-hunter--bisect-start'.
330 SAFE is a list of forms confirmed to not match the ASSERTION,
331 HEAD is a list of forms to be tested now, and TAIL is a list
332 which will be inspected if HEAD doesn't match ASSERTION."
333 (message "Testing: %s/%s" (cl-incf bug-hunter--i) bug-hunter--estimate)
334 ;; Used if the user quits.
335 (setq bug-hunter--current-head head)
336 (let ((ret-val (bug-hunter--run-and-test (append safe head) assertion)))
337 (cond
338 ((not tail)
339 (cl-assert ret-val nil)
340 (vector (length safe) ret-val))
341 ;; Issue in the head.
342 ((and ret-val (< (length head) 2))
343 (vector (length safe) ret-val))
344 (ret-val
345 (apply #'bug-hunter--bisect
346 assertion
347 safe
348 (bug-hunter--split head)))
349 ;; Issue in the tail.
350 (t (apply #'bug-hunter--bisect
351 assertion
352 (append safe head)
353 ;; If tail has length 1, we already know where the issue is,
354 ;; but we still do this to get the return value.
355 (bug-hunter--split tail))))))
356
357 (defun bug-hunter--bisect-start (forms assertion)
358 "Run a bisection search on list of FORMS using ASSERTION.
359 Returns a vector [n value], where n is the position of the first
360 element in FORMS which trigger ASSERTION, and value is the
361 ASSERTION's return value.
362
363 If ASSERTION is nil, n is the position of the first form to
364 signal an error and value is (bug-caught . ERROR-SIGNALED)."
365 (let ((bug-hunter--i 0)
366 (bug-hunter--current-head nil))
367 (condition-case-unless-debug nil
368 (apply #'bug-hunter--bisect assertion nil (bug-hunter--split forms))
369 (quit `[nil (bug-caught user-aborted ,bug-hunter--current-head)]))))
370
371 \f
372 ;;; Main functions
373 (defun bug-hunter-hunt (rich-forms assertion)
374 "Bisect RICH-FORMS using ASSERTION.
375 RICH-FORMS is a list with elements of the form: (EXPR LINE COL)
376 EXPR is an elisp expression. LINE and COL are the coordinates
377 in `bug-hunter--current-file' where the expression starts.
378 It is expected that one of EXPR is either throwing an error or
379 causing some undesirable effect (which triggers ASSERTION).
380
381 ASSERTION is either nil or an expression.
382 If nil, EXPRs are bisected until we find the first one that
383 throws errors.
384 If it is an expression, EXPRs are bisected by testing
385 ASSERTION. It should return nil if all is fine (e.g. if used
386 with \"emacs -Q\"), and should return non-nil when a problem
387 is detected.
388
389 Bug hunter will refuse to hunt if (i) an error is signaled or the
390 assertion is triggered while running emacs -Q, or (ii) no errors
391 are signaled and the assertion is not triggered after all EXPRs
392 are evaluated."
393 (let ((expressions (unless (eq (car-safe rich-forms) 'bug-caught)
394 (mapcar #'car rich-forms)))
395 (bug-hunter--estimate (ceiling (log (length rich-forms) 2))))
396 ;; Prepare buffer, and make sure they've seen it.
397 (switch-to-buffer (bug-hunter--init-report-buffer assertion bug-hunter--estimate))
398 (when (eq assertion 'interactive)
399 (read-char-choice "Please read the instructions above and type 6 when ready. " '(?6)))
400
401 (cond
402 ;; Check for errors when reading the init file.
403 ((not expressions)
404 (apply #'bug-hunter--report-error (cdr rich-forms))
405 (apply #'vector (cdr rich-forms)))
406
407 ;; Make sure there's a bug to hunt.
408 ((progn (bug-hunter--report "Doing some initial tests...")
409 (not (bug-hunter--run-and-test expressions assertion)))
410 (bug-hunter--report-user-error "Test failed.\n%s\n%s"
411 (if assertion
412 (concat "The assertion returned nil after loading the entire file.\n"
413 bug-hunter--assertion-reminder)
414 "No errors signaled after loading the entire file.
415 If you're looking for something that's not an error, use the
416 interactive hunt instead of the error hunt. If you have some
417 elisp proficiency, you can also use the assertion hunt, see this
418 link for some examples:
419 https://github.com/Malabarba/elisp-bug-hunter")
420 (or assertion "")))
421
422 ;; Make sure we're in a forest, not a volcano.
423 ((bug-hunter--run-and-test nil assertion)
424 (bug-hunter--report-user-error "Test failed.\n%s\n%s"
425 (if assertion
426 (concat "Assertion returned non-nil even on emacs -Q:"
427 bug-hunter--assertion-reminder)
428 "Detected a signaled error even on emacs -Q. I'm sorry, but there
429 is something seriously wrong with your Emacs installation.
430 There's nothing more I can do here.")
431 (or assertion "")))
432
433 (t
434 ;; Perform the actual hunt.
435 (bug-hunter--report "Initial tests done. Hunting for the cause...")
436 (let* ((result (bug-hunter--bisect-start expressions assertion)))
437 (if (not result)
438 (bug-hunter--report-user-error "No problem was found, despite our initial tests.\n%s"
439 "I have no idea what's going on.")
440 (let* ((pos (elt result 0))
441 (ret (elt result 1))
442 (linecol (when pos (cdr (elt rich-forms pos))))
443 (expression (when pos (elt expressions pos))))
444 (if (eq (car-safe ret) 'bug-caught)
445 (bug-hunter--report-error
446 (cdr ret) (car linecol) (cadr linecol) expression)
447 (bug-hunter--report-error
448 (list 'assertion-triggered ret)
449 (car linecol) (cadr linecol) expression)))))))))
450
451 (defconst bug-hunter--hunt-type-prompt
452 "To bisect interactively, type i
453 To use automatic error detection, type e
454 To provide a lisp assertion, type a
455 => ")
456
457 (defun bug-hunter--read-from-minibuffer ()
458 "Read a list of expressions from the minibuffer.
459 Wraps them in a progn if necessary to always return a single
460 form.
461
462 The user may decide to not provide input, in which case
463 'interactive is returned. Note, this is different from the user
464 typing `RET' at an empty prompt, in which case nil is returned."
465 (pcase (read-char-choice bug-hunter--hunt-type-prompt '(?i ?e ?a))
466 (`?i 'interactive)
467 (`?e nil)
468 (_
469 (require 'simple)
470 (let ((exprs
471 (with-temp-buffer
472 ;; Copied from `read--expression'.
473 (let ((minibuffer-completing-symbol t))
474 (minibuffer-with-setup-hook
475 (lambda ()
476 (add-hook 'completion-at-point-functions
477 #'elisp-completion-at-point nil t)
478 (run-hooks 'eval-expression-minibuffer-setup-hook))
479 (insert
480 (read-from-minibuffer
481 "Provide an assertion. This is a lisp expression that returns nil if (and only if) everything is fine:\n => "
482 nil read-expression-map nil 'read-expression-history))))
483 (goto-char (point-min))
484 (mapcar #'car (bug-hunter--read-buffer)))))
485 (if (cdr exprs)
486 (cons #'progn exprs)
487 (car exprs))))))
488
489 ;;;###autoload
490 (defun bug-hunter-file (file &optional assertion)
491 "Bisect FILE while testing ASSERTION.
492 All sexps in FILE are read and passed to `bug-hunter-hunt' as a
493 list. See `bug-hunter-hunt' for how to use ASSERTION."
494 (interactive
495 (list
496 (read-file-name "File to bisect: "
497 (file-name-directory (or (buffer-file-name) "./"))
498 nil t
499 (file-name-nondirectory (or (buffer-file-name) "./")))
500 (bug-hunter--read-from-minibuffer)))
501 (let ((bug-hunter--current-file file))
502 (bug-hunter-hunt (bug-hunter--read-contents file) assertion)))
503
504 ;;;###autoload
505 (defun bug-hunter-init-file (&optional assertion)
506 "Test ASSERTION throughout `user-init-file'.
507 All sexps inside `user-init-file' are read and passed to
508 `bug-hunter-hunt' as a list. See `bug-hunter-hunt' for how to use
509 ASSERTION."
510 (interactive (list (bug-hunter--read-from-minibuffer)))
511 (bug-hunter-file user-init-file assertion))
512
513 (provide 'bug-hunter)
514 ;;; bug-hunter.el ends here