Provide a better error for prefix-only path for PWD (#15817)

This commit is contained in:
Piepmatz 2025-05-24 21:11:26 +02:00 committed by GitHub
parent dbb30cc9e0
commit 89df01f829
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,7 @@ use nu_utils::IgnoreCaseExt;
use std::{
collections::{HashMap, HashSet},
fs::File,
path::{Component, MAIN_SEPARATOR},
sync::Arc,
};
@ -755,6 +756,19 @@ impl Stack {
let path = path.as_ref();
if !path.is_absolute() {
if matches!(path.components().next(), Some(Component::Prefix(_))) {
return Err(ShellError::GenericError {
error: "Cannot set $env.PWD to a prefix-only path".to_string(),
msg: "".into(),
span: None,
help: Some(format!(
"Try to use {}{MAIN_SEPARATOR} instead",
path.display()
)),
inner: vec![],
});
}
error("Cannot set $env.PWD to a non-absolute path")
} else if !path.exists() {
error("Cannot set $env.PWD to a non-existent directory")