From 28ed21864d9bba847e224b2c399dd8d36c95abe9 Mon Sep 17 00:00:00 2001 From: mengsuenyan <49850817+mengsuenyan@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:47:18 +0800 Subject: [PATCH] fixed the bug `~ | path type` return empty string (#9853) - this PR should close #9849 - fixes #9849 --- crates/nu-command/src/path/type.rs | 8 +++++++- crates/nu-command/tests/commands/path/type_.rs | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/path/type.rs b/crates/nu-command/src/path/type.rs index 6061aee604..665ae82967 100644 --- a/crates/nu-command/src/path/type.rs +++ b/crates/nu-command/src/path/type.rs @@ -1,5 +1,6 @@ use std::path::Path; +use nu_path::expand_tilde; use nu_protocol::ast::Call; use nu_protocol::engine::{EngineState, Stack}; use nu_protocol::{ @@ -78,7 +79,12 @@ If nothing is found, an empty string will be returned."# } fn r#type(path: &Path, span: Span, _: &Arguments) -> Value { - let meta = std::fs::symlink_metadata(path); + let meta = if path.starts_with("~") { + let p = expand_tilde(path); + std::fs::symlink_metadata(p) + } else { + std::fs::symlink_metadata(path) + }; Value::string( match &meta { diff --git a/crates/nu-command/tests/commands/path/type_.rs b/crates/nu-command/tests/commands/path/type_.rs index 040c77c987..71a86b3847 100644 --- a/crates/nu-command/tests/commands/path/type_.rs +++ b/crates/nu-command/tests/commands/path/type_.rs @@ -50,5 +50,14 @@ fn returns_type_of_existing_directory() { )); assert_eq!(actual.out, "file"); + + let actual = nu!(pipeline( + r#" + echo "~" + | path type + "# + )); + + assert_eq!(actual.out, "dir"); }) }