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
using module statement with a relative path not treated as relative to the location of the enclosing script when forward slashes are used #7424
Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Enginecore PowerShell engine, interpreter, and runtime
Update: PetSerAl has determined that the problem only surfaces with / as the path separator, not \.
Needless to say, / should be supported, because it is the native paths separator on Unix-like platforms and since it must be used for filesystem paths that work across all platforms.
Steps to reproduce
Set-Location/tmp # change to tmp dir. (make sure it exists)# Create a module'function Get-Foo { "hi from t.psm1" }'> t.psm1
# Create a script that references the module# with `using module` and a *relative path* with */*# Using '.\t.psm1` would *not* surface the problem.'using module ./t.psm1; Get-Foo'> t.ps1
# Invoke the script from the current location:
./t.ps1
# Change to a different location and invoke again.Set-Location/; /tmp/t.ps1
Expected behavior
hi from t.psm1
hi from t.psm1
Actual behavior
hi from t.psm1
/tmp/t.ps1 : The specified module '/t.psm1' was not loaded because no valid module file was found in any module directory.
# ...
That is, invoking the script from a location other than the one where it resides breaks the using module statement, because the relative path is seemingly interpreted relative to the current location rather than the script's.
As an aside: Fixing this also requires attention to symlinks: if the enclosing script is a symlink, its ultimate target's location should serve as the reference point.
Environment data
PowerShell Core v6.1.0-preview.4 on macOS 10.13.6
PowerShell Core v6.1.0-preview.4 on Ubuntu 16.04.4 LTS
PowerShell Core v6.1.0-preview.4 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.165)
Windows PowerShell v5.1.17134.165 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.165)
Reacted by Rob Holt, Patrick Meinecke, Maximilian Orsley, Bo Lingen, Jon Stelly and Santiago Squarzon
changed the title [-]`using module` statement with a relative path should be relative to the location of the enclosing script[/-][+]`using module` statement with a relative path not treated as relative to the location of the enclosing script when forward slashes are used[/+]on Aug 1, 2018
Wow Michael Klement (@mklement0) I just hit this, and like you said I had no idea using \ would be a workaround. You saved me on that! Hopefully I can fix this at some point
Reacted by Michael Klement, Maximilian Orsley and Rain Sallow (/u/ta11ow)
Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
PetSerAl commentedon Aug 1, 2018
using module ./t.psm1;->using module .\t.psm1;It seems PowerShell dislikes
/.mklement0 commentedon Aug 1, 2018
Thanks PetSerAl - it hadn't even occurred to me to try with
\(OP updated); should be an easy fix then.Ilya (@iSazonov): Thanks for labeling, but PowerShell not treating
/the same as\in this context is a bug, not an enhancement.[-]`using module` statement with a relative path should be relative to the location of the enclosing script[/-][+]`using module` statement with a relative path not treated as relative to the location of the enclosing script when forward slashes are used[/+]rjmholt commentedon May 16, 2019
Wow Michael Klement (@mklement0) I just hit this, and like you said I had no idea using
\would be a workaround. You saved me on that! Hopefully I can fix this at some pointrjmholt commentedon May 16, 2019
(I general I tend to use
/since Windows accepts both and *nix has a preference, especially when passing into .NET APIs)joshswimlane commentedon Apr 24, 2020
Any updates on this?
vexx32 commentedon Apr 24, 2020
I had a quick look (not an exhaustive one) and it seems like this might be a fairly simple fix:
PowerShell/src/System.Management.Automation/engine/parser/SymbolResolver.cs
Lines 493 to 509 in b7cb335
Line 502 here incorrectly assumes that a path must contain
\to be a valid path.iSazonov commentedon Apr 24, 2020
Best fix would be using a method from LocationGlobber - I guess we have already such one.
Close PowerShell#7424. Allow / in relative paths for `using module`
Allow `/` in relative paths for `using module` (#7424) (#12492)
Replace / with \ in using modules directives.
Allow `/` in relative paths for `using module` (PowerShell#7424) (Pow…