Subject: bug#65308: closed (Re: bug#65308: (find-file) botches buffer-file-name in -batch mode)

From: [email-masked] (GNU bug Tracking System)
Date: Tue, 15 Aug 2023 12:27:02 +0000

Your bug report

#65308: (find-file) botches buffer-file-name in -batch mode

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
65308: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=65308
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#65308: (find-file) botches buffer-file-name in -batch mode
From: Eli Zaretskii <[email-masked]>
To: Konstantin Kharlamov <[email-masked]>
Cc: [email-masked]
Date: Tue, 15 Aug 2023 15:26:37 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

> From: Konstantin Kharlamov <[email-masked]>
> Cc: [email-masked]
> Date: Tue, 15 Aug 2023 15:17:00 +0300
> 
> On Tue, 2023-08-15 at 15:03 +0300, Eli Zaretskii wrote:
> > There's no bug here.  To get what you want, run the command-line
> > arguments through expand-file-name _before_ calling find-file.
> 
> Oh, okay, thank you, I see. I settled with calling a `(kill-buffer)`
> after `(save-buffer)`, which I guess works because each time the buffer
> is killed we get back to the *scratch* buffer that has the original
> "current directory".

Yes, that will work, as will using save-current-buffer etc.  But
please note that relying on the default-directory of some random
buffer when using relative file names is always fraught with surprises
in Emacs, which is why I suggested to use expand-file-name instead.

Closing.

Subject: (find-file) botches buffer-file-name in -batch mode
From: Konstantin Kharlamov <[email-masked]>
To: [email-masked]
Date: Tue, 15 Aug 2023 14:25:16 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

This was found while trying to use Emacs in `-batch` mode to regexp-
replace many files in a project. Turns out calling `(save-buffer)` will
make it say that directory does not exist, and the reason for that is
that `buffer-file-name` gets mangled filename.

# Steps to reproduce

	$ mkdir dir && touch dir/1 dir/2
	$ emacs -batch --eval '(progn (dolist (file command-line-args-left) (find-file file) (print buffer-file-name))))' dir/1 dir/2                                    

	"/tmp/dir/1"

	"/tmp/dir/dir/2"

## Expected

Second print would say `/tmp/dir/2`

## Actual

Second print refers to non-existing `/tmp/dir/dir/2`.

# Versions tested

Reproducible with stable 29.1 and somewhat old master as of commit
04710bd01b2