From fcd415d4ea68ebdf415559cf114d1ec2e5117e4c Mon Sep 17 00:00:00 2001 From: Ted Lukin Date: Wed, 2 Sep 2026 20:15:32 +0700 Subject: [PATCH] fix improper cant convert error in path-relative-to (#18792) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Then: ```nu × Can't convert to prefix not found. ╭─[repl_entry #11:1:1] 1 │ "/home/pheenty/" | path relative-to "/home/pheenty/git" · ────────┬─────── · ╰── can't convert string to prefix not found ``` Now: ```nu × The argument path is not a parent of the input path. ╭─[repl_entry #1:1:1] 1 │ "/home/pheenty/" | path relative-to "/home/pheenty/git" · ────────┬─────── · ╰── prefix not found ``` Also changed two huge and almost identical `#[cfg(windows)]` and `#[cfg(not(windows))]` code blocks to just have three `if cfg!(windows)` inside while at was at it ## User-facing changes (Release notes) Fixed `path relative-to` providing improper error messages when its argument was not a parent of the input. --- crates/nu-command/src/path/relative_to.rs | 55 +++++++++-------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/crates/nu-command/src/path/relative_to.rs b/crates/nu-command/src/path/relative_to.rs index cebfe59114..ef8b2dd728 100644 --- a/crates/nu-command/src/path/relative_to.rs +++ b/crates/nu-command/src/path/relative_to.rs @@ -1,7 +1,7 @@ use super::PathSubcommandArguments; use nu_engine::command_prelude::*; use nu_path::expand_to_real_path; -use nu_protocol::engine::StateWorkingSet; +use nu_protocol::{engine::StateWorkingSet, shell_error::generic::GenericError}; use std::path::Path; struct Arguments { @@ -92,17 +92,24 @@ path." ) } - #[cfg(windows)] fn examples(&self) -> Vec> { vec![ Example { description: "Find a relative path from two absolute paths.", - example: r"'C:\Users\viking' | path relative-to 'C:\Users'", + example: if cfg!(windows) { + r"'C:\Users\viking' | path relative-to 'C:\Users'" + } else { + "'/home/viking' | path relative-to '/home'" + }, result: Some(Value::test_string("viking")), }, Example { description: "Find a relative path from absolute paths in list.", - example: r"[ C:\Users\viking, C:\Users\spam ] | path relative-to C:\Users", + example: if cfg!(windows) { + r"[ C:\Users\viking, C:\Users\spam ] | path relative-to C:\Users" + } else { + "[ /home/viking, /home/spam ] | path relative-to '/home'" + }, result: Some(Value::test_list(vec![ Value::test_string("viking"), Value::test_string("spam"), @@ -110,31 +117,11 @@ path." }, Example { description: "Find a relative path from two relative paths.", - example: r"'eggs\bacon\sausage\spam' | path relative-to 'eggs\bacon\sausage'", - result: Some(Value::test_string("spam")), - }, - ] - } - - #[cfg(not(windows))] - fn examples(&self) -> Vec> { - vec![ - Example { - description: "Find a relative path from two absolute paths.", - example: "'/home/viking' | path relative-to '/home'", - result: Some(Value::test_string("viking")), - }, - Example { - description: "Find a relative path from absolute paths in list.", - example: "[ /home/viking, /home/spam ] | path relative-to '/home'", - result: Some(Value::test_list(vec![ - Value::test_string("viking"), - Value::test_string("spam"), - ])), - }, - Example { - description: "Find a relative path from two relative paths.", - example: "'eggs/bacon/sausage/spam' | path relative-to 'eggs/bacon/sausage'", + example: if cfg!(windows) { + r"'eggs\bacon\sausage\spam' | path relative-to 'eggs\bacon\sausage'" + } else { + "'eggs/bacon/sausage/spam' | path relative-to 'eggs/bacon/sausage'" + }, result: Some(Value::test_string("spam")), }, ] @@ -156,12 +143,12 @@ fn relative_to(path: &Path, span: Span, args: &Arguments) -> Value { } Value::error( - ShellError::CantConvert { - to_type: e.to_string(), - from_type: "string".into(), + GenericError::new( + String::from("The argument path is not a parent of the input path."), + e.to_string(), span, - help: None, - }, + ) + .into(), span, ) }