-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Making $env.FILE_PWD and $env.CURRENT_FILE constants, renaming them to be more descriptive, and adding similar constants for any source file #12195
Description
Related problem
No response
Describe the solution you'd like
Hi,
I'd like to propose a few changes to $env.FILE_PWD and $env.CURRENT_FILE, and the addition of a similar feature to access the path of any source file (not only the path of the main script file or the directory that contains it).
Firstly, I'd like to propose to make variables like FILE_PWD and CURRENT_FILE constants, so they are usable at parse-time (they don't change during script execution, right? So why not make this information available at parse-time?).
Secondly, I'd like to suggest changing the names to something more descriptive and less confusing.
I don't think, FILE_PWD makes sense, to be honest (as mentioned in #7692, which IMO was closed without a good reason).
'PWD' represents the current working directory on UNIX-like systems (I always thought it stands for "Process Working Directory", but maybe that name was derived from the pwd command, which stands for "Print Working Directory").
As far as I can see, FILE_PWD contains the path to the directory that contains the main script file – and I can't come up with a reason why such a variable should contain "PWD". The closest I came was "Parent Working Directory" – but I think "working directory" doesn't fit well.
As afar as I know, the directory of a file in often called 'base directory', so FILE_BASEDIR seems more appropriate to me.
Even better would be SCRIPT_BASEDIR because "FILE" is too unspecific and not descriptive enough ("MAIN_SCRIPT_FILE_BASEDIR" would be ideal, but is probably too long).
Similarly, CURRENT_FILE seems too generic/unspecific. Something like SCRIPT_FILE (or MAIN_SCRIPT_FILE) seems more appropriate to me.
This also would be less confusing.
When I first saw CURRENT_FILE, I thought it points to the path of the (current) source file it is used in (similar to Rust's file macro), however it contains the path to the main script file.
Thirdly, I believe that something like Rust's file macro, that points to the path of the source file that it is used in would be useful, as well. Logging, or reading files relative to the current source file, would be two obvious examples.
So it would be great if there were constants like SRC_FILE and SRC_FILE_BASEDIR (or CURRENT_SRC_FILE and CURRENT_SRC_FILE_BASEDIR, which is more descriptive, but maybe a bit too long).
Besides the above, $nu – which contains already similar data and seems to be already a constant – would be a more appropriate location for such data.
So I'd like to propose to deprecate $env.FILE_PWD and $env.CURRENT_FILE and to add the following fields to $nu:
- script-file (maybe
script-pathwould be more consistent, compared to existing fields) - script-basedir
- src-file
- src-file-basedir
(An issue might be, that src-file and src-file-basedir would change for every source file, and wouldn't be constant.)
As far as I can see, $env.FILE_PWD and $env.CURRENT_FILE aren't documented anywhere, so I wouldn't consider the changes I propose a significant breaking change.
From what I remember, the above would fix/solve a couple of open issues.
Describe alternatives you've considered
No response
Additional context and details
No response