Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make status dirname work correctly in a script after $PWD changes #8562

Open
andmis opened this issue Dec 20, 2021 · 3 comments
Open

Make status dirname work correctly in a script after $PWD changes #8562

andmis opened this issue Dec 20, 2021 · 3 comments

Comments

@andmis
Copy link
Contributor

andmis commented Dec 20, 2021

~/tmp $ cat test.fish 
status dirname
cd ..
status dirname
~/tmp $ fish test.fish
.
.
~/tmp $ 

What do you think about making the second status dirname call return tmp, so that the mental model of status dirname is that at any point in a script, it returns the path to the folder containing that script?

@floam
Copy link
Member

floam commented Dec 25, 2021

I don't think so, it's specifically intended to provide you the path of the script you're running, not $PWD, I think?

You can use $PWD or pwd to get the current directory.

@faho
Copy link
Member

faho commented Dec 25, 2021

I don't think so, it's specifically intended to provide you the path of the script you're running, not $PWD, I think?

It is, yes. It's short for dirname (status filename).

Only it's not absolutized, so if you start a script in the current directory it will effectively do dirname test.fish, which doesn't have a directory, and so it will just print .. Which, of course, isn't useful after you've changed directory anymore.

There is a point to be made, there. Unfortunately it appears tricky to add this, because we don't currently store the script's directory anywhere, only the (relative) filename. So once you've cd'd, we've forgotten what it refers to to begin with.

It's easy enough to work around:

At the beginning of the script, run set -l dir (builtin realpath (status dirname)). Then use that whenever you need the dir.

(also ask yourself whether you need the script's directory to begin with - in my experience many people do this reflexively but never actually need it)

@faho faho added this to the fish-future milestone Dec 25, 2021
@andmis
Copy link
Contributor Author

andmis commented Dec 25, 2021

I don't think so, it's specifically intended to provide you the path of the script you're running, not $PWD, I think?

You can use $PWD or pwd to get the current directory.

@floam, sorry, here is a more detailed example. I would expect that the script at the end here would print Hello, world twice, since in my mind (status dirname)/foo means "the file called foo in the directory containing the current script". But what (status dirname)/foo actually means is "the file called foo at the same relative path to $PWD as the relative path to the current script when the script was launched".

~/tmp $ mkdir subdir
~/tmp $ echo Hello, world > subdir/foo
~/tmp $ echo cat \(status dirname\)/foo > subdir/script.fish
~/tmp $ cat subdir/script.fish 
cat (status dirname)/foo
~/tmp $ fish subdir/script.fish # Works fine
Hello, world
~/tmp $ echo cd .. >> subdir/script.fish
~/tmp $ echo cat \(status dirname\)/foo >> subdir/script.fish
~/tmp $ cat subdir/script.fish 
cat (status dirname)/foo
cd ..
cat (status dirname)/foo
~/tmp $ fish subdir/script.fish # Line 3 fails
Hello, world
cat: subdir/foo: No such file or directory
~/tmp [1] $ 

@andmis andmis closed this as completed Dec 25, 2021
@andmis andmis reopened this Dec 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants