OYIon / LiveSharp

Public repository for the LiveSharp project
96 stars 4 forks source link

Error with unsafe code #105

Open aboudoux opened 3 years ago

aboudoux commented 3 years ago

Hi,

We are testing livesharp for our Blazor product, and this one use some low level libraries which contains unsafe code.

So, blazor can connect to LiveSharpServer, but the log display an error due to the /unsafe switch that is not set for compilation.

14:33:35.827: Unsafe code may only appear if compiling with /unsafe
14:33:35.839: FileSaved handler failed
System.Exception: Serializing document failed
   at LiveSharp.VisualStudio.Services.LiveSharpHandler.CreateDynamicAssembly(Compilation compilation, Project project, ILogger logger)
   at LiveSharp.VisualStudio.Services.LiveSharpHandler.PrepareUpdatesAsync(Project project, SemanticModel semanticModel, String documentName, Boolean isDryRun)
14:33:35.861: Ended job: Dry run for BlazorApp1 (237)

To reproduce the problem, just create a new blazor server project, and replace the Counter.razor by the following code :

@page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 10;

    private void IncrementCount()
    {
        currentCount++;
        UnsafeTest.Test();
    }

    public static class UnsafeTest
    {
        public unsafe static void Test() {
            fixed (char* value = "safe") {
                char* ptr = value;
                while (*ptr != '\0') {
                    Console.WriteLine(*ptr);
                    ++ptr;
                }
            }
        }
    }
}

Check "Allow unsafe code" in the Build properties of the project, and run with LiveSharp.

Thanks for your help !

ionoy commented 3 years ago

Hello @aboudoux

You are the first who requested it, that's why it's not there yet.

Anyway, please try the attached NuGet package. Unsafe code is allowed by default now.

LiveSharp.1.6.37.zip

aboudoux commented 3 years ago

unfortunately the update does not seem to fix the problem. Do I also need to update Livesharp Server?

ionoy commented 3 years ago

This is a little more complicated than I initially thought. Can you come to https://gitter.im/LiveSharp/Lobby and PM me (@ionoy)? I need to know what stuff do you expect to work in the unsafe region. Taking a pointer from a string is easy enough, but I suspect you have something more complicated there.

ionoy commented 3 years ago

Hey @aboudoux

Are you using server-side Blazor? Because the most recent LiveSharp build should fix the unsafe and other IL issues for .NET Core projects.