From b86200938f8b9e9a2016cb2130c14d0bd3193a2f Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 26 Mar 2019 10:11:36 +0100 Subject: [PATCH] Always use "." for cd Nobody doesn't want to use $PWD to cd, so if $CDPATH does not include it that was a mistake. Bash also appends "." here. Fixes #4484. --- CHANGELOG.md | 1 + sphinx_doc_src/cmds/cd.rst | 2 +- src/path.cpp | 5 ++--- tests/cd.err | 3 +++ tests/cd.in | 10 ++++++++++ tests/cd.out | 6 ++++++ 6 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9649eb8f0d7..99c4789e39d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Add `$pipestatus` support - macOS Mojave: fish.app can actually run (#5727), 10.14.4's Terminal.app no longer causes an error on launch (#5725) - fish no longer requires buffering for the last function in a pipeline. +- cd now always checks the current directory, even if $CDPATH does not include it or "." (#4484). ### Syntax changes and new commands - None yet. diff --git a/sphinx_doc_src/cmds/cd.rst b/sphinx_doc_src/cmds/cd.rst index 2a6fcc748b99..623d853b85bf 100644 --- a/sphinx_doc_src/cmds/cd.rst +++ b/sphinx_doc_src/cmds/cd.rst @@ -13,7 +13,7 @@ Description If ``DIRECTORY`` is supplied, it will become the new directory. If no parameter is given, the contents of the ``HOME`` environment variable will be used. -If ``DIRECTORY`` is a relative path, the paths found in the ``CDPATH`` environment variable array will be tried as prefixes for the specified path. +If ``DIRECTORY`` is a relative path, the paths found in the ``CDPATH`` list will be tried as prefixes for the specified path, in addition to $PWD. Note that the shell will attempt to change directory without requiring ``cd`` if the name of a directory is provided (starting with ``.``, ``/`` or ``~``, or ending with ``/``). diff --git a/src/path.cpp b/src/path.cpp index 8349dd50e6e0..9377386645f5 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -173,9 +173,8 @@ maybe_t path_get_cdpath(const wcstring &dir, const wcstring &wd, if (auto cdpaths = env_vars.get(L"CDPATH")) { cdpathsv = cdpaths->as_list(); } - if (cdpathsv.empty()) { - cdpathsv.push_back(L"."); - } + // Always append $PWD + cdpathsv.push_back(L"."); for (wcstring next_path : cdpathsv) { if (next_path.empty()) next_path = L"."; if (next_path == L"." && !wd.empty()) { diff --git a/tests/cd.err b/tests/cd.err index 4653e2ee6241..fae7b99e01eb 100644 --- a/tests/cd.err +++ b/tests/cd.err @@ -7,3 +7,6 @@ #################### # Virtual PWD inheritance + +#################### +# CDPATH diff --git a/tests/cd.in b/tests/cd.in index 2d02d269ca0f..e70a7f902331 100644 --- a/tests/cd.in +++ b/tests/cd.in @@ -62,6 +62,16 @@ test (realpath $output_pwd) = $real_getcwd and echo "BogusPWD test 2 succeeded" or echo "BogusPWD test 2 failed: $output_pwd vs $real_getcwd" +# $CDPATH +logmsg CDPATH +set -g CDPATH $base +cd linkhome +test $PWD = $base/linkhome; and echo Gone to linkhome via CDPATH +set -g CDPATH /tmp +cd $base +test $PWD = $base; and echo Gone to base +cd linkhome +test $PWD = $base/linkhome; and echo Gone to linkhome via implicit . in CDPATH # cd back before removing the test directory again. cd $oldpwd diff --git a/tests/cd.out b/tests/cd.out index 8fd581f17e86..d3801a10a887 100644 --- a/tests/cd.out +++ b/tests/cd.out @@ -23,3 +23,9 @@ cd: PWD is /tmp/cdcomp_test/linkhome BogusPWD test 1 succeeded BogusPWD test 2 succeeded + +#################### +# CDPATH +Gone to linkhome via CDPATH +Gone to base +Gone to linkhome via implicit . in CDPATH