nu_cmd_lang/core_commands/
use_.rs

1use nu_engine::{
2    command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block, redirect_env,
3};
4use nu_protocol::{
5    ast::{Expr, Expression},
6    engine::CommandType,
7};
8
9#[derive(Clone)]
10pub struct Use;
11
12impl Command for Use {
13    fn name(&self) -> &str {
14        "use"
15    }
16
17    fn description(&self) -> &str {
18        "Use definitions from a module, making them available in your shell."
19    }
20
21    fn signature(&self) -> nu_protocol::Signature {
22        Signature::build("use")
23            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
24            .allow_variants_without_examples(true)
25            .required(
26                "module",
27                SyntaxShape::OneOf(vec![SyntaxShape::String, SyntaxShape::Nothing]),
28                "Module or module file (`null` for no-op).",
29            )
30            .rest(
31                "members",
32                SyntaxShape::Any,
33                "Which members of the module to import.",
34            )
35            .category(Category::Core)
36    }
37
38    fn search_terms(&self) -> Vec<&str> {
39        vec!["module", "import", "include", "scope"]
40    }
41
42    fn extra_description(&self) -> &str {
43        r#"See `help std` for the standard library module.
44See `help modules` to list all available modules.
45
46This command is a parser keyword. For details, check:
47  https://www.nushell.sh/book/thinking_in_nu.html"#
48    }
49
50    fn command_type(&self) -> CommandType {
51        CommandType::Keyword
52    }
53
54    fn run(
55        &self,
56        engine_state: &EngineState,
57        caller_stack: &mut Stack,
58        call: &Call,
59        input: PipelineData,
60    ) -> Result<PipelineData, ShellError> {
61        if call.get_parser_info(caller_stack, "noop").is_some() {
62            return Ok(PipelineData::empty());
63        }
64        let Some(Expression {
65            expr: Expr::ImportPattern(import_pattern),
66            ..
67        }) = call.get_parser_info(caller_stack, "import_pattern")
68        else {
69            return Err(ShellError::GenericError {
70                error: "Unexpected import".into(),
71                msg: "import pattern not supported".into(),
72                span: Some(call.head),
73                help: None,
74                inner: vec![],
75            });
76        };
77
78        // Necessary so that we can modify the stack.
79        let import_pattern = import_pattern.clone();
80
81        if let Some(module_id) = import_pattern.head.id {
82            // Add constants
83            for var_id in &import_pattern.constants {
84                let var = engine_state.get_var(*var_id);
85
86                if let Some(constval) = &var.const_val {
87                    caller_stack.add_var(*var_id, constval.clone());
88                } else {
89                    return Err(ShellError::NushellFailedSpanned {
90                        msg: "Missing Constant".to_string(),
91                        label: "constant not added by the parser".to_string(),
92                        span: var.declaration_span,
93                    });
94                }
95            }
96
97            // Evaluate the export-env block if there is one
98            let module = engine_state.get_module(module_id);
99
100            if let Some(block_id) = module.env_block {
101                let block = engine_state.get_block(block_id);
102
103                // See if the module is a file
104                let module_arg_str = String::from_utf8_lossy(
105                    engine_state.get_span_contents(import_pattern.head.span),
106                );
107
108                let maybe_file_path_or_dir = find_in_dirs_env(
109                    &module_arg_str,
110                    engine_state,
111                    caller_stack,
112                    get_dirs_var_from_call(caller_stack, call),
113                )?;
114                // module_arg_str maybe a directory, in this case
115                // find_in_dirs_env returns a directory.
116                let maybe_parent = maybe_file_path_or_dir.as_ref().and_then(|path| {
117                    if path.is_dir() {
118                        Some(path.to_path_buf())
119                    } else {
120                        path.parent().map(|p| p.to_path_buf())
121                    }
122                });
123
124                let mut callee_stack = caller_stack
125                    .gather_captures(engine_state, &block.captures)
126                    .reset_pipes();
127
128                // If so, set the currently evaluated directory (file-relative PWD)
129                if let Some(parent) = maybe_parent {
130                    let file_pwd = Value::string(parent.to_string_lossy(), call.head);
131                    callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
132                }
133
134                if let Some(path) = maybe_file_path_or_dir {
135                    let module_file_path = if path.is_dir() {
136                        // the existence of `mod.nu` is verified in parsing time
137                        // so it's safe to use it here.
138                        Value::string(path.join("mod.nu").to_string_lossy(), call.head)
139                    } else {
140                        Value::string(path.to_string_lossy(), call.head)
141                    };
142                    callee_stack.add_env_var("CURRENT_FILE".to_string(), module_file_path);
143                }
144
145                let eval_block = get_eval_block(engine_state);
146
147                // Run the block (discard the result)
148                let _ = eval_block(engine_state, &mut callee_stack, block, input)?;
149
150                // Merge the block's environment to the current stack
151                redirect_env(engine_state, caller_stack, &callee_stack);
152            }
153        } else {
154            return Err(ShellError::GenericError {
155                error: format!(
156                    "Could not import from '{}'",
157                    String::from_utf8_lossy(&import_pattern.head.name)
158                ),
159                msg: "module does not exist".to_string(),
160                span: Some(import_pattern.head.span),
161                help: None,
162                inner: vec![],
163            });
164        }
165
166        Ok(PipelineData::empty())
167    }
168
169    fn examples(&self) -> Vec<Example<'_>> {
170        vec![
171            Example {
172                description: "Define a custom command in a module and call it",
173                example: r#"module spam { export def foo [] { "foo" } }; use spam foo; foo"#,
174                result: Some(Value::test_string("foo")),
175            },
176            Example {
177                description: "Define a custom command that participates in the environment in a module and call it",
178                example: r#"module foo { export def --env bar [] { $env.FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
179                result: Some(Value::test_string("BAZ")),
180            },
181            Example {
182                description: "Use a plain module name to import its definitions qualified by the module name",
183                example: r#"module spam { export def foo [] { "foo" }; export def bar [] { "bar" } }; use spam; (spam foo) + (spam bar)"#,
184                result: Some(Value::test_string("foobar")),
185            },
186            Example {
187                description: "Specify * to use all definitions in a module",
188                example: r#"module spam { export def foo [] { "foo" }; export def bar [] { "bar" } }; use spam *; (foo) + (bar)"#,
189                result: Some(Value::test_string("foobar")),
190            },
191            Example {
192                description: "To use commands with spaces, like subcommands, surround them with quotes",
193                example: r#"module spam { export def 'foo bar' [] { "baz" } }; use spam 'foo bar'; foo bar"#,
194                result: Some(Value::test_string("baz")),
195            },
196            Example {
197                description: "To use multiple definitions from a module, wrap them in a list",
198                example: r#"module spam { export def foo [] { "foo" }; export def 'foo bar' [] { "baz" } }; use spam ['foo', 'foo bar']; (foo) + (foo bar)"#,
199                result: Some(Value::test_string("foobaz")),
200            },
201        ]
202    }
203}
204
205#[cfg(test)]
206mod test {
207    #[test]
208    fn test_examples() {
209        use super::Use;
210        use crate::test_examples;
211        test_examples(Use {})
212    }
213}