tonybaloney / CSnakes

https://tonybaloney.github.io/CSnakes/
MIT License
335 stars 25 forks source link

FromConda raises obscure exception when used with the folder instead of binary path #284

Closed homerokzam closed 1 month ago

homerokzam commented 1 month ago

Hi,

Congratulations on this fantastic project! I’m really excited to see how it evolves.

However, I’m encountering an issue with my project where I’m unable to generate C# (.cs) code based on my Python code as expected. I’ve followed the instructions but couldn’t get the C# code generation to work properly.

Here is a link to my project on GitHub: https://github.com/homerokzam/dotnet-python .

Any help or guidance would be greatly appreciated!

Best regards, Homero Kzam

tonybaloney commented 1 month ago
Rebuild started at 7:03 AM...
Restored C:\Users\anthonyshaw\Source\Repos\dotnet-python\Python.Core\Python.Core.csproj (in 3 ms).
1>------ Rebuild All started: Project: Python.Console, Configuration: Debug Any CPU ------
2>------ Rebuild All started: Project: Python.Core, Configuration: Debug Any CPU ------
Restored C:\Users\anthonyshaw\Source\Repos\dotnet-python\Python.Console\Python.Console.csproj (in 96 ms).
1>C:\Program Files\dotnet\sdk\9.0.100-rc.1.24452.12\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(326,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
2>C:\Program Files\dotnet\sdk\9.0.100-rc.1.24452.12\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(326,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
1>C:\Users\anthonyshaw\Source\Repos\dotnet-python\Python.Console\Program.cs(27,24,27,34): error CS1061: 'IPythonEnvironment' does not contain a definition for 'HelloWorld' and no accessible extension method 'HelloWorld' accepting a first argument of type 'IPythonEnvironment' could be found (are you missing a using directive or an assembly reference?)
1>Done building project "Python.Console.csproj" -- FAILED.
2>Python.Core -> C:\Users\anthonyshaw\Source\Repos\dotnet-python\Python.Core\bin\Debug\net8.0\Python.Core.dll
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
========== Rebuild completed at 7:03 AM and took 03.118 seconds ==========

Looking at the csproj I noticed 2 things:

  1. The Python file wasn't marked as an additional files. I noticed you have AdicionalFiles. I don't know if MSBuild accepts other languages for the build spec, but its best to use English
  2. The nuget package for CSnakes.Runtime needs to be installed in the project with the Python code, because that package has the source generator.

This is what your project file should look like:

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
  </PropertyGroup>

  <ItemGroup>
    <AdditionalFiles Include="hello_world.py">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </AdditionalFiles>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="CSnakes.Runtime" Version="1.0.21" />
  </ItemGroup>
</Project>

Once you do that, rebuild the projects individually.

On the python file, your function was missing type annotations. These are really important to CSnakes otherwise it will treat parameters and responses as "PyObject" and you have to manually do the conversion.

On the console project, I noticed some other issues:

  1. The second project wasn't a dependency
  2. HelloWorld wasn't constructed quite correctly
tonybaloney commented 1 month ago

Here's a diff of the required changes to get your project working https://github.com/homerokzam/dotnet-python/pull/1

homerokzam commented 1 month ago

Thank you so much for the detailed feedback!

I want to sincerely apologize for my oversight with the tag. I didn’t realize it was incorrect (using “AdicionalFiles”) and should have been in English. That was definitely my mistake, and thank you so much for pointing that out!

After following your advice and correcting the project file, I was able to successfully compile the project, and I can’t thank you enough for the guidance. It really helped me move forward!

However, I’m now encountering a runtime error when trying to execute the project. The error message is as follows: Exception has occurred: CLR/System.ComponentModel.Win32Exception An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Diagnostics.Process.dll: 'The FileName property should not be a directory unless UseShellExecute is set.' at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) at CSnakes.Runtime.ProcessUtils.ExecuteCommand(ILogger logger, ProcessStartInfo startInfo) at CSnakes.Runtime.ProcessUtils.ExecuteCommand(ILogger logger, String fileName, String arguments) at CSnakes.Runtime.Locators.CondaLocator.ExecuteCondaCommand(String arguments) at CSnakes.Runtime.Locators.CondaLocator..ctor(ILogger logger, String condaBinaryPath) at CSnakes.Runtime.ServiceCollectionExtensions.<>c__DisplayClass9_0.b__0(IServiceProvider sp) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext

Once again, thank you so much for your patience and help — it’s been instrumental in getting me this far!

Best regards

homerokzam commented 1 month ago

I found the issue! The problem was with the parameter in the .FromConda() method. The incorrect path was being used. The correct value for my environment is:

.FromConda("/opt/homebrew/Caskroom/miniconda/base/condabin/conda")

After updating it, the project runs as expected.

Thank you again for your help!

tonybaloney commented 1 month ago

That error message could be more helpful. I doubt you'll be the last person to confuse what that parameter should be