microsoft / react-native-windows

A framework for building native Windows apps with React.
https://microsoft.github.io/react-native-windows/
Other
16.35k stars 1.14k forks source link

"Could not load file or assembly" after installing latest Visual Studio 16.11.x (and 16.10.x) #8581

Closed tero-paananen closed 3 years ago

tero-paananen commented 3 years ago

After installing latest Visual Studio 16.11.2 and running react-native windows setup script in PowerShell we have problems to run our application in debug because Microsoft.ReactNative.Managed fails to this on app startup:

System.IO.FileLoadException
  HResult=0x80131040
  Message=Could not load file or assembly 'System.Linq, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
  Source=Microsoft.ReactNative.Managed
  StackTrace:
   at Microsoft.ReactNative.Managed.ReactPackageBuilderExtensions.InitAttributedModules(Assembly assembly) in C:\Users\xxx\Developer\app\packages\app\node_modules\react-native-windows\Microsoft.ReactNative.Managed\ReactPackageBuilderExtensions.cs:line 46
   at Microsoft.ReactNative.Managed.ReactPackageBuilderExtensions.<>c__DisplayClass4_0.<AddAttributedModules>b__1() in C:\Users\xxx\Developer\app\packages\app\node_modules\react-native-windows\Microsoft.ReactNative.Managed\ReactPackageBuilderExtensions.cs:line 53
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)

What would be changed? We have investigated this quite long. App starts by running command npx react-native run-windows --arch x64 .

Issue started after updating Visual Studio.

Environment

$ npx react-native --version 5.0.1

$ npx react-native info info Fetching system and libraries information... System: OS: Windows 10 10.0.19043 CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz Memory: 22.61 GB / 31.90 GB Binaries: Node: 12.22.5 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.5 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.14.14 - C:\Program Files\nodejs\npm.CMD Watchman: Not Found SDKs: Android SDK: Not Found Windows SDK: AllowDevelopmentWithoutDevLicense: Enabled AllowAllTrustedApps: Enabled Versions: 10.0.14393.0, 10.0.18362.0, 10.0.19041.0 IDEs: Android Studio: Not Found Visual Studio: 16.11.31624.102 (Visual Studio Community 2019) Languages: Java: Not Found npmPackages: @react-native-community/cli: Not Found react: Not Found react-native: 0.64.2 => 0.64.2 react-native-windows: 0.64.16 => 0.64.16 npmGlobalPackages: react-native: Not Found

Project

In solution i have package project that contains c# uwp and win32 console app. Debug app starts if i run only c# uwp application but if package project is as startup project debug fails that assembly error.

If i remove win32 app from package does not app start from package project still.

App config

<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion>10.0.18362.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.18362.0</TargetPlatformMinVersion>

<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
  <Version>6.2.8</Version>
</PackageReference>

Workaround

Read more about https://developercommunity2.visualstudio.com/t/Visual-Studio-2019-version-1610-Could/1434561?q=%5BVisual+Studio+2019+version+16.10%5D

Use VS 16.9.x then you do not need any workaround.

When using VS 16.11.1 or VS 16.11.2 (and seems that 16.10.x) Set this flag on your application project (.csproj) debug config <UseAppLocalCoreFramework>true</UseAppLocalCoreFramework>

Now VS 16.11.3 no debug build at least for me currently. I have to test more later.

tero-paananen commented 3 years ago

Do others have any problems with latest Visual Studio 16.11.2 ?

tero-paananen commented 3 years ago

I removed System.Linq from Global Assembly Cache but it did not help, same error still

gacutil -uf "System.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"

tero-paananen commented 3 years ago

I have tried "everything" else also:

tero-paananen commented 3 years ago

Interesting note.

When i build release store package from my app now with latest Visual Studio it added some System libraries into package. Previous build with older Visual Studio did not do that. Visual Studio added these for example libraries (dll) now: System.Collection.Immutable, System.Diagnostic.Contracts, System.Diagnostic.Runtime, System.Linq, System.Linq.Expressions, System.Threading. , System.Reflection.

On debug build there is same System libraries added into my package project /bin/x64/debug/folder for example that System.Linq. I do not know was these System libraries before on debug builds also.

What System library application try to use?

tero-paananen commented 3 years ago

There is same kind of VS issues

https://developercommunity.visualstudio.com/t/Visual-Studio-2019-version-1610UWP/1437211

https://developercommunity2.visualstudio.com/t/Visual-Studio-2019-version-1610-Could/1434561?q=%5BVisual+Studio+2019+version+16.10%5D

and i found note about extra flag for debug build to get work <UseAppLocalCoreFramework>true</UseAppLocalCoreFramework>

tero-paananen commented 3 years ago

I may be that i give up and install Visual Studio 16.9.9 to get fully working release builds...

Previous VS releases https://docs.microsoft.com/en-us/visualstudio/releases/2019/history#installing-an-earlier-release "Find latest 16.9.X version. Simply download “Professional” installer. Don’t worry. Double click the installer you download, and close the pop up in that installer when it starts, then you will find the community version. Click to install it !"

tero-paananen commented 3 years ago

What was invalid with release build? VS adds lot of libraries into build and that was not happened before for example System.Collection.Immutable, System.Diagnostic.Contracts, System.Diagnostic.Runtime, System.Linq, System.Linq.Expressions, System.Threading. , System.Reflection.

Release build of our app with same source code that before works a little different when building now, even with VS 16.9.9. I noted that it was react-native-storage that uses AsyncStorage which get stuck very easily for some reason staring now. I had to add some missing await calls into storage.js into react-native-storage. No idea why db stuck starts now maybe sqlite dll or something has changed? There was too many db request with missing await into AsyncStorage and db get stuck.

https://github.com/react-native-async-storage/async-storage/issues/671

tero-paananen commented 3 years ago

I use now VS 16.9.9 and release store build does not contain these additional libraries (above).

Finally the best solution may be use this flag in debug build <UseAppLocalCoreFramework>true</UseAppLocalCoreFramework> with latest Visual Studio 16.11.x and test that release works as you expect.

I may start using latest VS 16.11.3 on next week...what a mess this was and took about 30 hours to solve!

tero-paananen commented 3 years ago

I installed Visual Studio 16.11.3 and previous flag <UseAppLocalCoreFramework>true</UseAppLocalCoreFramework> is not enought here. Flag works on 16.10.x maybe and for sure 16.11.(1-2).

I have to revert back from 16.11.3 to 16.11.2. I start to cry...

Read more from https://developercommunity2.visualstudio.com/t/Visual-Studio-2019-version-1610-Could/1434561?q=%5BVisual+Studio+2019+version+16.10%5D

That mentioned new flag <Target Name="ResolvePackageDependenciesForBuild" /> into package .wapproj file did not help me - no debug build and error is that same assembly error - there is wrong dll's in debug build.

tero-paananen commented 3 years ago

Important note about chaning Visual Studio version!

I had problems to start using again VS 16.11.2 even i have previously managed to build with it with flag <UseAppLocalCoreFramework>true</UseAppLocalCoreFramework>

After uninstalling previous VS you have to clean some cache below. When i switched from VS 16.11.3 -> 16.11.2 i had to do following:

Visual Studio cache cleanup:

  1. Close Visual Studio - check that devenv.exe is not present in the Task Manager
  2. Remove files from %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\16.0\ComponentModelCache
  3. Remove files from %USERPROFILE%\AppData\Local\Temp

and after that cleaning i managed to run my app in the debug mode.

Now i have to check release build.

tero-paananen commented 3 years ago

VS 16.11.3 may work after again cleaning cache above but if currently installed VS 16.11.2 works i do not want to change anymore.

I have working debug and release build.

chrisglein commented 3 years ago

Hey @tero-paananen. Sorry this has been such a rough journey for you. Thanks for trying out the new AppServiceDemo sample and giving feedback. After reading through both threads my take on this is that there were some issues specific to VS versions, and also to some stale cached data when moving between VS versions. At this exact moment you're on a version that does work (one step behind latest), it's possible that with the cache cleaned up it'd work on a newer version, but you (understandably) don't want to mess with it more after getting something working.

You've provided good notes here that should help anyone else that runs into the same progression of issues upon update (thanks for that). But I think we're closed on this issue. Does that sound correct?

tero-paananen commented 3 years ago

It ok for me to close this. I hope that this long journey would help somebody else too.

tero-paananen commented 3 years ago

Saga continues. I am not able to upload my app into Windows Store https://github.com/microsoft/react-native-windows/issues/8846

tero-paananen commented 3 years ago

See updated hacks for Visual Studio 16.11.4-6 https://developercommunity.visualstudio.com/t/visual-studio-2019-version-1610uwpxamarin-runtime/1437211

tero-paananen commented 3 years ago

With Visual Studio 16.11.6 i have following setup of "additional" flags:

In my C# UWP project file .csproj

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <UseAppLocalCoreFramework>True</UseAppLocalCoreFramework>
    <AppxUseHardlinksIfPossible>False</AppxUseHardlinksIfPossible>    
   <AddSyntheticProjectReferencesForSolutionDependencies>False</AddSyntheticProjectReferencesForSolutionDependencies>
...

and in the end of file inside <Project> tags <Target Name="ResolvePackageDependenciesForBuild" />

in package project .wapproj

<ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|x64">
      <Configuration>Debug</Configuration>
      <Platform>x64</Platform>
      <AppxUseHardlinksIfPossible>False</AppxUseHardlinksIfPossible>
  <AddSyntheticProjectReferencesForSolutionDependencies>False</AddSyntheticProjectReferencesForSolutionDependencies>
    </ProjectConfiguration>
...

and in the end of file inside <Project> tags <Target Name="ResolvePackageDependenciesForBuild" />

chrisglein commented 3 years ago

Trying to sort out which issue reports are tracking which problems. What's the specific blocker on this one at this point? You reactivated against #8846, but that seems to be resolved. Are you just cataloging the workarounds?

tero-paananen commented 3 years ago

Trying to sort out which issue reports are tracking which problems. What's the specific blocker on this one at this point? You reactivated against #8846, but that seems to be resolved. Are you just cataloging the workarounds?

I only write down workaround / needed new flags to get this kind of solutions to build. If this could help someone.

asklar commented 2 years ago

Looks like this was due to this VS bug: https://developercommunity.visualstudio.com/t/Visual-Studio-2019-version-16113---Pac/1538001 fixed in 16.11.7 and 17.x

Kamek437 commented 5 months ago

I'm using the latest vs 2022 17.9.3 I think it is and getting:

ERROR: FileLoadException: Could not load file or assembly 'NuGet.VisualStudio.Contracts, Version=17.10.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I have yet to build anything at all with react native windows. Windows 11 Latest Visual Studio 2022 latest npx react-native -version : 13.6.6 npx react-native info :

System:
  OS: Windows 11 10.0.22631
  CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  Memory: 6.65 GB / 15.90 GB
Binaries:
  Node:
    version: 18.18.0
    path: C:\Program Files\nodejs\node.EXE
  Yarn:
    version: 3.6.4
    path: ~\OneDrive\Documents\node_modules\.bin\yarn.CMD
  npm:
    version: 9.6.3
    path: ~\OneDrive\Documents\node_modules\.bin\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK: Not Found
  Windows SDK:
    AllowDevelopmentWithoutDevLicense: Enabled
    AllowAllTrustedApps: Enabled
    Versions:
      - 10.0.19041.0
      - 10.0.20348.0
      - 10.0.22621.0
      - 10.0.26100.0
IDEs:
  Android Studio: Not Found
  Visual Studio:
    - 17.10.35004.147 (Visual Studio Community 2022)
Languages:
  Java:
    version: 21.0.3
    path: C:/Users/kamek/scoop/apps/microsoft-jdk/current/bin/javac.exe
  Ruby:
    version: 3.3.0
    path: C:\Ruby33-x64\bin\ruby.EXE
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.2
    wanted: 0.74.2
  react-native-windows:
    installed: 0.74.9
    wanted: 0.74.9
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found