GNU bug report logs - #65308
(find-file) botches buffer-file-name in -batch mode

Previous Next

Package: emacs;

Reported by: Konstantin Kharlamov <Hi-Angel <at> yandex.ru>

Date: Tue, 15 Aug 2023 11:25:02 UTC

Severity: normal

Tags: notabug

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 65308 in the body.
You can then email your comments to 65308 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#65308; Package emacs. (Tue, 15 Aug 2023 11:25:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Konstantin Kharlamov <Hi-Angel <at> yandex.ru>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Tue, 15 Aug 2023 11:25:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Konstantin Kharlamov <Hi-Angel <at> yandex.ru>
To: bug-gnu-emacs <at> gnu.org
Subject: (find-file) botches buffer-file-name in -batch mode
Date: Tue, 15 Aug 2023 14:25:16 +0300
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




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65308; Package emacs. (Tue, 15 Aug 2023 12:04:02 GMT) Full text and rfc822 format available.

Message #8 received at 65308 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Konstantin Kharlamov <Hi-Angel <at> yandex.ru>
Cc: 65308 <at> debbugs.gnu.org
Subject: Re: bug#65308: (find-file) botches buffer-file-name in -batch mode
Date: Tue, 15 Aug 2023 15:03:15 +0300
tags 65308 notabug
thanks

> From: Konstantin Kharlamov <Hi-Angel <at> 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.




Added tag(s) notabug. Request was from Eli Zaretskii <eliz <at> gnu.org> to control <at> debbugs.gnu.org. (Tue, 15 Aug 2023 12:04:03 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65308; Package emacs. (Tue, 15 Aug 2023 12:17:02 GMT) Full text and rfc822 format available.

Message #13 received at 65308 <at> debbugs.gnu.org (full text, mbox):

From: Konstantin Kharlamov <Hi-Angel <at> yandex.ru>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 65308 <at> debbugs.gnu.org
Subject: Re: bug#65308: (find-file) botches buffer-file-name in -batch mode
Date: Tue, 15 Aug 2023 15:17:00 +0300
On Tue, 2023-08-15 at 15:03 +0300, Eli Zaretskii wrote:
> tags 65308 notabug
> thanks
> 
> > From: Konstantin Kharlamov <Hi-Angel <at> 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".




Reply sent to Eli Zaretskii <eliz <at> gnu.org>:
You have taken responsibility. (Tue, 15 Aug 2023 12:27:02 GMT) Full text and rfc822 format available.

Notification sent to Konstantin Kharlamov <Hi-Angel <at> yandex.ru>:
bug acknowledged by developer. (Tue, 15 Aug 2023 12:27:02 GMT) Full text and rfc822 format available.

Message #18 received at 65308-done <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Konstantin Kharlamov <Hi-Angel <at> yandex.ru>
Cc: 65308-done <at> debbugs.gnu.org
Subject: Re: bug#65308: (find-file) botches buffer-file-name in -batch mode
Date: Tue, 15 Aug 2023 15:26:37 +0300
> From: Konstantin Kharlamov <Hi-Angel <at> yandex.ru>
> Cc: 65308 <at> 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.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 13 Sep 2023 11:24:07 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 337 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.