Skip to main content

nu_cmd_lang/core_commands/
export_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 ExportUse;
12
13impl Command for ExportUse {
14    fn name(&self) -> &str {
15        "export use"
16    }
17
18    fn description(&self) -> &str {
19        "Use definitions from a module and export them from this module."
20    }
21
22    fn signature(&self) -> nu_protocol::Signature {
23        Signature::build("export use")
24            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
25            .required("module", SyntaxShape::String, "Module or module file.")
26            .rest(
27                "members",
28                SyntaxShape::Any,
29                "Which members of the module to import.",
30            )
31            .category(Category::Core)
32    }
33
34    fn extra_description(&self) -> &str {
35        "This command is a parser keyword. For details, check:
36  https://www.nushell.sh/book/thinking_in_nu.html"
37    }
38
39    fn command_type(&self) -> CommandType {
40        CommandType::Keyword
41    }
42
43    fn run(
44        &self,
45        engine_state: &EngineState,
46        caller_stack: &mut Stack,
47        call: &Call,
48        input: PipelineData,
49    ) -> Result<PipelineData, ShellError> {
50        if call.get_parser_info(caller_stack, "noop").is_some() {
51            return Ok(PipelineData::empty());
52        }
53        let Some(Expression {
54            expr: Expr::ImportPattern(import_pattern),
55            ..
56        }) = call.get_parser_info(caller_stack, "import_pattern")
57        else {
58            return Err(ShellError::Generic(GenericError::new(
59                "Unexpected import",
60                "import pattern not supported",
61                call.head,
62            )));
63        };
64
65        // Necessary so that we can modify the stack.
66        let import_pattern = import_pattern.clone();
67
68        if let Some(module_id) = import_pattern.head.id {
69            // Add constants
70            for var_id in &import_pattern.constants {
71                let var = engine_state.get_var(*var_id);
72
73                if let Some(constval) = &var.const_val {
74                    caller_stack.add_var(*var_id, constval.clone());
75                } else {
76                    return Err(ShellError::NushellFailedSpanned {
77                        msg: "Missing Constant".to_string(),
78                        label: "constant not added by the parser".to_string(),
79                        span: var.declaration_span,
80                    });
81                }
82            }
83
84            // Evaluate the export-env block if there is one
85            let module = engine_state.get_module(module_id);
86
87            if let Some(block_id) = module.env_block {
88                let block = engine_state.get_block(block_id);
89
90                // See if the module is a file
91                let module_arg_str = String::from_utf8_lossy(
92                    engine_state.get_span_contents(import_pattern.head.span),
93                );
94
95                let maybe_file_path_or_dir = find_in_dirs_env(
96                    &module_arg_str,
97                    engine_state,
98                    caller_stack,
99                    get_dirs_var_from_call(caller_stack, call),
100                )?;
101                // module_arg_str maybe a directory, in this case
102                // find_in_dirs_env returns a directory.
103                let maybe_parent = maybe_file_path_or_dir.as_ref().and_then(|path| {
104                    if path.is_dir() {
105                        Some(path.to_path_buf())
106                    } else {
107                        path.parent().map(|p| p.to_path_buf())
108                    }
109                });
110
111                let mut callee_stack = caller_stack
112                    .gather_captures(engine_state, &block.captures)
113                    .reset_pipes();
114
115                // If so, set the currently evaluated directory (file-relative PWD)
116                if let Some(parent) = maybe_parent {
117                    let file_pwd = Value::string(parent.to_string_lossy(), call.head);
118                    callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
119                }
120
121                if let Some(path) = maybe_file_path_or_dir {
122                    let module_file_path = if path.is_dir() {
123                        // the existence of `mod.nu` is verified in parsing time
124                        // so it's safe to use it here.
125                        Value::string(path.join("mod.nu").to_string_lossy(), call.head)
126                    } else {
127                        Value::string(path.to_string_lossy(), call.head)
128                    };
129                    callee_stack.add_env_var("CURRENT_FILE".to_string(), module_file_path);
130                }
131
132                let eval_block = get_eval_block(engine_state);
133
134                // Run the block (discard the result)
135                let _ = eval_block(engine_state, &mut callee_stack, block, input)?;
136
137                // Merge the block's environment to the current stack
138                redirect_env(engine_state, caller_stack, &callee_stack);
139            }
140        } else {
141            return Err(ShellError::Generic(GenericError::new(
142                format!(
143                    "Could not import from '{}'",
144                    String::from_utf8_lossy(&import_pattern.head.name)
145                ),
146                "module does not exist",
147                import_pattern.head.span,
148            )));
149        }
150
151        Ok(PipelineData::empty())
152    }
153
154    fn examples(&self) -> Vec<Example<'_>> {
155        vec![Example {
156            description: "Re-export a command from another module.",
157            example: r#"module spam { export def foo [] { "foo" } }
158    module eggs { export use spam foo }
159    use eggs foo
160    foo
161            "#,
162            result: Some(Value::test_string("foo")),
163        }]
164    }
165
166    fn search_terms(&self) -> Vec<&str> {
167        vec!["reexport", "import", "module"]
168    }
169}