Skip to content

Add ScriptsToInclude to the Module Manifest. #24253

Open

Description

@iRon7

Summary of the new feature / enhancement

To extent on issue #18740 Best practice: import functions in a module with a more practical propose:
Adding a ScriptsToInclude to Module manifest elements would IMO allow for a safer (in terms of making module design misstakes) way to include scripts than dot-sourcing (invoking) multiple scripts from the <module>.psm1 file where only the embedded function (usually with the same name as the script file) is ment to be included.

Proposed technical implementation details (optional)

My expectation of a ScriptsToInclude module manifest element, is that it loads one or more scripts (possibly supporting wildcards) into the module as a function using the script name as the function name similar as installing a script from the PowerShell Gallery (Install-Script -Name <MyScript>).

Maintaining (as debugging) the script apart (or not) from the module would also be easier as one might simply be able to test it by using it's file name .\MyScript.ps1 (without dot-sourcing first) and even would be able compare the result with the already loaded module function (just named MyScript).

Activity

  1. added
    Issue-Enhancement
    Needs-Triage
    on Sep 6, 2024
  2. iRon7 commented on Sep 10, 2024

    @iRon7
    Author

    rhubarb-geek-nz ,

    Thanks for the feedback, what I meant with testing, is just testing the script(function) and not testing the whole module (which eventually comes at the end of the module deployment).
    Meaning that your "best solution" requires someone to completely rebuild the module and fully test that for every single development step in the concerned script(function). Meaning that Pester might throw dozens of errors after your rebuild, where a simple direct check from the terminal prompt, like $InputObject | .\MyScript -Some parameters could have immediately reveal a simple scripting misstake. Besides, by rebuilding and reloading the whole module, you will loose the reference of how the the orginal function reacted in the prior full module release..

  3. kilasuit commented on Nov 7, 2024

    @kilasuit
    Collaborator

    I can understand the ask, thou we already have NestedModules for importing into the Module Scope (which you could do what you want with) or the ScriptsToProcess field which will import any script you specify into the session before loading the module that's to be imported, which also could be used to do what you want, just differently.

    Not all scripts to add are a single function, or even named correctly, so adding this is not ideal either for the behaviour that I think that you are asking for this to manage for you.

    I personally can't see a need for this especially as we recommend a build and test process that rolls up into a single psm1 when working with script modules. Due to the amount of performance gains you get with it

    Regarding

    Besides, by rebuilding and reloading the whole module, you will loose the reference of how the the orginal function reacted in the prior full module release..

    One of the ways that i've used in developing locally in my own projects in a way that enables me to not loose this and until I am happy with how it functions, I may end up with 5..100 run sessions in that time split across devices and across multiple different days.
    I do this with Transcription enabled, and if it's a simple small change to a function definiton I often will just overwrite it in the Function:\ path & test out how this is manually.
    I then tend to run each new build of a module in a side-by-side session for inspection of intention vs actual behavior.
    That way I can inspect as I go and if I want to dump out to disk any session info I use Export-CLIXML for this so that I can inspect it at a later date if needs be.

  4. iRon7 commented on Nov 8, 2024

    @iRon7
    Author

    Ryan Yates (@kilasuit),

    Thank you for you feedback, comments and suggestions.

    Maybe this issue (and the one #18740 referring to) should be more of a discussion topic.

    I guess that the point I am trying to make comes down to that PowerShell module (.psm) files aren't (in a broad sense) as safe to work with as they could be. Using the NestedModules or ScriptsToProcess fields won't change that.

    You might compare this to a general PowerShell Data (.psd1) file: one could rename the .psd1 file to a .ps1 file and just invoke (dot-source) that, the results are the same but it is not recommended because it is not safe. PowerShell Data (.psd1) file are protected by the PowerShell Restricted Language Mode. (I bet that somewhere under the hood, the .SafeGetValue() method is used for this.)

    For a PowerShell module (.psm1) files (or any other way to define the module), there are no boundaries, I can only invoke script (without any restrictions) similar to invoking a .ps1 script in Full Language Mode, where essentially I only need to define a set of specific items as functions, classes, enums and statements as requires and using and possibly a few things more as (hardcoded) variable definitions (anything else could be done via the ScriptsToProcess field). You might almost think of a "PowerShell Definition Language Mode". But than, there is already a restricted manifest that is further bound by its predefined settings. Unfortunately each manifest settings is invoking one or more (cascaded) scripts to define the items (as functions, classes, etc.) rather than loading these definitions into the module...

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-EnhancementNeeds-Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions