Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is module auto-loading meant to be reliable?

Environment

I have the following folder structure where I keep powershell modules:

C:
  PsModules
     ...
     util
        util.psm1 (this contains implementation of 'Test-Function')
        util.test.ps1
     ftp
        ftp.psm1
        http.test.ps1
     ... 

There are about 50 folders and modules in c:\PsModules.

I have set environment variable PSModulePath to include c:\PsModules. This seems to meet the conditions for "well-formed modules" described in Microsoft's documentation and this answer.

Symptoms

Sometimes Test-Function is not found automatically when calling it from ISE. In fact, on any given fresh launch of ISE, there are always some (seemlingly unpredictable) modules that are not found automatically. The failure to automatically find Test-Function, for example, looks like this:

PS C:\> Test-Function
Test-Function : The term 'Test-Function' is not recognized as the name 
of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is 
correct and try again.
...
+ FullyQualifiedErrorId : CommandNotFoundException

At first glance, this seems to indicate that util.psm1 is not "well-formed". If it were not "well-formed", then ListAvailable shouldn't work. But it does work:

c:\> get-module -ListAvailable util
Directory: c:\PsModules\util

ModuleType Version    Name                 ExportedCommands
---------- -------    ----                 ----------------
Script     0.0        util

PS C:\> Test-Function
...
+ FullyQualifiedErrorId : CommandNotFoundException

Furthermore, after calling Get-Command for the module, the commands in the module are available for general use:

c:\> Get-Command -Module util
CommandType     Name                             ModuleName
-----------     ----                             ----------
Function        Test-Function                    util
c:\> Test-Function
Call to Test-Function succeeded!

Questions

  1. Is automatic discovery and module auto-loading supposed to be reliable and predictable?

  2. How do I troubleshoot why powershell is sometimes doesn't find a command until a call to Get-Command -module?

  3. Is it bad practice to rely on powershell to automatically load modules? If so, what is the good practice for automatically loading modules?

like image 668
alx9r Avatar asked Feb 22 '15 18:02

alx9r


1 Answers

I can't speak to whether module auto-loading is intended to be reliable, but I personally do not rely on it in finished code.

If I'm writing a script or module, I always use Import-Module TheModule -ErrorAction Stop, and often use #Requires -Module AciveDirectory,TheModule,SQLPs to ensure that the modules are available.

For interactive use in the PowerShell console or ISE, I do generally rely on auto-loading, but if it fails I just Import-Module manually for the session.

In situations where I always want a specific module loaded for an interactive session, I load it in a profile. To see the various profiles run this (from both ISE and Console):

$profile | Get-Member -MemberType NoteProperty

You can decide where you want to place the code for importing the module based on which user(s) and in which host(s) you want the module to be available.

So far I only do this for posh-git, but it seems like it would fit your use case well.

like image 142
briantist Avatar answered Oct 04 '22 04:10

briantist