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
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...
Dot-sourcing doesn't work with PowerShell classes
Dot-sourcing has minor security risks (especially if you're not listing all the file names in your psm1)
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.
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.
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.
Activity
iRon7 commentedon Dec 22, 2022
Related:
18740Best practice: import functions in a moduleJaykul commentedon Dec 26, 2022
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...
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.
Jaykul commentedon Dec 27, 2022
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:
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-Moduleeach ps1 file, or shove them inNestedModulesin 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.Jaykul commentedon Jul 22, 2023
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.