all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Why does (file-exists-p "") return t?
@ 2023-02-09 10:41 Edgar Vincent
  2023-02-09 10:56 ` Gregory Heytings
  0 siblings, 1 reply; 10+ messages in thread
From: Edgar Vincent @ 2023-02-09 10:41 UTC (permalink / raw)
  To: help-gnu-emacs

Hello everyone,

I just noticed that (file-exists-p “”) returns t on my system (Emacs 28.2 on Arch Linux).
I am not familiar with Emacs’ file handling internals, so the answer might be obvious.
However, I did not find any mention of this case in the docstring or the Elisp manual.

Thanks a lot,

Edgar Vincent


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why does (file-exists-p "") return t?
  2023-02-09 10:41 Why does (file-exists-p "") return t? Edgar Vincent
@ 2023-02-09 10:56 ` Gregory Heytings
  2023-02-09 11:10   ` Edgar Vincent
  0 siblings, 1 reply; 10+ messages in thread
From: Gregory Heytings @ 2023-02-09 10:56 UTC (permalink / raw)
  To: Edgar Vincent; +Cc: help-gnu-emacs


>
> I just noticed that (file-exists-p “”) returns t on my system (Emacs 
> 28.2 on Arch Linux).
>
> I am not familiar with Emacs’ file handling internals, so the answer 
> might be obvious.
>
> However, I did not find any mention of this case in the docstring or the 
> Elisp manual.
>

When the argument to file-exists-p is not an absolute file name, it is 
understood as being relative to default-directory.  Therefore with an 
empty string, file-exists-p checks whether default-directory exists, which 
is normally the case.  But

(let ((default-directory "/nonexistent")) (file-exists-p ""))

returns nil.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why does (file-exists-p "") return t?
  2023-02-09 10:56 ` Gregory Heytings
@ 2023-02-09 11:10   ` Edgar Vincent
  2023-02-09 13:36     ` Eli Zaretskii
  0 siblings, 1 reply; 10+ messages in thread
From: Edgar Vincent @ 2023-02-09 11:10 UTC (permalink / raw)
  To: Gregory Heytings; +Cc: help-gnu-emacs

Gregory Heytings <gregory@heytings.org> writes:

> When the argument to file-exists-p is not an absolute file name, it is
> understood as being relative to default-directory.  Therefore with an empty
> string, file-exists-p checks whether default-directory exists, which is normally
> the case.  But
>
> (let ((default-directory “/nonexistent”)) (file-exists-p “”))
>
> returns nil.

Thank you very much for your reply. This makes sense - and I’m sure it is mentioned somewhere
in the documentation.

Edgar Vincent



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why does (file-exists-p "") return t?
  2023-02-09 11:10   ` Edgar Vincent
@ 2023-02-09 13:36     ` Eli Zaretskii
  2023-02-09 21:05       ` Edgar Vincent
  0 siblings, 1 reply; 10+ messages in thread
From: Eli Zaretskii @ 2023-02-09 13:36 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Edgar Vincent <e-v@posteo.net>
> Cc: help-gnu-emacs@gnu.org
> Date: Thu, 09 Feb 2023 11:10:07 +0000
> 
> Gregory Heytings <gregory@heytings.org> writes:
> 
> > (let ((default-directory “/nonexistent”)) (file-exists-p “”))
> >
> > returns nil.
> 
> Thank you very much for your reply. This makes sense - and I’m sure it is mentioned somewhere
> in the documentation.

It is:

     Expanding ‘.’ or the empty string returns the default directory:

          (expand-file-name "." "/usr/spool/")
               ⇒ "/usr/spool"
          (expand-file-name "" "/usr/spool/")
               ⇒ "/usr/spool"



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why does (file-exists-p "") return t?
  2023-02-09 13:36     ` Eli Zaretskii
@ 2023-02-09 21:05       ` Edgar Vincent
  2023-02-10  4:20         ` Jean Louis
  0 siblings, 1 reply; 10+ messages in thread
From: Edgar Vincent @ 2023-02-09 21:05 UTC (permalink / raw)
  To: eliz, help-gnu-emacs

I didn’t know that file-exists-p used expand-file-name, but it does seem obvious.

Thanks for your reply!


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why does (file-exists-p "") return t?
  2023-02-09 21:05       ` Edgar Vincent
@ 2023-02-10  4:20         ` Jean Louis
  2023-02-11 10:10           ` Bruno Barbier
  2023-02-11 10:39           ` Eli Zaretskii
  0 siblings, 2 replies; 10+ messages in thread
From: Jean Louis @ 2023-02-10  4:20 UTC (permalink / raw)
  To: Edgar Vincent; +Cc: eliz, help-gnu-emacs

* Edgar Vincent <e-v@posteo.net> [2023-02-10 00:11]:
> I didn’t know that file-exists-p used expand-file-name, but it does seem obvious.

To avoid the problem maybe you should use `expand-file-name' before
you check it with `file-exists-p'

As otherwise you may check the wrong path.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why does (file-exists-p "") return t?
  2023-02-10  4:20         ` Jean Louis
@ 2023-02-11 10:10           ` Bruno Barbier
  2023-02-11 11:28             ` Jean Louis
  2023-02-11 10:39           ` Eli Zaretskii
  1 sibling, 1 reply; 10+ messages in thread
From: Bruno Barbier @ 2023-02-11 10:10 UTC (permalink / raw)
  To: Jean Louis, Edgar Vincent; +Cc: eliz, help-gnu-emacs

Jean Louis <bugs@gnu.support> writes:

> * Edgar Vincent <e-v@posteo.net> [2023-02-10 00:11]:
>> I didn’t know that file-exists-p used expand-file-name, but it does seem obvious.
>
> To avoid the problem maybe you should use `expand-file-name' before
> you check it with `file-exists-p'
>
> As otherwise you may check the wrong path.

Why ?

One of the first thing `file-exists-p' is doing is to call
`expand-file-name' to expand the file name.  I don't see how calling
`expand-file-name' before would help.

Bruno



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why does (file-exists-p "") return t?
  2023-02-10  4:20         ` Jean Louis
  2023-02-11 10:10           ` Bruno Barbier
@ 2023-02-11 10:39           ` Eli Zaretskii
  1 sibling, 0 replies; 10+ messages in thread
From: Eli Zaretskii @ 2023-02-11 10:39 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Fri, 10 Feb 2023 07:20:01 +0300
> From: Jean Louis <bugs@gnu.support>
> Cc: eliz@gnu.org, help-gnu-emacs@gnu.org
> 
> * Edgar Vincent <e-v@posteo.net> [2023-02-10 00:11]:
> > I didn’t know that file-exists-p used expand-file-name, but it does seem obvious.
> 
> To avoid the problem maybe you should use `expand-file-name' before
> you check it with `file-exists-p'

But there's no problem here.

All Emacs functions that accept file names use expand-file-name
internally before passing the file name to system APIs, because every
file name in Emacs can be relative to the current buffer's
default-directory.  IOW, Emacs pretends to change its current
directory to the buffer's default-directory when you switch buffers.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why does (file-exists-p "") return t?
  2023-02-11 10:10           ` Bruno Barbier
@ 2023-02-11 11:28             ` Jean Louis
  2023-02-11 19:34               ` Bruno Barbier
  0 siblings, 1 reply; 10+ messages in thread
From: Jean Louis @ 2023-02-11 11:28 UTC (permalink / raw)
  To: Bruno Barbier; +Cc: Edgar Vincent, eliz, help-gnu-emacs

* Bruno Barbier <brubar.cs@gmail.com> [2023-02-11 13:10]:
> > * Edgar Vincent <e-v@posteo.net> [2023-02-10 00:11]:
> >> I didn’t know that file-exists-p used expand-file-name, but it does seem obvious.
> >
> > To avoid the problem maybe you should use `expand-file-name' before
> > you check it with `file-exists-p'
> >
> > As otherwise you may check the wrong path.
> 
> Why ?
> 
> One of the first thing `file-exists-p' is doing is to call
> `expand-file-name' to expand the file name.  I don't see how calling
> `expand-file-name' before would help.

I understand you and I am sure in most use cases it is not necessary
as it will be expanded later. 

Sometimes I wish to expand it before for reason that
`default-directory' sometimes changes:

- practically, I sometimes wish to have "/home/user/public_html" as part of
  directory, and to check for file existence only if that is part of
  the path.

- When I use Tramp, my program my give me wrong answer by design, as I
  maybe did not want to check anything on remote system, but program
  is not aware of it by alone.

Of course, to avoid similar cases, I can keep `default-directory':

(let ((default-directory "something"))
 (before doing something else))

There is pertinent use case where I am receiving "" from database, and
instead of `expand-file-name' I want to consider it "no file name",
and checking would take place only when there is no empty string, as
otherwis I would get information how file exists for "" which is not
meant to be by design.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why does (file-exists-p "") return t?
  2023-02-11 11:28             ` Jean Louis
@ 2023-02-11 19:34               ` Bruno Barbier
  0 siblings, 0 replies; 10+ messages in thread
From: Bruno Barbier @ 2023-02-11 19:34 UTC (permalink / raw)
  To: Jean Louis; +Cc: Edgar Vincent, eliz, help-gnu-emacs

Jean Louis <bugs@gnu.support> writes:

> * Bruno Barbier <brubar.cs@gmail.com> [2023-02-11 13:10]:
>> > As otherwise you may check the wrong path.
>> 
>> Why ?
>> 
>> One of the first thing `file-exists-p' is doing is to call
>> `expand-file-name' to expand the file name.  I don't see how calling
>> `expand-file-name' before would help.
>
> I understand you and I am sure in most use cases it is not necessary
> as it will be expanded later. 

>
> Sometimes I wish to expand it before for reason that
> `default-directory' sometimes changes:

IIUC, you are saying that it may be better to expand immediately and to
use the expanded version, to not risk the file to be expanded later,
possibly in a wrong/unknown context.

Very good point, indeed.

Thanks for the explanation and the advice, Jean.

Bruno



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-02-11 19:34 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-09 10:41 Why does (file-exists-p "") return t? Edgar Vincent
2023-02-09 10:56 ` Gregory Heytings
2023-02-09 11:10   ` Edgar Vincent
2023-02-09 13:36     ` Eli Zaretskii
2023-02-09 21:05       ` Edgar Vincent
2023-02-10  4:20         ` Jean Louis
2023-02-11 10:10           ` Bruno Barbier
2023-02-11 11:28             ` Jean Louis
2023-02-11 19:34               ` Bruno Barbier
2023-02-11 10:39           ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.