Closed nightroman closed 1 year ago
Also, the code Path.GetDirectoryName(AppContext.BaseDirectory)
looks a little "not safe".
Here is for example what I get by [AppContext]::BaseDirectory
in my pwsh and powershell:
C:\Bin\pwsh\
C:\windows\System32\WindowsPowerShell\v1.0\
Note the trailing backslashes.
Path.GetDirectoryName
returns:
C:\Bin\pwsh
C:\windows\System32\WindowsPowerShell\v1.0
Now imaging AppContext.BaseDirectory
were returning the same paths but without trailing backslashes.
Then we would get:
> [IO.Path]::GetDirectoryName('C:\Bin\pwsh')
C:\Bin
> [IO.Path]::GetDirectoryName('C:\windows\System32\WindowsPowerShell\v1.0')
C:\windows\System32\WindowsPowerShell
I.e. we get different directories. This all looks a little strange.
What was the original intent? What directory should be obtained by Engine.GetLocalPath()
?
@nightroman Thanks for reporting the issue.
The paths that you've provided look correct.
_LocalPath
is used for a specific case around CLI hosting, where the assembly is used directly instead of from within a PowerShell host. It's not required in the case you have outlined, so it probably just need some additional handling and test cases.
The paths that you've provided look correct.
By chance, as I described...
What is the code supposed to do by Path.GetDirectoryName(AppContext.BaseDirectory)
?
AppContext.BaseDirectory
? (as GetDirectoryName
usually does) Then why is it?It does the latter.
But in any case, regardless of this, in my scenario AppContext.BaseDirectory
is an empty string, so Path.GetDirectoryName
returns null and this null causes exception later in Path.Combine
.
By the way, I think it all might work fine if you remove Path.GetDirectoryName
call. My case included because an empty string (not null) might be processed fine later. But I'm just speculating.
If you want me to test an intermediate beta, please let me know, will do any time.
The paths that you've provided look correct.
By chance, as I described...
What is the code supposed to do by
Path.GetDirectoryName(AppContext.BaseDirectory)
?
- get the parent of
AppContext.BaseDirectory
? (asGetDirectoryName
usually does) Then why is it?- or trim the trailing backslash? (the choice of the method is strange then)
It does the latter.
But in any case, regardless of this, in my scenario
AppContext.BaseDirectory
is an empty string, soPath.GetDirectoryName
returns null and this null causes exception later inPath.Combine
.By the way, I think it all might work fine if you remove
Path.GetDirectoryName
call. My case included because an empty string (not null) might be processed fine later. But I'm just speculating.
Sure I get the point Path.GetDirectoryName
is probably not required, it was required for the previous implementation because it was a file path but not now just AppContext.BaseDirectory
. Thanks for the call out.
Either way some additional test cases are a good idea and Engine.GetLocalPath()
can be optimized out for some cases.
@BernieWhite 2.7.0-B0001 works fine in my unusual scenario, thank you.
v2.4.2 works fine with the following command:
Starting from v2.5.0 the above may fail in a particular app hosting .NET Core and PS Core:
In v2.6.0 the underlying exception and stack is (from debugger):
The culprit is this method in the newly added
Engine.cs
:and this change (the old code apparently worked fine):
The problem is that
AppContext.BaseDirectory
is an empty string in my scenario. The scenario is:Far Manager
) has a C++/CLI plugin hosting .NET 7.0 (FarNet
)FarNet.PowerShellFar
)If you need to install the above bits and reproduce the issue, it's possible, I will guide. Or instead we may try find and use something more reliable than
AppContext.BaseDirectory
.What is the assumption about
AppContext.BaseDirectory
(and_LocalPath
)? What is it expected to get (and_LocalPath
to be)? Per the docs:Is
_LocalPath
supposed to be based on a directory for probing assemblies or something else by design? Having this answer we may find a more reliable way of getting this value.If I get the code right:
pwsh.exe
orpowershell.exe
directories).