Repository navigation
Description
Problem statement:
- I would like to use a self-contained version of the Parser in an application that uses the full .Net Framework (4.7.2), however, due to the
System.Management.AutomationNuGet package being compiled and published againstnetcoreapp2.1, I cannot use it in full .Net, even when trying to use it in a .Net Standard project and publishing a self-contained version of it. I would like to have a compiled version of it to be able to run it in full .Net, even limited functionality (i.e. only having the Parser working but not being able to host a runtime) would be OK for my scenario.
Would it be feasible (with reasonable effort) and would a PR be accepted to allow compilation and publication against netstandard2.0? Or is there a better way of using and shipping a self-contained version of the Parser in full .net, i.e. to not having to depend on the installed version of PowerShell? Steve Lee (@SteveL-MSFT)
As far as I can see the dependent project Microsoft.PowerShell.CoreCLR.Eventing would need to be changed to target netstandard2.0 as well. A quick experiment shows that most compilation errors can be resolved by adding the System.Reflection.Emit, System.Reflection.Emit.Lightweight, System.Memory, System.Collections.Immutable and System.Runtime.Loader NuGet packages to SMA, the remaining compilation errors that would need to be resolved are only about the EnumerationOptions type not being available in .Net Standard and seemed to be not too difficult to handle via conditional compilation to use an overload of Directory.GetDirectories without EnumerationOptions the (and by looking at the history, those new API calls were only recently added by Dongbo Wang (@daxian-dbw) as an optimisation)
D:\PowerShell\src\System.Management.Automation\engine\Modules\ModuleUtils.cs(17,43): error CS0234: The type or namespace name 'EnumerationOptions' does not exist in the namespace 'System.IO' (are you missing an assembly reference?) [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\Modules\ModuleUtils.cs(24,43): error CS0234: The type or namespace name 'EnumerationOptions' does not exist in the namespace 'System.IO' (are you missing an assembly reference?) [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
After resolving them, I suddenly got a lot more compilation errors, some I can resolve, but some about dynamics are a bit tricky and don't have a blame history other than the initial commit (not even in the source depot branch) to figure out how code was converted from full .net to netcore as a hint on how to make it netstandard compliant.
D:\PowerShell\src\System.Management.Automation\engine\ExperimentalFeature\GetExperimentalFeatureCommand.cs(103,50): error CS1061: 'Dictionar
Add' and no extension method 'TryAdd' accepting a first argument of type 'Dictionary<string, ExperimentalFeature>' could be found (are you m
\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\Modules\ModuleSpecification.cs(314,20): error CS0103: The name 'HashCode' does not exi
ion\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\Modules\ModuleCmdletBase.cs(2407,37): error CS1503: Argument 1: cannot convert from 'c
m.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\Modules\ImportModuleCommand.cs(643,53): error CS1503: Argument 1: cannot convert from
tem.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\namespaces\EnvironmentProvider.cs(201,36): error CS1061: 'Dictionary<string, DictionaryEntry>
TryAdd' accepting a first argument of type 'Dictionary<string, DictionaryEntry>' could be found (are you missing a using directive or an ass
em.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(431,42): error CS0656: Missing compiler requ
[D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\parser\PSType.cs(1237,56): error CS1061: 'TypeBuilder' does not contain a definition f
argument of type 'TypeBuilder' could be found (are you missing a using directive or an assembly reference?) [D:\PowerShell\src\System.Manag
D:\PowerShell\src\System.Management.Automation\engine\parser\PSType.cs(1240,75): error CS1061: 'TypeBuilder' does not contain a definition f
argument of type 'TypeBuilder' could be found (are you missing a using directive or an assembly reference?) [D:\PowerShell\src\System.Manag
D:\PowerShell\src\System.Management.Automation\engine\DscResourceSearcher.cs(136,25): error CS0656: Missing compiler required member 'Micros
\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(2871,46): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(2927,42): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(3018,42): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(3150,37): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(3227,38): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(3275,42): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(3326,46): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(3405,38): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(3463,46): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(3526,38): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(4107,40): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\CommandCompletion\CompletionCompleters.cs(4623,32): error CS0656: Missing compiler req
' [D:\PowerShell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\PSVersionInfo.cs(647,34): error CS1503: Argument 1: cannot convert from 'char' to 'str
.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\PSVersionInfo.cs(647,59): error CS1503: Argument 1: cannot convert from 'char' to 'str
.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\PSVersionInfo.cs(647,84): error CS1503: Argument 1: cannot convert from 'char' to 'str
.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\runtime\Operations\MiscOps.cs(2707,29): error CS0656: Missing compiler required member
hell\src\System.Management.Automation\System.Management.Automation.csproj]
D:\PowerShell\src\System.Management.Automation\engine\runtime\Operations\MiscOps.cs(3017,32): error CS0656: Missing compiler required member
hell\src\System.Management.Automation\System.Management.Automation.csproj]
Is it easier to try to make it compile it directly net472?
Activity
SteveL-MSFT commentedon Jul 30, 2018
The use case is valid, but currently it's probably not trivial to get SMA.dll and dependent assemblies to build targeting .Net Std 2.0. We should eventually see what that would take and add that as a supported runtime in the SMA nupkg. For now, unless you want to complete this work and submit as a PR, it would be easier to multi-target net472 and netcoreapp21 (assuming you want it to work in PSCore6.1).
iSazonov commentedon Jul 31, 2018
System.Reflection.EmitandSystem.IO.Enumerationwill in .Net Standard 2.1https://github.com/dotnet/standard/tree/master/docs/planning/netstandard-vnext#major-features
We could wait .Net Standard 2.1 - with it the solution will be easier.
bergmeister commentedon Jul 31, 2018
I tried compiling against net461 today (net472 was more difficult due to missing APIs), some code inside
#if !CORECLRseems to not work any more due missing implementations, likePSSnapin(yes, I know, they are not supported any more), orInternalMISerializer,AdapterCodeMethods, etc., so I just commented some of that code out for a proof of concept (the work is here).After some work I am now left with only 1 compilation error, which is due to the missing implementation of
PowerShellAssemblyLoadContextin theClrFacadehere and I could not find an implementation of it in the code base for full .net (I also looked in the source depot branch for it). Any help/pointers would be appreciated.SteveL-MSFT commentedon Jul 31, 2018
The
PowerShellAssemblyLoadContextcode was originally wrapped by#if CORECLRpragmas as it wasn't needed by .NET Framework.iSazonov commentedon Aug 1, 2018
Christoph Bergmeister (@bergmeister) I guess you want to get only AST tree, do not you? In the case we could try to separate the Parser code and don't compile whole SMA.
bergmeister commentedon Aug 1, 2018
Ilya (@iSazonov) Yes, just having the Parser only would be sufficient and I already tried briefly if I could extract that code only but it did not seem to be much easier (due to coupling with security and runtime classes) unless you have some specific ideas on how to approach it.
But being able to compile SMA against full .net or .net standard would be more beneficial to the project itself I guess (because someone else could take it further afterwards and compile also the rest against full .net or mono and create a new version of PowerShell)
I had some success with Steve's tip but after that was fixed, more compilation errors started to appear so I am working through those at the moment
iSazonov commentedon Aug 1, 2018
I believe that the main direction of development is migration from .Net Framework to .Net Core. So I would not invest in compatibility with .Net Framework. Seem some of .Net Standard 2.0 is already lacking in .Net Framework. With .Net Standard 2.1, the outbreak will increase.
I expect that PowerShell Core will be consistent (compiled) with .Net Standard Future in a few years and again the outbreak with .Net Framework will increase.
Also if we'll add improvements in Parser with Span<T>, Memory<T> and Buffers we can not compile the Parser in .Net Framework at all.
bergmeister commentedon Aug 1, 2018
Ilya (@iSazonov) Yes, I can already see that .Net Core is ahead of .Net Framework in terms of API calls. Yes, .Net standard would be the more ideal solution but as Steve said, it would be quite tricky with netstandard2.0, therefore multi-targeting .Net Core and Full .Net is probably easier for me achieve my goal of getting it to run on full .Net, but I will see and report how I get along
Until .Net Core 3.0 is out, the full .Net framework will still be predominant.
iSazonov commentedon Aug 2, 2018
Christoph Bergmeister (@bergmeister) MSFT announced .Net Core 3.0 Preview will be released later this year. Seems .Net Standard too. I do believe that it is more profitable for you to start working in this direction now.
[-]Compile SMA against netstandard2.0 to run on full .Net[/-][+]Compile SMA against netstandard or multitarget to make it compile for full .Net[/+]bergmeister commentedon Aug 7, 2018
I have a working prototype in my branch here that needed special treatment only in a few places and some WIndows PowerShell Code to be brought back in order to compile against net472 AND not define the
CORECLRconstant (the latter is optional and is up for discussion if it should be defined in this case or not; as an alternative, one could've also just removed some#if !CORECLRsections that required those treatments, some of them like PSSnapins would even make sense to be completely removed).Therefore I do not see much of a burden to re-enable compilation against net472 and fullclr with only a compilation check in CI. I saw only 5 method calls that used new .Net Core 2.1 APIs that are not available in
net472and for some of them I created adapters using extension methods, the rest (like e.g. Spans) could all be brought back to full .Net via NuGet packages. Not setting theCORECLRconstant for compilation against full .net requires to either remove some fullclr code or to bring back some fullclr code that is just not compiled at the moment or in the source-depot branch. Compiling with theCORECLRcould be possible as well as far as I have seen.I do not expect full support from MSFT for an SMA package compiled against net472, but having it in the mainstream branch and it being published by MSFT would be helpful.
Since compilation against netstandard is not trivial and and
netstandard2.1is not even out yet, I'd agree with Steve Lee (@SteveL-MSFT) that multi-targeting fornet472(with the coreclr or fullclr option?) is the easiest option where I can issue a PR nowSeparating out the Parser seemed to me to be quite difficult.
Can MSFT please state if it is OK to multi-target SMA for
net472for the moment and if theCORECLRcompile constant should be set in this case.cc Travis Plunk (@TravisEz13) Steve Lee (@SteveL-MSFT)
SteveL-MSFT commentedon Aug 7, 2018
Christoph Bergmeister (@bergmeister) I don't think we want to bring FullCLR code back into the code base as it makes changes complicated and we don't validate the FullCLR code. It would be better if you need this to fork the project and maintain what you need until netstandard2.1 is available and (ideally) resolves the need to multi-target
12 remaining items
bergmeister commentedon Apr 17, 2020
Ilya (@iSazonov) PSSA has DSC specific rules and using the
-SaveDSCDependencyswitch, it downloads required modules from the PSGallery by parsing occurrences ofImport-DSCResourceso that it can later load them into a runspace in order to be able to parse a DSC script, therefore I think that PSSA does not need public Parser APIs for DSC but it probably needs the parser to 'work' when parsing a DSC resource.https://github.com/PowerShell/PSScriptAnalyzer/blob/b1a81874fbe61ebaf1a0b9631f337b4f06296625/Engine/Generic/ModuleDependencyHandler.cs#L424
iSazonov commentedon Apr 17, 2020
Christoph Bergmeister (@bergmeister) Who creates the runspace? If PSSA we could pass it as argument to the Parser.
bergmeister commentedon Apr 17, 2020
Ilya (@iSazonov) PSSA creates the runspace here. What happens is that if calling
Parser.ParseFilefails here, then it extracts the missing module names based on parsing extents ofImport-DSCResourceor the error message and then downloads them to a local temp folder here, which it temporarily adds to thePSModulePathenvironment variable here so that the Parser can pick up the downloaded dependenciesiSazonov commentedon Apr 17, 2020
Looking the PSSA code I see that it utilizes full PowerShell Engine and I wonder that you request standalone nupkg for Parser.
bergmeister commentedon Apr 17, 2020
Ilya (@iSazonov) At the time being the request was for a company internal software that only needed the Parser for static script parsing, which was not as involved as PSSA. I'm not working for that company any more but the request for having a nupkg for the Parser in general is still valid I think and has multiple use cases.
iSazonov commentedon Apr 17, 2020
Rob Holt (@rjmholt) Could you please share how PSES uses the Parser? Can it really benefit from the standalone Parser or we have the same as for PSSA?
rjmholt commentedon Apr 20, 2020
PSES uses the parser to perform script file analysis, like reference discovery, but it also runs PowerShell. PSES integrates with the installed PowerShell on a system and won't run without it, so there's no strong need for a standalone parser, but it would add universality perhaps. I think PSSA has a much stronger need, since it's used to analyse scripts going to other PowerShell implementations.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.