zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: A way to find a bug (or explain a missuse of) realpath expansion?
Date: Thu, 2 May 2024 09:03:19 -0700	[thread overview]
Message-ID: <CAH+w=7Y98ma86ZxGRLG4TZKe0MwJYvQ5XKNiCSHyMQCBz-4AMw@mail.gmail.com> (raw)
In-Reply-To: <ZjNJluK3-kygQHvN@prometheus>

On Thu, May 2, 2024 at 1:06 AM Marc Chantreux <mc@unistra.fr> wrote:
>
> On Tue, Apr 30, 2024 at 08:43:51PM -0700, Bart Schaefer wrote:
> > > The /usr shade of path will disapear from debian distribution
> > Can you provide a reference for this?  Web search is clogged with
> > reports of accidental deletions of the /usr tree, etc.
>
>         https://lists.debian.org/debian-devel-announce/2023/10/msg00003.html
>         https://wiki.debian.org/UsrMerge

Ah, I must have misunderstood "shade of path will disappear".

> > You're trying to use a glob qualifier form (:A) but you're not doing
> > globbing, you're doing parameter expansion.
>
> I was globing to remove non-existing directories.

What I meant was, syntactically, globbing does not occur in the order
that you think it did here:
  ${(u):-${foo%/}(:A)}
That is, the stuff to the right of :- in that expression is not
globbed until after the entire parameter expansion is resolved, so the
(u) flag does not "see" the result of globbing, it just sees
  /usr/bin /bin(:A)
and does nothing useful with it.

Note also that (:A) has been appended to the entire $foo expansion,
not to each element of the array $foo, so even once you get to the
globbing step only /bin has realpath applied to it -- the result would
be different for the reversed order
  foo=( {,/usr}/bin )

There's no way to embed filename generation (globbing) inside a
${param} expansion without using an intermediate command substitution.
The three $-prefixed forms of expansion -- parameter ${var}, command
$(cmd), and arithmetic $((expr)) -- are processed "together"
left-to-right and inside-out, but the other five -- history, process,
brace, tilde, and globbing -- are strictly independent.  (The
Expansion introduction could be more explicit about this.)

> But let's say it wasn't intentional: don't you think the result is
> flawed anyway? I mean: I don't understand how the same expansion on
> 2 identical elements of the list can lead to 2 different results.

In your attempt to work this out step-by-step you have
  foo=( $foo(:A) )        ; print -l ">>>> realpath" $foo
but there again $foo(:A) is
  /usr/bin /usr/bin /bin /bin(:A)
What you wanted was
  foo=( $^foo(:A) )
to get
  /usr/bin(:A) /usr/bin(:A) /bin(:A) /bin(:A)

Conversely, ${foo:A} is one step (parameter expansion) rather than two
(parameter, then glob) so realpath is "automatically" applied to every
element of the array.

Does that explain it?

You could have removed all the non-existing directories earlier in the
process, like this:
  foo=( {/usr,}/bin{,/}(/N) ) ; print -l ">>>> setting foo" $foo
That attaches the (/N) -- match directories, discard non-matches -- to
the brace expansion, yielding (before globbing)
  /usr/bin(/N) /usr/bin(/N) /bin(/N) /bin(/N)

In fact you could do the entire process right there:
  typeset -aU foo=( {/usr,}/bin{,/}(/N:A) )
  print -l $foo
"Create an array foo of unique elements and assign the realpaths of
the existing directories in the set /usr/bin /bin /usr/bin/ /bin/"

> [...] so the underlining
> question of my precedent mail was: "is the code of zsh easy to dive in"?

I would have to say it's not, at least for what you're trying to
accomplish here.


      reply	other threads:[~2024-05-02 16:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-30 13:58 Marc Chantreux
2024-05-01  3:43 ` Bart Schaefer
2024-05-02  8:06   ` Marc Chantreux
2024-05-02 16:03     ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7Y98ma86ZxGRLG4TZKe0MwJYvQ5XKNiCSHyMQCBz-4AMw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).