UiPath / CoreWF

WF runtime ported to work on .NET 6
MIT License
1.13k stars 214 forks source link

VisualBasicDesignerHelper.CreatePrecompiledVisualBasicValue Throws an Exception When Given A Valid VB Expression #83

Closed StephenWeatherhead closed 4 years ago

StephenWeatherhead commented 4 years ago

The method "VisualBasicDesignerHelper.CreatePrecompiledVisualBasicValue" throws an exception when given a valid VB expression. The same program runs in .NET Framework with System.Activities without an issue. The exception is:

System.Activities.ExpressionParser.SourceExpressionException: 'Compiler error(s) encountered processing expression ""Test" = "Test"".Microsoft.CodeAnalysis.Scripting.CompilationErrorException: (1) : error BC30002: Type 'Func' is not defined. at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.ThrowIfAnyCompilationErrors(DiagnosticBag diagnostics, DiagnosticFormatter formatter) at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.CreateExecutor[T](ScriptCompiler compiler, Compilation compilation, Boolean emitDebugInformation, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Scripting.Script1.GetExecutor(CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Scripting.Script1.CommonGetExecutor(CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Scripting.Script1.TryGetPrecedingExecutors(Script lastExecutedScriptInChainOpt, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Scripting.Script1.GetPrecedingExecutors(CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Scripting.Script1.RunAsync(Object globals, Func2 catchException, CancellationToken cancellationToken) at Microsoft.VisualBasic.Activities.HostedCompiler.CompileExpression(String expressionString, Func`2 getVariableType, ScriptOptions options, Type lambdaReturnType) in \CoreWf\Microsoft\VisualBasic\Activities\HostedCompiler.cs:line 37 at Microsoft.VisualBasic.Activities.VisualBasicHelper.Compile[T](LocationReferenceEnvironment environment, Boolean isLocationReference) in \CoreWf\Microsoft\VisualBasic\Activities\VisualBasicHelper.cs:line 507'

The following program runs fine in .NET Framework System.Activities. When it is run using .NET Core CoreWf System.Activities, it throws the above exception.

using Microsoft.VisualBasic.Activities;
using System;
using System.Activities;
using System.Activities.Statements;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace DesignerHelperBugCore
{
    class Program
    {
        static void Main(string[] args)
        {
            RefEnv refEnv = new RefEnv();
            Activity activity = VisualBasicDesignerHelper.CreatePrecompiledVisualBasicValue(
                typeof(Boolean),
                "\"Test\" = \"Test\"",
                new string[0],
                AppDomain.CurrentDomain.GetAssemblies().Select<Assembly, string>(x =>     x.FullName),
                refEnv,
                out var returnType,
                out var compileError,
                out var settings);
            Console.WriteLine($"The type of activity is {activity.GetType()}");
            Console.ReadLine();
        }
    }
    class RefEnv : LocationReferenceEnvironment
    {
        Activity _root = new WriteLine { Text = "Test" };
        public override Activity Root => _root;

        public override IEnumerable<LocationReference> GetLocationReferences()
        {
            return new LocationReference[0];
        }

        public override bool IsVisible(LocationReference locationReference)
        {
            return false;
        }

        public override bool TryGetLocationReference(string name, out LocationReference result)
        {
            result = null;
            return false;
        }
    }
} 

I suspect this is related to Microsoft.CodeAnalysis.VisualBasic.Scripting not having a .NET Core equivalent but instead being substituted with the .NET Framework version: https://github.com/UiPath/corewf/issues/49

Thank you for your help

lbargaoanu commented 4 years ago

That's not part of our API. But if you want to fix it anyway, a PR is welcome. See #47 for details.

StephenWeatherhead commented 4 years ago

I have ported Microsoft.CodeAnalysis.VisualBasic.Scripting to .NET Core. It seems that wasn't the issue. The issue is that the way expressions are compiled in VisualBasicHelper has changed in CoreWf. It uses a different way than .NET Framework System.Activities. This explains the difference in behaviour.

github-actions[bot] commented 4 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.