Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[.NET 9.0 Preview 5] Could not load file or assembly NuGet.ProjectModel #9037

Closed
alexeygritsenko opened this issue Jun 15, 2024 · 15 comments · Fixed by #9098
Closed

[.NET 9.0 Preview 5] Could not load file or assembly NuGet.ProjectModel #9037

alexeygritsenko opened this issue Jun 15, 2024 · 15 comments · Fixed by #9098
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects. Area: Bindings Issues in Java Library Binding projects. bug Component does not function as intended.
Milestone

Comments

@alexeygritsenko
Copy link

Description

Hi, I'm looking forward to AndroidMavenLibrary, this is what I've been waiting for for a very long time and want to try. After all, for full-fledged development, we always lacked the libraries that developers use in Android Studio.

So I installed:
VS 2022 Preview
.NET 9.0 Preview 5
.NET 9.0 MAUI Workload

And added to Android Binding project

<ItemGroup>
  <AndroidMavenLibrary Include="com.squareup.okhttp3:okhttp" Version="4.9.3" />
</ItemGroup>

then output error:

1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028: System.IO.FileNotFoundException: Could not load file or assembly 'NuGet.ProjectModel, Version=6.9.1.3, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The specified file cannot be found.
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028: File name: 'NuGet.ProjectModel, Version=6.9.1.3, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028:    at Xamarin.Android.Tasks.NuGetPackageVersionFinder.Create(String filename, TaskLoggingHelper log)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028:    at Xamarin.Android.Tasks.DependencyResolver..ctor(String lockFile, TaskLoggingHelper log) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/JavaDependencyVerification.cs:line 124
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028:    at Xamarin.Android.Tasks.JavaDependencyVerification.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/JavaDependencyVerification.cs:line 77
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028:    at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25

Steps to Reproduce

see above

Link to public reproduction project repository

No response

Version with bug

Unknown/Other

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@drasticactions
Copy link

I don't believe this is related to the MAUI UI project. This is dotnet Android SDK tooling. Can you try this in a dotnet Android project (dotnet new android) that doesn't implement the MAUI UI tools? If it fails there, then I think you want the android tooling repo instead. @jonathanpeppers what do you think?

Copy link

Hi @alexeygritsenko. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@jonathanpeppers jonathanpeppers transferred this issue from dotnet/maui Jun 17, 2024
@jonathanpeppers jonathanpeppers added this to the .NET 9 milestone Jun 17, 2024
@jonathanpeppers jonathanpeppers added Area: App+Library Build Issues when building Library projects or Application projects. Area: Bindings Issues in Java Library Binding projects. labels Jun 17, 2024
@jonathanpeppers
Copy link
Member

@alexeygritsenko I would imagine this works fine with dotnet build? And it's only broken in Visual Studio or .NET framework MSBuild.exe?

@alexeygritsenko
Copy link
Author

@drasticactions, Yes, this is reproduced in android native project (without MAUI), I didn't know that you have dotnet/android repo. Sorry if I posted this topic in the wrong place.
@jonathanpeppers, I did not think about that. You're right. I just checked through the command line and there is no such error.

In this case, when can I expect Visual Studio support?

@jpobst
Copy link
Contributor

jpobst commented Jun 17, 2024

While we should of course address this root issue, also note that we already maintain an okhttp3 binding: https://www.nuget.org/packages/Square.OkHttp3.

@dotnet dotnet deleted a comment from github-actions bot Jun 17, 2024
@jonathanpeppers
Copy link
Member

I had a memory of "how does this work without us shipping NuGet.ProjectModel.dll"?

But I can't remember where I posted that comment:

@alexeygritsenko
Copy link
Author

While we should of course address this root issue, also note that we already maintain an okhttp3 binding: https://www.nuget.org/packages/Square.OkHttp3.

Thanks. As for Square.OkHttp3 library, I know that it already exists in Nuget, I was just interested in testing how it works. In the future, I want to connect libraries that are not available on Nuget in this way, for example mikepenz/FastAdapter

@jpobst
Copy link
Contributor

jpobst commented Jun 17, 2024

I had a memory of "how does this work without us shipping NuGet.ProjectModel.dll"?

I suspect it works because the msbuild process will very likely already have the NuGet libraries loaded in order to do the project build. However they may not be the version we expect, or perhaps a VS build can be broken up into small enough pieces that NuGet isn't in that msbuild process.

I think we will likely need to ILRepack the libraries to ensure we always have and use our expected versions.

@jonathanpeppers
Copy link
Member

I think we will likely need to ILRepack the libraries to ensure we always have and use our expected versions.

I wanted to remove ILRepack, can we just ship the assembly like we normally would?

@drasticactions
Copy link

@drasticactions, Yes, this is reproduced in android native project (without MAUI), I didn't know that you have dotnet/android repo. Sorry if I posted this topic in the wrong place.

No worries! It's hard to tell sometimes where issues should go, since these tools are so interlinked and the brands can make it hard to tell what is going on. We just wanna make sure it gets to the right place. It's always better to file an issue somewhere than not file it at all.

@jpobst
Copy link
Contributor

jpobst commented Jun 17, 2024

I wanted to remove ILRepack, can we just ship the assembly like we normally would?

Given that there is very likely a copy of this assembly already loaded in the msbuild process, and it may not match our version, I think we need it ILRepack'd but I could be wrong. I would think the same about other assemblies we currently ILRepack like Newtonsoft.JSON as well.

@jonathanpeppers
Copy link
Member

No it's strong named and versioned, so it should work to just ship a copy.

@jpobst
Copy link
Contributor

jpobst commented Jun 17, 2024

No it's strong named and versioned, so it should work to just ship a copy.

That should work for .NET framework which supports strong naming, but I suspect it won't work on .NET 6+ which doesn't check strong names.

For .NET Core and .NET 5+, strong-named assemblies do not provide material benefits. The runtime never validates the strong-name signature, nor does it use the strong-name for assembly binding.

(https://learn.microsoft.com/en-us/dotnet/standard/assembly/strong-named)

For example, let's say we use an API added in Newtonsoft.Json 12.0, and a user's project includes another MSBuild task assembly that references and ships Newtonsoft.Json 11.0.

On a dotnet build, if their MSBuild task assembly is loaded first it will load 11.0 and I suspect our task would fail.

@jpobst jpobst added the bug Component does not function as intended. label Jul 9, 2024
@dellis1972
Copy link
Contributor

The old problem was that VS had third party extensions (ILSpy/Refactor) which shipped incompatible assembly versions for things like Cecil and NewtonSoft. These would break our tasks, so ILRepack was the only option.

Someone needs to test this in VS to make sure we do not hit any clashes.

@jonathanpeppers
Copy link
Member

I think this PR will work in both VS and dotnet build:

An old sample I have is:

  1. VS / .NET framework should work, because the NuGet assemblies are strong-named and versioned. .NET framework can only load a single assembly with the same "identity", no matter if it is a different file path.

  2. dotnet build should work. Even though it doesn't validate strong names, it can successfully load different assemblies from <UsingTask/>. dotnet build should be able to load assemblies of the same name with different paths.

ILRepack seems more useful for fixing Mono MSBuild and VS Mac, as Mono didn't do either 1 or 2.

If we are concerned, we could try different versions of Newtonsoft.Json in my sample above?

jonathanpeppers pushed a commit that referenced this issue Aug 6, 2024
…9098)

Fixes: #9037

Attempting to use `@(AndroidMavenLibrary)` in Visual Studio 17.11 P3,
which ships .NET 9 P5, like this:

	<ItemGroup>
	  <AndroidMavenLibrary Include="com.squareup.okhttp3:okhttp" Version="4.9.3" />
	</ItemGroup>

gives you the error:

	…\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028: System.IO.FileNotFoundException: Could not load file or assembly 'NuGet.ProjectModel, Version=6.9.1.3, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The specified file cannot be found.
	…\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028: File name: 'NuGet.ProjectModel, Version=6.9.1.3, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
	…\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028:    at Xamarin.Android.Tasks.NuGetPackageVersionFinder.Create(String filename, TaskLoggingHelper log)
	…\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028:    at Xamarin.Android.Tasks.DependencyResolver..ctor(String lockFile, TaskLoggingHelper log) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/JavaDependencyVerification.cs:line 124
	…\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028:    at Xamarin.Android.Tasks.JavaDependencyVerification.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/JavaDependencyVerification.cs:line 77
	…\Microsoft.Android.Sdk.Windows\34.99.0-preview.5.308\tools\Xamarin.Android.Bindings.JavaDependencyVerification.targets(27,5): error XAJDV7028:    at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25

We need to ship `NuGet.ProjectModel.dll` as well as its dependencies
to ensure they are always available to our MSBuild tasks.

Note that NuGet does not ship `.pdb` files for these assemblies in
their NuGet packages.  We will need to exclude these assemblies from
`.pdb` checking.

This wasn't caught by CI because CI tests use `dotnet build`, *not*
Visual Studio, and the `dotnet` install contains `NuGet.ProjectModel`
and related dependencies:

	% find bin/Debug/dotnet -iname NuGet.Project\*
	bin/Debug/dotnet/sdk/…/Sdks/Microsoft.NET.Sdk/tools/net472/NuGet.ProjectModel.dll
	bin/Debug/dotnet/sdk/…/NuGet.ProjectModel.dll
	…
@github-actions github-actions bot locked and limited conversation to collaborators Sep 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App+Library Build Issues when building Library projects or Application projects. Area: Bindings Issues in Java Library Binding projects. bug Component does not function as intended.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants