Skip to content

Conversation

@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Feb 9, 2018

PR Summary

Fix #5515
Fix #4814
Fix #5158

Close #5725

  • Can compile a source from parameters (TypeDefinition and MemberDefinition).
  • Can compile from files.
  • Can compile only to a file (without assembly loading).
  • Don't recompile and don't reload if the sources have not changed.
  • Implemented -IgnoreWarnings - ignore warnings as errors. By default the cmdlet consider warnings as errors.
  • Added VisualBasic support.
  • Added new -CompilerOptions parameter allow set Roslyn command line parameters including:
    • Parser options.
    • Compile options.
    • Emit options.

ATTENTION: The CompilerOptions can be specified along with other options like -OutputAssembly, -Language and -IgnoreWarnings. The explicit setting parameters will take precedence over the same settings specified in -CompileOptions.

See docs about the compiler options:
https://github.com/dotnet/roslyn/blob/master/docs/compilers/CSharp/CommandLine.md
https://github.com/dotnet/roslyn/blob/master/docs/compilers/Visual%20Basic/CommandLine.md

ATTENTION: -OutputType default is Library. If -OutputType is absent the -OutputType default overlaps a value in CompileOptions. In other words output type ("target" ot "t" in command line) is always ignored in CompileOptions. We have to use -OutputType to set an output type.

@TravisEz13 @PaulHigin Please audit security of the -ExtendedOptions parameter.

PR Checklist

Note: Please mark anything not applicable to this PR NA.

@iSazonov iSazonov added the Documentation Needed in this repo Documentation is needed in this repo label Feb 9, 2018
@TravisEz13 TravisEz13 dismissed a stale review February 13, 2018 18:02

issue was fixed

JamesWTruher
JamesWTruher previously approved these changes Feb 13, 2018
Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

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

Haven't finished all but want to share the feedback early. Will continue review from CheckDuplicateTypes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

#Closed

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It allows compile a source code on PowerShell Core and Windows PowerShell with different set of usings. Sometimes .Net Core and .Net Framework is different in that.

Copy link
Member

Choose a reason for hiding this comment

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

But the Add-Type in Windows PowerShell doesn't support this parameter in FromSource set, and thus it won't bring us this benefit. I would rather have UsingNamespace only belong to FromMember set, just like today.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe the scenario can be important if someone has a portable code template and have to compile in Windows PowerShell and PowerShell Core with different usings. If the scenario is important we should enhance Windows PowerShell too. If no I'll remove this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, will do. (I wanted reduce size of the PR)

Copy link
Collaborator Author

@iSazonov iSazonov Feb 28, 2018

Choose a reason for hiding this comment

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

Done. #Closed

Copy link
Member

Choose a reason for hiding this comment

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

Is -PassThru available in all parameters set? If so, then the original [Parameter()] is more preferred as it's easy to tell that a parameter is in all parameter sets.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The membership is under question. I want to get feedback. If no one objects to this '-PassThru available in all parameters set ' then I will use [Parameter()].

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently it is not in "FromAssemblyName".
I think it makes sense to add?

Copy link
Member

Choose a reason for hiding this comment

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

Add-Type -AssemblyName F:\pscore\Microsoft.PowerShell.Commands.Utility.dll -PassThru returns all available types from the DLL, so I think we should keep the same.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This works.

Copy link
Member

Choose a reason for hiding this comment

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

I think currently Add-Type -TypeDefinition $code -OutputAssembly f:\test.dll only produces the assembly without loading it. So is this new parameter really needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We should always load assembly by default otherwise it is a breaking change.

Copy link
Member

Choose a reason for hiding this comment

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

Add-Type -TypeDefinition $code -OutputAssembly f:\test.dll doesn't load the assembly today, so why would it be a breaking change?

I tried the following in PSCore:

[System.AppDomain]::CurrentDomain.GetAssemblies() | Out-File F:\old.txt
Add-Type -TypeDefinition $code -OutputAssembly F:\test.dll
[System.AppDomain]::CurrentDomain.GetAssemblies() | Out-File F:\new.txt

One assembly is loaded when calling Add-Type, and it's System.ValueTuple.dll. I don't see the test.dll being loaded.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

Copy link
Member

Choose a reason for hiding this comment

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

Will this handle the case that the code in those files references to symbols defined in other files?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My understanding is yes - it is the same parser/compiler session. But I don't test this.

Copy link
Member

Choose a reason for hiding this comment

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

Can you please verify it? ParseText is a static method, so it's hard to say for sure that one signle parser session is used for all those files.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. Works well.

Add-Type -Path C:\tmp\1.cs,c:\tmp\2.cs
[Test.AddType.CSharpTest2]::Add2(1,2)
3

1.cs

namespace Test.AddType
        {
            public class CSharpTest1
            {
                public static int Add1(int a, int b)
                {
                    return (a + b);
                }
            }
        }

2.cs

namespace Test.AddType
        {
            public class CSharpTest2
            {
                public static int Add2(int a, int b)
                {
                    return CSharpTest1.Add1(a, b);
                }
            }
        }

Copy link
Member

Choose a reason for hiding this comment

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

nit: By looking at this assertion here, I feel maybe arrange this if {} else if {} else if {} else {} in a switch of parameter name would be cleaner.

Copy link
Collaborator Author

@iSazonov iSazonov Feb 28, 2018

Choose a reason for hiding this comment

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

Done. #Closed

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better if you make _syntaxTree also a local variable. It can be passed to IsSourceCodeUpdated and CSharpCompileToAssembly.
I think it's better to make it clear what a method consumes and produces in its signature, and having the arguments like compilationOptions, emitOptions, syntaxTree passed into methods helps it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am waiting #6141 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Similar comments as in CSharpSourceCodeProcessing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Do we honor -PassThru in this case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, see below in DoEmitAndLoadAssemply

@iSazonov iSazonov added the Breaking-Change breaking change that may affect users label Feb 28, 2018
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It allows compile a source code on PowerShell Core and Windows PowerShell with different set of usings. Sometimes .Net Core and .Net Framework is different in that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, will do. (I wanted reduce size of the PR)

Copy link
Member

Choose a reason for hiding this comment

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

errorPosition is not used anywhere. Do you intend to use it to replace lineSpan.StartLinePosition.Character at line 1397?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, thanks - it's more readable.
Done.

Copy link
Member

Choose a reason for hiding this comment

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

I don't see the change that addresses this comment in your new commit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, lost commit.
Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe change to if (!char.IsWhiteSpace(errorLineString[i]))? It's more readable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought about it but there's a lot of other characters
https://msdn.microsoft.com/en-us/library/t809ektx%28v=vs.110%29.aspx
Is it ok?

Copy link
Member

Choose a reason for hiding this comment

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

As long as they are whitespace characters, I don't see a difference. Do you?

Copy link
Collaborator Author

@iSazonov iSazonov Mar 14, 2018

Choose a reason for hiding this comment

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

I fixed this but ...
This come from original code and I'm afraid the code isn't exactly correct because we're replacing one tab with a single space. Perhaps it was necessary for CodeDom. I hope that Roslyn don't use tabs in error messages. Although these whitespaces can come from the code that it compiles. Therefore we have to clean the error message too or remove this cleanup at all. Thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Math.Max(0, lineSpan.StartLinePosition.Character - i)

Is the Math.Max call really needed? It seems to me that errorPosition would be -ge i.

Copy link
Collaborator Author

@iSazonov iSazonov Mar 13, 2018

Choose a reason for hiding this comment

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

When I researched how to make the error report I was looking for code examples. Now I do not remember where I saw this pattern.
I think this is because the lineSpan and the error position are completely different Roslyn code paths - so we should protect.

Copy link
Member

Choose a reason for hiding this comment

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

For CSharp or VB?

Copy link
Collaborator Author

@iSazonov iSazonov Mar 13, 2018

Choose a reason for hiding this comment

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

VisualBasic
Fixed. #Closed.

Copy link
Member

Choose a reason for hiding this comment

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

I guess this applies to both csharp and vb. If that's the case, for CSharp should be removed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

OK, then never mind. #Closed.

Copy link
Member

Choose a reason for hiding this comment

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

But the Add-Type in Windows PowerShell doesn't support this parameter in FromSource set, and thus it won't bring us this benefit. I would rather have UsingNamespace only belong to FromMember set, just like today.

Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/PowerShell/PowerShell/pull/6141/files#r171029964
I want to point it out again that this is a breaking change in that System and System.Runtime.InteropServices are not included when -UsingNamespace is not specified after this change.

Copy link
Collaborator Author

@iSazonov iSazonov Mar 13, 2018

Choose a reason for hiding this comment

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

If we add this we get errors:

>  $guid = [Guid]::NewGuid().ToString().Replace("-","")
>         $CSharpCode1 = @"
>> using System;
>> using System.Runtime.InteropServices;
>>         namespace Test.AddType
>>         {
>>             public class CSharpTest1$guid
>>             {
>>                 public static int Add1(int a, int b)
>>                 {
>>                     return (a + b);
>>                 }
>>             }
>>         }
>> "@
PS C:\Users\sie\Documents\GitHub\iSazonov\PowerShell> Add-type -TypeDefinition $CSharpCode1
Add-type : (2,1): hidden CS8019: Unnecessary using directive.
using System.Runtime.InteropServices;
^
At line:1 char:1
+ Add-type -TypeDefinition $CSharpCode1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: ((2,1): hidden C...sing directive.:CSDiagnostic) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-type : (1,1): hidden CS8019: Unnecessary using directive.
using System;
^
At line:1 char:1
+ Add-type -TypeDefinition $CSharpCode1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: ((1,1): hidden C...sing directive.:CSDiagnostic) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently Appveyor CI failed with the same error in log line 3827. 😕

Can I rebase to fix an error with File.wxs?

Copy link
Member

Choose a reason for hiding this comment

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

The error seems caused by a warning that more than one using system; is specified. I'm sure there is a way to deal with it.

Feel free to rebase.

Copy link
Member

Choose a reason for hiding this comment

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

There are two problems here:

  1. The assert at this location doesn't do anything useful because when the list is initially created, its count will be 0.
  2. I don't know how many people uses debug mode binaries. It could happen that this assert fails without us knowing. This happened before to an assert in NativeComamndParameterBinder.

Copy link
Member

Choose a reason for hiding this comment

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

What if -CompileOnly and -PassThru are both specified?
As mentioned in some other places, I don't think we need this parameter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently -PassThru will be ignored.
The switch came from #5725 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

As I stated in comments above #6141 (comment), when using -OutputAssembly, Add-Type only compiles the assembly without loading it. So I don't think -CompileOnly is really needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently we always load generated assembly. So with -CompileOnly we address new scenario.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Based on the comments in SyntaxTree, you can directly call ToString() without having to call GetText() first.

Copy link
Collaborator Author

@iSazonov iSazonov Mar 13, 2018

Choose a reason for hiding this comment

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

Fixed. #Closed

Copy link
Member

Choose a reason for hiding this comment

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

IAssemblySymbol has a property TypeNames, of the type ICollection<string>. Maybe we can directly use that property to avoid a visitor (simplify the code)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Seems we have to use the recursion dotnet/roslyn#6138

Copy link
Member

@daxian-dbw daxian-dbw Mar 13, 2018

Choose a reason for hiding this comment

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

The linked issue is quite old (2015), and it doesn't mention the IAssemblySymbol.TypeNames property. If it were me, I would try this property to see if it works.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tested - IAssemblySymbol.TypeNames doesn't return full type names.

Copy link
Member

Choose a reason for hiding this comment

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

AsParallel() is likely to be less performant when it comes to a small collection. In the case of Add-Type, most usages involve only a few types being compiled, so AsParallel might not serve our interest here.

If we use the property IAssemblySymbol.TypeNames, then we can decide whether to use AsParallel based on the Count (the property returns an ICollection of string).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd remove the AsParallel at all but but don't found how.

Copy link
Member

Choose a reason for hiding this comment

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

If IAssemblySymbol.TypeNames works, we won't need the visitor code :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

AsParallel() was replaced with foreach.
Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Question: Is the name here a namespace-fully-qualified name?

Copy link
Collaborator Author

@iSazonov iSazonov Mar 13, 2018

Choose a reason for hiding this comment

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

Yes, I checked this in debuger (but I don't found implementation in Roslyn).
From our tests: "Test.AddType.CSharpTest1ed31e6c201cc4ca9b1ed0493e79434f7".

I renamed a variable in the visitor and add new comment.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks. #Closed

Copy link
Member

Choose a reason for hiding this comment

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

It looks to me the VisualBasicSourceCodeProcessing and CSharpSourceCodeProcessing can be merged into one method.
For example:

private CommandLineArguments ParseOptions(Language language, IEnumerable<string> args)
{
    var sdkDirectory = sdkDirectory ?? s_defaultSdkDirectory;
    var baseDirectory = this.SessionState.Path.CurrentLocation.Path;
    switch (language)
    {
        case CSharp:
            return CSharpCommandLineParser.Default.Parse(...);
        case VB:
            return VisualBasicCommandLineParser.Default.Parse(...)
    }
}

private CompilationOptions GetDefaultCompilationOptions(language, outputKind) 
{
    switch (language)
    {
        case CSharp:
            new CSharpCompilationOptions(OutputAssemblyTypeToOutputKind(OutputType));
        case VB:
            return new VisualBasicCompilationOptions(outputKind: OutputAssemblyTypeToOutputKind(OutputType));
    }
}

private SyntaxTree ParseText(language, sourceText, parseOptions, file)
{
    switch (lanaguage)
    {
         case CSharp:
              return CSharptSyntextTree.ParseText(sourceText, (CSharpParseOptions) parseOptions)
         case VB:
              return VisualBasicSyntaxTree.ParseText(sourceText, (VBParserParseOptions) parseOptions)
    }
}

private void CompileToAssembly(language, syntaxTrees, compilationOptions, emitOptions)
{
    IEnumerable<PortableExecutableReference> references = GetPortableExecutableReferences();
    switch (language)
    {
        case CSharp:
            compilation = CSharpCompilation.Create(..., (CSharpCompilationOptions) compilationOptions, ...)
        case VB:
            compilation = VisualBasicCompilation.Create(..., (VBCompilationOptions) compilationOptions, ...)
    }
    DoEmitAndLoadAssemply(compilation, emitOptions);
}

Then we can have a method named CompileSourceCode to replace the VisualBasicSourceCodeProcessing and CSharpSourceCodeProcessing, like this:

private void CompileSourceCode(language)
{
    if (ExtendedOptions != null)
    {
        var arguments = ParseOptions(language, args);
        ...
    }
    else
    {
        compilationOptions = GetDefaultCompilationOptions(langauge);
    }
    ...
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Typo --> parcer

Copy link
Collaborator Author

@iSazonov iSazonov Mar 13, 2018

Choose a reason for hiding this comment

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

Fixed. #Closed

Copy link
Member

Choose a reason for hiding this comment

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

Maybe the check of diagnisticRecord.IsSuppressed should be put at the outmost of this method. If isSuppressed is true, no need to go through the loop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, I don't understand - we get diagnisticRecord in the loop - how we can move it out of the loop?

Copy link
Member

Choose a reason for hiding this comment

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

My bad. Never mind this comment. #Closed.

@daxian-dbw
Copy link
Member

@iSazonov I left more comments in many of my existing comments to reply to your responses. I add #Closed those of my comments that are closed. For the rest, they are still open and need your response. Thanks!

@iSazonov
Copy link
Collaborator Author

@daxian-dbw I'm trying to address your comments but some might miss because Github hides them. 😄 Also the browser freezes on his too large PR.

@daxian-dbw
Copy link
Member

some might miss because Github hides them

Every time I revisit this PR, I open every hidden comment left by myself and see if you respond to my last reply. For comments that is closed, a #closed tag is added to the last message of the comment thread.

@iSazonov
Copy link
Collaborator Author

iSazonov commented Mar 16, 2018

@daxian-dbw I have collected almost all open comments for a quicker response.

  1. UsingNamespace in [Parameter(ParameterSetName = "FromSource")]

I believe the scenario can be important if someone has a portable code template and have to compile in Windows PowerShell and PowerShell Core with different usings. If the scenario is important we should enhance Windows PowerShell too. If no I'll remove this.


[daxian] I don't see such a change can meet the bar for Windows PowerShell. I think you can remove this for now. If a demand comes up at a later time, we can revisit this idea.


[iSazonov] Fixed.

  1. CompileOnly

Sorry, I already forget my thoughts - Add-Type -TypeDefinition $code -OutputAssembly f:\test.dll in Windows PowerShell loads the dll only if -PassTru present. It is hidden logic, this is not an obvious behavior of the -PassTru parameter. If we want load the dll and don't want -PassTru we have to use:

Add-Type -TypeDefinition $code -OutputAssembly f:\test.dll -PassTru > $null

So the idea is to add new parameter for better UX.


[daxian] IMO, -CompileOnly is not needed for Add-Type. Currently, Add-Type doesn't load an assembly when -OutputAssembly is specified. Otherwise, it produces an in-memory assembly which has to be loaded. When -PassThru is specified, you basically are asking Add-Type to load the assembly because you want it to return all types.

[daxian] If we don't break the current behavior, then the -CompileOnly parameter is not useful at all, because for Add-Type $code -OutputAssembly f:\test.dll -CompileOnly, -CompileOnly is redundant as it doesn't load the assembly anyways; for Add-Type $code -CompileOnly, it's useless because we have to load an in-memory assembly.

[daxian] For -CompileOnly to be useful, you have to break the current behavior to make Add-Type $code -OutputAssembly f:\test.dll load the assembly by default, and add -CompileOnly if you don't want it to be loaded. I don't think this breaking change is acceptable.


[iSazonov] Fixed.

  1. WarnAsError and IgnoreWarnings

In master branch code -IgnoreWarnings comply with default behavior and really do nothing.
The master branch code already has a breaking change with 6.0.0 GA (really we haven't any code for -IgnoreWarnings) - and the breaking change is that user can not get "warnings as errors" with absent -IgnoreWarnings or with -IgnoreWarnings:$false.

What is fix we want get now? Should we keep CodeDom (Windows PowerShell) default? It is main question. If so it will be again a breaking change with 6.0.0 GA.

Roslyn has opposite default.

I'd prefer use new parameter and new default.

I assumed we could keep the -IgnoreWarnings old parameter for backward compatibility, but as I said we've already full broken it and it's better to remove it altogether.


In master branch code -IgnoreWarnings comply with default behavior and really do nothing.

[daxian] It's broken and needs to be fixed. That parameter should have the same behavior as in Windows PowerShell. The behavior of -IgnoreWarnings in Windows PowerShell is very reasonable -- treat warnings as errors by default, and don't treat warning as errors when -IgnoreWarnings is specified (see an example in my comment here). Keep in mind that the current Add-Type in PSCore is partially broken, the does-nothing -IgnoreWarnings parameter is one of the broken pieces. We should respect the existing behavior in Windows PowerShell for script portability, unless the existing behavior doesn't make sense at all within the new environment


[iSazonov] Fixed.

  1. Done: ExtendedOptions -> CompilerOptions

  2. Removed: Diagnostics.Assert(defaultRefAssemblies.Count < 150...

  3. if (!char.IsWhiteSpace(errorLineString[i]))

I fixed this but ...
This come from original code and I'm afraid the code isn't exactly correct because we're replacing one tab with a single space. Perhaps it was necessary for CodeDom. I hope that Roslyn don't use tabs in error messages. Although these whitespaces can come from the code that it compiles. Therefore we have to clean the error message too or remove this cleanup at all. Thoughts?


or remove this cleanup at all.

[daxian] What code does "this cleanup" refer to?
[daxian] As for white characters other than spaces in the error message, I think they should be rare. What do you mean by "original code" here? The existing code in Add-Type? If that's the case, then we should just stick with it because I don't head complaints about error reporting for the existing Add-Type.


[iSazonov] Thanks! Clear. Fixed.

  1. IAssemblySymbol.TypeNames

Not addressed still


[daxian] I don't see your response on another big item -- merging the VisualBasicSourceCodeProcessing and CSharpSourceCodeProcessing into one method (see my comment here). Hope you can think about it.


[iSazonov] Yes, of course. I was hoping to avoid switch, but there seems to be no other way.

@iSazonov iSazonov force-pushed the addtype-enhance1 branch 3 times, most recently from 029802e to 5b0093a Compare March 19, 2018 13:14
@iSazonov
Copy link
Collaborator Author

CI Appveyor failed on files.wxs - I can not fix this.

@TravisEz13
Copy link
Member

new file {$(env.ProductSourcePath)\Microsoft.CodeAnalysis.VisualBasic.dll} need to be added to {C:\projects\powershell\tools\packaging\..\..\assets\Files.wxs}

Why can you not fix this?

@PowerShell PowerShell deleted a comment from PaulHigin Apr 24, 2018
@PowerShell PowerShell deleted a comment from PaulHigin Apr 24, 2018
@PowerShell PowerShell deleted a comment from PaulHigin Apr 24, 2018
@PowerShell PowerShell deleted a comment from iSazonov Apr 24, 2018
@PowerShell PowerShell deleted a comment from iSazonov Apr 24, 2018
@PowerShell PowerShell deleted a comment from iSazonov Apr 24, 2018
@PowerShell PowerShell deleted a comment from TravisEz13 Apr 24, 2018
@PowerShell PowerShell deleted a comment from TravisEz13 Apr 24, 2018
@daxian-dbw
Copy link
Member

I have to delete 30 old comments in order to make the web browser able to open this PR page. Wait for the @PowerShell/powershell-committee's final call to merge this PR.

@SteveL-MSFT
Copy link
Member

@daxian-dbw this is a long PR, can you summarize what the committee needs to review?

@daxian-dbw
Copy link
Member

@SteveL-MSFT I need the committee's approval to merge the PR as is (with the VB related code).
If the committee decides to not have VB support in Add-Type, then we can use a separate PR to clean it up.

@SteveL-MSFT SteveL-MSFT added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Apr 25, 2018
@SteveL-MSFT
Copy link
Member

@PowerShell/powershell-committee is ok with taking this PR as-is and having a separate discussion about removing VB support

@daxian-dbw daxian-dbw merged commit ffa7e4b into PowerShell:master Apr 26, 2018
@daxian-dbw
Copy link
Member

@iSazonov Thanks for the hard work to get Add-Type refactored!

@iSazonov
Copy link
Collaborator Author

@daxian-dbw Thanks for great comments and help!

@iSazonov iSazonov deleted the addtype-enhance1 branch April 26, 2018 05:55
rjmholt added a commit that referenced this pull request Apr 27, 2018
@daxian-dbw daxian-dbw removed the Breaking-Change breaking change that may affect users label Jun 7, 2018
@daxian-dbw
Copy link
Member

Remove Breaking-Change label because no breaking change is introduced in this PR.

@daxian-dbw daxian-dbw added the Breaking-Change breaking change that may affect users label Jun 7, 2018
@daxian-dbw
Copy link
Member

Add back the Breaking-Change label as @dantraMSFT pointed out that some error strings were removed meaning that we are using different FullyQualifiedName for those error conditions.

@rjmholt
Copy link
Collaborator

rjmholt commented Jun 7, 2018

🔨

@iSazonov
Copy link
Collaborator Author

iSazonov commented Jun 8, 2018

We have never considered changing FullyQualifiedName as breaking change. However, I agree to leave this label because updates to the cmdlet are significant.

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

Labels

Breaking-Change breaking change that may affect users Committee-Reviewed PS-Committee has reviewed this and made a decision

Projects

None yet

7 participants