Subject: bug#49924: 28.0.50; `package-quickstart-refresh' fails for "long" (according to `print-length') `package-alist'

From: Pal Gloss <[email-masked]>
Date: Sat, 7 Aug 2021 10:45:37 +0200

Following #49922, I was able to diagnose & fix an issue with
`package-quickstart-refresh' when `print-length' is lower than
`(length package-activated-list)'.  There are two possible
user-visible symptoms, caused by the presence of the "..." ellipsis
in the `package-quickstart-file':
1. A warning in *Compile-Log* about a free variable ‘\.\.\.’
2. When restarting emacs, an error message about a missing package
   named "..."

I did not make any experiments, but I am pretty sure that it also would
be possible to produce a broken `package-quickstart-file' with
`print-level' suitably low (1 or 2, maybe even 3?).

;; !!! This reproduction scenario ruins your                       !!!
;; !!! $HOME/.emacs.d/package-quickstart.el unless you are careful !!!
;; !!! Run it with:                                                !!!
;; env HOME=/tmp emacs -Q

(let ((print-length 2))
  (require 'package)
  (package-quickstart-refresh))

;; There is a warning in *Compile-Log*
;;
;; Compiling file c:/msys64/tmp/.emacs.d/package-quickstart.el at Sat Aug  7 10:09:10 2021
;; Entering directory ‘c:/msys64/tmp/.emacs.d/’
;; package-quickstart.el:4:30: Warning: reference to free variable ‘\.\.\.’

Now look inside the generated "package-quickstart.el" with

(find-file package-quickstart-file)

And you will see "(setq package-activated-list ...)" (literally with 3
dots) instead of "(setq package-activated-list (append <text elided for brevity>))".

There is another way to reproduce it that can work with a more "likely"
(or reasonable) value of `print-length', but this is not with `emacs -Q'
because it requires a non-NIL `package-alist', i.e. an existing emacs
configuration with some outside packages:

;; !!! This reproduction scenario ruins your                 !!!
;; !!! `package-quickstart-file'.  Regenerate it afterwards. !!!
;; Assuming you have at least 4 external packages activated in
;; your configuration
(progn
  (package-initialize)
  (let ((print-length (- (length package-alist) 1)))
    (package-quickstart-refresh)))

Now look inside the generated "package-quickstart.el" with

(find-file package-quickstart-file)

And you will see that a package named "..." is added to the
`package-activated-list'.  Next time you start emacs, the package
initialization will fail because there is no such package.

My work-around is to rebind `print-level' and `print-length' before
calling `package-quickstart-refresh' (I do so periodically in the
background):

;; -------- From my early-init.el file --------
;; Enable package quickstart ...
(funcall (or (get 'package-quickstart 'custom-set)
             'set-default)
         'package-quickstart t)
;; ... and make sure it stays up to date
(run-with-idle-timer
 120 ; seconds
 'repeat
 (lambda ()
   (let (;; don't abbreviate lists: the `package-activated-list' is long and
         ;; needs to be complete, otherwise Emacs will search for a package
         ;; called "...".
         (print-length nil)
         ;; I haven't had a problem with it yet, but better safe than sorry.
         (print-level nil))
     (package-quickstart-refresh))))
;; -------- End of my early-init.el file --------

In GNU Emacs 28.0.50 (build 1, x86_64-w64-mingw32)
 of 2021-08-06 built on <hostname>
Repository revision: 0afab352e02d7070a8ced2af53c1d01217400cdf
Repository branch: master
Windowing system distributor 'Microsoft Corp.', version 6.3.9600
System Description: Microsoft Windows 8.1 (v6.3.0.9600.20069)

Configured using:
 'configure --with-xml2 --with-zlib --without-pop
 --prefix=/home/username/emacs-native-comp/dest
 --without-compress-install --without-dbus --with-native-compilation
 --with-modules 'CFLAGS=-O2 -g3'
 PKG_CONFIG_PATH=/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig'

Configured features:
ACL GIF GMP GNUTLS HARFBUZZ JPEG JSON LCMS2 LIBXML2 MODULES NATIVE_COMP
NOTIFY W32NOTIFY PDUMPER PNG RSVG SOUND THREADS TIFF TOOLKIT_SCROLL_BARS
XPM ZLIB

Important settings:
  value of $LC_CTYPE: fr_FR.UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: cp1252

Major mode: ELisp/l

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
rfc822 mml mml-sec epa derived epg epg-config gnus-util rmail
rmail-loaddefs time-date mm-decode mm-bodies mm-encode mail-parse
rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045
ietf-drums mm-util mail-prsvr mail-utils modus-vivendi-theme
modus-themes pcase compile text-property-search comint ansi-color ring
pp info finder-inf package browse-url url url-proxy url-privacy
url-expand url-methods url-history url-cookie url-domsuf url-util
mailcap url-handlers url-parse auth-source eieio eieio-core
eieio-loaddefs password-cache json map url-vars comp comp-cstr warnings
subr-x rx cl-seq cl-macs cl-extra help-mode seq byte-opt gv cl-loaddefs
cl-lib bytecomp byte-compile cconv iso-transl tooltip eldoc electric
uniquify ediff-hook vc-hooks lisp-float-type mwheel dos-w32 ls-lisp
disp-table term/w32-win w32-win w32-vars term/common-win tool-bar dnd
fontset image regexp-opt fringe tabulated-list replace newcomment
text-mode elisp-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads w32notify
w32 lcms2 multi-tty make-network-process native-compile emacs)

Memory information:
((conses 16 174962 29708)
 (symbols 48 12845 1)
 (strings 32 34208 2217)
 (string-bytes 1 1074693)
 (vectors 16 20567)
 (vector-slots 8 314808 27474)
 (floats 8 82 46)
 (intervals 56 304 0)
 (buffers 992 15))