-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add parse-time constant $NU_CURRENT_FILE
#14305
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
Conversation
d2f709f
to
8cf2f9b
Compare
an early review, i would make that constant upper case, i.e. |
Maybe it would be better to name it |
Implementation seems good, could you add some tests for it? E.g., test that the file is set correctly in source/use/overlay use and also recovers |
What about Edit: note from @devyn regarding this:
|
8cf2f9b
to
56e2261
Compare
$current_file
$NU_CURRENT_FILE
Renamed as suggested. |
…time (#14303) Alternative solution to: - #12195 The other approach: - #14305 # Description Adds ~`path const`~ `path 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. - Useful for any script or module that makes use of non nuscript files. - Removes the need for `$env.CURRENT_FILE` and `$env.FILE_PWD`. - Can be used in modules, sourced files or scripts. # Examples ```nushell # ~/.config/nushell/scripts/foo.nu const paths = { self: (path self), dir: (path self .), sibling: (path self sibling), parent_dir: (path self ..), cousin: (path self ../cousin), } export def main [] { $paths } ``` ```nushell > use foo.nu > foo ╭────────────┬────────────────────────────────────────────╮ │ self │ /home/user/.config/nushell/scripts/foo.nu │ │ dir │ /home/user/.config/nushell/scripts │ │ sibling │ /home/user/.config/nushell/scripts/sibling │ │ parent_dir │ /home/user/.config/nushell │ │ cousin │ /home/user/.config/nushell/cousin │ ╰────────────┴────────────────────────────────────────────╯ ``` Trying to run in a non-const context ```nushell > path self Error: × this command can only run during parse-time ╭─[entry #1:1:1] 1 │ path self · ─────┬──── · ╰── can't run after parse-time ╰──── help: try assigning this command's output to a const variable ``` Trying to run in the REPL i.e. not in a file ```nushell > const foo = path self Error: × Error: nu:🐚:file_not_found │ │ × File not found │ ╭─[entry #3:1:13] │ 1 │ const foo = path self │ · ─────┬──── │ · ╰── Couldn't find current file │ ╰──── │ ╭─[entry #3:1:13] 1 │ const foo = path self · ─────┬──── · ╰── Encountered error during parse-time evaluation ╰──── ``` # Comparison with #14305 ## Pros - Self contained implementation, does not require changes in the parser. - More concise usage, especially with parent directories. --------- Co-authored-by: Darren Schroeder <[email protected]>
script here refers to the entrypoint file when running scripts with nu like `nu script.nu`
56e2261
to
6fdfa70
Compare
I think you can implement it easily by tracking where In the current version, the |
Closing this, as Also bindings that don't follow the existing scoping rules and are scoped only to files (regardless of sourcing/importing) are pretty hard to do cleanly. |
Addresses
$env.FILE_PWD
and$env.CURRENT_FILE
constants, renaming them to be more descriptive, and adding similar constants for any source file #12195The other approach:
path self
command for getting absolute paths to files at parse time #14303Description
Adds
$NU_CURRENT_FILE
, a parse-time constant that is the absolute path of the source file containing it.$env.CURRENT_FILE
and$env.FILE_PWD
.Examples
The following example is written with the assumption that
path expand
can not run during parse-time.Comparison with #14303
Pros
$env.CURRENT_FILE
Cons
null
.