Skip to content

Use Roslyn source generators for cdxml cmdletization #12760

Closed

Description

Summary of the new feature/enhancement

Currently, when a cdxml module is imported the engine takes the following steps:

  1. Parse an XML document
  2. Build a string representing an entire PowerShell module based on the XML
  3. The generated module essentially just builds args for a C# based CmdletAdapter implementation

If source generators were used instead, the first two steps would be moved to compile time and the last would have less overhead. They would also be less prone to some of PowerShell's module scope quirks like ErrorAction inheritance.

Activity

  1. added
    Issue-Enhancement
    on May 22, 2020
  2. added
    WG-Engine
    on May 23, 2020
  3. iSazonov commented on May 23, 2020

    @iSazonov
    Collaborator

    the last would have less overhead.

    What do you mean? Performance? More fast startup?

  4. SeeminglyScience commented on May 23, 2020

    @SeeminglyScience
    ContributorAuthor

    What do you mean? Performance? More fast startup?

    Both (assuming by start up you mean import time, afaik no cdxml modules are imported at process init).

  5. iSazonov commented on May 24, 2020

    @iSazonov
    Collaborator

    What steps will be?

    Import-Module cdxml

    • Parse xml
    • Generate C# file
    • Compile C# to binary module
    • Load binary modue
  6. vexx32 commented on May 24, 2020

    @vexx32
    Collaborator

    I think what Patrick Meinecke (@SeeminglyScience) is suggesting is that rather than shipping the XML and parsing it into a module at import time, we could create a build task to generate a binary module so that we only need to ship the module DLL. That way, all of that processing is front-loaded and users just end up importing the module DLL directly.

  7. SeeminglyScience commented on May 24, 2020

    @SeeminglyScience
    ContributorAuthor

    Yeah, Roslyn source generators attach like analyzers. Except instead of emitting diagnostic records, they add additional source files to the obj path.

  8. iSazonov commented on May 25, 2020

    @iSazonov
    Collaborator

    I don't understand why it should be in the repo. If a developer want create a binary module the developer can use any tools.

  9. SeeminglyScience commented on May 25, 2020

    @SeeminglyScience
    ContributorAuthor

    Ilya (@iSazonov) Not sure what you mean.

    This issue is about creating a Roslyn source generator that generates binary modules from cdxml files.

  10. iSazonov commented on May 25, 2020

    @iSazonov
    Collaborator

    This issue is about creating a Roslyn source generator that generates binary modules from cdxml files.

    You can use Roslyn source generator or another tool to create custom binary module. I ask why it should be in PowerShell repo?

  11. SeeminglyScience commented on May 25, 2020

    @SeeminglyScience
    ContributorAuthor

    I'm not sure why it wouldn't be, but sure it can go in a new repo I guess.

  12. iSazonov commented on May 25, 2020

    @iSazonov
    Collaborator

    From my understanding you say about new tool/module to create specific binary modules.
    I would expect that you describe how this should work in a full-fledged RFC.

  13. SeeminglyScience commented on May 25, 2020

    @SeeminglyScience
    ContributorAuthor

    From my understanding you say about new tool/module to create specific binary modules.

    I would say that the suggestion is an alternative/addition to the current cmdletization API that would result in better performance and UX. It's like saying PowerShell is a compiler, it's not wrong but it's not the whole picture.

    I would expect that you describe how this should work in a full-fledged RFC.

    Yeah I think if this gets implemented it might be a good idea for the implementer to submit an RFC. If this is something you're interested in pursuing, I'd be happy to expand on any particular detail that may be unclear.

  14. iSazonov commented on May 25, 2020

    @iSazonov
    Collaborator

    I would say that

    This is too blurry said. If you have a clear idea, then share the exact specifications.

  15. SeeminglyScience commented on May 25, 2020

    @SeeminglyScience
    ContributorAuthor

    So currently when a cdxml module is imported, PowerShell reads the cdxml file and uses it to generate a PowerShell script module. My suggestion is that instead of generating a PowerShell script module, it generates a binary module. And instead of doing it at import time, it's done at compile time. A good way to do this would be via Roslyn source generators.

  16. 10 remaining items

  17. lzybkr commented on Jul 23, 2020

    @lzybkr
    Contributor

    Dongbo Wang (@daxian-dbw) - IIRC, command discovery would generate PowerShell from CDXML and you don't always search CDXML modules, but when you do, it stands out in a profile.

    One could modify command discovery, but that solution feels too targeted. CDXML modules can be slow to load and the generated code feels quite bloated. For example the Storage modules takes 2s to load on my machine and generates nearly 40,000 lines of PowerShell.

  18. iSazonov commented on Jul 23, 2020

    @iSazonov
    Collaborator

    I see a simplest thing we can do is to add a cache for compiled CDXML modules. Since we already have a cache for the command discovery it does not add more security risk. (I'd work on this if MSFT team want and we could include this in 7.1.)

    The next thing we could do is try to improve the CDXML module compiler.

    I suggest to open new issue for these optimizations and close the issue because RSGs is more for external projects like https://github.com/PowerShell/PlasterBuild and https://github.com/PowerShell/generator-powershell.

    GitHub
    Provides common build tasks for PowerShell module projects - PowerShell/PlasterBuild
    GitHub
    Create PowerShell modules and scripts using Yeoman! - PowerShell/generator-powershell
  19. daxian-dbw commented on Jul 23, 2020

    @daxian-dbw
    Member

    Thanks Jason Shirk (@lzybkr), I absolutely agree there is space to improve the CDXML modules (loading and execution).

    Ilya (@iSazonov) do you mean caching the generated powershell script for CDXML modules? It's different from the cache for command discovery. The latter only contains metadata information (which command name belongs to which module), but no code for execution. I don't recommend to do it -- it's not an urgent thing that needs to be mitigated with a workaround. If we want to invest in this area, then we should directly improve the ScriptWriter and works on the tool that produces assemblies from CDXML files.

  20. iSazonov commented on Jul 23, 2020

    @iSazonov
    Collaborator

    do you mean caching the generated powershell script for CDXML modules?

    No, I mean to generate CDXML script, compile it and cache the assembly. Then we could improve ScriptWriter.

  21. daxian-dbw commented on Jul 23, 2020

    @daxian-dbw
    Member

    No, I mean to generate CDXML script, compile it and cache the assembly.

    This already sounds like the tool that Patrick Meinecke (@SeeminglyScience) is asking for 😄

  22. iSazonov commented on Jul 23, 2020

    @iSazonov
    Collaborator

    A tool is an external thing. I suggest to do this on the fly and check the cache folder before compile CDXML in next time.

  23. vexx32 commented on Jul 23, 2020

    @vexx32
    Collaborator

    I don't see a need to duplicate the effort there. If we're going to have such functionality it makes more sense to me that we segregate its code to a separate dll that a small console app can be built to interface with in a minimal fashion. That way, PowerShell can cache built CDXML modules until their current authors get around to shipping compiled assemblies for them, and authors wanting to write CDXML modules have the option of just shipping the compiled assembly and not requiring PowerShell to perform the caching.

    Over time we could deprecate the caching method and eventually remove the CDXML caching entirely, requiring authors to precompile the modules.

  24. lzybkr commented on Jul 23, 2020

    @lzybkr
    Contributor

    If one was considering building something that might someday get integrated into the Windows build - I would start with a standalone tool that does not depend on PowerShell - probably with a simple exe wrapper around a dll that could be consumed by PowerShell.

  25. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  26. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  27. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  28. added
    Resolution-No Activity
    on Nov 16, 2023
  29. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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-EnhancementResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions