Skip to content

using module statement with a relative path not treated as relative to the location of the enclosing script when forward slashes are used #7424

@mklement0

Description

@mklement0

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    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

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions