Subject: Re: bug#48137: 27.2; `package-install-file' fails when loading a package file with DOS line endings

From: Ioannis Kappas <[email-masked]>
Date: Sun, 16 May 2021 10:09:07 +0100

On Sat, May 15, 2021 at 2:52 PM Ioannis Kappas <[email-masked]> wrote:
> I shall have a look next whether we could always load the package with
> `insert-file-contents-literally' but parse headers with the correct
> encoding (`find-operation-coding-system' looks like a promising fn
> to determine the correct encoding from a literal file buffer).

Please find below a patch to read package headers from a temporarily
decoded buffer, while keeping the literal buffer (which will be used
to install the package) intact.

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index ecb2573cab..a7a11bc6cc 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -2122,8 +2122,22 @@ package-install-from-buffer
             ((derived-mode-p 'tar-mode)
              (package-tar-file-info))
             (t
-             (save-excursion
-              (package-buffer-info)))))
+             ;; Package headers should be parsed from decoded text
+             ;; (see Bug#48137) where possible.
+             (if (and (eq buffer-file-coding-system 'no-conversion)
+                      buffer-file-name)
+                 (let* ((package-buffer (current-buffer))
+                        (decoding-system
+                         (car (find-operation-coding-system
'insert-file-contents
+                                                            (cons
buffer-file-name
+
package-buffer)))))
+                   (with-temp-buffer
+                     (insert-buffer-substring package-buffer)
+                     (decode-coding-region (point-min) (point-max)
decoding-system)
+                     (package-buffer-info)))
+
+               (save-excursion
+                 (package-buffer-info))))))
          (name (package-desc-name pkg-desc)))
     ;; Download and install the dependencies.
     (let* ((requires (package-desc-reqs pkg-desc))
@@ -2148,6 +2162,7 @@ package-install-file
           (setq default-directory file)
           (dired-mode))
       (insert-file-contents-literally file)
+      (set-visited-file-name file)
       (when (string-match "\\.tar\\'" file) (tar-mode)))
     (package-install-from-buffer)))

Notes:
- The logic can be moved to `package-buffer-info'.
- Ideally, I would have liked to only copy and header section to the
  temporary buffer for decoding, but the `(while (comment-forward))`
  trick that I've tried fails to move past the headers section in
  literal buffers with CRLF pairs. Perhaps it is better after all not
  to try and use any regex ops on a 'binary buffer?

thanks