tonerdo / pose

Replace any .NET method (including static and non-virtual) with a delegate
MIT License
1.07k stars 75 forks source link

System.InvalidProgramException : JIT Compiler encountered an internal limitation. #36

Open zamaleev opened 5 years ago

zamaleev commented 5 years ago

The code under test is:

using System;
using System.IO;

using FluentValidation;

using NullGuard;

namespace App.Validators
{
    public sealed class OptionsValidator : AbstractValidator<Options>
    {
        private readonly Options options;

        private readonly Lazy<bool> isValid;

        public bool IsValid => this.isValid.Value;

        public OptionsValidator(Options options, [AllowNull] Action<string> onError = null)
        {
            this.options = options;

            this.isValid = new Lazy<bool>(() =>
                {
                    var result = this.Validate(this.options);

                    if (onError != null && !result.IsValid)
                    {
                        foreach (var failure in result.Errors)
                        {
                            onError(failure.ErrorMessage);
                        }
                    }

                    return result.IsValid;
                });

            this.RuleFor(x => x.In)
                .Cascade(CascadeMode.StopOnFirstFailure)
               .NotEmpty().WithMessage("error")
                   .Must(x => Directory.Exists(x)).WithMessage("error");
        }
    }
}

The Test code is:

using System.IO;

using AutoFixture.Xunit2;
using FluentAssertions;
using Pose;
using Xunit;

namespace App.Validators
{
    public sealed class OptionsValidatorTests
    {
        [Theory,
            InlineAutoData]
        public void NegativeTest0002(Options options)
        {
            var directory =
                Shim.Replace(() => Directory.Exists(Is.A<string>()))
                       .With((string x) => false);

            PoseContext.Isolate(() =>
                {
                    var error = string.Empty;

                    var validator = new OptionsValidator(options, x => { error = x; });

                    validator.IsValid.Should().BeFalse();
                    error.Should().NotBeNullOrEmpty();
                    error.Should().BeEquivalentTo("error");
                }, 

                directory);
        }
    }
}

Using this code, I get the following exception:

[xUnit.net 00:00:01.25]       System.InvalidProgramException : JIT Compiler encountered an internal limitation.
[xUnit.net 00:00:01.25]       Stack Trace:
[xUnit.net 00:00:01.25]            at dynamic_System.Linq.Expressions.TypedParameterExpression_.ctor(TypedParameterExpression , Type , String )
[xUnit.net 00:00:01.25]            at stub_ctor_System.Linq.Expressions.TypedParameterExpression_.ctor(Type , String , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:01.25]            at stub_System.Linq.Expressions.ParameterExpression_Make(Type , String , Boolean , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:01.25]            at dynamic_System.Linq.Expressions.Expression_Parameter(Type , String )
[xUnit.net 00:00:01.25]            at stub_System.Linq.Expressions.Expression_Parameter(Type , String , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:01.25]            at dynamic_App.Validators.OptionsValidator_.ctor(OptionsValidator , Options , Action`1 )
[xUnit.net 00:00:01.25]            at stub_ctor_App.Validators.OptionsValidator_.ctor(Options , Action`1 , RuntimeMethodHandle , RuntimeTypeHandle )
[xUnit.net 00:00:01.25]            at dynamic_App.Validators.OptionsValidatorTests+<>c__DisplayClass1_0_<NegativeTest0002>b__2(<>c__DisplayClass1_0 )
jairov4 commented 5 years ago

Im getting the same error, during execution of third party code. I think that third party is using heavy reflection.

Tarig0 commented 5 years ago

For me this comes up when the isolated path has a "? : " operation on it

lando786 commented 5 years ago

Encountered this as well, as @jairov4 mentioned third party tool is involved.

jackalvrus commented 4 years ago

I've encountered this when calling System.IO.File.ReadAllText from within my code under test.