nu-path crate refactor (#3730)

* Resolve rebase artifacts

* Remove leftover dependencies on removed feature

* Remove unnecessary 'pub'

* Start taking notes and fooling around

* Split canonicalize to two versions; Add TODOs

One that takes `relative_to` and one that doesn't.
More TODO notes.

* Merge absolutize to and rename resolve_dots

* Add custom absolutize fn and use it in path expand

* Convert a couple of dunce::canonicalize to ours

* Update nu-path description

* Replace all canonicalize with nu-path version

* Remove leftover dunce dependencies

* Fix broken autocd with trailing slash

Trailing slash is preserved *only* in paths that do not contain "." or
"..". This should be fixed in the future to cover all paths but for now
it at least covers basic cases.

* Use dunce::canonicalize for canonicalizing

* Alow cd recovery from non-existent cwd

* Disable removed canonicalize functionality tests

Remove unused import

* Break down nu-path into separate modules

* Remove unused public imports

* Remove abundant cow mapping

* Fix clippy warning

* Reformulate old canonicalize tests to expand_path

They wouldn't work with the new canonicalize.

* Canonicalize also ~ and ndots; Unify path joining

Also, add doc comments in nu_path::expansions.

* Add comment

* Avoid expanding ndots if path is not valid UTF-8

With this change, no lossy path->string conversion should happen in the
nu-path crate.

* Fmt

* Slight expand_tilde refactor; Add doc comments

* Start nu-path integration tests

* Add tests TODO

* Fix docstring typo

* Fix some doc strings

* Add README for nu-path crate

* Add a couple of canonicalize tests

* Add nu-path integration tests

* Add trim trailing slashes tests

* Update nu-path dependency

* Remove unused import

* Regenerate lockfile
This commit is contained in:
Jakub Žádník
2021-08-28 15:59:09 +03:00
committed by GitHub
parent 1c1c58e802
commit d95375d494
38 changed files with 1320 additions and 653 deletions

View File

@ -104,7 +104,7 @@ fn run_with_stdin(
let process_args = command_args
.iter()
.map(|(arg, _is_literal)| {
let arg = nu_path::expand_tilde_string(Cow::Borrowed(arg));
let arg = nu_path::expand_tilde(arg).to_string_lossy().to_string();
#[cfg(not(windows))]
{

View File

@ -4,7 +4,7 @@ use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_path::canonicalize;
use nu_path::canonicalize_with;
use nu_protocol::{CommandAction, ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
@ -56,7 +56,7 @@ impl WholeStreamCommand for SubCommand {
tag,
}) = load_path
{
let path = canonicalize(shell_manager.path(), load_path).map_err(|_| {
let path = canonicalize_with(load_path, shell_manager.path()).map_err(|_| {
ShellError::labeled_error(
"Cannot load plugins from directory",
"directory not found",

View File

@ -31,12 +31,12 @@ impl WholeStreamCommand for AutoenvTrust {
value: UntaggedValue::Primitive(Primitive::String(ref path)),
tag: _,
}) => {
let mut dir = fs::canonicalize(path)?;
let mut dir = nu_path::canonicalize(path)?;
dir.push(".nu-env");
dir
}
_ => {
let mut dir = fs::canonicalize(std::env::current_dir()?)?;
let mut dir = nu_path::canonicalize(std::env::current_dir()?)?;
dir.push(".nu-env");
dir
}

View File

@ -30,7 +30,7 @@ impl WholeStreamCommand for AutoenvUntrust {
value: UntaggedValue::Primitive(Primitive::String(ref path)),
tag: _,
}) => {
let mut dir = fs::canonicalize(path)?;
let mut dir = nu_path::canonicalize(path)?;
dir.push(".nu-env");
dir
}

View File

@ -6,6 +6,7 @@ use log::debug;
use nu_engine::StringOrBinary;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_path::canonicalize;
use nu_protocol::{CommandAction, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::{AnchorLocation, Span, Tagged};
use std::path::{Path, PathBuf};
@ -193,7 +194,7 @@ pub fn fetch(
// TODO: I don't understand the point of this? Maybe for better error reporting
let mut cwd = PathBuf::from(cwd);
cwd.push(location);
let nice_location = dunce::canonicalize(&cwd).map_err(|e| match e.kind() {
let nice_location = canonicalize(&cwd).map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => ShellError::labeled_error(
format!("Cannot find file {:?}", cwd),
"cannot find file",

View File

@ -2,7 +2,7 @@ use super::{operate, PathSubcommandArguments};
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_path::expand_path;
use nu_path::{canonicalize, expand_path};
use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Span;
use std::{borrow::Cow, path::Path};
@ -95,7 +95,7 @@ impl WholeStreamCommand for PathExpand {
}
fn action(path: &Path, tag: Tag, args: &PathExpandArguments) -> Value {
if let Ok(p) = dunce::canonicalize(path) {
if let Ok(p) = canonicalize(path) {
UntaggedValue::filepath(p).into_value(tag)
} else if args.strict {
Value::error(ShellError::labeled_error(

View File

@ -7,6 +7,7 @@ use std::path::PathBuf;
use nu_engine::WholeStreamCommand;
use nu_engine::{evaluate_baseline_expr, shell::CdArgs};
use nu_errors::ShellError;
use nu_path::{canonicalize, trim_trailing_slash};
use nu_protocol::{
hir::{ExternalArgs, ExternalCommand, SpannedExpression},
Primitive, UntaggedValue,
@ -137,10 +138,10 @@ fn maybe_autocd_dir(cmd: &ExternalCommand, ctx: &mut EvaluationContext) -> Optio
let path_name = if name.ends_with(std::path::is_separator)
|| (cmd.args.is_empty()
&& PathBuf::from(name).is_dir()
&& dunce::canonicalize(name).is_ok()
&& canonicalize(name).is_ok()
&& !ctx.host().lock().is_external_cmd(name))
{
Some(name)
Some(trim_trailing_slash(name))
} else {
None
};