Skip to content

Add-Type fails when more than a single source code file is given #4814

Description

It appears Add-Type on powershell core attempts to concatenate all source code files when compiling. The "using" directive of all but the first source file causes syntax error

Scripts to reproduce
https://github.com/psteniusubi/add-type

Steps to reproduce

Add-Type -Path Type1.cs,Type2.cs

Expected behavior

Should compile without error. Works on PowerShell for Windows


Actual behavior

Add-Type : (14) : A using clause must precede all other elements defined in the namespace except extern alias declarations
At line:1 char:1
+ Add-Type -Path Type1.cs,Type2.cs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. Compilation errors occurred.
At line:1 char:1
+ Add-Type -Path Type1.cs,Type2.cs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.6
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added this to the milestone on Sep 13, 2017
  2. psteniusubi commented on Oct 25, 2017

    @psteniusubi
    Author

    A workaround for anyone who runs into this issue:

    If you want to use Add-Type to compile more than a single .cs file, then move your "using" directives inside "namespace" declarations:

    namespace Example {
        using System;
        public class ExampleClass {
        }
    }
  3. aaronsace commented on Nov 25, 2017

    @aaronsace

    I'm baffled as to how/why this issue has not been fixed. Most files have at least one 'using' at the top so all C# files are going to have to be changed as Petteri Stenius (@psteniusubi) suggested.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions