Skip to main content

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