From 407a455cfde9a67b1328dff4742f1e2465c5edfe Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Mon, 18 Jul 2022 20:45:30 +0200 Subject: [PATCH] realpath: Use physical PWD This was an inadvertent change from cc632d6ae95bdf3ebde068358397666b6a1b3b68. Because we used wgetcwd directly before, we always got the "physical" resolved $PWD. There's an argument to be made to use the logical $PWD here as well but I prefer not to make changes lik that in a random commit without good reason. --- src/builtins/realpath.cpp | 8 +++++++- tests/checks/realpath.fish | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/builtins/realpath.cpp b/src/builtins/realpath.cpp index 2637b84e5..7d7aaa29a 100644 --- a/src/builtins/realpath.cpp +++ b/src/builtins/realpath.cpp @@ -101,7 +101,13 @@ maybe_t builtin_realpath(parser_t &parser, io_streams_t &streams, const wch return STATUS_CMD_ERROR; } } else { - wcstring absolute_arg = string_prefixes_string(L"/", arg) ? arg : path_apply_working_directory(arg, parser.vars().get_pwd_slash()); + // We need to get the *physical* pwd here. + auto realpwd = wrealpath(parser.vars().get_pwd_slash()); + if (!realpwd) { + streams.err.append_format(L"builtin %ls: realpath failed: %s\n", cmd, std::strerror(errno)); + return STATUS_CMD_ERROR; + } + wcstring absolute_arg = string_prefixes_string(L"/", arg) ? arg : path_apply_working_directory(arg, *realpwd); streams.out.append(normalize_path(absolute_arg, /* allow leading double slashes */ false)); } diff --git a/tests/checks/realpath.fish b/tests/checks/realpath.fish index 635e66c3f..2f4dfbf54 100644 --- a/tests/checks/realpath.fish +++ b/tests/checks/realpath.fish @@ -72,6 +72,19 @@ else echo "fish-symlink not handled correctly: $real_path != $expected_real_path" >&2 end +# But the $PWD is still resolved +set -l oldpwd $PWD +cd $XDG_DATA_HOME/fish-symlink +set -l real_path (builtin realpath -s $data_home_realpath/fish-symlink) +set -l expected_real_path "$data_home_realpath/fish-symlink" +if test "$real_path" = "$expected_real_path" + echo "fish-symlink handled correctly" + # CHECK: fish-symlink handled correctly +else + echo "fish-symlink not handled correctly: $real_path != $expected_real_path" >&2 +end +cd $oldpwd + set -l real_path (builtin realpath -s .) set -l expected_real_path (pwd -P) # Physical working directory. if test "$real_path" = "$expected_real_path"