Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop referencing Microsoft.PowerShell.Security when the core snapin is used #17771

Merged
merged 4 commits into from Aug 1, 2022

Conversation

daxian-dbw
Copy link
Member

@daxian-dbw daxian-dbw commented Jul 26, 2022

PR Summary

This is a follow-up change of #16355.
The strong reference to Microsoft.PowerShell.Security is not needed anymore after merging #16355.

There are whitespace only changes in the PR, so it's easier to review by ignoring the whitespaces:
https://github.com/PowerShell/PowerShell/pull/17771/files?w=1

PR Checklist

@ghost ghost assigned PaulHigin Jul 26, 2022
@daxian-dbw daxian-dbw requested a review from iSazonov July 26, 2022 04:22
@daxian-dbw daxian-dbw assigned iSazonov and unassigned PaulHigin Jul 26, 2022
@iSazonov iSazonov self-requested a review July 26, 2022 05:19
@iSazonov
Copy link
Collaborator

iSazonov commented Jul 26, 2022

It seems there are more dependences on the module :-) Easy to remove.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jul 26, 2022
@daxian-dbw
Copy link
Member Author

/cc @iSazonov @SteveL-MSFT @JamesWTruher

It turns out it's sort-of intentional to load Microsoft.PowerShell.Security.dll eagerly.
After this change (and #16355), loading of the Microsoft.PowerShell.Security module fails with the following error (see the same failure in CI):

PS:1> Import-Module Microsoft.PowerShell.Security
Import-Module: The following error occurred while loading the extended type data file:
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(19) : Error in type "System.Security.AccessControl.ObjectSecurity": The "Type" node must have "Members", "TypeConverters", or "TypeAdapters".
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(52) : Error: CodeProperty should use a getter or setter method.
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(53) : Error: Unable to find type [Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase].
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(45) : Error: CodeProperty should use a getter or setter method.
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(46) : Error: Unable to find type [Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase].
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(38) : Error: CodeProperty should use a getter or setter method.
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(39) : Error: Unable to find type [Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase].
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(31) : Error: CodeProperty should use a getter or setter method.
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(32) : Error: Unable to find type [Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase].
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(24) : Error: CodeProperty should use a getter or setter method.
, C:\arena\Modules\Microsoft.PowerShell.Security\Security.types.ps1xml(25) : Error: Unable to find type [Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase].

This is because the processing of type.ps1xml and format.ps1xml happens before loading NestedModules, and hence when the type.ps1xml references a type from the nested module, the type cannot be resolved when processing the type.ps1xml.

The use-nested-module.zip is a simple repro of this behavior. Unzip it, and run import-Module <path-to>\conflict will demonstrate the problem.

image

However, it will work fine when using RootModule = 'conflict.dll' instead of NestedModules = @('conflict.dll') in the module manifest. I believe the root module is specially handled with eager loading. (use-root-module.zip)

image

Both PowerShell 7.0 and 7.2 has the same behavior. But on Windows PowerShell, both use-nested-module and use-root-module will fail to process the type.ps1xml file.


QUESTION: Changing the loading order of nested module would be a non-trivial work which is quite risky. Given that, shall we revert #16355?

@iSazonov
Copy link
Collaborator

@daxian-dbw Your question raise more questions:

  1. Is PS team strategy to decouple built-in modules actual or it was deprecated?
  2. Should we keep circular dependencies Circular dependency at runtime between System.Management.Automation and Microsoft.PowerShell.Security #14095?
  3. Why do built-in modules use NestedModules instead of RootModule in psd1? Is it a bug?

@daxian-dbw
Copy link
Member Author

daxian-dbw commented Jul 29, 2022

A workaround is to add RequiredAssemblies = "Microsoft.PowerShell.Security.dll" in addition to the NestedModules. Required assemblies are always loaded before processing type/format files, and this allows the module to remain a manifest module.

I have pushed a commit with that change. Although it looks weirdly redundant, I think it should be acceptable.


> Why do built-in modules use NestedModules instead of RootModule in psd1? Is it a bug?

I don't think it's a bug or oversight. My guess is:

  1. Declaring the security.dll as a root module doesn't solve the problem in Widows PowerShell, as I mentioned above:

    But on Windows PowerShell, both use-nested-module and use-root-module will fail to process the type.ps1xml file.

  2. Back in Windows PowerShell time, some built-in modules contain both .psm1 and .dll nested modules, and making the parent module a manifest module makes perfect sense.

NestedModules = "Microsoft.PowerShell.Security.dll"
# 'Security.types.ps1xml' refers to types from 'Microsoft.PowerShell.Security.dll' and thus requiring to load the assembly before processing the type file.
# We declare 'Microsoft.PowerShell.Security.dll' in 'RequiredAssemblies' so as to make sure it's loaded before the type file processing.
RequiredAssemblies = "Microsoft.PowerShell.Security.dll"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Copy link
Member

@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The workaround with RequiredAssemblies is low risk and I think this is a change we should have to decouple them

@daxian-dbw daxian-dbw merged commit 2051c5f into PowerShell:master Aug 1, 2022
@daxian-dbw daxian-dbw deleted the security branch August 1, 2022 17:11
@daxian-dbw daxian-dbw assigned daxian-dbw and unassigned iSazonov Aug 1, 2022
@ghost
Copy link

ghost commented Aug 12, 2022

🎉v7.3.0-preview.7 has been released which incorporates this pull request.:tada:

Handy links:

@TravisEz13 TravisEz13 mentioned this pull request Sep 30, 2022
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants