Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,8 @@ protected override void EndProcessing()
// These dictionaries prevent reloading already loaded and unchanged code.
// We don't worry about unbounded growing of the cache because in .Net Core 2.0 we can not unload assemblies.
// TODO: review if we will be able to unload assemblies after migrating to .Net Core 2.1.
private static readonly HashSet<string> s_sourceTypesCache = new();
private static readonly Dictionary<int, Assembly> s_sourceAssemblyCache = new();
private static readonly ConcurrentDictionary<string, object> s_sourceTypesCache = new();
private static readonly ConcurrentDictionary<int, Assembly> s_sourceAssemblyCache = new();

private static readonly string s_defaultSdkDirectory = Utils.DefaultPowerShellAppBase;

Expand Down Expand Up @@ -963,7 +963,7 @@ private CompilationOptions GetDefaultCompilationOptions()
}
}

private bool isSourceCodeUpdated(List<SyntaxTree> syntaxTrees, out Assembly assembly)
private bool IsSourceCodeUpdated(List<SyntaxTree> syntaxTrees, out Assembly assembly)
{
Diagnostics.Assert(syntaxTrees.Count != 0, "syntaxTrees should contains a source code.");

Expand Down Expand Up @@ -1048,7 +1048,7 @@ private void SourceCodeProcessing()
{
// if the source code was already compiled and loaded and not changed
// we get the assembly from the cache.
if (isSourceCodeUpdated(syntaxTrees, out Assembly assembly))
if (IsSourceCodeUpdated(syntaxTrees, out Assembly assembly))
{
CompileToAssembly(syntaxTrees, compilationOptions, emitOptions);
}
Expand Down Expand Up @@ -1138,7 +1138,7 @@ public override void VisitNamedType(INamedTypeSymbol symbol)
// It is namespace-fully-qualified name
var symbolFullName = symbol.ToString();

if (s_sourceTypesCache.TryGetValue(symbolFullName, out _))
if (s_sourceTypesCache.ContainsKey(symbolFullName))
{
DuplicateSymbols.Add(symbolFullName);
}
Expand All @@ -1153,13 +1153,13 @@ private static void CacheNewTypes(ConcurrentBag<string> newTypes)
{
foreach (var typeName in newTypes)
{
s_sourceTypesCache.Add(typeName);
s_sourceTypesCache.TryAdd(typeName, null);
}
}

private void CacheAssembly(Assembly assembly)
{
s_sourceAssemblyCache.Add(_syntaxTreesHash, assembly);
s_sourceAssemblyCache.TryAdd(_syntaxTreesHash, assembly);
}

private void DoEmitAndLoadAssembly(Compilation compilation, EmitOptions emitOptions)
Expand All @@ -1178,8 +1178,6 @@ private void DoEmitAndLoadAssembly(Compilation compilation, EmitOptions emitOpti

if (emitResult.Success)
{
// TODO: We could use Assembly.LoadFromStream() in future.
// See https://github.com/dotnet/corefx/issues/26994
ms.Seek(0, SeekOrigin.Begin);
Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(ms);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,16 @@ public class AttributeTest$guid : PSCmdlet
param($outputType)
{ Add-Type -TypeDefinition "Hello" -OutputType $outputType } | Should -Throw -ErrorId 'AssemblyTypeNotSupported,Microsoft.PowerShell.Commands.AddTypeCommand'
}

It "Can run with the same C# code simultaneously from multiple Runspaces" {
$script = {
$source = 'public class BasicTest {}'
1..10 | ForEach-Object -ThrottleLimit 10 -Parallel {
Add-Type -TypeDefinition $using:source
}
}

pwsh -noprofile -command $script.ToString()
$LASTEXITCODE | Should -Be 0
}
}