GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Block type update in Add-Type cmdlet · PowerShell/PowerShell@c7f7825

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/Microsoft.PowerShell.Commands.Utility/commands/utility
    • AddType.cs
  • test/powershell/Modules/Microsoft.PowerShell.Utility
    • Add-Type.Tests.ps1
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs‎

Copy file name to clipboardExpand all lines: src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs
+5-15Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ protected override void EndProcessing()
625625
// These dictionaries prevent reloading already loaded and unchanged code.
626626
// We don't worry about unbounded growing of the cache because in .Net Core 2.0 we can not unload assemblies.
627627
// TODO: review if we will be able to unload assemblies after migrating to .Net Core 2.1.
628-
private static readonly Dictionary<string, int> s_sourceTypesCache = new Dictionary<string, int>();
628+
private static readonly HashSet<string> s_sourceTypesCache = new HashSet<string>();
629629
private static readonly Dictionary<int, Assembly> s_sourceAssemblyCache = new Dictionary<int, Assembly>();
630630

631631
private static readonly string s_defaultSdkDirectory = Utils.DefaultPowerShellAppBase;
@@ -1052,7 +1052,7 @@ private void CompileToAssembly(List<SyntaxTree> syntaxTrees, CompilationOptions
10521052

10531053
private void CheckDuplicateTypes(Compilation compilation, out ConcurrentBag<string> newTypes)
10541054
{
1055-
AllNamedTypeSymbolsVisitor visitor = new AllNamedTypeSymbolsVisitor(_syntaxTreesHash);
1055+
AllNamedTypeSymbolsVisitor visitor = new AllNamedTypeSymbolsVisitor();
10561056
visitor.Visit(compilation.Assembly.GlobalNamespace);
10571057

10581058
foreach (var symbolName in visitor.DuplicateSymbols)
@@ -1084,16 +1084,9 @@ private void CheckDuplicateTypes(Compilation compilation, out ConcurrentBag<stri
10841084
// Visit symbols in all namespaces and collect duplicates.
10851085
private class AllNamedTypeSymbolsVisitor : SymbolVisitor
10861086
{
1087-
private readonly int _hash;
1088-
10891087
public readonly ConcurrentBag<string> DuplicateSymbols = new ConcurrentBag<string>();
10901088
public readonly ConcurrentBag<string> UniqueSymbols = new ConcurrentBag<string>();
10911089

1092-
public AllNamedTypeSymbolsVisitor(int hash)
1093-
{
1094-
_hash = hash;
1095-
}
1096-
10971090
public override void VisitNamespace(INamespaceSymbol symbol)
10981091
{
10991092
// Main cycle.
@@ -1109,12 +1102,9 @@ public override void VisitNamedType(INamedTypeSymbol symbol)
11091102
// It is namespace-fully-qualified name
11101103
var symbolFullName = symbol.ToString();
11111104

1112-
if (s_sourceTypesCache.TryGetValue(symbolFullName, out int hash))
1105+
if (s_sourceTypesCache.TryGetValue(symbolFullName, out _))
11131106
{
1114-
if (hash == _hash)
1115-
{
1116-
DuplicateSymbols.Add(symbolFullName);
1117-
}
1107+
DuplicateSymbols.Add(symbolFullName);
11181108
}
11191109
else
11201110
{
@@ -1127,7 +1117,7 @@ private void CacheNewTypes(ConcurrentBag<string> newTypes)
11271117
{
11281118
foreach (var typeName in newTypes)
11291119
{
1130-
s_sourceTypesCache.Add(typeName, _syntaxTreesHash);
1120+
s_sourceTypesCache.Add(typeName);
11311121
}
11321122
}
11331123

Collapse file

‎test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1‎

Copy file name to clipboardExpand all lines: test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Describe "Add-Type" -Tags "CI" {
6565
$code = @"
6666
using System.Management.Automation;
6767
[System.Management.Automation.Cmdlet("Get", "Thing$guid", ConfirmImpact = System.Management.Automation.ConfirmImpact.High, SupportsPaging = true)]
68-
public class AttributeTest$guid : PSCmdlet
68+
public class SMAAttributeTest$guid : PSCmdlet
6969
{
7070
protected override void EndProcessing()
7171
@@ -207,6 +207,12 @@ public class AttributeTest$guid : PSCmdlet
207207
{ Add-Type -CompilerOptions "/platform:anycpuERROR" -Language CSharp -MemberDefinition "public static string TestString() { return ""}" -Name "TestType1" -Namespace "TestNS" -ErrorAction Stop } | Should -Throw -ErrorId "SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand"
208208
}
209209

210+
It "Throw if the type already exists" {
211+
Add-Type -TypeDefinition "public class Foo$guid {}"
212+
{ Add-Type -TypeDefinition "public class Foo$guid { public int Bar {get {return 42;} }" -ErrorAction SilentlyContinue } | Should -Throw -ErrorId "COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand"
213+
$error[1].FullyQualifiedErrorId | Should -BeExactly "TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand"
214+
}
215+
210216
It "OutputType parameter requires that the OutputAssembly parameter be specified." {
211217
$code = "public static string TestString() {}"
212218
{ Add-Type -TypeDefinition $code -OutputType Library } | Should -Throw -ErrorId "OUTPUTTYPE_REQUIRES_ASSEMBLY,Microsoft.PowerShell.Commands.AddTypeCommand"

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.