nu_command/misc/
source.rs

1use nu_engine::{command_prelude::*, get_eval_block_with_early_return};
2use nu_path::canonicalize_with;
3use nu_protocol::{BlockId, engine::CommandType, shell_error::io::IoError};
4
5/// Source a file for environment variables.
6#[derive(Clone)]
7pub struct Source;
8
9impl Command for Source {
10    fn name(&self) -> &str {
11        "source"
12    }
13
14    fn signature(&self) -> Signature {
15        Signature::build("source")
16            .input_output_types(vec![(Type::Any, Type::Any)])
17            .required(
18                "filename",
19                SyntaxShape::OneOf(vec![SyntaxShape::Filepath, SyntaxShape::Nothing]),
20                "The filepath to the script file to source (`null` for no-op).",
21            )
22            .category(Category::Core)
23    }
24
25    fn description(&self) -> &str {
26        "Runs a script file in the current context."
27    }
28
29    fn extra_description(&self) -> &str {
30        r#"This command is a parser keyword. For details, check:
31  https://www.nushell.sh/book/thinking_in_nu.html"#
32    }
33
34    fn command_type(&self) -> CommandType {
35        CommandType::Keyword
36    }
37
38    fn run(
39        &self,
40        engine_state: &EngineState,
41        stack: &mut Stack,
42        call: &Call,
43        input: PipelineData,
44    ) -> Result<PipelineData, ShellError> {
45        if call.get_parser_info(stack, "noop").is_some() {
46            return Ok(PipelineData::empty());
47        }
48        // Note: two hidden positionals are used here that are injected by the parser:
49        // 1. The block_id that corresponded to the 0th position
50        // 2. The block_id_name that corresponded to the file name at the 0th position
51        let block_id: i64 = call.req_parser_info(engine_state, stack, "block_id")?;
52        let block_id_name: String = call.req_parser_info(engine_state, stack, "block_id_name")?;
53        let block_id = BlockId::new(block_id as usize);
54        let block = engine_state.get_block(block_id).clone();
55        let cwd = engine_state.cwd_as_string(Some(stack))?;
56        let pb = std::path::PathBuf::from(block_id_name);
57        let parent = pb.parent().unwrap_or(std::path::Path::new(""));
58        let file_path = canonicalize_with(pb.as_path(), cwd)
59            .map_err(|err| IoError::new(err.not_found_as(NotFound::File), call.head, pb.clone()))?;
60
61        // Note: We intentionally left out PROCESS_PATH since it's supposed to
62        // to work like argv[0] in C, which is the name of the program being executed.
63        // Since we're not executing a program, we don't need to set it.
64
65        // Save the old env vars so we can restore them after the script has ran
66        let old_file_pwd = stack.get_env_var(engine_state, "FILE_PWD").cloned();
67        let old_current_file = stack.get_env_var(engine_state, "CURRENT_FILE").cloned();
68
69        // Add env vars so they are available to the script
70        stack.add_env_var(
71            "FILE_PWD".to_string(),
72            Value::string(parent.to_string_lossy(), Span::unknown()),
73        );
74        stack.add_env_var(
75            "CURRENT_FILE".to_string(),
76            Value::string(file_path.to_string_lossy(), Span::unknown()),
77        );
78
79        let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
80        let return_result = eval_block_with_early_return(engine_state, stack, &block, input);
81
82        // After the script has ran, restore the old values unless they didn't exist.
83        // If they didn't exist prior, remove the env vars
84        if let Some(old_file_pwd) = old_file_pwd {
85            stack.add_env_var("FILE_PWD".to_string(), old_file_pwd.clone());
86        } else {
87            stack.remove_env_var(engine_state, "FILE_PWD");
88        }
89        if let Some(old_current_file) = old_current_file {
90            stack.add_env_var("CURRENT_FILE".to_string(), old_current_file.clone());
91        } else {
92            stack.remove_env_var(engine_state, "CURRENT_FILE");
93        }
94
95        return_result
96    }
97
98    fn examples(&self) -> Vec<Example> {
99        vec![
100            Example {
101                description: "Runs foo.nu in the current context",
102                example: r#"source foo.nu"#,
103                result: None,
104            },
105            Example {
106                description: "Runs foo.nu in current context and call the command defined, suppose foo.nu has content: `def say-hi [] { echo 'Hi!' }`",
107                example: r#"source ./foo.nu; say-hi"#,
108                result: None,
109            },
110            Example {
111                description: "Sourcing `null` is a no-op.",
112                example: r#"source null"#,
113                result: None,
114            },
115            Example {
116                description: "Source can be used with const variables.",
117                example: r#"const file = if $nu.is-interactive { "interactive.nu" } else { null }; source $file"#,
118                result: None,
119            },
120        ]
121    }
122}