devlooped / jq

A nuget distribution of the official JQ implementation, for easy consumption from .NET
MIT License
3 stars 0 forks source link

JQ.Path returns the wrong value in LinqPad #17

Open Arithmomaniac opened 2 months ago

Arithmomaniac commented 2 months ago

Describe the Bug

JQ.Path incorrectly assumes the jq.exe assembly will always be in AppContext.BaseDirectory, when that is not the case. This causes FileNotFoundExceptions at runtime.

Steps to Reproduce

Using LinqPad 8:

JQ.Path.Dump();
typeof(JQ).Assembly.Location.Dump();

try { var _ = (await JQ.ExecuteAsync("""{"records":[]}""", """.records[]""")); }
catch (Exception ex) { ex.ToString().Dump(); }

Expected Behavior

The path points to the actual location on disk (C:\Users\arithmomaniac\.nuget\packages\devlooped.jq\1.7.1.1\tools\jq-windows-amd64.exe)

Exception with Stack Trace

Console output:

C:\Users\arithmomaniac\AppData\Local\Temp\LINQPad8\_pkwjxppc\shadow-3\lib\jq-windows-amd64.exe
C:\Users\arithmomaniac\.nuget\packages\Devlooped.JQ\1.7.1.1\lib\net8.0\JQ.dll
System.IO.FileNotFoundException: JQ executable not found.
File name: 'C:\Users\arithmomaniac\AppData\Local\Temp\LINQPad8\_pkwjxppc\shadow-3\lib\jq-windows-amd64.exe'
   at Devlooped.JQ.ExecuteAsync(String json, String query) in /_/src/JQ/JQ.cs:line 60
   at UserQuery.Main() in C:\Users\arithmomaniac\AppData\Local\Temp\LINQPad8\_pkwjxppc\vrmzoe\LINQPadQuery:line 4

Version Info

dotnet 8 Devlooped.JQ 1.7.1.1

Additional Info

Seems like the right way to "include" the executable may be more like this: https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#generatepathproperty

Back this issue Back this issue

kzu commented 2 months ago

Thank you for reporting the issue. Since my initial usage of this tool is within the context of a dotnet global tool (and since the included executable is provided as a runtime asset), I did not consider this LINQPad-specific usage. The way we include the binaries in the containing app is:

    <Content Include="$(MSBuildThisFileDirectory)../tools/*.*" 
             Link="lib/%(Filename)%(Extension)"
             Visible="false"
             CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

It seems LINQPad isn't running the code as a console app and is setting up things differently. Note that out approach works even in unit tests, so there's something about this particular scenario.

I don't have access to LINQPad 8 premium (required to install arbitrary nuget packages), so I can't work on this at the moment.

If you feel generous and want to gift me that version (I do have premium of v5, but that one doesn't even allow me to install the package), I'd be very grateful and fix this for you: https://www.linqpad.net/Purchase.aspx.

Arithmomaniac commented 2 months ago

Instead of gifting you a license (not that you don't deserve it 😉), you can see a similar result in NetPad, a free clone:

C:\Users\avilevin\AppData\Local\Temp\NetPad\Processes\0fdf6648-fd68-468f-88f5-c149a180c0f2\lib\jq-windows-amd64.exe
C:\Users\avilevin\AppData\Local\Temp\NetPad\Processes\0fdf6648-fd68-468f-88f5-c149a180c0f2\JQ.dll
System.IO.FileNotFoundException: JQ executable not found.
File name: 'C:\Users\avilevin\AppData\Local\Temp\NetPad\Processes\0fdf6648-fd68-468f-88f5-c149a180c0f2\lib\jq-windows-amd64.exe'
   at Devlooped.JQ.ExecuteAsync(String json, String query) in /_/src/JQ/JQ.cs:line 60
   at Program.<Main>$(String[] args) in :line 26

(Here JQ.dll isn't even pointing to the package directory; that might be a bug in NetPad itself. If you try the GeneratePathProperty guidance and it still doesn't work, I'll file the bug with them.)

kzu commented 2 months ago

Hi @Arithmomaniac. I'll consider a PR for this. For the primary usage in multiple core scenarios (console apps, web apps, azure functions), the current approach is working great for me, so this is very low priority ATM.

kzu commented 2 months ago

You can now back a specific issue to help prioritizing: back an issue 🙏