microsoft / react-native-windows

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

Experimental nuget needs autolink-windows when app consumes library #csharp #11976

Open caiovictor opened 1 year ago

caiovictor commented 1 year ago

Problem Description

Hello everybody,

I work on an RNW v0.70.19 project where I have a csharp app that consumes a lot of csharp and cpp libraries, all in RNW as well. Recently I'm trying to use the experimental nuget to improve the build performance of my solution, but I've been having some problems making use of the experimental nuget in my project.

I had a lot of errors in my project, so I decided to test it in a new app to find out if my project's architecture is compatible with the RNW standard.

How I did an PoC of experimental nuget:

  1. I created a new app in csharp, because the app in my project is csharp.
  2. Also created two libraries, one in scharp and the other in cpp. Added both as dependencies in my app, installed and did autolink.
  3. I noticed the csharp library references Microsoft.ReactNative.Managed and Microsoft.ReactNative.Managed.CodeGen locally (from node_modules) projects, and even includes them within the solution.
  4. Then I removed manually this two projects of my solution and try to build again, but as these projects were added by autolink, I got autolink error. rnw-error-autolink

NOTE: If I added just cpp library, the solution build corretly. So I believe that the error is only in the csharp library.

Steps To Reproduce

  1. Clone https://github.com/caiovictor/rnw-070-experimental-nuget
  2. Open app solution ./myApp/windows/myApp.sln on VS and build.
  3. Get autolink error.

Expected Results

The solution (app + library) is expected to build correctly.

CLI version

9.3.4

Environment

react v18.1.0
react-native v0.70.10
react-native-windows v0.70.19

Target Platform Version

10.0.19041

Target Device(s)

Desktop

Visual Studio Version

Visual Studio 2019

Build Configuration

Debug

Snack, code example, screenshot, or link to a repository

No response

jonthysell commented 1 year ago

First off, RNW 0.70 is no longer supported, please use a supported version of RNW: https://microsoft.github.io/react-native-windows/support

Also, how did you create the libraries? It looks like you ran npx react-native init and created app projects named MyLibrary. That's an app project with a library name - please use the instructions for making a new library project: https://microsoft.github.io/react-native-windows/docs/native-modules-setup#creating-a-new-native-module-library-project

Since you want to use the nugets, please include the experimental nuget flag when creating the library projects too - that will prevent the spurious project entries in the solution file.

Finally, the output give suggests a command to fix the error. Did you try running the command?

microsoft-github-policy-service[bot] commented 1 year ago

This issue is reported on an older version of React Native for Windows than is current, and is believed to be fixed in the latest version. Please try to update to the latest version and confirm if your issue still occurs. Per-version documentation is available here

caiovictor commented 1 year ago

First off, RNW 0.70 is no longer supported, please use a supported version of RNW: https://microsoft.github.io/react-native-windows/support

Also, how did you create the libraries? It looks like you ran npx react-native init and created app projects named MyLibrary. That's an app project with a library name - please use the instructions for making a new library project: https://microsoft.github.io/react-native-windows/docs/native-modules-setup#creating-a-new-native-module-library-project

Since you want to use the nugets, please include the experimental nuget flag when creating the library projects too - that will prevent the spurious project entries in the solution file.

Finally, the output give suggests a command to fix the error. Did you try running the command?

Thanks for the reply @jonthysell

I hadn't seen that v0.70 was no longer supported :grimacing:

But I created the app with these commands that I put in the readme.

And yes, I included experimental nuget flag, here.

I'll try it on a supported version and give you feedback ASAP.

caiovictor commented 1 year ago

The same error happens for RNW 0.71. I added on this repo: https://github.com/caiovictor/rnw-071-experimental-nuget

Steps:

  1. I created another repository in the recommended patterns.
  2. When adding the library as a dependency of the app, the autolink is requested, however the autolink adds Microsoft.ReactNative.Managed and Microsoft.ReactNative.Managed.CodeGen in the solution, however the app and the library have the experimental nuget flag enabled. image

Using react: 18.1.0 react-native: ^0.71.0 react-native-windows: 0.71.27

caiovictor commented 1 year ago

Also tested on 0.72 and error persist. Added on https://github.com/caiovictor/rnw-072-experimental-nuget image

react: 18.2.0 react-native: 0.72.3 react-native-windows: 0.72.4

caiovictor commented 1 year ago

Quick update, I found an weird condition on autolink source, where always is added Microsoft.ReactNative.Managed and Microsoft.ReactNative.Managed.CodeGen in CSharp native modules, but using experimental nugets we dont need this.

So, I created a patch in my repo v0.71 experimental nuget to @react-native-windows/cli. I just added an condition to verify if experimental nuget flag is enabled, if so, we don't add those projects in the solution, otherwise, we add them normally as before.

This way the app build normally.

I would like to know if this really can be the fix for this issue, if so can I open a PR for this.

jonthysell commented 1 year ago

I would like to know if this really can be the fix for this issue, if so can I open a PR for this.

Thanks for your repro on 0.71! Yes, if the app project is building and your csharp lib is getting registered and available to the JS, then your fix looks fine and please open a PR.