Prevent generating empty autoload files

* lisp/emacs-lisp/loaddefs-gen.el (loaddefs-generate): Remove
optimisation that would mistakenly discard old loaddefs in case a file
was not modified by EXTRA-DATA is non-nil.  (Bug#62734)
This commit is contained in:
Philip Kaludercic 2023-04-30 13:17:09 +02:00
parent 2bcf11d0ef
commit dd21003878

View file

@ -597,73 +597,63 @@ instead of just updating them with the new/changed autoloads."
defs)))))) defs))))))
(progress-reporter-done progress)) (progress-reporter-done progress))
;; If we have no autoloads data, but we have EXTRA-DATA, then ;; First group per output file.
;; generate the (almost) empty file anyway. (dolist (fdefs (seq-group-by (lambda (x) (expand-file-name (car x)))
(if (and (not defs) extra-data) defs))
(let ((loaddefs-file (car fdefs))
hash)
(with-temp-buffer (with-temp-buffer
(insert (loaddefs-generate--rubric output-file nil t)) (if (and updating (file-exists-p loaddefs-file))
(search-backward "\f") (insert-file-contents loaddefs-file)
(insert extra-data) (insert (loaddefs-generate--rubric
(ensure-empty-lines 1) loaddefs-file nil t include-package-version))
(write-region (point-min) (point-max) output-file nil 'silent)) (search-backward "\f")
;; We have some data, so generate the loaddef files. First (when extra-data
;; group per output file. (insert extra-data)
(dolist (fdefs (seq-group-by (lambda (x) (expand-file-name (car x))) (ensure-empty-lines 1)))
defs)) (setq hash (buffer-hash))
(let ((loaddefs-file (car fdefs)) ;; Then group by source file (and sort alphabetically).
hash) (dolist (section (sort (seq-group-by #'cadr (cdr fdefs))
(with-temp-buffer (lambda (e1 e2)
(if (and updating (file-exists-p loaddefs-file)) (string<
(insert-file-contents loaddefs-file) (file-name-sans-extension
(insert (loaddefs-generate--rubric (file-name-nondirectory (car e1)))
loaddefs-file nil t include-package-version)) (file-name-sans-extension
(search-backward "\f") (file-name-nondirectory (car e2)))))))
(when extra-data (pop section)
(insert extra-data) (let* ((relfile (file-relative-name
(ensure-empty-lines 1))) (cadar section)
(setq hash (buffer-hash)) (file-name-directory loaddefs-file)))
;; Then group by source file (and sort alphabetically). (head (concat "\n\f\n;;; Generated autoloads from "
(dolist (section (sort (seq-group-by #'cadr (cdr fdefs)) relfile "\n\n")))
(lambda (e1 e2) (when (file-exists-p loaddefs-file)
(string< ;; If we're updating an old loaddefs file, then see if
(file-name-sans-extension ;; there's a section here for this file already.
(file-name-nondirectory (car e1))) (goto-char (point-min))
(file-name-sans-extension (if (not (search-forward head nil t))
(file-name-nondirectory (car e2))))))) ;; It's a new file; put the data at the end.
(pop section) (progn
(let* ((relfile (file-relative-name (goto-char (point-max))
(cadar section) (search-backward "\f\n" nil t))
(file-name-directory loaddefs-file))) ;; Delete the old version of the section.
(head (concat "\n\f\n;;; Generated autoloads from " (delete-region (match-beginning 0)
relfile "\n\n"))) (and (search-forward "\n\f\n;;;")
(when (file-exists-p loaddefs-file) (match-beginning 0)))
;; If we're updating an old loaddefs file, then see if (forward-line -2)))
;; there's a section here for this file already. (insert head)
(goto-char (point-min)) (dolist (def (reverse section))
(if (not (search-forward head nil t)) (setq def (caddr def))
;; It's a new file; put the data at the end. (if (stringp def)
(progn (princ def (current-buffer))
(goto-char (point-max)) (loaddefs-generate--print-form def))
(search-backward "\f\n" nil t)) (unless (bolp)
;; Delete the old version of the section. (insert "\n")))))
(delete-region (match-beginning 0) ;; Only write the file if we actually made a change.
(and (search-forward "\n\f\n;;;") (unless (equal (buffer-hash) hash)
(match-beginning 0))) (write-region (point-min) (point-max) loaddefs-file nil 'silent)
(forward-line -2))) (byte-compile-info
(insert head) (file-relative-name loaddefs-file (car (ensure-list dir)))
(dolist (def (reverse section)) t "GEN")))))))
(setq def (caddr def))
(if (stringp def)
(princ def (current-buffer))
(loaddefs-generate--print-form def))
(unless (bolp)
(insert "\n")))))
;; Only write the file if we actually made a change.
(unless (equal (buffer-hash) hash)
(write-region (point-min) (point-max) loaddefs-file nil 'silent)
(byte-compile-info
(file-relative-name loaddefs-file (car (ensure-list dir)))
t "GEN"))))))))
(defun loaddefs-generate--print-form (def) (defun loaddefs-generate--print-form (def)
"Print DEF in a format that makes sense for version control." "Print DEF in a format that makes sense for version control."