Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 11 additions & 31 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ private void Initialize(
#if CORECLR
// For Full CLR logger is loaded via Composition
// But for Core CLR we need to load it explicitly
this.Loggers = GetInterfaceImplementationsFromAssembly<ILogger>();
this.Loggers = GetInterfaceImplementationsFromAssembly<ILogger>(typeof(ScriptAnalyzer).GetTypeInfo().Assembly);
#endif
#region Initializes Rules

Expand Down Expand Up @@ -866,36 +866,12 @@ private List<string> GetValidCustomRulePaths(string[] customizedRulePath, PathIn
}


private IEnumerable<T> GetInterfaceImplementationsFromAssembly<T>() where T : class
private IEnumerable<T> GetInterfaceImplementationsFromAssembly<T>(Assembly assembly) where T : class
{
string dirName = Path.GetDirectoryName(typeof(ScriptAnalyzer).GetTypeInfo().Assembly.Location);
var dllPaths = Directory.EnumerateFiles(dirName, "*.dll", SearchOption.TopDirectoryOnly);
var objects = new List<T>();
foreach (var dllPath in dllPaths)
{
outputWriter.WriteVerbose(string.Format("Found Assembly: {0}", dllPath));
var objectsFromOneFile = GetInterfaceImplementationsFromAssembly<T>(dllPath);
objects.AddRange(objectsFromOneFile);
}
return objects;
}

private IEnumerable<T> GetInterfaceImplementationsFromAssembly<T>(string ruleDllPath) where T : class
{
string fileName = Path.GetFileNameWithoutExtension(ruleDllPath);
var assemblyName = new AssemblyName(fileName);
outputWriter.WriteVerbose(string.Format("Loading Assembly:{0}", assemblyName.FullName));

Assembly dll = Assembly.Load(assemblyName);

if (dll == null)
{
outputWriter.WriteVerbose(string.Format("Cannot load {0}", ruleDllPath));
return Enumerable.Empty<T>();
}
outputWriter.WriteVerbose(string.Format("Loading Assembly:{0}", assembly.FullName));

var rules = new List<T>();
foreach (Type type in dll.ExportedTypes)
foreach (Type type in assembly.ExportedTypes)
{
if (type.IsInterface
|| type.IsAbstract
Expand Down Expand Up @@ -927,9 +903,13 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
this.ExternalRules = null;

#if CORECLR
this.ScriptRules = GetInterfaceImplementationsFromAssembly<IScriptRule>();
this.TokenRules = GetInterfaceImplementationsFromAssembly<ITokenRule>();
this.DSCResourceRules = GetInterfaceImplementationsFromAssembly<IDSCResourceRule>();
// Hosts can place the analyzer beside hundreds of unrelated assemblies.
// Built-in rules live in one known assembly; probing every adjacent DLL
// loads host code and can fail on dependencies that analysis never needs.
var builtInRules = Assembly.Load(new AssemblyName("Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules"));
Comment thread
darrenator[bot] marked this conversation as resolved.
Resolved
this.ScriptRules = GetInterfaceImplementationsFromAssembly<IScriptRule>(builtInRules);
this.TokenRules = GetInterfaceImplementationsFromAssembly<ITokenRule>(builtInRules);
this.DSCResourceRules = GetInterfaceImplementationsFromAssembly<IDSCResourceRule>(builtInRules);
#else
// An aggregate catalog that combines multiple catalogs.
using (AggregateCatalog catalog = new AggregateCatalog())
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,23 @@ Get-TestFailures
The documentation in this section can be found in
[Using PSScriptAnalyzer](https://learn.microsoft.com/powershell/utility-modules/psscriptanalyzer/using-scriptanalyzer).

### Built-in assembly discovery in the Immense fork

On PowerShell Core, built-in rules are loaded from
`Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules` and loggers from the
analyzer engine assembly. Other DLLs beside the engine are not searched for rules
or loggers. This avoids loading unrelated host assemblies on every analysis and
prevents their missing dependencies from breaking analysis.

`-IncludeRule` and `-ExcludeRule` select rules after discovery; they do not filter
DLLs. Explicit PowerShell custom rules still use `-CustomRulePath`, including its
existing wildcard and recursion support. Putting a custom compiled rule or logger
DLL beside the engine no longer implicitly registers it on PowerShell Core. This
change does not add compiled custom-rule support to the Core implementation.

See [rule discovery measurements](docs/RuleDiscoveryPerformance.md) for a
reproducible before/after benchmark and its limits.

## Contributions are welcome

There are many ways to contribute:
Expand Down
77 changes: 77 additions & 0 deletions Tests/Engine/RuleAssemblyDiscovery.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Describe 'PowerShell Core built-in rule discovery' -Skip:($PSVersionTable.PSEdition -ne 'Core') {
BeforeAll {
$moduleRoot = (Get-Module PSScriptAnalyzer).ModuleBase
$powerShell = (Get-Process -Id $PID).Path
$childScript = Join-Path $TestDrive 'Test-Discovery.ps1'
@'
param($ModulePath, $CustomRulePath)
$ErrorActionPreference = 'Stop'
Import-Module $ModulePath
$rules = @(Get-ScriptAnalyzerRule)
$diagnostics = @(Invoke-ScriptAnalyzer -ScriptDefinition 'gci' -IncludeRule PSAvoidUsingCmdletAliases)
$custom = @(Invoke-ScriptAnalyzer -ScriptDefinition 'gci' -Settings @{
CustomRulePath = $CustomRulePath
IncludeDefaultRules = $true
IncludeRules = @('PSAvoidUsingCmdletAliases', 'Measure-RequiresRunAsAdministrator')
})
$customOnly = @(Invoke-ScriptAnalyzer -ScriptDefinition 'gci' -CustomRulePath $CustomRulePath)
$formatted = Invoke-Formatter -ScriptDefinition 'gci'
[pscustomobject]@{
Rules = @($rules.RuleName)
Diagnostics = @($diagnostics.RuleName)
Custom = @($custom.RuleName)
CustomOnly = @($customOnly.RuleName)
Formatted = $formatted
UnrelatedLoaded = @([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {
-not $_.IsDynamic -and [IO.Path]::GetFileName($_.Location) -eq 'Unrelated.Host.dll'
}).Count
} | ConvertTo-Json -Compress
'@ | Set-Content $childScript

# Compile outside the child: loading it in the test runner must not hide
# whether discovery loads it in the fresh process.
$managedDll = Join-Path $TestDrive 'Unrelated.Host.dll'
Add-Type -TypeDefinition 'public class UnrelatedDiscoveryFixture {}' -OutputAssembly $managedDll
$dependencyDll = Join-Path $TestDrive 'MissingDependency.dll'
Add-Type -TypeDefinition 'public class MissingDiscoveryDependency {}' -OutputAssembly $dependencyDll
$missingDependencyDll = Join-Path $TestDrive 'MissingDependency.Host.dll'
Add-Type -TypeDefinition 'public class DerivedDiscoveryFixture : MissingDiscoveryDependency {}' `
-ReferencedAssemblies $dependencyDll -OutputAssembly $missingDependencyDll
}

It 'ignores an unrelated <Kind> DLL and preserves built-in and explicit custom rules' -TestCases @(
@{ Kind = 'invalid' }
@{ Kind = 'managed' }
@{ Kind = 'missing dependency' }
) {
param($Kind)
$copy = Join-Path $TestDrive $Kind
Copy-Item $moduleRoot $copy -Recurse
$adjacentDll = Join-Path $copy 'PSv7/Unrelated.Host.dll'
if ($Kind -eq 'invalid') {
Set-Content $adjacentDll 'Not a managed assembly'
} elseif ($Kind -eq 'missing dependency') {
Copy-Item $missingDependencyDll $adjacentDll
} else {
Copy-Item $managedDll $adjacentDll
}

$output = & $powerShell -NoLogo -NoProfile -File $childScript `
-ModulePath (Join-Path $copy 'PSScriptAnalyzer.psd1') `
-CustomRulePath "$PSScriptRoot/samplerule/samplerule.psm1" 2>&1
$LASTEXITCODE | Should -Be 0 -Because ($output | Out-String)
$result = $output | ConvertFrom-Json
$result.Rules.Count | Should -Be 69
$result.Rules | Should -Contain 'PSAvoidUsingCmdletAliases'
$result.Rules | Should -Contain 'PSDSCUseIdenticalParametersForDSC'
$result.Diagnostics | Should -Be @('PSAvoidUsingCmdletAliases')
$result.Custom | Should -Contain 'PSAvoidUsingCmdletAliases'
$result.Custom | Should -Contain 'samplerule\Measure-RequiresRunAsAdministrator'
$result.CustomOnly | Should -Be @('samplerule\Measure-RequiresRunAsAdministrator')
$result.Formatted | Should -Be 'gci'
$result.UnrelatedLoaded | Should -Be 0
}
}
141 changes: 141 additions & 0 deletions docs/RuleDiscoveryPerformance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Built-in rule discovery performance

## Why this changed

[ImmyBot PR 11257](https://github.com/immense/immybot/pull/11257) exposed a
PowerShell Core discovery problem: PSScriptAnalyzer enumerated every `*.dll`
beside its engine, loaded each assembly, and walked its exported types four times
per invocation (loggers, script rules, token rules, DSC rules). In a host's output
directory, unrelated DLLs made analysis expensive and missing dependencies could
abort it entirely. Rule-name filters run too late to prevent this work.

The engine now discovers loggers in its own assembly and rules in the known
`Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules` assembly. It creates
fresh rule instances each time, preserving per-invocation configuration. No cache,
environment variable, or glob setting is required. Explicit PowerShell custom
rules still use `CustomRulePath`, including its wildcard/recursion support.

Compatibility: adjacency alone no longer registers third-party compiled rules or
loggers on PowerShell Core. This change does not add Core support for compiled
custom-rule paths. The legacy full-framework MEF implementation is unchanged.

## Measurements

Measured November 17, 2026, on macOS 27.2 arm64, PowerShell 7.6.1 / .NET 10.0.7.
Both builds used SDK 10.0.400, `PSV7Release`, `net6`, and identical dependencies.
Baseline: `immybot-latest` commit `43c1fa343c2dbfc8ba9ec3b1641ab141087954d2`.
The fixed build differs only in `Engine/ScriptAnalyzer.cs`.

The normal module directory contains six managed DLLs. The crowded fixture adds
410 unrelated managed DLLs, each exporting 100 empty classes. This is a synthetic
scaling experiment, not the actual ImmyBot application directory.

Each build/layout/operation used five fresh processes. Each process records its
first call, makes five warm-up calls, then measures 30 calls. Build order alternates
between runs. Module import and fixture generation are outside the timings.
Numbers below are medians across the five processes; warm values are medians of
each process's 30 calls. Verbose inspection counts are collected separately.

| Operation / layout | Before warm (ms) | After warm (ms) | Before first call (ms) | After first call (ms) |
| --- | ---: | ---: | ---: | ---: |
| Discovery, no extra DLLs | 0.479 | 0.276 | 45.848 | 44.336 |
| Analysis, no extra DLLs | 3.080 | 1.112 | 56.245 | 63.492 |
| Discovery, 410 extra DLLs | 15.685 | 0.252 | 118.493 | 25.831 |
| Analysis, 410 extra DLLs | 15.770 | 0.363 | 144.083 | 34.432 |

Discovery is `Get-ScriptAnalyzerRule`: initialization and rule listing, without
script analysis. Analysis is a complete `Invoke-ScriptAnalyzer -ScriptDefinition
'gci' -IncludeRule PSAvoidUsingCmdletAliases` call, including initialization. This
small script and single rule isolate the discovery penalty; they do not represent
the cost of running every rule on a large script.

With 410 extra DLLs, warm discovery was 62.2x faster and the full alias-analysis
call was 43.4x faster. Individual process warm medians ranged from 12.341–15.983 ms
before versus 0.251–0.288 ms after for discovery, and 14.702–29.902 ms before versus
0.339–0.482 ms after for analysis.

| Crowded-layout metric | Before | After |
| --- | ---: | ---: |
| Assembly type inspections per call | 1,664 | 4 |
| Unrelated fixture assemblies loaded | 410 | 0 |
| Discovery allocated bytes per warm call | 3,937,252 | 190,920 |
| Analysis allocated bytes per warm call | 3,929,312 | 182,481 |
| Discovery first-call working-set increase | 28.59 MiB | 2.52 MiB |
| Analysis first-call working-set increase | 28.11 MiB | 2.34 MiB |

All 40 processes returned identical results within each operation: the same 69
built-in rule names, or the same single alias diagnostic. The deterministic
assembly counts corroborate the timing result. Working set and timings are noisy
on a shared machine; the clean-layout cold analysis did not improve. These numbers
are not an editor keystroke latency claim, do not measure PSES's debounce, and do
not establish how much of ImmyBot's historical slowness this accounts for.

## Reproduce

Build separate durable checkouts of the baseline commit and the PR head with the
same SDK and dependencies. From a directory using the selected SDK (the checkout's
`global.json` pins 6.0.403), run for each checkout:

```powershell
dotnet build "$source/Rules/Rules.csproj" -c PSV7Release -f net6 -m:1 `
-p:BuildInParallel=false -p:ImportDirectoryBuildProps=false `
-p:ImportDirectoryBuildTargets=false -p:ManagePackageVersionsCentrally=false
if ($LASTEXITCODE) { throw 'Build failed' }
```

The last three properties isolate these independent checkouts from a containing
repository's MSBuild configuration. They were identical in both measured builds.
On shared ImmyBot development machines, also apply the machine's load check and
build lock.

Prepare a module directory for each build, with `PSScriptAnalyzer.psd1`,
`PSScriptAnalyzer.psm1`, `ScriptAnalyzer.types.ps1xml`, and
`ScriptAnalyzer.format.ps1xml` from `Engine`; `Engine/Settings` as `Settings`; and
`PSCompatibilityCollector/profiles` as `compatibility_profiles`. Put these six
DLLs from `Rules/bin/PSV7Release/net6` in its `PSv7` subdirectory:

- `Microsoft.Windows.PowerShell.ScriptAnalyzer.dll`
- `Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll`
- `Microsoft.PowerShell.CrossCompatibility.dll`
- `Pluralize.NET.dll`
- `Microsoft.VisualStudio.Threading.dll`
- `Microsoft.VisualStudio.Validation.dll`

The threading dependencies are necessary for this fork even though the old
`build.ps1` copy list omits them. Use the same non-analyzer assets for both builds.
The recorded run used these assets from ImmyBot's installed private 1.21.0 package
and replaced the six DLLs with the respective source build outputs.

Run the harness from the PR checkout in PowerShell Core:

```powershell
$results = ./tools/Measure-RuleDiscovery.ps1 `
-BeforeModulePath $beforeModule -AfterModulePath $afterModule `
-Runs 5 -Iterations 30 -AdjacentAssemblyCount 410 -TypesPerAssembly 100
$results | ConvertTo-Json -Depth 5 | Set-Content $resultsPath
```

Use temporary directories for modules/results. The harness creates and removes
its own fixture directory. It emits raw samples, rule names, assembly inspection
and load counts, allocated bytes, working-set deltas, and runtime version. Each
child process fails if the expected built-in rule count or alias diagnostic changes.

## Regression validation

`Tests/Engine/RuleAssemblyDiscovery.tests.ps1` copies the real module into an
isolated directory and invokes a fresh PowerShell process for each of three cases:
an invalid DLL, an unrelated managed DLL, and a managed DLL with a missing base-type
dependency. All three fail on the baseline and pass on the fixed build. Each case
also checks built-in rule listing/diagnostics, formatting, a custom PowerShell rule
selected through settings with built-ins, and custom-only analysis.

The final focused discovery/custom-rule/settings/formatter suite passed 138 tests
with 2 skips, including all three new regression cases. The broader engine/rule
suite passed 1,276 tests with 148 skips and
10 failures, all reproduced on the unchanged baseline in the same environment:
2 attempts to write outside the sandbox, 3 help-description checks for the newer
`ProgressAction` common parameter, and 5 existing compatibility-profile assertions.
Both source builds succeeded with the same four existing warnings.

This PR does not publish a new package or change ImmyBot's package reference. Those
steps and verification in an actual editor process remain integration work.
Loading