Skip to content

Add @generatePwshEnv PowerShell code generator - #1314

Open
jsskeen-utah wants to merge 2 commits into
dmno-dev:mainfrom
jsskeen-utah:joe/codegen-pwsh
Open

jsskeen-utah wants to merge 2 commits into
dmno-dev:mainfrom
jsskeen-utah:joe/codegen-pwsh

Conversation

@jsskeen-utah

@jsskeen-utah jsskeen-utah commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a @generatePwshEnv code generator. It writes a .ps1 file that compiles the C# output from @generateCsharpEnv with Add-Type and returns the typed object from Get-Env. The C# class does the parsing, decryption, and validation, so the PowerShell side only wraps it.

Env.ps1
  Add-Type (C# from generateCsharpEnvSrc, guarded by -as [type])
  Get-EnvSensitiveKeys
  Get-Env            # [Env]::Load()

Property types and Get-Help property descriptions are read from the generated C# declarations, so they can't drift from the class.

functionName= sets the function name (default Get-Env). It must be Verb-Noun.

Requires PowerShell 7.4+ (.NET 8). The generated C# uses the AesGcm tag-size constructor, which only exists on .NET 8, and the shared C# output uses C# 11 required members that PowerShell 7.2 and 7.3 can't compile. The generated module checks the version first and throws a clear error on older versions.

Windows PowerShell 5.1 is not a target. Its .NET Framework runtime can't load the generated code, and the C# output needs the modern .NET APIs. PowerShell 7 is the path Microsoft is taking for in-box PowerShell (see PowerShell 7 is coming to Windows), but 5.1 stays on existing Windows versions, so users on those will need to install PowerShell 7.4+ to use this generator.

Known limitation

A key whose name clashes with a member the generated C# class defines makes the module fail to compile. This affects LOAD (clashes with Load()), SensitiveKeys, DecryptBlob, and the object members (GetType, ToString, Equals, GetHashCode, MemberwiseClone, Finalize). The bug is in the C# emitter, not this PR: #1313. The PowerShell output inherits it and needs no change once that is fixed.

Evidence

  • Before: no PowerShell output.
  • After: packages/varlock/src/env-graph/test/type-generation passes (146 tests), including new pwsh.test.ts coverage for the wrapper, functionName, the namespace hash, and help generation.
  • Manual checks with pwsh 7: dot-sourcing twice in one session, property types, Get-Help -Full, and a blob encrypted by varlock's own encryptEnvBlobSync loading correctly.
  • End to end: varlock run --inject blob -- pwsh ./test.ps1 against a schema with @encryptInjectedEnv.
  • Environments tested:
OS PowerShell Result
Debian (Linux) 7.6.5 Pass (encrypted and plaintext blobs)
Windows 11 Pro 7.7.0 Pass (encrypted and plaintext blobs)
Windows 11 Pro 7.4.22 Pass (encrypted and plaintext blobs)
Windows 11 Pro Windows PowerShell 5.1.26100 Version guard throws the 7.4 requirement message

Merge Danger

Door: two-way. The change adds a decorator and new files. Nothing existing changes behavior, and the C# emitter is untouched.

Blast radius: limited to schemas that opt in with @generatePwshEnv.

Generates a .ps1 file that compiles the C# output from @generateCsharpEnv with Add-Type and returns the typed object from Get-Env. The C# class decrypts and validates the env blob, so the PowerShell side only wraps it.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The generated loader cannot compile on PowerShell 7.2 or 7.3 despite advertising support for both.

Approach: Reusing the C# loader is sound, but the PowerShell runtime requirement must match the emitted code.

Reviewed changes include generator registration, emitted scripts, tests, and documentation.

  • PowerShell loader: Adds typed loading, sensitive-key listing, customizable function names, and schema-hashed type reuse.
  • Help and documentation: Adds property help, integration instructions, reference entries, and navigation.
  • Validation: All 146 code-generation tests passed; plaintext loading, repeated dot-sourcing, help, and encrypted loading worked on PowerShell 7.6.10.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using gpt-6.1-sol | 𝕏

The generated C# uses the AesGcm tag-size constructor, which only exists on .NET 8. PowerShell 7.2 and 7.3 run on .NET 6 and 7, and their compilers do not support the C# 11 required members that the shared C# output uses. The generated module now checks the PowerShell version first and throws a clear error on older versions.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes since the prior review align the runtime requirement with the generated C#.

  • Enforced PowerShell 7.4: Added an explicit version check before compilation.
  • Aligned documentation: Updated generated help and both documentation pages to require 7.4+.
  • Added regression coverage: Verified all 147 code-generation tests pass, plus runtime guard rejection and successful compilation on PowerShell 7.6.10. PowerShell 7.4 itself was not available for execution.

Pullfrog  | View workflow run | Using gpt-6.1-sol | 𝕏

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant