mirror of
https://github.com/nushell/nushell
synced 2025-01-15 14:44:14 +00:00
Add canonicalization to source & use paths (#421)
Also added file path print to FileNotFound error
This commit is contained in:
parent
405a4e58c7
commit
ee45755ea9
3 changed files with 102 additions and 81 deletions
|
@ -187,7 +187,7 @@ pub enum ParseError {
|
|||
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
|
||||
ExportNotFound(#[label = "could not find imports"] Span),
|
||||
|
||||
#[error("File not found")]
|
||||
#[error("File not found: {0}")]
|
||||
#[diagnostic(code(nu::parser::file_not_found), url(docsrs))]
|
||||
FileNotFound(String),
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use nu_path::canonicalize;
|
||||
use nu_protocol::{
|
||||
ast::{
|
||||
Block, Call, Expr, Expression, ImportPattern, ImportPatternHead, ImportPatternMember,
|
||||
|
@ -7,7 +8,6 @@ use nu_protocol::{
|
|||
span, Exportable, Overlay, Span, SyntaxShape, Type, CONFIG_VARIABLE_ID,
|
||||
};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::{
|
||||
lex, lite_parse,
|
||||
|
@ -679,7 +679,7 @@ pub fn parse_use(
|
|||
// TODO: Do not close over when loading module from file
|
||||
// It could be a file
|
||||
if let Ok(module_filename) = String::from_utf8(import_pattern.head.name) {
|
||||
let module_path = Path::new(&module_filename);
|
||||
if let Ok(module_path) = canonicalize(&module_filename) {
|
||||
let module_name = if let Some(stem) = module_path.file_stem() {
|
||||
stem.to_string_lossy().to_string()
|
||||
} else {
|
||||
|
@ -718,6 +718,10 @@ pub fn parse_use(
|
|||
Some(ParseError::ModuleNotFound(spans[1])),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
error = error.or(Some(ParseError::FileNotFound(module_filename)));
|
||||
(ImportPattern::new(), Overlay::new())
|
||||
}
|
||||
} else {
|
||||
return (
|
||||
garbage_statement(spans),
|
||||
|
@ -990,6 +994,7 @@ pub fn parse_source(
|
|||
working_set: &mut StateWorkingSet,
|
||||
spans: &[Span],
|
||||
) -> (Statement, Option<ParseError>) {
|
||||
let mut error = None;
|
||||
let name = working_set.get_span_contents(spans[0]);
|
||||
|
||||
if name == b"source" {
|
||||
|
@ -998,17 +1003,14 @@ pub fn parse_source(
|
|||
// Some of the others (`parse_let`) use it, some of them (`parse_hide`) don't.
|
||||
let (call, call_span, err) =
|
||||
parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
|
||||
error = error.or(err);
|
||||
|
||||
// Command and one file name
|
||||
if spans.len() >= 2 {
|
||||
let name_expr = working_set.get_span_contents(spans[1]);
|
||||
if let Ok(filename) = String::from_utf8(name_expr.to_vec()) {
|
||||
let source_file = Path::new(&filename);
|
||||
|
||||
let path = source_file;
|
||||
let contents = std::fs::read(path);
|
||||
|
||||
if let Ok(contents) = contents {
|
||||
if let Ok(path) = canonicalize(&filename) {
|
||||
if let Ok(contents) = std::fs::read(&path) {
|
||||
// This will load the defs from the file into the
|
||||
// working set, if it was a successful parse.
|
||||
let (block, err) = parse(
|
||||
|
@ -1056,6 +1058,9 @@ pub fn parse_source(
|
|||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error = error.or(Some(ParseError::FileNotFound(filename)));
|
||||
}
|
||||
} else {
|
||||
return (
|
||||
garbage_statement(spans),
|
||||
|
@ -1070,7 +1075,7 @@ pub fn parse_source(
|
|||
ty: Type::Unknown,
|
||||
custom_completion: None,
|
||||
}])),
|
||||
err,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1090,7 +1095,6 @@ pub fn parse_register(
|
|||
) -> (Statement, Option<ParseError>) {
|
||||
use std::{path::PathBuf, str::FromStr};
|
||||
|
||||
use nu_path::canonicalize;
|
||||
use nu_plugin::plugin::{get_signature, PluginDeclaration};
|
||||
use nu_protocol::Signature;
|
||||
|
||||
|
|
|
@ -24,6 +24,17 @@ pub struct ImportPattern {
|
|||
}
|
||||
|
||||
impl ImportPattern {
|
||||
pub fn new() -> Self {
|
||||
ImportPattern {
|
||||
head: ImportPatternHead {
|
||||
name: vec![],
|
||||
span: Span::unknown(),
|
||||
},
|
||||
members: vec![],
|
||||
hidden: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn span(&self) -> Span {
|
||||
let mut spans = vec![self.head.span];
|
||||
|
||||
|
@ -50,3 +61,9 @@ impl ImportPattern {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ImportPattern {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue