nu 0.112.2

A new type of shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
mod command;
mod command_context;
mod config_files;
mod experimental_options;
mod ide;
mod logger;
mod run;
mod signals;
#[cfg(unix)]
mod terminal;
mod test_bins;

use crate::{
    command::parse_cli_args_from_env,
    config_files::set_config_path,
    logger::{configure, logger},
};
use log::{Level, trace};
use miette::Result;
use nu_cli::gather_parent_env_vars;
use nu_engine::{convert_env_values, exit::cleanup_exit};
use nu_lsp::LanguageServer;
use nu_path::absolute_with;
use nu_protocol::{
    ByteStream, Config, IntoValue, PipelineData, ShellError, Span, Spanned, Type, Value,
    engine::{EngineState, Stack},
    record, report_shell_error,
};
use nu_std::load_standard_library;
use nu_utils::perf;
use run::{run_commands, run_file, run_repl};
use signals::ctrlc_protection;
use std::{
    borrow::Cow,
    path::{PathBuf, absolute},
    str::FromStr,
    sync::Arc,
};

/// Get the directory where the Nushell executable is located.
fn current_exe_directory() -> PathBuf {
    let mut path = std::env::current_exe().expect("current_exe() should succeed");
    path.pop();
    path
}

/// Get the current working directory from the environment.
fn current_dir_from_environment() -> PathBuf {
    let cwd = std::env::current_dir();
    let pwd = std::env::var("PWD");
    match (cwd, pwd) {
        // If current_dir and PWD are the same then use PWD
        // so the path isn't unnecessarily canonicalized on Unix systems.
        (Ok(cwd), Ok(pwd)) => {
            if matches!(same_file::is_same_file(&cwd, &pwd), Ok(true)) {
                pwd.into()
            } else {
                cwd
            }
        }
        // Otherwise prefer current_dir in case it has diverged from PWD
        (Ok(cwd), _) => cwd,
        (_, Ok(pwd)) => pwd.into(),
        _ => {
            if let Some(home) = nu_path::home_dir() {
                home.into_std_path_buf()
            } else {
                current_exe_directory()
            }
        }
    }
}

fn main() -> Result<()> {
    let entire_start_time = nu_utils::time::Instant::now();
    let mut start_time = nu_utils::time::Instant::now();
    miette::set_panic_hook();
    let miette_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |x| {
        crossterm::terminal::disable_raw_mode().expect("unable to disable raw mode");
        miette_hook(x);
    }));

    let engine_state = EngineState::new();

    // Parse commandline args very early and load experimental options to allow loading different
    // commands based on experimental options.
    let parsed = parse_cli_args_from_env().unwrap_or_else(|err| {
        report_shell_error(None, &engine_state, &err.into());
        std::process::exit(1)
    });
    let parsed_nu_cli_args = parsed.nu;
    let script_name = parsed.script_name;
    let args_to_script = parsed.args_to_script;

    experimental_options::load(&engine_state, &parsed_nu_cli_args, !script_name.is_empty());

    let mut engine_state = command_context::add_command_context(engine_state);

    // Provide `version` the features of this nu binary
    let cargo_features = env!("NU_FEATURES").split(",").map(Cow::Borrowed).collect();
    nu_cmd_lang::VERSION_NU_FEATURES
        .set(cargo_features)
        .expect("unable to set VERSION_NU_FEATURES");

    // Get the current working directory from the environment.
    let init_cwd = current_dir_from_environment();

    // Custom additions
    let delta = {
        let mut working_set = nu_protocol::engine::StateWorkingSet::new(&engine_state);
        working_set.add_decl(Box::new(nu_cli::NuHighlight));
        working_set.add_decl(Box::new(nu_cli::Print));
        working_set.render()
    };

    if let Err(err) = engine_state.merge_delta(delta) {
        report_shell_error(None, &engine_state, &err);
    }

    #[cfg(feature = "mcp")]
    let handle_ctrlc = !parsed_nu_cli_args.mcp;
    #[cfg(not(feature = "mcp"))]
    let handle_ctrlc = true;
    if handle_ctrlc {
        ctrlc_protection(&mut engine_state);
    }

    #[cfg(all(feature = "rustls-tls", feature = "network"))]
    nu_command::tls::CRYPTO_PROVIDER.default();

    // Begin: Default NU_LIB_DIRS, NU_PLUGIN_DIRS
    // Set default NU_LIB_DIRS and NU_PLUGIN_DIRS here before the env.nu is processed. If
    // the env.nu file exists, these values will be overwritten, if it does not exist, or
    // there is an error reading it, these values will be used.
    let nushell_config_path: PathBuf = nu_path::nu_config_dir().map(Into::into).unwrap_or_default();
    if let Ok(xdg_config_home) = std::env::var("XDG_CONFIG_HOME")
        && !xdg_config_home.is_empty()
    {
        if nushell_config_path
            != absolute_with(&xdg_config_home, &init_cwd)
                .unwrap_or(PathBuf::from(&xdg_config_home))
                .join("nushell")
        {
            report_shell_error(
                None,
                &engine_state,
                &ShellError::InvalidXdgConfig {
                    xdg: xdg_config_home,
                    default: nushell_config_path.display().to_string(),
                },
            );
        } else if let Some(old_config) = dirs::config_dir()
            .and_then(|p| absolute(p).ok())
            .map(|p| p.join("nushell"))
        {
            let xdg_config_empty = nushell_config_path
                .read_dir()
                .map_or(true, |mut dir| dir.next().is_none());
            let old_config_empty = old_config
                .read_dir()
                .map_or(true, |mut dir| dir.next().is_none());
            if !old_config_empty && xdg_config_empty {
                eprintln!(
                    "WARNING: XDG_CONFIG_HOME has been set but {} is empty.\n",
                    nushell_config_path.display(),
                );
                eprintln!(
                    "Nushell will not move your configuration files from {}",
                    old_config.display()
                );
            }
        }
    }

    let default_nushell_completions_path = if let Some(mut path) = nu_path::data_dir() {
        path.push("nushell");
        path.push("completions");
        path.into()
    } else {
        std::path::PathBuf::new()
    };

    let mut default_nu_lib_dirs_path = nushell_config_path.clone();
    default_nu_lib_dirs_path.push("scripts");

    // Parse include paths from -I flag
    let include_paths = &parsed_nu_cli_args.include_path;

    let mut default_nu_plugin_dirs_path = nushell_config_path;
    default_nu_plugin_dirs_path.push("plugins");
    engine_state.add_env_var("NU_PLUGIN_DIRS".to_string(), Value::test_list(vec![]));
    let mut working_set = nu_protocol::engine::StateWorkingSet::new(&engine_state);
    // No source span — this is a built-in variable defined at startup
    let var_id = working_set.add_variable(
        b"$NU_PLUGIN_DIRS".into(),
        Span::unknown(),
        Type::List(Box::new(Type::String)),
        false,
    );
    working_set.set_variable_const_val(
        var_id,
        Value::test_list(vec![
            Value::test_string(default_nu_plugin_dirs_path.to_string_lossy()),
            Value::test_string(current_exe_directory().to_string_lossy()),
        ]),
    );
    engine_state.merge_delta(working_set.render())?;
    // End: Default NU_LIB_DIRS, NU_PLUGIN_DIRS

    // This is the real secret sauce to having an in-memory sqlite db. You must
    // start a connection to the memory database in main so it will exist for the
    // lifetime of the program. If it's created with how MEMORY_DB is defined
    // you'll be able to access this open connection from anywhere in the program
    // by using the identical connection string.
    #[cfg(feature = "sqlite")]
    let db = nu_command::open_connection_in_memory_custom()?;
    #[cfg(feature = "sqlite")]
    db.last_insert_rowid();

    // keep this condition in sync with the branches at the end
    engine_state.is_interactive = parsed_nu_cli_args.interactive_shell.is_some()
        || (parsed_nu_cli_args.testbin.is_none()
            && parsed_nu_cli_args.commands.is_none()
            && script_name.is_empty()
            && !parsed_nu_cli_args.lsp);

    engine_state.is_login = parsed_nu_cli_args.login_shell.is_some();
    engine_state.history_enabled = parsed_nu_cli_args.no_history.is_none();
    engine_state.is_lsp = parsed_nu_cli_args.lsp;

    let use_color = engine_state
        .get_config()
        .use_ansi_coloring
        .get(&engine_state);

    // Set up logger
    let level_opt = parsed_nu_cli_args
        .log_level
        .as_ref()
        .map(|level| level.item.clone());
    let target_opt = parsed_nu_cli_args
        .log_target
        .as_ref()
        .map(|target| target.item.clone());
    let file_opt = parsed_nu_cli_args.log_file.as_ref().map(|f| f.item.clone());

    // Preliminary validation of combinations that should fail regardless of
    // whether a log level is provided.
    if file_opt.is_some() && target_opt.as_deref() != Some("file") {
        eprintln!("ERROR: --log-file requires --log-target file");
        std::process::exit(1);
    }

    if target_opt.as_deref() == Some("file") && file_opt.is_none() {
        eprintln!("ERROR: --log-target file requires --log-file");
        std::process::exit(1);
    }

    // Enforce that when logging to a file with a custom path, the user must also specify a log level.
    if target_opt.as_deref() == Some("file") && file_opt.is_some() && level_opt.is_none() {
        eprintln!("ERROR: --log-target file with --log-file requires --log-level");
        std::process::exit(1);
    }

    if let Some(level) = level_opt {
        let level = if Level::from_str(&level).is_ok() {
            level
        } else {
            eprintln!(
                "ERROR: log library did not recognize log level '{level}', using default 'info'"
            );
            "info".to_string()
        };
        let target = target_opt.unwrap_or_else(|| "stderr".to_string());

        let make_filters = |filters: &Option<Vec<Spanned<String>>>| {
            filters.as_ref().map(|filters| {
                filters
                    .iter()
                    .map(|filter| filter.item.clone())
                    .collect::<Vec<String>>()
            })
        };
        let filters = logger::Filters {
            include: make_filters(&parsed_nu_cli_args.log_include),
            exclude: make_filters(&parsed_nu_cli_args.log_exclude),
        };

        // logger now expects the closure to return a `Result` so that we can surface configuration errors such as missing `--log-file` when the target is `file`.
        logger(|builder| configure(&level, &target, file_opt.as_deref(), filters, builder))?;
        // info!("start logging {}:{}:{}", file!(), line!(), column!());
        perf!("start logging", start_time, use_color);
    }

    start_time = nu_utils::time::Instant::now();
    set_config_path(
        &mut engine_state,
        init_cwd.as_ref(),
        "config.nu",
        "config-path",
        parsed_nu_cli_args.config_file.as_ref(),
    );

    set_config_path(
        &mut engine_state,
        init_cwd.as_ref(),
        "env.nu",
        "env-path",
        parsed_nu_cli_args.env_file.as_ref(),
    );
    perf!("set_config_path", start_time, use_color);

    #[cfg(unix)]
    {
        start_time = nu_utils::time::Instant::now();
        terminal::acquire(engine_state.is_interactive);
        perf!("acquire_terminal", start_time, use_color);
    }

    start_time = nu_utils::time::Instant::now();
    // No source span — default config is synthesized at startup
    engine_state.add_env_var(
        "config".into(),
        Config::default().into_value(Span::unknown()),
    );
    perf!("$env.config setup", start_time, use_color);

    engine_state.add_env_var(
        "ENV_CONVERSIONS".to_string(),
        Value::test_record(record! {}),
    );

    start_time = nu_utils::time::Instant::now();
    // First, set up env vars as strings only
    gather_parent_env_vars(&mut engine_state, init_cwd.as_ref());
    perf!("gather env vars", start_time, use_color);

    let mut stack = Stack::new();
    start_time = nu_utils::time::Instant::now();
    let config = engine_state.get_config();
    let use_color = config.use_ansi_coloring.get(&engine_state);
    // Translate environment variables from Strings to Values
    if let Err(e) = convert_env_values(&mut engine_state, &mut stack) {
        report_shell_error(None, &engine_state, &e);
    }
    perf!("Convert path to list", start_time, use_color);

    // Set up NU_LIB_DIRS: constant = defaults + env + -I, env = env + -I
    start_time = nu_utils::time::Instant::now();
    {
        /// Parse a string into a list of paths, splitting on the given separators.
        fn parse_path_list(value: &str, separators: &[char]) -> Vec<String> {
            value
                .split(|c| separators.contains(&c))
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect()
        }

        // Get user-set paths from NU_LIB_DIRS env var (after conversion to Values)
        let mut user_lib_dirs: Vec<String> =
            if let Some(val) = engine_state.get_env_var("NU_LIB_DIRS") {
                match val {
                    Value::List { vals, .. } => vals
                        .iter()
                        .filter_map(|v| v.as_str().ok())
                        .map(|s| s.to_string())
                        .collect(),
                    Value::String { val, .. } => {
                        // Split on platform-specific path separators
                        parse_path_list(val, if cfg!(windows) { &[';'] } else { &[':'] })
                    }
                    _ => vec![],
                }
            } else {
                vec![]
            };

        // Append paths from -I flag, which can contain multiple paths separated by :, ;, or \x1e for backwards compatibility
        if let Some(spanned) = include_paths {
            let paths = parse_path_list(&spanned.item, &[':', ';', '\x1e']);
            user_lib_dirs.extend(paths);
        }

        // Combine default paths with user-set paths
        let default_paths = vec![
            default_nu_lib_dirs_path.to_string_lossy().to_string(),
            default_nushell_completions_path
                .to_string_lossy()
                .to_string(),
        ];
        let all_lib_dirs: Vec<String> = user_lib_dirs.into_iter().chain(default_paths).collect();

        // Convert to Value list for setting env vars and constants
        // No source span — these are startup-computed library directory paths
        let all_lib_dir_values: Vec<Value> = all_lib_dirs
            .iter()
            .map(|s| Value::string(s.clone(), Span::unknown()))
            .collect();

        // Set $env.NU_LIB_DIRS to the full list (defaults + user-set)
        // No source span — startup env var setup
        engine_state.add_env_var(
            "NU_LIB_DIRS".to_string(),
            Value::list(all_lib_dir_values.clone(), Span::unknown()),
        );

        // Set $NU_LIB_DIRS as a constant with the same full list
        let mut working_set = nu_protocol::engine::StateWorkingSet::new(&engine_state);
        // No source span — built-in constant defined at startup
        let var_id = working_set.add_variable(
            b"$NU_LIB_DIRS".into(),
            Span::unknown(),
            Type::List(Box::new(Type::String)),
            false, // is_mutable
        );
        working_set
            .set_variable_const_val(var_id, Value::list(all_lib_dir_values, Span::unknown()));
        engine_state.merge_delta(working_set.render())?;
    }
    perf!("$env.NU_LIB_DIRS/$NU_LIB_DIRS setup", start_time, use_color);

    // No source span — startup constant
    engine_state.add_env_var(
        "NU_VERSION".to_string(),
        Value::string(env!("CARGO_PKG_VERSION"), Span::unknown()),
    );

    if parsed_nu_cli_args.no_std_lib.is_none() {
        load_standard_library(&mut engine_state)?;
    }

    // IDE commands
    if let Some(ide_goto_def) = parsed_nu_cli_args.ide_goto_def {
        ide::goto_def(&mut engine_state, &script_name, &ide_goto_def);

        return Ok(());
    } else if let Some(ide_hover) = parsed_nu_cli_args.ide_hover {
        ide::hover(&mut engine_state, &script_name, &ide_hover);

        return Ok(());
    } else if let Some(ide_complete) = parsed_nu_cli_args.ide_complete {
        let cwd = std::env::current_dir().expect("Could not get current working directory.");
        engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy()));

        ide::complete(Arc::new(engine_state), &script_name, &ide_complete);

        return Ok(());
    } else if let Some(max_errors) = parsed_nu_cli_args.ide_check {
        ide::check(&mut engine_state, &script_name, &max_errors);

        return Ok(());
    } else if parsed_nu_cli_args.ide_ast.is_some() {
        ide::ast(&mut engine_state, &script_name);

        return Ok(());
    }

    start_time = nu_utils::time::Instant::now();
    if let Some(testbin) = &parsed_nu_cli_args.testbin {
        let dispatcher = test_bins::new_testbin_dispatcher();
        let test_bin = testbin.item.as_str();
        match dispatcher.get(test_bin) {
            Some(test_bin) => test_bin.run(),
            None => {
                if ["-h", "--help"].contains(&test_bin) {
                    test_bins::show_help(&dispatcher);
                } else {
                    eprintln!("ERROR: Unknown testbin '{test_bin}'");
                    std::process::exit(1);
                }
            }
        }
        std::process::exit(0)
    } else {
        // If we're not running a testbin, set the current working directory to
        // the location of the Nushell executable. This prevents the OS from
        // locking the directory where the user launched Nushell.
        std::env::set_current_dir(current_exe_directory())
            .expect("set_current_dir() should succeed");
    }
    perf!("run test_bins", start_time, use_color);

    start_time = nu_utils::time::Instant::now();
    let input = if let Some(redirect_stdin) = &parsed_nu_cli_args.redirect_stdin {
        trace!("redirecting stdin");
        PipelineData::byte_stream(ByteStream::stdin(redirect_stdin.span)?, None)
    } else {
        trace!("not redirecting stdin");
        PipelineData::empty()
    };
    perf!("redirect stdin", start_time, use_color);

    start_time = nu_utils::time::Instant::now();
    // Set up the $nu constant before evaluating config files (need to have $nu available in them)
    engine_state.generate_nu_constant();
    perf!("create_nu_constant", start_time, use_color);

    #[cfg(feature = "plugin")]
    if let Some(plugins) = &parsed_nu_cli_args.plugins {
        use nu_plugin_engine::{GetPlugin, PluginDeclaration};
        use nu_protocol::{ErrSpan, PluginIdentity, RegisteredPlugin, engine::StateWorkingSet};

        // Load any plugins specified with --plugins
        start_time = nu_utils::time::Instant::now();

        let mut working_set = StateWorkingSet::new(&engine_state);
        for plugin_filename in plugins {
            // Make sure the plugin filenames are absolute
            let filename = absolute_with(&plugin_filename.item, &init_cwd)
                .map_err(|err| {
                    nu_protocol::shell_error::io::IoError::new_internal_with_path(
                        err,
                        "Could not resolve plugin path",
                        PathBuf::from(&plugin_filename.item),
                    )
                })
                .map_err(ShellError::from)?;

            let identity = PluginIdentity::new(&filename, None)
                .err_span(plugin_filename.span)
                .map_err(ShellError::from)?;

            // Create the plugin and add it to the working set
            let plugin = nu_plugin_engine::add_plugin_to_working_set(&mut working_set, &identity)?;

            // Spawn the plugin to get the metadata and signatures
            let interface = plugin.clone().get_plugin(None)?;

            // Set its metadata
            plugin.set_metadata(Some(interface.get_metadata()?));

            // Add the commands from the signature to the working set
            for signature in interface.get_signature()? {
                let decl = PluginDeclaration::new(plugin.clone(), signature);
                working_set.add_decl(Box::new(decl));
            }
        }
        engine_state.merge_delta(working_set.render())?;

        perf!("load plugins specified in --plugins", start_time, use_color)
    }

    start_time = nu_utils::time::Instant::now();

    #[cfg(feature = "mcp")]
    if parsed_nu_cli_args.mcp {
        perf!("mcp starting", start_time, use_color);
        // Mark MCP mode before config evaluation so startup scripts can adapt behavior.
        engine_state.is_mcp = true;
        let mcp_transport_kind = parsed_nu_cli_args
            .mcp_transport
            .as_ref()
            .map(|value| value.item.as_str());
        let is_stdio_transport = !matches!(mcp_transport_kind, Some("http"));

        if parsed_nu_cli_args.no_config_file.is_none() {
            let mut stack = if is_stdio_transport {
                // Keep MCP stdio transport clean by capturing startup stdout in stack output.
                // This is cross-platform and avoid spilling stdout into mcp messages.
                // The `print` command also checks for MCP/LCP before printing to stdout.
                nu_protocol::engine::Stack::new().collect_value()
            } else {
                nu_protocol::engine::Stack::new()
            };
            config_files::setup_config(
                &mut engine_state,
                &mut stack,
                #[cfg(feature = "plugin")]
                parsed_nu_cli_args.plugin_file,
                parsed_nu_cli_args.config_file,
                parsed_nu_cli_args.env_file,
                parsed_nu_cli_args.login_shell.is_some(),
            );
        }
        let transport = match mcp_transport_kind {
            Some("http") => {
                let port = parsed_nu_cli_args.mcp_port.unwrap_or(8080);
                nu_mcp::McpTransport::Http { port }
            }
            _ => nu_mcp::McpTransport::Stdio,
        };
        nu_mcp::initialize_mcp_server(engine_state, transport)?;
        return Ok(());
    }

    if parsed_nu_cli_args.lsp {
        perf!("lsp starting", start_time, use_color);

        if parsed_nu_cli_args.no_config_file.is_none() {
            let mut stack = nu_protocol::engine::Stack::new();
            config_files::setup_config(
                &mut engine_state,
                &mut stack,
                #[cfg(feature = "plugin")]
                parsed_nu_cli_args.plugin_file,
                parsed_nu_cli_args.config_file,
                parsed_nu_cli_args.env_file,
                false,
            );
        }

        LanguageServer::initialize_stdio_connection(engine_state)?.serve_requests()?
    } else if let Some(commands) = parsed_nu_cli_args.commands.clone() {
        run_commands(
            &mut engine_state,
            stack,
            parsed_nu_cli_args,
            use_color,
            &commands,
            input,
            entire_start_time,
        );

        cleanup_exit(0, &engine_state, 0);
    } else if !script_name.is_empty() {
        run_file(
            &mut engine_state,
            stack,
            parsed_nu_cli_args,
            use_color,
            script_name,
            args_to_script,
            input,
        );

        cleanup_exit(0, &engine_state, 0);
    } else {
        // Environment variables that apply only when in REPL
        engine_state.add_env_var("PROMPT_INDICATOR".to_string(), Value::test_string("> "));
        engine_state.add_env_var(
            "PROMPT_INDICATOR_VI_NORMAL".to_string(),
            Value::test_string("> "),
        );
        engine_state.add_env_var(
            "PROMPT_INDICATOR_VI_INSERT".to_string(),
            Value::test_string(": "),
        );
        engine_state.add_env_var(
            "PROMPT_MULTILINE_INDICATOR".to_string(),
            Value::test_string("::: "),
        );
        engine_state.add_env_var(
            "TRANSIENT_PROMPT_MULTILINE_INDICATOR".to_string(),
            Value::test_string(""),
        );
        engine_state.add_env_var(
            "TRANSIENT_PROMPT_COMMAND_RIGHT".to_string(),
            Value::test_string(""),
        );
        let mut shlvl = engine_state
            .get_env_var("SHLVL")
            .map(|x| x.as_str().unwrap_or("0").parse::<i64>().unwrap_or(0))
            .unwrap_or(0);
        shlvl += 1;
        // No source span — startup env var
        engine_state.add_env_var("SHLVL".to_string(), Value::int(shlvl, Span::unknown()));

        run_repl(
            &mut engine_state,
            stack,
            parsed_nu_cli_args,
            entire_start_time,
        )?;

        cleanup_exit(0, &engine_state, 0);
    }

    Ok(())
}