You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make $PSScriptRoot and $PSCommandPath also reflect the true filesystem locations for scripts invoked via symlinks #11054
Symlinks (symbolic links) are a staple of the Unix world and are becoming increasingly common on Windows too.
For scripts invoked via symlinks, the "reflective" variables that contain the running script's location (directory, $PSScriptRoot) and $PSCommandPath currently reflect only the link's information, not (also) the underlying file's information.
(Reporting the link's information is consistent with the behavior of POSIX-like shells such as Bash, via $0 and $BASH_SOURCE).
While you can manually investigate the paths contained in these variables to find the true script location and file name, it would be nice to have this information automatically available.
As an aside: Even manual investigation is currently nontrivial - see #10640
One option that doesn't require introducing new automatic variables (which is always problematic) is to add a NoteProperty member name, say, .Target to the $PSScriptRoot and $PSCommandPath variables that reflects the underlying script file's true directory and full filename, respectively.
That way, you could use $PSCommandPath.Target to get the running script's true full path, for instance.
While somewhat obscure, this follows the pattern of the NoteProperty members the $PROFILE automatic variable is decorated with, which reflect the paths of the various profile types and scopes.
Documenting these non-obvious properties clearly should be sufficient, however.
No, but the yet-to-be-implemented Convert-Path -Canonical would help - see #10640
It seems there is no single place where we set value to an automatic variable.
I haven't looked, but are you saying that these specific automatic variables are filled in different places based on whether the code unit is a script or a module?
Also we could change type of the variable from string to FileInfo but it looks as a breaking change.
While that would be nice, I fear it is too problematic a change.
Actually, the more I think about this: I think just using Convert-Path -Canonical on demand should be enough for this use case, so I'll close this issue.
are you saying that these specific automatic variables are filled in different places based on whether the code unit is a script or a module?
I mean that we haven't methods like SetPSCommandPathVariable() and SetPSScriptRootVariable(). (It is true for all automatic variables). We use one universal method SetAutomaticVariable().
Really we set these variables in 2 places - for module and for script block.
Activity
iSazonov commentedon Nov 14, 2019
Can Resolve-Path help in the scenario?
It seems there is no single place where we set value to an automatic variable. So it is not easy to add a property.
Also we could change type of the variable from string to FileInfo but it looks as a breaking change.
mklement0 commentedon Nov 14, 2019
No, but the yet-to-be-implemented
Convert-Path -Canonicalwould help - see #10640I haven't looked, but are you saying that these specific automatic variables are filled in different places based on whether the code unit is a script or a module?
While that would be nice, I fear it is too problematic a change.
mklement0 commentedon Nov 14, 2019
Actually, the more I think about this: I think just using
Convert-Path -Canonicalon demand should be enough for this use case, so I'll close this issue.iSazonov commentedon Nov 14, 2019
I mean that we haven't methods like SetPSCommandPathVariable() and SetPSScriptRootVariable(). (It is true for all automatic variables). We use one universal method SetAutomaticVariable().
Really we set these variables in 2 places - for module and for script block.