* bug#65308: (find-file) botches buffer-file-name in -batch mode
@ 2023-08-15 11:25 Konstantin Kharlamov
2023-08-15 12:03 ` Eli Zaretskii
0 siblings, 1 reply; 4+ messages in thread
From: Konstantin Kharlamov @ 2023-08-15 11:25 UTC (permalink / raw)
To: 65308
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* bug#65308: (find-file) botches buffer-file-name in -batch mode
2023-08-15 11:25 bug#65308: (find-file) botches buffer-file-name in -batch mode Konstantin Kharlamov
@ 2023-08-15 12:03 ` Eli Zaretskii
2023-08-15 12:17 ` Konstantin Kharlamov
0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2023-08-15 12:03 UTC (permalink / raw)
To: Konstantin Kharlamov; +Cc: 65308
tags 65308 notabug
thanks
> From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> Date: Tue, 15 Aug 2023 14:25:16 +0300
>
> $ 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`.
IMO, this is the expected behavior: relative file names are
interpreted relative to the current buffer's default-directory. After
the first find-file, the current buffer visits the file "/tmp/dir/1",
whose default-directory is "/tmp/dir/".
There's no bug here. To get what you want, run the command-line
arguments through expand-file-name _before_ calling find-file.
^ permalink raw reply [flat|nested] 4+ messages in thread
* bug#65308: (find-file) botches buffer-file-name in -batch mode
2023-08-15 12:03 ` Eli Zaretskii
@ 2023-08-15 12:17 ` Konstantin Kharlamov
2023-08-15 12:26 ` Eli Zaretskii
0 siblings, 1 reply; 4+ messages in thread
From: Konstantin Kharlamov @ 2023-08-15 12:17 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 65308
On Tue, 2023-08-15 at 15:03 +0300, Eli Zaretskii wrote:
> tags 65308 notabug
> thanks
>
> > From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> > Date: Tue, 15 Aug 2023 14:25:16 +0300
> >
> > $ 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`.
>
> IMO, this is the expected behavior: relative file names are
> interpreted relative to the current buffer's default-directory.
> After
> the first find-file, the current buffer visits the file "/tmp/dir/1",
> whose default-directory is "/tmp/dir/".
>
> 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".
^ permalink raw reply [flat|nested] 4+ messages in thread
* bug#65308: (find-file) botches buffer-file-name in -batch mode
2023-08-15 12:17 ` Konstantin Kharlamov
@ 2023-08-15 12:26 ` Eli Zaretskii
0 siblings, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2023-08-15 12:26 UTC (permalink / raw)
To: Konstantin Kharlamov; +Cc: 65308-done
> From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> Cc: 65308@debbugs.gnu.org
> 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.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-08-15 12:26 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-15 11:25 bug#65308: (find-file) botches buffer-file-name in -batch mode Konstantin Kharlamov
2023-08-15 12:03 ` Eli Zaretskii
2023-08-15 12:17 ` Konstantin Kharlamov
2023-08-15 12:26 ` Eli Zaretskii
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).