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

Closed

Description

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)

Activity

  1. PetSerAl commented on Aug 1, 2018

    @PetSerAl
    Contributor

    using module ./t.psm1; -> using module .\t.psm1;

    It seems PowerShell dislikes /.

  2. added
    Issue-Enhancement
    WG-Engine
    on Aug 1, 2018
  3. mklement0 commented on Aug 1, 2018

    @mklement0
    ContributorAuthor

    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.

  4. 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
  5. added
    Issue-Bug
    and removed
    Issue-Enhancement
    on Aug 2, 2018
  6. rjmholt commented on May 16, 2019

    @rjmholt
    Collaborator

    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

  7. rjmholt commented on May 16, 2019

    @rjmholt
    Collaborator

    (I general I tend to use / since Windows accepts both and *nix has a preference, especially when passing into .NET APIs)

  8. joshswimlane commented on Apr 24, 2020

    @joshswimlane

    Any updates on this?

  9. vexx32 commented on Apr 24, 2020

    @vexx32
    Collaborator

    I had a quick look (not an exhaustive one) and it seems like this might be a fairly simple fix:

    string fullyQualifiedNameStr = usingStatementAst.Name.Value;
    if (WildcardPattern.ContainsWildcardCharacters(fullyQualifiedNameStr))
    {
    wildcardCharactersUsed = true;
    return null;
    }
    // case 1: relative path. Relative for file in the same folder should include .\
    bool isPath = fullyQualifiedNameStr.Contains(@"\");
    if (isPath && !LocationGlobber.IsAbsolutePath(fullyQualifiedNameStr))
    {
    string rootPath = Path.GetDirectoryName(_parser._fileName);
    if (rootPath != null)
    {
    fullyQualifiedNameStr = Path.Combine(rootPath, fullyQualifiedNameStr);
    }

    Line 502 here incorrectly assumes that a path must contain \ to be a valid path.

  10. added
    Up-for-Grabs
    on Apr 24, 2020
  11. iSazonov commented on Apr 24, 2020

    @iSazonov
    Collaborator

    Best fix would be using a method from LocationGlobber - I guess we have already such one.

  12. added a commit that references this issue on Apr 25, 2020
    c4bd4d5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugResolution-FixedUp-for-GrabsWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions