Skip to content

Conversation

@daxian-dbw
Copy link
Member

PR Summary

For modules that target both Windows PowerShell and PowerShell Core, they are usually built against either netstandard2.0, or net462, so that one single set of assemblies can work on both .NET Framework and .NET Core.

For those modules, the API Assembly.LoadFile is usually used to load a dependency assembly into a separate load context when working in PowerShell Core to isolate the dependency, so as to avoid assembly loading conflicts with other modules. This usually happens when the dependency is commonly used by modules, such as Newtonsoft.Json and YamlDotNet.

Today, assemblies loaded by Assembly.LoadFile are discoverable by PowerShell's type resolution, and this breaks the isolation those modules are looking for when using Assembly.LoadFile. Since they are built against netstandard2.0 and net462, they cannot use the AssemblyLoadContext APIs for creating a custom load context but have to depend on Assembly.LoadFile which loads an assembly in a separate assembly load context.

This PR is a breaking change.

PR Context

Assemblies loaded by Assembly.LoadFile are made discoverable by #12203.
However, the original issues that PR was to address is just for assemblies loaded from memory by Assembly.Load(byte[]), and Assembly.LoadFile was never the ask.

PR Checklist

@pull-request-quantifier-deprecated

This PR has 17 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +11 -6
Percentile : 6.8%

Total files changed: 2

Change summary by file extension:
.cs : +4 -2
.ps1 : +7 -4

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@iSazonov iSazonov added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log labels Sep 7, 2022
@daxian-dbw
Copy link
Member Author

I'm not sure if we want to make this breaking change, given that it has been the way it works since PowerShell 6.
We can keep the existing behavior, and discourage using Assembly.LoadFile for dependency isolation. @SeeminglyScience, what's your thoughts on this?

@daxian-dbw daxian-dbw closed this Sep 7, 2022
@daxian-dbw
Copy link
Member Author

daxian-dbw commented Sep 7, 2022

I think it's better to keep the current behavior unchanged. For modules built against netstandard2.0 or net462, we can have a AssemblyLoadContextProxy type that wraps some simple reflection APIs to create a custom ALC and load an assembly by path to that ALC. See the following as an example:
https://github.com/PowerShell/platyPS/blob/8ca28935c376ae8dc36ac142e4960fc6d3b725e5/src/ModuleInitAndCleanup.cs#L74-L103

@daxian-dbw daxian-dbw deleted the load branch September 7, 2022 16:50
@iSazonov
Copy link
Collaborator

iSazonov commented Sep 8, 2022

It seems general question is - should Engine use only default ALC for PowerShell's type resolution? Maybe it makes sense to make this configurable?

@daxian-dbw
Copy link
Member Author

daxian-dbw commented Sep 8, 2022

should Engine use only default ALC for PowerShell's type resolution?

It definitely needs to resolve types from assemblies loaded by Assembly.Load(byte[]). Many users depend on it.

Even though there is no report on Assembly.LoadFile when the regression was introduced by #11088, types from assemblies loaded by Assembly.LoadFile can be resolved by PowerShell in all prior versions of PowerShell Core (and I think it's the same to Windows PowerShell, but didn't verify). I don't think it's a breaking change that we can accept, and hence close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants