Skip to content

Module structure #157

Closed
Closed

Description

@ravager-dk

An idea would be to add a segment about module structuring like this:
https://ramblingcookiemonster.github.io/Building-A-PowerShell-Module/

Activity

  1. iRon7 commented on Dec 22, 2022

    @iRon7
  2. Jaykul commented on Dec 26, 2022

    @Jaykul
    Member

    The normal reason for shipping a module as a bunch of separate files is to make it easier to debug. Dot-sourcing lets you get some of the benefits of a module while still keeping all the files separate so you can find things in your editor and deal with breakpoints easily.

    Whatever you do, make sure you're not only ever testing individual files instead of the merged module. That change can obscure problems that will pop up when all the files are executed together.

    In my opinion you should assume your end-users don't care about debugging, and while you might keep your source files separate during authoring, you should merge them all to one file for testing and shipping them, because...

    1. Dot-sourcing doesn't work with PowerShell classes
    2. Dot-sourcing has minor security risks (especially if you're not listing all the file names in your psm1)
    3. Dot-sourcing it slows down module import (particularly when code-signing).

    As a side note: Some organizations are required to run with the "AllSigned" execution policy, or even enforce specific trusted certs -- so even if you don't sign the module yourself, your users may have to sign it before they can use it.

  3. Jaykul commented on Dec 27, 2022

    @Jaykul
    Member

    For what it's worth, @iRon7 if you don't merge all the files, you miss out on 1-3 above, but if you also don't dot-source, you loose ModuleScope, along with the ability to share private functions -- which are powerful and useful features, and the main reasons to build modules in the first place. Honestly, if you want to give those up, you're probably not writing a module.

    If you need to ship scripts, you can publish individual scripts.

    If you need to ship a collection of scripts, you could put them all in the module folder, and have a one-liner .psm1:

    $Env:Path += ";$PSScriptRoot"

    Or export an alias that points at the file (Invoke-Build does that).

    This way, the scripts are each fully independent, and can't break each other (at least, not accidentally).

    You could also Import-Module each ps1 file, or shove them in NestedModules in your psd1. Either way, they're stand-alone and don't get in each other's scope (at least, not without intent), and you're more likely to list them all out and prevent someone dropping an extra in without anyone noticing.

  4. Jaykul commented on Jul 22, 2023

    @Jaykul
    Member

    Thoroughly discussed in #171 and all these years later, I think the general idea I proposed there is basically accepted as best practice by the community. We should write that up.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions