Azure / azure-functions-powershell-worker

PowerShell language worker for Azure Functions.
MIT License
206 stars 54 forks source link

Reference module with Functions-specific command declarations #577

Open AnatoliB opened 3 years ago

AnatoliB commented 3 years ago

When authoring PowerShell functions, the Functions-specific commands (such as Push-OutputBinding, Invoke-ActivityFunction, etc.) are difficult to discover. The user must find the names and invocation syntax in the documentation and type them in correctly. These commands cannot be discovered interactively via the regular PowerShell means (Get-Help, Get-Command), and VSCode cannot help with autocomplete, IntelliSense, etc., which complicates functions authoring.

These commands are defined in an internal module that is automatically loaded by the worker during function execution. However, this module cannot be imported into a standalone PowerShell session because of the dependency on worker's internals.

What we can do is provide a special reference module containing all the commands with parameters and help text, with a "do-nothing" implementation. We will update this module on adding or changing commands, and we will distribute it together with the PowerShell worker (so it always stays in sync with the actual implementation).

Challenge: it would be great if we could make sure this module automatically loaded when Functions are authored in VSCode. At the same time, we don't want this module to be loaded into the sessions used for executing Functions code, so including this module into the global PSModulePath is probably not a good idea. In the worst case, we can document manual steps, and this will be an improvement comparing to the current situation, but can we find a better way?

Francisco-Gamino commented 3 years ago

A possible solution is to have a module in the PowerShell worker repro that has the same cmdlet signature and help content. The user will have to download this module and append the path of this module to $env:PSModulePath.

JustinGrote commented 3 years ago

As a workaround, I usually just import the Powershell Worker DLL module from the func tools worker and then I get intellisense. I've considered making a module that generates proxy commands that enable intellisense but when run just simple output warning "In order to test this function you must run it using func start"

image

4c74356b41 commented 3 years ago

where can you get that module?

JustinGrote commented 3 years ago

where can you get that module?

It's buried in the azure functions core tools. I imported it here, you can find it relative to the azure-functions-core-tools part of the path. ipmo "C:\Users\JGrote\scoop\apps\nodejs-lts\current\bin\node_modules\azure-functions-core-tools\bin\workers\powershell\7\Microsoft.Azure.Functions.PowerShellWorker.dll"

PalmEmanuel commented 1 year ago

An expanded workaround to get intellisense when developing Azure Functions is to first import the DLL, and then the manifest to also get the script module functions. I wrote a one-liner to find it based on the location of the function core tools (command func).

# Find and import PowerShellWorker 7.2 module, first import compiled dll module, then the manifest file
Get-ChildItem "$(Split-Path (Get-Command 'func').Source -Parent)\*\7.2" -Recurse |
    Get-ChildItem -Recurse -Filter 'Microsoft.Azure.Functions.PowerShellWorker*' -File |
    Where-Object { $_.Extension -in '.dll','.psd1' } |
    Import-Module