Add path self command for getting absolute paths to files at parse time#14303
Add path self command for getting absolute paths to files at parse time#14303fdncred merged 7 commits intonushell:mainfrom
path self command for getting absolute paths to files at parse time#14303Conversation
|
an early review: following what's in #12195 sounds better and simpler to me than having a new command 😋 |
|
Yeah, I'm also not sure I like this just because the name is not very self-explanatory. If you're reading this and you don't know about it already you're probably not going to know what it does. Whereas |
|
I would suggest renaming If we reuse the syntax of ~> path self --help
Get a const of the current files path.
Optionally, append an additional path to the current files directory.
Usage:
> path self ...(append)
Flags:
-h, --help: Display the help message for this command
Parameters:
...append <string>: Path to append to the current files directory.
Input/output types:
╭───┬──────────────┬──────────────╮
│ # │ input │ output │
├───┼──────────────┼──────────────┤
│ 0 │ nothing │ string │
╰───┴──────────────┴──────────────╯
Examples:
Get the path of the current file
> path self
/home/scripts/foo.nu
Get the directory of the current file
> path self .
/home/scripts
Get the parent directory of the current file
> path self ..
/home
Append a path to the current files directory
> path self sibling
/home/scripts/sibling
From the current file go up one directory and append a path
> path self .. cousin
/home/../cousin |
|
|
I don't remember there being any nushell commands that are limited to a const context. |
It simplifies things immensely, and let's It can't run after parse-time, because it wouldn't be running within the context of the file. |
Not sure how to have examples run in parse-time.
path const command for getting absolute paths to files at parse timepath self command for getting absolute paths to files at parse time
|
Renamed as suggested. |
|
@Bahex I hope you don't mind. I added a couple tweaks. The TODO wasn't needed because that's checked with the input_output_types and just tweaks your examples and help. |
|
Thanks for working on this! |
|
@Bahex I'm getting the error below when running When I put this in my env.nu or config.nu const const_env = path self
print $"Hello, from ($const_env)!"Am I just using it wrong or is this an edge case that is a new bug? Or maybe something needs to change in Lines 75 to 138 in 9411458 |
# Description fixes [this](#14303 (comment)) where lsp and ide integration would produce the following error --- ```sh nu --ide-check 100 "/path/to/env.nu" ``` with ```nu const const_env = path self ``` would lead to ``` Error: nu::shell::file_not_found × File not found ╭─[/path/to/env.nu:1:19] 1 │ const const_env = path self · ────┬──── · ╰── Couldn't find current file ╰──── ``` # Tests + Formatting - :green_circle: `cargo fmt --all` - :green_circle: `cargo clippy --workspace`
|
Hey guys, really love this command, but just have a quick question about the semantics: I see that the returned path is expanded, ie if a nushell file is a symlink, |
…self` Nushell 0.101+) See: nushell/nushell#14303
|
This is super useful for nu-test as I needed it where $env.CURRENT_FILE wasn't defined (in a |
|
@suimong It's mostly because of how nushell resolves the paths to modules/files. For example A possible workaround for your use case would be to have short files that set some constants and then source the file that contains the actual logic. const ENTRY_POINT = (path self .)
source logic.nu# logic.nu
# rather than using `path self` directly, use the pre-set constant variable
do-something $ENTRY_POINT |
|
I see. @Bahex thanks for the explanation and the workaround. Still, having |
|
path find? |
Sorry I don't follow. We don't seem to have this command? Or is it a new command proposed in the spirit of path self? |
|
You mention it above and I'm wondering the same thing, what is path find? |
|
Ah, stupid me. I mean |

Alternative solution to:
$env.FILE_PWDand$env.CURRENT_FILEconstants, renaming them to be more descriptive, and adding similar constants for any source file #12195The other approach:
$NU_CURRENT_FILE#14305Description
Adds
path constpath self, a parse-time only command for getting the absolute path of the source file containing it, or any file relative to the source file.$env.CURRENT_FILEand$env.FILE_PWD.Examples
Trying to run in a non-const context
Trying to run in the REPL i.e. not in a file
Comparison with #14305
Pros