@@ -31,6 +31,7 @@ public class Helper
31
31
32
32
private readonly Lazy < CommandInfoCache > _commandInfoCacheLazy ;
33
33
private readonly RunspacePool _runSpacePool ;
34
+ private readonly object _testModuleManifestLock = new object ( ) ;
34
35
35
36
#endregion
36
37
@@ -303,35 +304,39 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
303
304
errorRecord = null ;
304
305
PSModuleInfo psModuleInfo = null ;
305
306
Collection < PSObject > psObj = null ;
306
- using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
307
+ // Test-ModuleManifest is not thread safe
308
+ lock ( _testModuleManifestLock )
307
309
{
308
- ps . RunspacePool = _runSpacePool ;
309
- ps . AddCommand ( "Test-ModuleManifest" )
310
- . AddParameter ( "Path" , filePath )
311
- . AddParameter ( "WarningAction" , ActionPreference . SilentlyContinue ) ;
312
- try
310
+ using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
313
311
{
314
- psObj = ps . Invoke ( ) ;
315
- }
316
- catch ( CmdletInvocationException e )
317
- {
318
- // Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
319
- // throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
320
- // we consume it to allow execution of the of this method.
321
- if ( e . InnerException == null || e . InnerException . GetType ( ) != typeof ( System . NullReferenceException ) )
312
+ ps . RunspacePool = _runSpacePool ;
313
+ ps . AddCommand ( "Test-ModuleManifest" )
314
+ . AddParameter ( "Path" , filePath )
315
+ . AddParameter ( "WarningAction" , ActionPreference . SilentlyContinue ) ;
316
+ try
322
317
{
323
- throw ;
318
+ psObj = ps . Invoke ( ) ;
319
+ }
320
+ catch ( CmdletInvocationException e )
321
+ {
322
+ // Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
323
+ // throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
324
+ // we consume it to allow execution of the of this method.
325
+ if ( e . InnerException == null || e . InnerException . GetType ( ) != typeof ( System . NullReferenceException ) )
326
+ {
327
+ throw ;
328
+ }
329
+ }
330
+ if ( ps . HadErrors && ps . Streams != null && ps . Streams . Error != null )
331
+ {
332
+ var errorRecordArr = new ErrorRecord [ ps . Streams . Error . Count ] ;
333
+ ps . Streams . Error . CopyTo ( errorRecordArr , 0 ) ;
334
+ errorRecord = errorRecordArr ;
335
+ }
336
+ if ( psObj != null && psObj . Any ( ) && psObj [ 0 ] != null )
337
+ {
338
+ psModuleInfo = psObj [ 0 ] . ImmediateBaseObject as PSModuleInfo ;
324
339
}
325
- }
326
- if ( ps . HadErrors && ps . Streams != null && ps . Streams . Error != null )
327
- {
328
- var errorRecordArr = new ErrorRecord [ ps . Streams . Error . Count ] ;
329
- ps . Streams . Error . CopyTo ( errorRecordArr , 0 ) ;
330
- errorRecord = errorRecordArr ;
331
- }
332
- if ( psObj != null && psObj . Any ( ) && psObj [ 0 ] != null )
333
- {
334
- psModuleInfo = psObj [ 0 ] . ImmediateBaseObject as PSModuleInfo ;
335
340
}
336
341
}
337
342
return psModuleInfo ;
0 commit comments