Skip to content

ImportPSModule on InitialSessionState should be params. #6005

Closed

Description

@twitchax

Functions in the automation API which take arrays should take them as params.

For example this should be:

        public void ImportPSModule(params string[] name)
        {
            if (name == null)
                throw new ArgumentNullException("name");
            foreach (string n in name)
            {
                ModuleSpecificationsToImport.Add(new ModuleSpecification(n));
            }
        }

A developer should not have to call this by newing up an array of strings.

Activity

  1. added
    WG-Engine
    on Jan 24, 2018
  2. iSazonov commented on Jan 24, 2018

    @iSazonov
    Collaborator

    It seems we get extra allocation.

    /cc Jason Shirk (@lzybkr) Dongbo Wang (@daxian-dbw)

  3. twitchax commented on Jan 24, 2018

    @twitchax
    Author

    Ilya (@iSazonov), what do you mean you get extra allocation?

  4. iSazonov commented on Jan 24, 2018

    @iSazonov
    Collaborator

    In both cases we do new string[]. Although it is so critical - we call this method rarely.

  5. twitchax commented on Jan 24, 2018

    @twitchax
    Author

    Yeah. I meant more for the sake of developers. Regardless of the number of times PowerShell 6 calls it, writing an app which hosts PowerShell needs to explicitly new up a string[], which is not syntactically preferred.

  6. lzybkr commented on Jan 24, 2018

    @lzybkr
    Contributor

    FWIW, I believe adding params is a breaking change if you don't recompile your code - you could end up with a missing method exception. This might be a non-issue as there are so few hosts that this might affect.

    A better change that might be sufficient is to add some overloads that take 1, 2, or 3 parameters. These overloads would avoid creating an array and cover the most common cases.

  7. twitchax commented on Jan 24, 2018

    @twitchax
    Author

    How is adding params a breaking change?

    Any caller will have to do

    state.ImportPSModule(new string[] { "Module1", "Module2" });

    That line will still compile with params. How often do developers upgrade their packages and not recompile? Also, I would need to look at the IL, but my impression was that adding params would not change the signature of the method: it would simply allow the caller to use the syntactic sugar. In the end, only the caller needs to take params into account when using the syntactic sugar call: in the case of a method that already news up an

    It is totally cool to make methods that take 1, 2, or 3 parameters, but that only solves part of the problem, as their may be developers that need more. The BCL does this by providing methods with 1, 2, or 3 parameters, and then a fourth overload that takes a params as its fourth parameter.

  8. twitchax commented on Jan 24, 2018

    @twitchax
    Author

    Actually, it looks like the IL method signature is the same...there is just an attribute applied for syntactic purposes (I would guess).

    private static void WithArray(string[] array)
    {
    }

    goes to

    .method private hidebysig static 
    	void WithArray (
    		string[] 'array'
    	) cil managed 
    {
    	// Method begins at RVA 0x205e
    	// Code size 2 (0x2)
    	.maxstack 8
    
    	IL_0000: nop
    	IL_0001: ret
    } // end of method Program::WithArray

    And with params, this

    private static void WithParams(params string[] array)
    {
    }

    goes to

    .method private hidebysig static 
    	void WithParams (
    		string[] 'array'
    	) cil managed 
    {
    	.param [1]
    	.custom instance void [System.Runtime]System.ParamArrayAttribute::.ctor() = (
    		01 00 00 00
    	)
    	// Method begins at RVA 0x2061
    	// Code size 2 (0x2)
    	.maxstack 8
    
    	IL_0000: nop
    	IL_0001: ret
    } // end of method Program::WithParams

    So, even if a developer did not rebuild against new binaries with params, it should still work exactly the same.

  9. lzybkr commented on Jan 24, 2018

    @lzybkr
    Contributor

    I'm assuming that the callsig in the reference will look different because of the attribute on the parameter.

    For example, C++/CLI uses an attribute to distinguish between int and long when they map to the same CLI type.

  10. twitchax commented on Jan 25, 2018

    @twitchax
    Author

    It seems to work fine. I just tried this scenario, and the call works fine, even if you do not rebuild after a dependency updates to use params.

  11. twitchax commented on Jan 25, 2018

    @twitchax
    Author

    This change is obviously not required, it is just normally expected that methods like this take params since it just makes life easier for developers.

  12. lzybkr commented on Jan 25, 2018

    @lzybkr
    Contributor

    Thanks for checking, my memory was fuzzy. I might have been remembering modopt/modreq which is on the signature, not on the references.

  13. iSazonov commented on Jan 25, 2018

    @iSazonov
    Collaborator

    It is public API - we should PowerShell Committee conclusion.
    /cc Steve Lee (@SteveL-MSFT)

  14. added
    Review - Committee
    on Jan 25, 2018
  15. powercode commented on Jan 25, 2018

    @powercode
    Collaborator

    Maybe not on topic, but related:
    Is is possible to filter the imported commands when adding a module to the InitialSessionState?

    Like we have on Import-Module with for example -Cmdlet?

    Otherwise I'd like an overload on ImportPSModule with filtering options.

  16. iSazonov commented on Jan 25, 2018

    @iSazonov
    Collaborator

    Internally we use Import-Module cmdlet to import a module in InitialSessionState.

  17. SteveL-MSFT commented on Feb 21, 2018

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this. We believe this is the right change and we should look at all the other public apis to see if this is applicable elsewhere. It doesn't appear to be a breaking change based on the discussion.

  18. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Feb 21, 2018
  19. iSazonov commented on Feb 22, 2018

    @iSazonov
    Collaborator

    I found only public static void RemoveKeyHandler(string[] key) and public static int Main(string[] args). The first has still another overload. So I believe there's nothing more to fix.

  20. added
    Up-for-Grabs
    on Feb 22, 2018
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

    Committee-ReviewedResolution-FixedUp-for-GrabsWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions