Skip to content

Commit

Permalink
Fix issue with new arch cpp-app apps not finding the bundle in releas…
Browse files Browse the repository at this point in the history
…e builds (#12754)

## Description

The new cpp-app template (and related apps `playground-composition` and `e2e-test-app-fabric`) were all set with an invalid `BundleRootPath`. This causes the app to throw an error because it can't parse the given path from the URI, furthermore, since it was a URI based on the current working directory, it might not even be the right directory to load the bundle from.

This PR fixes the issue by instead using the correct `file://` URI scheme and also looking for the bundle path relative to the location of the app exe itself. This PR also re-aligns the apps to use the similar instance settings setup of the older UWP app (using both the `BUNDLE` and `_DEBUG` macros appropriately).

Partially resolves #12752

### Type of Change
- Bug fix (non-breaking change which fixes an issue)

### Why
So that release builds work for the apps using the new template.


### What
See above.

## Screenshots
![image](https://github.com/microsoft/react-native-windows/assets/10852185/4a839f09-996b-464d-ada3-1878bdf835e6)

## Testing
Tested a new app works with the changes.

## Changelog
Should this change be included in the release notes: _yes_

Fixed issue with new arch cpp-app apps not finding the bundle in release builds
  • Loading branch information
jonthysell committed Feb 22, 2024
1 parent b0a01e7 commit e694482
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix issue with new arch cpp-app apps not finding the bundle in release builds",
"packageName": "react-native-windows",
"email": "jthysell@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,34 @@ void UpdateRootViewSizeToAppWindow(
winrt::Microsoft::ReactNative::ReactNativeHost CreateReactNativeHost(
HWND hwnd,
const winrt::Microsoft::UI::Composition::Compositor &compositor) {
WCHAR workingDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH, workingDir);
WCHAR appDirectory[MAX_PATH];
GetModuleFileNameW(NULL, appDirectory, MAX_PATH);
PathCchRemoveFileSpec(appDirectory, MAX_PATH);

auto host = winrt::Microsoft::ReactNative::ReactNativeHost();
// Disable until we have a 3rd party story for custom components
// RegisterAutolinkedNativeModulePackages(host.PackageProviders()); // Includes any
// autolinked modules

// Include any autolinked modules
// RegisterAutolinkedNativeModulePackages(host.PackageProviders());

host.PackageProviders().Append(winrt::make<RNTesterAppReactPackageProvider>());
host.PackageProviders().Append(winrt::AutomationChannel::ReactPackageProvider());

#if BUNDLE
host.InstanceSettings().JavaScriptBundleFile(L"index.windows");
host.InstanceSettings().DebugBundlePath(L"index");
host.InstanceSettings().BundleRootPath(std::wstring(L"file://").append(appDirectory).append(L"\\Bundle\\").c_str());
host.InstanceSettings().UseFastRefresh(false);
#else
host.InstanceSettings().JavaScriptBundleFile(L"index");
host.InstanceSettings().UseFastRefresh(true);
#endif

host.InstanceSettings().BundleRootPath(std::wstring(L"file:").append(workingDir).append(L"\\Bundle\\").c_str());
host.InstanceSettings().DebuggerBreakOnNextLine(false);
#if _DEBUG
host.InstanceSettings().UseDirectDebugger(true);
host.InstanceSettings().UseFastRefresh(true);
#endif
host.InstanceSettings().UseDeveloperSupport(true);
#else
host.InstanceSettings().UseDirectDebugger(false);
host.InstanceSettings().UseDeveloperSupport(false);
#endif

// Test App hooks into JS console.log implementation to record errors/warnings
host.InstanceSettings().NativeLogger([](winrt::Microsoft::ReactNative::LogLevel level, winrt::hstring message) {
Expand Down
6 changes: 2 additions & 4 deletions packages/e2e-test-app-fabric/windows/RNTesterApp-Fabric/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

// Windows Header Files
#include <windows.h>

#pragma push_macro("GetCurrentTime")
#undef GetCurrentTime
#include <pathcch.h>
#include <unknwn.h>

// Playground pch.h
#include <CppWinRTIncludes.h>
#include <winrt/Microsoft.ReactNative.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Foundation.h>
#pragma pop_macro("GetCurrentTime")

// C RunTime Header Files
#include <malloc.h>
Expand All @@ -31,5 +30,4 @@
#include <tchar.h>

// reference additional headers your program requires here
#include <unknwn.h>
#include <winrt/base.h>
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ struct WindowData {
if (!m_bundleFile.empty()) {
PCWSTR appName = (m_bundleFile == LR"(Samples\rntester)") ? L"RNTesterApp" : L"Bootstrap";

WCHAR workingDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH, workingDir);
WCHAR appDirectory[MAX_PATH];
GetModuleFileNameW(NULL, appDirectory, MAX_PATH);
PathCchRemoveFileSpec(appDirectory, MAX_PATH);

auto host = Host();
// Disable until we have a 3rd party story for custom components
Expand All @@ -150,7 +151,7 @@ struct WindowData {
host.InstanceSettings().JavaScriptBundleFile(m_bundleFile);

host.InstanceSettings().BundleRootPath(
std::wstring(L"file:").append(workingDir).append(L"\\Bundle\\").c_str());
std::wstring(L"file://").append(appDirectory).append(L"\\Bundle\\").c_str());
host.InstanceSettings().UseDeveloperSupport(true);

// Currently there is only SystemVisualSiteBridge which supports hosing ContentIslands within System
Expand Down
3 changes: 1 addition & 2 deletions packages/playground/windows/playground-composition/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
#define WINRT_LEAN_AND_MEAN 1

#include <windows.h>

// When WINAPI_FAMILY is DESKTOP_APP, windows.h creates a macro for GetCurrentTime, which conflicts with other headers
#undef GetCurrentTime

#include <pathcch.h>
#include <unknwn.h>

#include <CppWinRTIncludes.h>
Expand Down
21 changes: 14 additions & 7 deletions vnext/templates/cpp-app/windows/MyApp/MyApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ void UpdateRootViewSizeToAppWindow(
winrt::Microsoft::ReactNative::ReactNativeHost CreateReactNativeHost(
HWND hwnd,
const winrt::Microsoft::UI::Composition::Compositor &compositor) {
WCHAR workingDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH, workingDir);
WCHAR appDirectory[MAX_PATH];
GetModuleFileNameW(NULL, appDirectory, MAX_PATH);
PathCchRemoveFileSpec(appDirectory, MAX_PATH);

auto host = winrt::Microsoft::ReactNative::ReactNativeHost();

Expand All @@ -57,16 +58,22 @@ winrt::Microsoft::ReactNative::ReactNativeHost CreateReactNativeHost(

host.PackageProviders().Append(winrt::make<CompReactPackageProvider>());

#if BUNDLE
host.InstanceSettings().JavaScriptBundleFile(L"index.windows");
host.InstanceSettings().DebugBundlePath(L"index");
host.InstanceSettings().BundleRootPath(std::wstring(L"file://").append(appDirectory).append(L"\\Bundle\\").c_str());
host.InstanceSettings().UseFastRefresh(false);
#else
host.InstanceSettings().JavaScriptBundleFile(L"index");
host.InstanceSettings().UseFastRefresh(true);
#endif

host.InstanceSettings().BundleRootPath(std::wstring(L"file:").append(workingDir).append(L"\\Bundle\\").c_str());
host.InstanceSettings().DebuggerBreakOnNextLine(false);
#if _DEBUG
host.InstanceSettings().UseDirectDebugger(true);
host.InstanceSettings().UseFastRefresh(true);
#endif
host.InstanceSettings().UseDeveloperSupport(true);
#else
host.InstanceSettings().UseDirectDebugger(false);
host.InstanceSettings().UseDeveloperSupport(false);
#endif

winrt::Microsoft::ReactNative::ReactCoreInjection::SetTopLevelWindowId(
host.InstanceSettings().Properties(), reinterpret_cast<uint64_t>(hwnd));
Expand Down
3 changes: 2 additions & 1 deletion vnext/templates/cpp-app/windows/MyApp/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#define WINRT_LEAN_AND_MEAN 1

// Windows Header Files
#include <unknwn.h>
#include <windows.h>
#undef GetCurrentTime
#include <pathcch.h>
#include <unknwn.h>

// WinRT Header Files
#include <winrt/base.h>
Expand Down
2 changes: 1 addition & 1 deletion vnext/templates/cpp-lib/windows/MyLib/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#define WINRT_LEAN_AND_MEAN 1

// Windows Header Files
#include <unknwn.h>
#include <windows.h>
#undef GetCurrentTime
#include <unknwn.h>

// WinRT Header Files
#include <winrt/base.h>
Expand Down

0 comments on commit e694482

Please sign in to comment.