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

Fixes issues with packaged asset resolution #10621

Merged
merged 2 commits into from
Oct 14, 2023

Conversation

rozele
Copy link
Collaborator

@rozele rozele commented Sep 22, 2022

Description

Type of Change

Erase all that don't apply.

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Why

Image.resolveAssetSource has two different code paths - one for resolving assets from the packager server when running in standard debug builds and one for resolving assets when the JS package is pre-bundled.

Previously, we were not providing the bundle root path to the SourceCodeModule for pre-bundled apps, and it was up to each individual native module and view manager that depends on resolving asset paths with Image.resolveAssetSource to replace file:// with the bundle root path pulled from the InstanceSettingsSnapshot.

Even after fixing the issue with SourceCodeModule, there is still one more issue for unpackaged apps, whose asset paths may be Windows file paths (e.g., C:\...) rather than packaged asset paths (e.g., ms-appx://).

Resolves #10620

What

This change ensures that SourceCodeModule provides the bundle root path as the scriptURL constant and patches the bug in the upstream resolveAssetSource module to skip coersion of the bundle root path to a form like file://.

Please note, this is technically a "breaking change", any 3rd party native modules or view managers that leveraged Image.resolveAssetSource assuming it would return a file:// path with a relative asset path will now see that the path is already fully qualified with the bundle root path.

Testing

Created bundled version of Playground, ran RNTester, and confirmed packaged image assets still resolve correctly (in this case, the thumbs up image is a package asset):

image

Microsoft Reviewers: Open in CodeFlow

@rozele rozele requested review from a team as code owners September 22, 2022 14:43
@rozele rozele added the Breaking Change This PR will break existing apps and should be part of the known breaking changes for the release label Sep 22, 2022
@rozele
Copy link
Collaborator Author

rozele commented Sep 22, 2022

Please note, I've labeled this as, strictly speaking, this will change the contract of Image.resolveAssetSource, but assuming any 3rd party modules are doing appropriate URI validation, it's unlikely for anything to break.

@rozele rozele force-pushed the issue10620 branch 2 times, most recently from bc37366 to 41a3c9a Compare September 22, 2022 15:05
}

function getScriptURL(): ?string {
if (_scriptURL === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see where _scriptURL comes from, but under which conditions would it be undefined? note that for image urls we support several non x-plat schemes apart from http/https, like ms-appx, ms-resource, resource, and some others; want to make sure this doesn't force these urls to be only "http/https" or "file" schemed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just memoizing a result.

This change should be independent of the actual bundle root path, whether it's ms-appx://, ms-resource:// or C:\... for unpackaged apps.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 74 to 76
if (/*packager_assert && */ ris.uri.find("file://") == 0) {
ris.uri.replace(0, 7, ris.bundleRootPath);
}
Copy link
Member

@asklar asklar Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea is that if you say file://foo.png, this turns into ms-appx:///Assets/Bundle/foo.png

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but now with the change to Image.resolveAssetSource, the URI will already come in as ms-appx:///Assets/Bundle/foo.png

Copy link
Collaborator Author

@rozele rozele Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are truly consuming a bundled asset in React Native, you should do so by using:

<Image source={require('foo.png')} />

Rather than relying on an obscure implementation detail like:

<Image source={{uri: 'file://foo.png'}} />

So, this is another surface that is a "breaking change", but to be fair, whoever was depending on this implementation detail that they should not have been depending on it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether our internal partners are aware of this / using the right format, TBH. @stmoy FYI.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, scratch that, using file://asset would never work because the resolved asset source would not include the internal '__packager_asset' flag, so this probably isn't breaking on that front.

It would only affect callsites where you use Image.resolveAssetPath and pass the result to some custom module or view manager that does not do proper validation on the URI (assumes that URIs will start with file://).

We needed zero code changes in Messenger when taking in this change. We were however able to delete a bunch of code for custom URI resolution.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, scratch that, using file://asset would never work because the resolved asset source would not include the internal '__packager_asset' flag, so this probably isn't breaking on that front.

It would only affect callsites where you use Image.resolveAssetPath and pass the result to some custom module or view manager that does not do proper validation on the URI (assumes that URIs will start with file://).

We needed zero code changes in Messenger when taking in this change. We were however able to delete a bunch of code for custom URI resolution.

rozele added a commit to rozele/react-native-windows that referenced this pull request Oct 23, 2022
Summary:
`Image.resolveAssetSource` has two different code paths - one for
resolving assets from the packager server when running in standard debug
builds and one for resolving assets when the JS package is pre-bundled.

Previously, we were not providing the bundle root path to the
`SourceCodeModule` for pre-bundled apps, and it was up to each
individual native module and view manager that depends on resolving
asset paths with `Image.resolveAssetSource` to replace `file://` with
the bundle root path pulled from the InstanceSettingsSnapshot.

Even after fixing the issue with `SourceCodeModule`, there is still one
more issue for unpackaged apps, whose asset paths may be Windows file
paths (e.g., `C:\...`) rather than packaged asset paths (e.g.,
`ms-appx://`).

This change ensures that SourceCodeModule provides the bundle root path
as the `scriptURL` constant and patches the bug in the upstream
`resolveAssetSource` module to skip coersion of the bundle root path to
a form like `file://`.

Resolves microsoft#10620

Test Plan: See test plan in D39735423

Reviewers: lyahdav, shawndempsey, chpurrer, mcenk, jacke, helenistic, mowang

Reviewed By: mowang

Differential Revision: https://phabricator.intern.facebook.com/D39732643

Tasks: T132565343, T131482365
@chiaramooney chiaramooney requested a review from asklar November 2, 2022 23:33
@acoates-ms
Copy link
Contributor

@rozele, looks like this PR breaks one of the integration tests.
I think this one isn't loading the image correctly for some reason.

source={require('react-native-windows/IntegrationTests/blue_square.png')}

rozele added a commit to rozele/react-native-windows that referenced this pull request Mar 2, 2023
Summary:
`Image.resolveAssetSource` has two different code paths - one for
resolving assets from the packager server when running in standard debug
builds and one for resolving assets when the JS package is pre-bundled.

Previously, we were not providing the bundle root path to the
`SourceCodeModule` for pre-bundled apps, and it was up to each
individual native module and view manager that depends on resolving
asset paths with `Image.resolveAssetSource` to replace `file://` with
the bundle root path pulled from the InstanceSettingsSnapshot.

Even after fixing the issue with `SourceCodeModule`, there is still one
more issue for unpackaged apps, whose asset paths may be Windows file
paths (e.g., `C:\...`) rather than packaged asset paths (e.g.,
`ms-appx://`).

This change ensures that SourceCodeModule provides the bundle root path
as the `scriptURL` constant and patches the bug in the upstream
`resolveAssetSource` module to skip coersion of the bundle root path to
a form like `file://`.

Resolves microsoft#10620

Test Plan: See test plan in D43159275

Reviewers: shawndempsey, chpurrer, lefever, helenistic

Reviewed By: chpurrer

Differential Revision: https://phabricator.intern.facebook.com/D43158676

Tasks: T140424672
rozele added a commit to rozele/react-native-windows that referenced this pull request Mar 2, 2023
Summary:
Prior to PR microsoft#10621, the ImageViewManager for Paper and
ImageViewComponent for Fabric required a workaround where file:// URIs
were remapped to an absolute path using the InstanceSettings from the
ReactContext.

After microsoft#10621, the URI is already fully resolved by the time it reaches
the native renderer for Image, so we do not need to do any URI rewrites.

Test Plan:
Images still load in RNTester:
{F868525679}

Reviewers: shawndempsey, helenistic, chpurrer, lefever

Reviewed By: chpurrer

Differential Revision: https://phabricator.intern.facebook.com/D43159275

Tasks: T140424672
@asklar asklar removed their assignment Mar 15, 2023
@chiaramooney chiaramooney added the Needs: Author Feedback The issue/PR needs activity from its author (label drives bot activity) label Mar 29, 2023
@chiaramooney
Copy link
Contributor

Waiting on merge conflict resolution and integration test to pass.

@rozele
Copy link
Collaborator Author

rozele commented Apr 2, 2023

Thanks @chiaramooney - I'll rebase and take another look at the failing integration test.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 An issue that had been tagged "Needs: Author Feedback" has received activity (label applied by bot) and removed Needs: Author Feedback The issue/PR needs activity from its author (label drives bot activity) labels Apr 2, 2023
@rozele rozele force-pushed the issue10620 branch 2 times, most recently from 75781e5 to 8950402 Compare April 2, 2023 23:42
@rozele
Copy link
Collaborator Author

rozele commented Jun 2, 2023

@chiaramooney I think all the tests should pass now. The latest failure looks like a flaky build issue.

@chiaramooney
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@chiaramooney
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rozele
Copy link
Collaborator Author

rozele commented Jul 17, 2023

@chiaramooney I've had terrible luck running the integration tests locally, not sure how to fix this.

@TatianaKapos
Copy link
Contributor

@rozele Would you mind rebasing off main one more time? Just to see if that will help :)

rozele added a commit to rozele/react-native-windows that referenced this pull request Oct 10, 2023
Summary:
`Image.resolveAssetSource` has two different code paths - one for
resolving assets from the packager server when running in standard debug
builds and one for resolving assets when the JS package is pre-bundled.

Previously, we were not providing the bundle root path to the
`SourceCodeModule` for pre-bundled apps, and it was up to each
individual native module and view manager that depends on resolving
asset paths with `Image.resolveAssetSource` to replace `file://` with
the bundle root path pulled from the InstanceSettingsSnapshot.

Even after fixing the issue with `SourceCodeModule`, there is still one
more issue for unpackaged apps, whose asset paths may be Windows file
paths (e.g., `C:\...`) rather than packaged asset paths (e.g.,
`ms-appx://`).

This change ensures that SourceCodeModule provides the bundle root path
as the `scriptURL` constant and patches the bug in the upstream
`resolveAssetSource` module to skip coersion of the bundle root path to
a form like `file://`.

Resolves microsoft#10620

Test Plan: See test plan in D43159275

Reviewers: shawndempsey, chpurrer, lefever, helenistic

Reviewed By: chpurrer

Differential Revision: https://phabricator.intern.facebook.com/D43158676

Tasks: T140424672
rozele added a commit to rozele/react-native-windows that referenced this pull request Oct 10, 2023
Summary:
`Image.resolveAssetSource` has two different code paths - one for
resolving assets from the packager server when running in standard debug
builds and one for resolving assets when the JS package is pre-bundled.

Previously, we were not providing the bundle root path to the
`SourceCodeModule` for pre-bundled apps, and it was up to each
individual native module and view manager that depends on resolving
asset paths with `Image.resolveAssetSource` to replace `file://` with
the bundle root path pulled from the InstanceSettingsSnapshot.

Even after fixing the issue with `SourceCodeModule`, there is still one
more issue for unpackaged apps, whose asset paths may be Windows file
paths (e.g., `C:\...`) rather than packaged asset paths (e.g.,
`ms-appx://`).

This change ensures that SourceCodeModule provides the bundle root path
as the `scriptURL` constant and patches the bug in the upstream
`resolveAssetSource` module to skip coersion of the bundle root path to
a form like `file://`.

Resolves microsoft#10620

Test Plan: See test plan in D43159275

Reviewers: shawndempsey, chpurrer, lefever, helenistic

Reviewed By: chpurrer

Differential Revision: https://phabricator.intern.facebook.com/D43158676

Tasks: T140424672
@rozele
Copy link
Collaborator Author

rozele commented Oct 10, 2023

@rozele Would you mind rebasing off main one more time? Just to see if that will help :)

Rebased :)

rozele and others added 2 commits October 10, 2023 13:00
`Image.resolveAssetSource` has two different code paths - one for
resolving assets from the packager server when running in standard debug
builds and one for resolving assets when the JS package is pre-bundled.

Previously, we were not providing the bundle root path to the
`SourceCodeModule` for pre-bundled apps, and it was up to each
individual native module and view manager that depends on resolving
asset paths with `Image.resolveAssetSource` to replace `file://` with
the bundle root path pulled from the InstanceSettingsSnapshot.

Even after fixing the issue with `SourceCodeModule`, there is still one
more issue for unpackaged apps, whose asset paths may be Windows file
paths (e.g., `C:\...`) rather than packaged asset paths (e.g.,
`ms-appx://`).

This change ensures that SourceCodeModule provides the bundle root path
as the `scriptURL` constant and patches the bug in the upstream
`resolveAssetSource` module to skip coersion of the bundle root path to
a form like `file://`.

Resolves microsoft#10620
@acoates-ms acoates-ms merged commit e3c7f02 into microsoft:main Oct 14, 2023
YajurG pushed a commit to YajurG/react-native-windows that referenced this pull request Oct 18, 2023
* Fixes issues with packaged asset resolution

`Image.resolveAssetSource` has two different code paths - one for
resolving assets from the packager server when running in standard debug
builds and one for resolving assets when the JS package is pre-bundled.

Previously, we were not providing the bundle root path to the
`SourceCodeModule` for pre-bundled apps, and it was up to each
individual native module and view manager that depends on resolving
asset paths with `Image.resolveAssetSource` to replace `file://` with
the bundle root path pulled from the InstanceSettingsSnapshot.

Even after fixing the issue with `SourceCodeModule`, there is still one
more issue for unpackaged apps, whose asset paths may be Windows file
paths (e.g., `C:\...`) rather than packaged asset paths (e.g.,
`ms-appx://`).

This change ensures that SourceCodeModule provides the bundle root path
as the `scriptURL` constant and patches the bug in the upstream
`resolveAssetSource` module to skip coersion of the bundle root path to
a form like `file://`.

Resolves microsoft#10620

* Change files
@jonthysell jonthysell removed the Needs: Attention 👋 An issue that had been tagged "Needs: Author Feedback" has received activity (label applied by bot) label Oct 30, 2023
@jonthysell jonthysell added the New Architecture Broad category for issues that apply to the RN "new" architecture of Turbo Modules + Fabric label Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Fabric Support Facebook Fabric Area: Image Breaking Change This PR will break existing apps and should be part of the known breaking changes for the release New Architecture Broad category for issues that apply to the RN "new" architecture of Turbo Modules + Fabric
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

SourceCodeModule returns empty string for bundle root path
6 participants