Repository: nushell/nushell
File: src/main.rs (Rust)

Code before:
        let var_id = working_set.add_variable(
            b"$NU_LIB_DIRS".into(),
            span,
            Type::List(Box::new(Type::String)),
            false,
        );
        working_set.set_variable_const_val(var_id, Value::list(vals, span));
        engine_state.merge_delta(working_set.render())?;
    }
    perf!("NU_LIB_DIRS setup", start_time, use_color);

    start_time = std::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);

    engine_state.add_env_var(
        "NU_VERSION".to_string(),
        Value::string(env!("CARGO_PKG_VERSION"), Span::unknown()),
    );

    // Add SHLVL if interactive
    if engine_state.is_interactive {
        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;
        engine_state.add_env_var("SHLVL".to_string(), Value::int(shlvl, 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(());

Review comment (suggestion):
Not sure if this `.as_int` works.

`Value::as_int` won't convert a string in an environment variable from the outside environment if that is stored as a `Value::String`. Due to the `unwrap_or` any errors will be coerced to a new `i64` which will then be stored as a `Value::Int` internally. I am not as up to speed on env conversion behavior as @NotTheDr01ds if we have semantics that will try to coerce to int for external env vars on startup. Otherwise this has to return to the old logic to still pickup a `$SHLVL` from an outside shell.

Code after:
        let var_id = working_set.add_variable(
            b"$NU_LIB_DIRS".into(),
            span,
            Type::List(Box::new(Type::String)),
            false,
        );
        working_set.set_variable_const_val(var_id, Value::list(vals, span));
        engine_state.merge_delta(working_set.render())?;
    }
    perf!("NU_LIB_DIRS setup", start_time, use_color);

    start_time = std::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);

    engine_state.add_env_var(
        "NU_VERSION".to_string(),
        Value::string(env!("CARGO_PKG_VERSION"), Span::unknown()),
    );

    // Add SHLVL if interactive
    if engine_state.is_interactive {
        let mut shlvl = engine_state
            .get_env_var("SHLVL")
            .map(|x| x.as_int().unwrap_or(0))
            .unwrap_or(0);
        shlvl += 1;
        engine_state.add_env_var("SHLVL".to_string(), Value::int(shlvl, 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(());