Skip to content

source usability #5853

@CAD97

Description

@CAD97

Imho, source has an interesting usability problem. And not the deliberate one of source being a parser #include, but a more ingrained one: where should relative paths be relative to?

In interactive mode, source should be cwd relative. This is the current behavior of source; if I do source foo.nu, then this runs foo.nu from the lookup path in the current context.

I'm not sure of the exact lookup path, but a bit of testing shows that it's $nu.history-path | path dirname | path join "scripts" and cwd.

When used as a scripting language, though, source really would like to be source-relative, like #[path = "{}"] modRust, #include "{}"C, import from "{}"JS, etc.

Even more fun is that if I have e.g. source git-completions.nu in my $nu.config-path script and a git-completions.nu in my scripts file, then the scripts directory script gets sourced... unless my startup directory happens to have a git-completions.nu in it as well, in which case that one is used first.

But the real problem is that if I write some larger nushell script that I want to break into multiple files, I'm now restricting what cwds it can be run in, because all source commands are parser-cwd-relative, and all nu commands are block-cwd-relative, and there's no way to get a this-file-relative path.

In bash, you can do something like source "$(dirname "$0")"/helper.sh to source a sibling file. There is no equivalent for nushell that I can find.

So I have a proposal: make source use file-relative rather than cwd-relative lookup (while still falling back to looking in scripts). Imho this brings it more inline with other compiled languages. Interactive mode would just say that the line editor file is "in" cwd to retain the current interactive behavior.

.....and now I'm seriously considering whether we would want #pragma once.....

key value
version 0.64.0
branch
commit_hash d1c719a
build_os windows-x86_64
build_target x86_64-pc-windows-msvc
rust_version rustc 1.61.0 (fe5b13d68 2022-05-18)
rust_channel stable-x86_64-pc-windows-msvc
cargo_version cargo 1.61.0 (a028ae42f 2022-04-29)
pkg_version 0.64.0
build_time 2022-06-15 03:10:01 +00:00
build_rust_channel release
features database, dataframe, default, trash, which, zip
installed_plugins

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions