pamidur / aspect-injector

AOP framework for .NET (c#, vb, etc)
Apache License 2.0
745 stars 112 forks source link

Aspect Injector with Rider #125

Closed IZaiarnyi closed 2 years ago

IZaiarnyi commented 4 years ago

I'm new to C# and start my first project with Rider on MacOS.

I created the solution based on netcoreapp 3.1, installed AspectInjector Version="2.3.1" with the help of NuGet and build my source. TestProject1.zip

Actual result: Aspect Injector processing has failed. See other errors

Expected result: The solution is successfully built

Moreover, I see some problem with Aspect Analyzer image

Screen Shot 2020-03-12 at 6 25 49 PM

What do I do wrong?

pamidur commented 4 years ago

Hi @IZaiarnyi , let's try to figure what goes wrong here! First, last screenshot shots analyzers and issues that could happen, so that won't help. Could you try to go console, navigate to folder where *.sln is and execute 'dotnet build' there? It should reveal the real error.

IZaiarnyi commented 4 years ago

Thank you for your quick response. After executing 'dotnet build' I got this message

 It was not possible to find any compatible framework version
  The framework 'Microsoft.NETCore.App', version '2.1.0' was not found.
    - The following frameworks were found:
        3.1.2 at [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

However, I got a successful build when I tried to build it one more time with the help of Rider. Currently, I cannot reproduce the error with either 'dotnet build' or Rider build.

pamidur commented 4 years ago

That's weird, because I've seen your project targets netcoreapp3.1. I guess I can be some weird issue with Rider itself. You can try to install sdk from here https://dotnet.microsoft.com/download/dotnet-core/2.1 and try again with dotnet build

IZaiarnyi commented 4 years ago

It is really weird I am not able to reproduce this error anymore. Probably, it is some setting of Rider that I missed configuring. I believe that we can close the issue and thank you for your help.

pamidur commented 4 years ago

Always welcome! Feel free to reopen if happens again

panosru commented 4 years ago

I bumped upon that issue today. I want to try AspectInjector but I'm unable to build the solution.

I created a new solution in rider with a console project.

csproj:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="AspectInjector" Version="2.4.1" />
    </ItemGroup>

</Project>

Program.cs:

namespace AspectInjerctorDemo
{
    using System;
    using AspectInjector.Broker;

    public class Foo
    {
        [MyAttribute]
        public void Bar(int val)
        {
            Console.WriteLine(val);
        }
    }

    [Aspect(Scope.Global)]
    [Injection(typeof(MyAttribute))]
    public class MyAttribute : Attribute
    {
        [Advice(Kind.Before)]
        public void PreProcess([Argument(Source.Name)] string name)
        {
            Console.WriteLine($"Calling {name} method");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var asd = new Foo();
            asd.Bar(3);
        }
    }
}

When I build the project from rider I'm getting: image

I also have errors in analyzers: image

When I run dotnet build in terminal I have no issues: image

Rider Toolset and Build section point to the same dotnet binary: image

I'm on Rider v2020.2

pamidur commented 4 years ago

Hi @panosru , it seems like an issue with Rider. Could you please dig and find the whole build log produced by Rider when build happens?

There should be a message that right before 'Aspect Injector processing has failed. See other errors'.

One more question - are you sure you have error in analyzers? I'm not that familiar with Rider, and the screen you posted looks good to me

panosru commented 4 years ago

@pamidur I'm sorry, regarding analyzers it was my mistake, I have no errors in analyzers.

The part related to AspectInjector is the following:

18:21:50.585     1>Target "_ASI_InjectAspectsCore: (TargetId:73)" in file "/Users/panosru/.nuget/packages/aspectinjector/2.4.1/buildTransitive/netstandard2.0/AspectInjector.targets" from project "/Volumes/Projects/RND/VisualStudio/JustDemo/AOP/AspectInjerctorDemo/AspectInjerctorDemo.csproj" (target "InjectAspects" depends on it):
                   Task "Message" skipped, due to false condition; ( '$(_ASI_Debug)' == 'true' ) was evaluated as ( 'false' == 'true' ).
                   Using "Exec" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
                   Task "Exec" (TaskId:46)
                     /var/folders/11/5vttgmqd6z327wdlspbkxg940000gn/T/tmp19e3825d66ac4e0f9f7b401451aaae51.exec.cmd: line 2: dotnet: command not found (TaskId:46)
                     The command "dotnet --roll-forward-on-no-candidate-fx 2 "/Users/panosru/.nuget/packages/aspectinjector/2.4.1/buildTransitive/netstandard2.0/../../build/_bin/AspectInjector.dll"  -rf "/Volumes/Projects/RND/VisualStudio/JustDemo/AOP/AspectInjerctorDemo/obj/Debug/netcoreapp3.1/AspectInjerctorDemo.dll._asi_refs" "/Volumes/Projects/RND/VisualStudio/JustDemo/AOP/AspectInjerctorDemo/obj/Debug/netcoreapp3.1/AspectInjerctorDemo.dll"" exited with code 127. (TaskId:46)
                   Done executing task "Exec". (TaskId:46)
                   Using "Error" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
                   Task "Error" (TaskId:47)
18:21:50.597     1>AspectInjector : error AI_FAIL: Aspect Injector processing has failed. See other errors.
                   Done executing task "Error" -- FAILED. (TaskId:47)

And the full build log is here.

pamidur commented 4 years ago

This one looks weird

line 2: dotnet: command not found

Idk if this issue has something to do with it https://github.com/dotnet/sdk/issues/5777, but I doubt. It seems like Rider has it's own way to do stuff and for some reason it does not work.

I have just tried to install Rider on my laptop(windows) and it works just fine.

Could you please drop contents of /var/folders/11/5vttgmqd6z327wdlspbkxg940000gn/T/tmp19e3825d66ac4e0f9f7b401451aaae51.exec.cmd ?

pamidur commented 4 years ago

I have no errors in analyzers

It just shows that particular Analyzer may find errors I guess

pamidur commented 4 years ago

As a temporal measure it might help to disable ReSharper Build image

https://www.jetbrains.com/help/rider/Settings_Toolset_and_Build.html#build-engine

It seems like "ReSharper Build" is an attempt to optimize something. However the MSBuild (dotnet build) is quite good as well and supports incremental build, so I honestly don't see why they have this option be default., considering it might break things.

panosru commented 4 years ago

/var/folders/11/5vttgmqd6z327wdlspbkxg940000gn/T/tmp19e3825d66ac4e0f9f7b401451aaae51.exec.cmd is a temp file that most likely is created during the build process and then is removed because I don't see it in the path: image

Disabling ReSharper build doesn't help, the result log is identical with the one I posted previously.

pamidur commented 4 years ago

Could you please create a new csproj file with the following content and build it in Rider

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="dotnet --version" />
  </Target>

</Project>
panosru commented 4 years ago

@pamidur I did it and I got the following error: image

Log:

19:33:53.241     2>Target "PreBuild: (TargetId:24)" in project "/Volumes/Projects/RND/VisualStudio/JustDemo/AOP/ConsoleApp1/ConsoleApp1.csproj" (target "PreBuildEvent" depends on it):
                   Using "Exec" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
                   Task "Exec" (TaskId:17)
                     Task Parameter:Command=dotnet --version (TaskId:17)
                     dotnet --version (TaskId:17)
                     /var/folders/11/5vttgmqd6z327wdlspbkxg940000gn/T/tmpb3bde8463f284346a0797ccaf8fb9fe1.exec.cmd: line 2: dotnet: command not found (TaskId:17)
19:33:53.250     2>/Volumes/Projects/RND/VisualStudio/JustDemo/AOP/ConsoleApp1/ConsoleApp1.csproj(8,9): error MSB3073: The command "dotnet --version" exited with code 127.
                   Done executing task "Exec" -- FAILED. (TaskId:17)

Full log here.

Could it be a Rider bug? At least on macOS? If so, then I'll create an issue in JetBrains YouTrack.

I wonder if that could somehow be related to https://youtrack.jetbrains.com/issue/RIDER-42713

pamidur commented 4 years ago

I was to create the issue on their tracker too, but the one you've mentioned looks it is related. Have you tried their workaround - to add dotnet bin to system's PATH?

panosru commented 4 years ago

I am indeed using zsh, but I just tested with bash to make sure and dotnet bin is in bash Path as well.

Zsh (~/.zshenv): image

Bash (~/.bash_profile): image

In mac, there isn't /etc/profile.d, there is /etc/profile file, but you cannot modify it even with sudo vi.

If you proceed on creating an issue for Rider, feel free to use all the info I posted here, including the images, also if you need any additional testing on mac, let me know and I'll do it as soon as possible.

pamidur commented 4 years ago

I'm not a mac user, I found this on internets:

    To permanently add a PATH entry for all users on Mac OS (global), the /etc/paths file is used.
    sudo nano /etc/paths
panosru commented 4 years ago

@pamidur /etc/paths already had the path that lead to dotnet binary: image

So I guess it isn't PATH issue, it must be something else.

contractcooker commented 3 years ago

I am having this same issue. I can build via terminal with dotnet build but not from within rider. Curious if anyone has figured out the root cause here.

pamidur commented 3 years ago

Try building this csproj alone:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="dotnet --version" />
  </Target>

</Project>

If it doesn't then the reason is that the Rider screws environment variables when it triggers the build. Basically when the build starts the Rider creates another shell process without copying envs

pamidur commented 3 years ago

You may want to compare outputs of

echo $PATH

and dotnet build this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Message Text="$(PATH)" Importance="high" />
  </Target>

</Project>
contractcooker commented 3 years ago

Just wanted to report back. Went to try your suggestions today and everything is working as it should.

panosru commented 3 years ago

I'm terribly sorry for not replying in time (lol, 1 year late...), I couldn't spend time on it due to various reasons, but today I tried it again from scratch and it works fine! I believe this issue can be closed :)

pamidur commented 2 years ago

Thank you for circling back to us! Glad to know it is fixed now!