Subject: Re: bug#79036: [PATCH] Fix pdb tracking for remote filenames

From: Liu Hui <[email-masked]>
Date: Thu, 17 Jul 2025 18:20:59 +0800

On Thu, Jul 17, 2025 at 4:46 PM Eli Zaretskii <[email-masked]> wrote:

> So now, Liu Hui, please explain the problem in more detail.
>
> I stared at the code for some time, and I don't understand why it goes
> to such great lengths here:
>
>   (let* ((file-name-prospect (concat (file-remote-p default-directory)
>                               file-name))
>          (file-buffer (get-file-buffer file-name-prospect)))
>     (unless file-buffer
>       (cond
>        ((file-exists-p file-name-prospect)
>         (setq file-buffer (find-file-noselect file-name-prospect)))
>        ((and (not (equal file-name file-name-prospect))
>              (file-exists-p file-name))
>         ;; Fallback to a locally available copy of the file.
>         (setq file-buffer (find-file-noselect file-name-prospect))))
>
> Why not just call find-file-noselect with FILE-NAME as its argument?
> The function find-file-noselect will itself check if there's already a
> buffer visiting that file, so it's unnecessary to do that "by hand"
> here.  And I don't see the reason for that 'concat' dance, either,
> since find-file-noselect again does everything that needs to be done
> internally.  So what am I missing here?

Because file-name may be not the actual file. file-name is actually
extracted from the pdb output in the python shell, e.g.

    >>> f()
    > /ssh:server:/tmp/test.py(3)f()
    (Pdb)

Generally, pdb shows local file names. But if we send the code using
C-c C-c from python buffers (i.e. step 3), python-send-string passes
(buffer-file-name), which may be a remote file name, to python
process. Therefore, pdb may also shows remote file names.

Then, if file-name is remote, we don't prepend any prefix (the updated
behavior in the patch) and calling find-file-noselect is enough.

If we execute "from test import f; f()" in the remote python shell,
instead of C-c C-c, the pdb output is:

    >>> from test import f; f()
    > /tmp/test.py(3)f()
    -> return 1
    (Pdb)

Then file-name "/tmp/test.py" is local, but the actually file is
remote. Therefore, the code tries to prepend the remote prefix of
python shell to file-name by concat.

There are also other cases, e.g. sending code in a local file to a
remote python process, or sending code in a remote file to a local
python process. Therefore, the code becomes complex.

In fact, I just find a bug when sending code in a local file to a
remote python process:

1. emacs -Q and start a remote python shell
2. Create a local python file, e.g. /tmp/foo.py
3. Insert the following code in foo.py and press C-c C-c to send it
   to the remote python shell:

   def g():
       breakpoint()
       return 2

4. Execute g() in the python shell to trigger the pdb

Result: Emacs tries to open /ssh:server:/tmp/foo.py instead of
/tmp/foo.py.

I will try to update the patch to fix both problems.