Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
944cc45
Add issue 2205 regression test
Copilot Aug 19, 2026
d7d278c
Correct issue 2205 regression assertion
Copilot Aug 19, 2026
b8e7410
Add recursive issue 2205 regression scenario
Copilot Aug 19, 2026
3d791b2
Isolate issue 2205 regression fixture
Copilot Aug 19, 2026
3326269
Add issue 2205 failure regression
Copilot Aug 19, 2026
c51a892
Isolate issue 2205 failing regression
Copilot Aug 19, 2026
39b5356
Do not fail analysis on transient command lookup failures (issue 2205)
Copilot Aug 19, 2026
bc604c9
Address code review: evict only the faulted cache entry, clarify test…
Copilot Aug 19, 2026
9734b62
Merge pull request #1 from jessehouwing/copilot/create-unit-test-for-…
jessehouwing Aug 19, 2026
476e1a3
Serialize command info lookups on a single dedicated runspace
Copilot Aug 19, 2026
c4321d8
Make concurrency test safe against Helper singleton initialization order
Copilot Aug 19, 2026
ae52d7c
Address review: take the runspace lock on both dispose paths
Copilot Aug 19, 2026
469fd0a
Merge pull request #2 from jessehouwing/copilot/marshal-concurrent-ca…
jessehouwing Aug 19, 2026
8a75d6f
Merge branch 'PowerShell:main' into main
jessehouwing Sep 14, 2026
af4cb17
Add isolated cold and warm ScriptAnalyzer benchmark workflow
Copilot Sep 14, 2026
acd3f00
Expand performance comparison to Linux and semver repository workload
Copilot Sep 14, 2026
7a233e4
Preserve full PowerShell source tree in performance benchmarks
Copilot Sep 14, 2026
ac6dab8
Pin download-artifact to patched v4.3.0 commit
Copilot Sep 14, 2026
d82000d
Merge pull request #4 from jessehouwing/copilot/add-workflow-dispatch…
jessehouwing Sep 14, 2026
ada251b
Initial plan
Copilot Sep 14, 2026
36f9aef
Harden command lookup against Get-Command resolution failures
Copilot Sep 14, 2026
b13bae1
Avoid runspace races when resolving exported function parameters
Copilot Sep 14, 2026
e1ea2fb
Merge pull request #5 from jessehouwing/copilot/fix-benchmark-upstrea…
jessehouwing Sep 14, 2026
936b71f
Merge remote-tracking branch 'origin/main' into copilot/fix-benchmark…
Copilot Sep 14, 2026
1a928b6
Serialize command metadata reads with runspace command lookups
Copilot Sep 14, 2026
9ac604a
Merge pull request #7 from jessehouwing/copilot/fix-benchmark-fork-se…
jessehouwing Sep 14, 2026
b3d23b2
Record three-way benchmark requirement
Copilot Sep 14, 2026
ffdfa6f
Merge branch 'main' into main
bergmeister Sep 14, 2026
4125239
Merge branch 'main' into main
bergmeister Sep 14, 2026
37e07fd
Optimize synchronized metadata and add three-way performance benchmarks
Copilot Sep 14, 2026
aacafd8
Disable telemetry collection in performance comparisons
Copilot Sep 14, 2026
8cfecbf
Add consolidated performance matrix summary job
Copilot Sep 14, 2026
7b22321
Merge pull request #9 from jessehouwing/copilot/perf-additional-metri…
jessehouwing Sep 14, 2026
0d11274
Make PowerShell engine retries selectable and observable
Copilot Sep 14, 2026
028c67c
Fix spurious retries for unknown commands and verify retry modes
Copilot Sep 14, 2026
078fc8b
Centralize command metadata recovery and remove rule-level catches
Copilot Sep 14, 2026
7976235
Preserve negative caching while evicting exhausted lookup failures
Copilot Sep 14, 2026
c440009
Merge pull request #10 from jessehouwing/copilot/add-if-else-statements
jessehouwing Sep 14, 2026
1b37795
Update perf workflow for retry ifdef variants
Copilot Sep 14, 2026
95c089c
Merge pull request #11 from jessehouwing/copilot/add-perf-workflow-retry
jessehouwing Sep 14, 2026
ecebce4
Merge branch 'PowerShell:main' into main
jessehouwing Sep 14, 2026
f819022
Restore full benchmark matrix and distinct retry build variants
Copilot Sep 14, 2026
beeb3f9
Merge pull request #12 from jessehouwing/copilot/add-benchmark-compar…
jessehouwing Sep 14, 2026
c8e59b0
Remove engine retries and performance telemetry
jessehouwing Sep 14, 2026
d5e87f6
Remove perf branch from performance benchmark
jessehouwing Sep 14, 2026
7d4a11f
Merge pull request #13 from jessehouwing/perf
jessehouwing Sep 14, 2026
b8472db
Use C# 5 compatible syntax in Add-Type test fixtures
jessehouwing Sep 15, 2026
4d3b644
Skip the Get- prefix probe for command names containing a hyphen
jessehouwing Sep 17, 2026
c9e460d
Resolve commands the analyzed script defines against the script
jessehouwing Sep 17, 2026
6129442
Do not resolve member names as commands in UseShouldProcessCorrectly
jessehouwing Sep 17, 2026
b695606
Use a cross-platform command in the method-versus-command tests
jessehouwing Sep 17, 2026
a18d689
Call ShouldProcess in the fixture that declares support for it
jessehouwing Sep 17, 2026
2a36c98
Resolve commands across the files of a dot-sourced group
jessehouwing Sep 17, 2026
94ba0cf
Remove the command lookup instrumentation counters
jessehouwing Sep 17, 2026
6fcf7d5
Collect before each timed run in the performance harness
jessehouwing Sep 17, 2026
09f4323
Spend the tiering ramp before the warm measurement
jessehouwing Sep 17, 2026
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
436 changes: 436 additions & 0 deletions .github/workflows/performance.yml

Large diffs are not rendered by default.

314 changes: 271 additions & 43 deletions Engine/CommandInfoCache.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Engine/CommandLookupKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override int GetHashCode()
unchecked
{
int hash = 17;
hash = hash * 31 + Name.ToUpperInvariant().GetHashCode();
hash = hash * 31 + StringComparer.OrdinalIgnoreCase.GetHashCode(Name);
hash = hash * 31 + CommandTypes.GetHashCode();
return hash;
}
Expand Down
24 changes: 24 additions & 0 deletions Engine/CommandParameterSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;

namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
{
/// <summary>Detached parameter facts, safe to consume without driving a PowerShell runspace.</summary>
public sealed class CommandParameterSnapshot
{
public string Name { get; }
public bool SwitchParameter { get; }
public ReadOnlyCollection<string> Aliases { get; }

internal CommandParameterSnapshot(ParameterMetadata parameter)
{
Name = parameter.Name;
SwitchParameter = parameter.SwitchParameter;
Aliases = new ReadOnlyCollection<string>(parameter.Aliases.ToArray());
}
}
}
252 changes: 252 additions & 0 deletions Engine/DotSourceScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation.Language;

namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
{
/// <summary>
/// Groups the files of one analysis run by dot-source relationship and reports, per file, the
/// functions that are in scope for it.
/// </summary>
/// <remarks>
/// Files that a common root dot-sources share a session state at run time, so a call from one to a
/// function defined in another binds locally. The relationship is not visible from the calling file:
/// a module's lib files call each other without dot-sourcing, and are only in scope together because
/// the .psm1 sourced them all. Membership is therefore resolved over the whole run.
/// </remarks>
internal sealed class DotSourceScope
{
private readonly Dictionary<string, HashSet<string>> namesByFile;
private readonly Dictionary<string, ParsedFile> parsedByFile;

private DotSourceScope(
Dictionary<string, HashSet<string>> namesByFile,
Dictionary<string, ParsedFile> parsedByFile)
{
this.namesByFile = namesByFile;
this.parsedByFile = parsedByFile;
}

private struct ParsedFile
{
internal ScriptBlockAst Ast;
internal Token[] Tokens;
internal ParseError[] Errors;
}

internal IEnumerable<string> GetFunctionsInScope(string filePath)
{
if (filePath != null && namesByFile.TryGetValue(Normalize(filePath), out var names))
{
return names;
}
return Array.Empty<string>();
}

/// <summary>
/// Hands over the syntax tree this scope already produced for a file, so that the file is parsed
/// once per run rather than once here and again when it is analyzed. The entry is dropped on the
/// way out, which keeps the trees alive only until the files that own them have been analyzed.
/// </summary>
internal bool TryTakeParse(string filePath, out ScriptBlockAst ast, out Token[] tokens, out ParseError[] errors)
{
if (filePath != null && parsedByFile.TryGetValue(Normalize(filePath), out var parsed))
{
parsedByFile.Remove(Normalize(filePath));
ast = parsed.Ast;
tokens = parsed.Tokens;
errors = parsed.Errors;
return ast != null;
}

ast = null;
tokens = null;
errors = null;
return false;
}

/// <param name="retainParses">
/// Whether to keep the syntax trees for <see cref="TryTakeParse"/> to hand back. Only sound when the
/// files cannot change between this pre-pass and the analysis; -Fix rewrites them, so it opts out.
/// </param>
internal static DotSourceScope Build(IReadOnlyList<string> filePaths, bool retainParses = true)
{
var known = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var path in filePaths)
{
known.Add(Normalize(path));
}

var definitions = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
var parsed = new Dictionary<string, ParsedFile>(StringComparer.OrdinalIgnoreCase);
var dotSourced = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);

foreach (var path in filePaths)
{
var normalized = Normalize(path);
var functions = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
definitions[normalized] = functions;

ScriptBlockAst ast;
Token[] tokens;
ParseError[] errors;
try
{
ast = Parser.ParseFile(path, out tokens, out errors);
}
catch
{
continue;
}
if (ast == null)
{
continue;
}

if (retainParses)
{
parsed[normalized] = new ParsedFile { Ast = ast, Tokens = tokens, Errors = errors };
}

foreach (FunctionDefinitionAst function in ast.FindAll(node => node is FunctionDefinitionAst, true))
{
if (!string.IsNullOrWhiteSpace(function.Name))
{
functions.Add(function.Name);
}
}

foreach (CommandAst command in ast.FindAll(node => node is CommandAst, true))
{
var target = ResolveDotSourceTarget(command, path);
if (target != null && known.Contains(target))
{
if (!dotSourced.TryGetValue(normalized, out var targets))
{
targets = new List<string>();
dotSourced[normalized] = targets;
}
targets.Add(target);
}
}
}

// A file sees the functions of everything it dot-sources, transitively, and also those of the
// files an ancestor sourced alongside it: at run time they all land in that ancestor's session
// state. So a file's scope is the union of the closures of the entry points that reach it.
// Merging symmetrically instead would be coarser than the language: a test script that sources
// one library would be credited with every other library that shares a root.
var namesByFile = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
foreach (var file in definitions.Keys)
{
namesByFile[file] = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}

foreach (var entryPoint in definitions.Keys)
{
var closure = Closure(entryPoint, dotSourced);
var names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var member in closure)
{
if (definitions.TryGetValue(member, out var defined))
{
names.UnionWith(defined);
}
}

foreach (var member in closure)
{
if (namesByFile.TryGetValue(member, out var scope))
{
scope.UnionWith(names);
}
}
}

return new DotSourceScope(namesByFile, parsed);
}

/// <summary>
/// Returns <paramref name="start"/> together with everything it dot-sources, directly or indirectly.
/// </summary>
private static List<string> Closure(string start, Dictionary<string, List<string>> dotSourced)
{
var reached = new List<string> { start };
var visited = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { start };
var pending = new Stack<string>();
pending.Push(start);

while (pending.Count > 0)
{
if (!dotSourced.TryGetValue(pending.Pop(), out var targets))
{
continue;
}

foreach (var target in targets)
{
// The visited set also stops a dot-source cycle from looping forever.
if (visited.Add(target))
{
reached.Add(target);
pending.Push(target);
}
}
}

return reached;
}

private static string ResolveDotSourceTarget(CommandAst command, string containingFile)
{
if (command.InvocationOperator != TokenKind.Dot || command.CommandElements.Count == 0)
{
return null;
}

string raw = null;
switch (command.CommandElements[0])
{
case StringConstantExpressionAst constant: raw = constant.Value; break;
case ExpandableStringExpressionAst expandable: raw = expandable.Value; break;
}
if (string.IsNullOrWhiteSpace(raw))
{
return null;
}

var directory = Path.GetDirectoryName(Path.GetFullPath(containingFile));
// The only variables worth expanding are the ones that name the script's own location.
raw = raw.Replace("$PSScriptRoot", directory).Replace("${PSScriptRoot}", directory);
if (raw.IndexOf('$') >= 0)
{
return null;
}

try
{
return Normalize(Path.IsPathRooted(raw) ? raw : Path.Combine(directory, raw));
}
catch
{
return null;
}
}

private static string Normalize(string path)
{
try
{
return Path.GetFullPath(path);
}
catch
{
return path;
}
}
}
}
74 changes: 65 additions & 9 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
return psModuleInfo;
}

/// <summary>Shares validation between built-in rules during one syntax-tree analysis only.</summary>
public PSModuleInfo GetModuleManifestForAnalysis(string filePath, out IEnumerable<ErrorRecord> errorRecords)
{
var cache = ModuleManifestAnalysisCache.Current;
return cache == null
? GetModuleManifest(filePath, out errorRecords)
: cache.Get(this, filePath, out errorRecords);
}

/// <summary>
/// Checks if the error record is MissingMemberException
/// </summary>
Expand Down Expand Up @@ -399,18 +408,22 @@ public HashSet<string> GetExportedFunction(Ast ast)
List<string> exportFunctionsCmdlet = Helper.Instance.CmdletNameAndAliases("export-modulemember");

// find functions exported
IEnumerable<Ast> cmdAsts = ast.FindAll(item => item is CommandAst
&& exportFunctionsCmdlet.Contains((item as CommandAst).GetCommandName(), StringComparer.OrdinalIgnoreCase), true);

CommandInfo exportMM = Helper.Instance.GetCommandInfo("export-modulemember", CommandTypes.Cmdlet);

// switch parameters
IEnumerable<ParameterMetadata> switchParams = (exportMM != null) ? exportMM.Parameters.Values.Where<ParameterMetadata>(pm => pm.SwitchParameter) : Enumerable.Empty<ParameterMetadata>();
var cmdAsts = ast.FindAll(item => item is CommandAst
&& exportFunctionsCmdlet.Contains((item as CommandAst).GetCommandName(), StringComparer.OrdinalIgnoreCase), true).ToArray();
if (cmdAsts.Length == 0)
{
return exportedFunctions;
}

if (exportMM == null)
// Export-ModuleMember has no dynamic parameters. Resolve names from its static
// metadata instead of ResolveParameter(), which re-enters the cached command's
// runspace and races with command lookups and metadata queries on other rule threads.
var parameters = GetCommandParameterSnapshot("export-modulemember", CommandTypes.Cmdlet);
if (parameters == null)
{
return exportedFunctions;
}
var switchParams = parameters.Values.Where(pm => pm.SwitchParameter);

foreach (CommandAst cmdAst in cmdAsts)
{
Expand All @@ -429,7 +442,20 @@ public HashSet<string> GetExportedFunction(Ast ast)
if (ceAst is CommandParameterAst)
{
var paramAst = ceAst as CommandParameterAst;
var param = exportMM.ResolveParameter(paramAst.ParameterName);
CommandParameterSnapshot param;
if (!parameters.TryGetValue(paramAst.ParameterName, out param))
{
param = parameters.Values.FirstOrDefault(pm =>
pm.Aliases.Contains(paramAst.ParameterName, StringComparer.OrdinalIgnoreCase));
if (param == null)
{
var matches = parameters.Values.Where(pm =>
pm.Name.StartsWith(paramAst.ParameterName, StringComparison.OrdinalIgnoreCase)
|| pm.Aliases.Any(alias => alias.StartsWith(paramAst.ParameterName, StringComparison.OrdinalIgnoreCase)))
.Take(2).ToArray();
param = matches.Length == 1 ? matches[0] : null;
}
}

if (param == null)
{
Expand Down Expand Up @@ -671,6 +697,36 @@ public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null,
return CommandInfoCache.GetCommandInfo(name, commandTypes: commandType, bypassCache: bypassCache);
}

/// <summary>
/// Retrieves command parameters with centralized recovery; returns null when metadata is unavailable.
/// </summary>
public Dictionary<string, ParameterMetadata> GetCommandParameters(
string name, CommandTypes? commandType = null, bool bypassCache = false)
{
return CommandInfoCache.GetCommandParameters(name, commandType, bypassCache);
}

/// <summary>
/// Retrieves command parameter sets with centralized recovery; returns null when metadata is unavailable.
/// </summary>
public ReadOnlyCollection<CommandParameterSetInfo> GetCommandParameterSets(string name)
{
return CommandInfoCache.GetCommandParameterSets(name);
}

/// <summary>Gets detached parameter facts or null when unavailable; only static cmdlet metadata is cached.</summary>
public IReadOnlyDictionary<string, CommandParameterSnapshot> GetCommandParameterSnapshot(
string name, CommandTypes? commandType = null, bool bypassCache = false)
{
return CommandInfoCache.GetParameterSnapshot(name, commandType, bypassCache);
}

/// <summary>Gets the mandatory parameter summary under a single runspace lock, or null when unavailable.</summary>
public IReadOnlyList<string> GetMandatoryParameterNames(string name)
{
return CommandInfoCache.GetMandatoryParameterNames(name);
}

/// <summary>
/// Returns the get, set and test targetresource dsc function
/// </summary>
Expand Down
Loading