Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nushell parameter --include-path/-I to be used as expected #12912

Open
fdncred opened this issue May 20, 2024 · 0 comments
Open

Allow nushell parameter --include-path/-I to be used as expected #12912

fdncred opened this issue May 20, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@fdncred
Copy link
Collaborator

fdncred commented May 20, 2024

Related problem

Currently, the -I/--include-path parameter is used for the vscode lsp integration. I may be helpful to allow others to use this functionality outside of the lsp. This is a known issue and I'm sure there is at least one issue opened up on it. My response to those issues was it was never designed to be used that way, but having thought more about it, I'm not sure why it couldn't be used that way.

Describe the solution you'd like

From my research, I've deduced that NU_LIB_DIRS is initially set around line 82 of main.rs. Then, around like 244, if it's specified as a parameter that originally setting is overwritten (this is where the lsp functionality is). Finally, if it's read out of the env.nu, or a specified env.nu, both those values are overwritten.

Seems like it would be appropriate to append these entries to the NU_LIB_DIRS env var and de-dupe them so the running environment of NU_LIB_DIRS could be specified on the command line and used for that session.

Describe alternatives you've considered

Seems like we need one function somewhere that can be reused in the several places that NU_LIB_DIRS is built. I started working on it but lost passion for this feature. I'm not sure this is right but this is the direction I was going.

    if let Some(include_path) = &parsed_nu_cli_args.include_path {
        let span = include_path.span;
        let vals: Vec<_> = include_path
            .item
            .split('\x1e') // \x1e is the record separator character (a character that is unlikely to appear in a path)
            .map(|x| Value::string(x.trim().to_string(), span))
            .collect();

        // Let's check to see if there is already a NU_LIB_DIRS set in the environment
        // If so, we want to append to it and not just overwrite it
        if let Some(pre_existing_libdirs) = engine_state.get_env_var("NU_LIB_DIRS") {
            let mut both: Vec<_> = pre_existing_libdirs
                .as_list()
                .into_iter()
                .flat_map(|it| {
                    it.iter().map(|x| {
                        x.to_path()
                            .expect("internal error: failed to convert lib path")
                    })
                })
                .map(|it| {
                    Value::string(
                        it.into_os_string()
                            .into_string()
                            .expect("internal error: failed to convert OS path"),
                        Span::unknown(),
                    )
                })
                .collect();
            // use pre-existing NU_LIB_DIRS entreis and new ones just specified
            // TODO: Don't add duplicates
            both.extend(vals);
            engine_state.add_env_var("NU_LIB_DIRS".into(), Value::list(both, span));
        } else {
            engine_state.add_env_var("NU_LIB_DIRS".into(), Value::list(vals, span));
        }
    }

Additional context and details

No response

@fdncred fdncred added enhancement New feature or request needs-triage An issue that hasn't had any proper look and removed needs-triage An issue that hasn't had any proper look labels May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant