nu_command/misc/
source.rs

1use nu_engine::{command_prelude::*, get_eval_block_with_early_return};
2use nu_path::canonicalize_with;
3use nu_protocol::{engine::CommandType, shell_error::io::IoError, BlockId};
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).map_err(|err| {
59            IoError::new(
60                err.kind().not_found_as(NotFound::File),
61                call.head,
62                pb.clone(),
63            )
64        })?;
65
66        // Note: We intentionally left out PROCESS_PATH since it's supposed to
67        // to work like argv[0] in C, which is the name of the program being executed.
68        // Since we're not executing a program, we don't need to set it.
69
70        // Save the old env vars so we can restore them after the script has ran
71        let old_file_pwd = stack.get_env_var(engine_state, "FILE_PWD").cloned();
72        let old_current_file = stack.get_env_var(engine_state, "CURRENT_FILE").cloned();
73
74        // Add env vars so they are available to the script
75        stack.add_env_var(
76            "FILE_PWD".to_string(),
77            Value::string(parent.to_string_lossy(), Span::unknown()),
78        );
79        stack.add_env_var(
80            "CURRENT_FILE".to_string(),
81            Value::string(file_path.to_string_lossy(), Span::unknown()),
82        );
83
84        let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
85        let return_result = eval_block_with_early_return(engine_state, stack, &block, input);
86
87        // After the script has ran, restore the old values unless they didn't exist.
88        // If they didn't exist prior, remove the env vars
89        if let Some(old_file_pwd) = old_file_pwd {
90            stack.add_env_var("FILE_PWD".to_string(), old_file_pwd.clone());
91        } else {
92            stack.remove_env_var(engine_state, "FILE_PWD");
93        }
94        if let Some(old_current_file) = old_current_file {
95            stack.add_env_var("CURRENT_FILE".to_string(), old_current_file.clone());
96        } else {
97            stack.remove_env_var(engine_state, "CURRENT_FILE");
98        }
99
100        return_result
101    }
102
103    fn examples(&self) -> Vec<Example> {
104        vec![
105            Example {
106                description: "Runs foo.nu in the current context",
107                example: r#"source foo.nu"#,
108                result: None,
109            },
110            Example {
111                description: "Runs foo.nu in current context and call the command defined, suppose foo.nu has content: `def say-hi [] { echo 'Hi!' }`",
112                example: r#"source ./foo.nu; say-hi"#,
113                result: None,
114            },
115            Example {
116                description: "Sourcing `null` is a no-op.",
117                example: r#"source null"#,
118                result: None,
119            },
120            Example {
121                description: "Source can be used with const variables.",
122                example: r#"const file = if $nu.is-interactive { "interactive.nu" } else { null }; source $file"#,
123                result: None,
124            }
125        ]
126    }
127}