microsoft / PTVS

Python Tools for Visual Studio
https://aka.ms/PTVS
Apache License 2.0
2.53k stars 675 forks source link

Python project reports a .NET Framwork error when .NET Core library is referenced #7406

Open vsfeedback opened 1 year ago

vsfeedback commented 1 year ago

This issue has been moved from a ticket on Developer Community.


[severity:It bothers me. A fix would be nice] Python project reports a problem (when it references a .NET Core library project): "Project '.. \ClassLibrary\ClassLibrary.csproj' targets 'net6.0'. It cannot be referenced by a project that targets '. NETFramework,Version=v4.7.2'. PythonExample C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 1830"

Steps to reproduce: Create a .NET Core library and reference it by Python project.

The Python interpreter has pythonnet package installed and loads and executes .NET Core library successfully. Just the error is without meaning as Python projects use Python interpreter and not .NET framework.

The example project illustrating the error is attached.


Original Comments

Feedback Bot on 2/15/2023, 10:13 PM:

(private comment, text removed)


Original Solutions

(no solutions)

rajko-horvat commented 1 year ago

PythonExample.zip

This is the example code that produces the error.

int19h commented 1 year ago

Python projects pretend to be .NET Framework projects to integrate with VS web tooling. This is the unfortunate side effect.

That said, references from Python projects to .NET ones are generally of limited use; I'm not even sure we set up the search path properly in this case. Is there any particular functionality that you expect to become available if the reference worked?

rajko-horvat commented 1 year ago

Hi, It's other way around. I'm referencing .NET core library project in Python console project because Python uses .NET core library. In that context it should not matter what type of .NET project(s) are referenced in Python project as long as Python code uses them properly, and the aforementioned error should not be generated at all.

Also, the generated binary .NET library file is in a proper place for Python code to use it.

I hope that clears the things a little.

int19h commented 1 year ago

That's the kind of reference I was referring to, as well: a Python project referencing a non-Python one. This really only does one thing: it adds the output directory of the other project to the Python project search paths. This feature was originally designed with C++ DLL projects in mind, though, so I'm not even sure that it works correctly for .NET project.

For now, I think, this is just not a supported scenario for this feature. The workaround is to manually add a (relative) search path to point at the output of the .NET project for the same effect.

If we do decide to implement this, we'd need to rework all our .targets files to build on top of .NET Core .targets instead of .NET Framework ones. This might be non-trivial when it comes to wiring up web tooling - we use a lot of ASP.NET WebForms machinery, but it's not supported in .NET Core.

rajko-horvat commented 1 year ago

It works correctly as it outputs the .NET core library in Python directory successfully. Well, I had to rework our ASP.NET WebForms and code to .NET core and it is not trivial, but once you get a sense of it it goes smoothly ;) I have to admit, some things in .NET core are so much easier to implement than in ASP.NET.

It would be a nice to implement these things in the future :)