cd: fix path when trying to cd out of the root directory

Part of #12704
This commit is contained in:
Nahor
2026-04-30 11:30:16 -07:00
committed by Johannes Altmanninger
parent d885e0efd7
commit 4f539dffaf
2 changed files with 15 additions and 1 deletions

View File

@@ -358,7 +358,14 @@ pub fn path_apply_cdpath(dir: &wstr, wd: &wstr, env_vars: &dyn Environment) -> V
// We want to return an absolute path (see issue 6220)
if ![Some('/'), Some('~')].contains(&path.chars().next()) {
abspath = wd.to_owned();
abspath.push('/');
// Do not add a second slash if `wd` already ends with one
// (typically, when it's the root directory).
// This could result in unwanted paths (e.g. `//<path>`, which
// on Windows, is a remote directory).
if abspath.chars().next_back() != Some('/') {
abspath.push('/');
}
}
abspath.push_utfstr(&path);

View File

@@ -407,3 +407,10 @@ cd (string repeat 4096 a)
# CHECKERR: ^
# CHECKERR: in function 'cd' with arguments '{{.*}}'
# CHECKERR: called on line {{\d+}} of file {{.*}}/cd.fish
# Ensures `cd` doesn't create `<pwd>+/+<dir>` internally, when pwd is `/`, i.e.
# results in `//<dir>`. This test will (hopefully) fail on platforms where such
# a path has a special meaning (e.g. Windows would fail trying to access a server
# named "bin")
cd /
cd bin