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

Capture ios|android sourcemaps as deploy job artifacts #9641

Merged
merged 4 commits into from
Jul 18, 2022

Conversation

kidroca
Copy link
Contributor

@kidroca kidroca commented Jun 30, 2022

Details

Capture sourcemap files as deploy step artifacts
The artifacts can then be used in another job/workflow to decode Firebase Crashlytics stacktraces

Sample workflow

  1. On Firebase Crashlytics event
  2. Get stacktrace, platform and version from Crashlytics
  3. Find the sourcemap artifacts for the given platform and version
  4. Use npm run symbolicate:{platform} < crashlytics.stactrace.txt to decode the stack traces

The symbolicate command should be able to replace just the stack trace content of a text containing stack traces leaving the rest of the text intact

Storing and sharing artifacts between workflows: https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts

Fixed Issues

$ #9293

Tests

Requirements to be able to test:

  • Have JS stack traces (from Firebase Crashlytics)
  • know the platform, app version of the stack trace
  • find the commit which released the given app version
  • check out a new branch from it and cherry pick the changes from this merge request
    • this is so we can (re)generate the same source and create source maps from it
    • if this seems like to much work you can try to use this PR and try or force errors/stack traces for the current version

Test Steps

  • Verify production builds are producing source map files:

    • iOS:
      1. run npx react-native run-ios --configuration Release --device "Your Device Name"
      2. Wait for the build to succeed
      3. A file called main.jsbundle.map should be available inside the project root - Expensify/App/main.jsbundle.map
    • Android:
      1. run npx react-native run-android --variant=release
      2. Wait for the build to succeed
      3. A file android/app/build/generated/sourcemaps/react/release/index.android.bundle.map should be available
  • Use the generated source maps to symbolicate (decode) the stack traces

    • use the new symbolicate:{platform} commands
    • stacktraces are expected to come from stdin, for testing purposes we can reroute stdin to a file
      1. Prepare some stack traces to a {platform}.stacktrace.txt file
      2. Build a source map for the version of the app/platform these stack traces are from
      3. feed that file to the symbolicate command
      npm run symbolicate:ios < ios.stacktrace.txt > ios.decoded.stacktrace.txt
      1. Decoded stack traces should be pointing to original files, some easy to distinguish examples are Onyx calls that point to node_modules/react-native-onyx/lib/Onyx.js

Notes on stack traces format

Decoding would fail or be incorrect if the stack traces format does not match
The format should be {anyText}:{lineNumber}:{colNumber}
anyText is preserved while lineNumber:colNumber are replaced with original file and original line:column
Line numbers should start from 1 (not 0)
More details and examples of valid/invalid and decoded stack traces can be seen here: #9293 (comment)

Example

  • Platform: Android
  • Version: 1.1.70-1
Minified stack trace content
.keyChanged (address at index.android.bundle:1:816124:)
Steps
  1. Checkout version 1.1.70-1
  2. Cherry pick commits from this PR
  3. Build the Android app (to generate the .map file)
  4. Save the above example stack trace as android.stacktrace.txt
  5. Run npm run symbolicate:android < android.stacktrace.txt > android.decoded.stacktrace.txt
  • alternative usage: Instead of doing 4 and 5 is to use the symbolicate command per line and number like so
    npm run symbolicate:android 1 816124
stack trace is decoded to
.keyChanged (address at node_modules/react-native-onyx/lib/Onyx.js:319:keyChanged:)

PR Review Checklist

Contributor (PR Author) Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • I verified there are no console errors (if there’s a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained “why” the code was doing something instead of only explaining “what” the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named “index.js”. All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • Any functional components have the displayName property
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose and it is
  • If a new CSS style is added I verified that:
    • A similar style doesn’t already exist
    • The style can’t be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.

PR Reviewer Checklist

  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • I verified there are no console errors (if there’s a console error not related to the PR, report it or open an issue for it to be fixed)
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained “why” the code was doing something instead of only explaining “what” the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named “index.js”. All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • Any functional components have the displayName property
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn’t already exist
    • The style can’t be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.

QA Steps

After this is deployed, verify that you can download the sourcemap artifacts for the platformDeploy.yml workflow run in which this was deployed.

No code changes
Minimal changes to ios build process - the app should still build and launch as before

Screenshots

Web

n/a

Mobile Web

n/a

Desktop

n/a

iOS

Android

kidroca added 4 commits June 30, 2022 16:47
"Bundle React Native code and images" is the phase that
generates the JS bundle using
`node_modules/react-native/scripts/react-native-xcode.sh`

Providing a SOURCEMAP_FILE env variable would make the
script generate a `.map` at build time
The file can then be accessed from App project root as
`ios.jsbundle.map`
At the moment there's a big in react-native with SOURCEMAP_FILE and hermes enabled
facebook/react-native#32497
@kidroca kidroca force-pushed the kidroca/feature/sourcemaps-alt branch from d6378f2 to eabc197 Compare June 30, 2022 17:13
@@ -32,7 +32,9 @@
"gh-actions-build": "./.github/scripts/buildActions.sh",
"gh-actions-validate": "./.github/scripts/validateActionsAndWorkflows.sh",
"analyze-packages": "ANALYZE_BUNDLE=true webpack --config config/webpack/webpack.common.js --env.envFile=.env.production",
"check-metro-bundler-port": "node config/checkMetroBundlerPort.js"
"check-metro-bundler-port": "node config/checkMetroBundlerPort.js",
"symbolicate:android": "npx metro-symbolicate android/app/build/generated/sourcemaps/react/release/index.android.bundle.map",
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these scripts used by react native automatically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are these scripts used by react native automatically?

I'm not sure what do you mean - how would they be used automatically by react-native?

I've outlined sample usage in the "Details" section.

Something like this:

  1. Build pipeline runs and saves sourcemaps somewhere
  2. Firebase Crashlytics captures a crash
  3. Something uses crash report data to retrieve the source maps for the build (version)
  4. And runs npm run symbolicate:{platform} < crashlytics.stactrace.txt where .stacktrace.txt are js stack traces from "Firebase Crashlytics"

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, thanks for the explanation. Sounds like a good plan

@kidroca kidroca marked this pull request as ready for review July 14, 2022 13:15
@kidroca kidroca requested a review from a team as a code owner July 14, 2022 13:15
@melvin-bot melvin-bot bot requested review from johnmlee101 and removed request for a team July 14, 2022 13:15
@johnmlee101
Copy link
Contributor

@roryabraham you mind being a full reviewer here as well? I'm a bit out of my understanding here and want to make sure I have a qualified set of second 👀

@@ -93,6 +93,12 @@ jobs:
env:
VERSION: ${{ env.VERSION_CODE }}

- name: Archive Android sourcemaps
uses: actions/upload-artifact@v3
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noting here that even though we normally use SHAs for community actions instead of vX tags, I think it might be fine to make exceptions for GitHub-maintained actions in the actions/ org.

What do you think @AndrewGable ?

@@ -32,7 +32,9 @@
"gh-actions-build": "./.github/scripts/buildActions.sh",
"gh-actions-validate": "./.github/scripts/validateActionsAndWorkflows.sh",
"analyze-packages": "ANALYZE_BUNDLE=true webpack --config config/webpack/webpack.common.js --env.envFile=.env.production",
"check-metro-bundler-port": "node config/checkMetroBundlerPort.js"
"check-metro-bundler-port": "node config/checkMetroBundlerPort.js",
"symbolicate:android": "npx metro-symbolicate android/app/build/generated/sourcemaps/react/release/index.android.bundle.map",
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, thanks for the explanation. Sounds like a good plan

@melvin-bot
Copy link

melvin-bot bot commented Jul 14, 2022

npm has a package.json file and a package-lock.json file. It seems you updated one without the other, which is usually a sign of a mistake. If you are updating a package make sure that you update the version in package.json then run npm install

@roryabraham roryabraham added the InternalQA This pull request required internal QA label Jul 14, 2022
@melvin-bot
Copy link

melvin-bot bot commented Jul 14, 2022

npm has a package.json file and a package-lock.json file. It seems you updated one without the other, which is usually a sign of a mistake. If you are updating a package make sure that you update the version in package.json then run npm install

@kidroca
Copy link
Contributor Author

kidroca commented Jul 15, 2022

npm has a package.json file and a package-lock.json file. It seems you updated one without the other, which is usually a sign of a mistake. If you are updating a package make sure that you update the version in package.json then run npm install

The change is intentional - we added new scripts to package.json which doesn't change package-lock.json

Copy link
Contributor

@johnmlee101 johnmlee101 left a comment

Choose a reason for hiding this comment

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

AFAICT it looks good on my end, will wait for the final comments/review

@roryabraham
Copy link
Contributor

So I got an Android production build working and see the sourcemap being generated. So that's good 👍

However, using the following stacktrace copy/pasted from Firebase Crashlytics:

Non-fatal Exception: io.invertase.firebase.crashlytics.JavaScriptError: Cannot read property split of undefined
       at .getTextMatrix(address at index.android.bundle:1:3403705)
       at .WrappedText(address at index.android.bundle:1:3403814)
       at .renderWithHooks(address at index.android.bundle:1:157704)
       at .beginWork$1(address at index.android.bundle:1:197179)
       at .performUnitOfWork(address at index.android.bundle:1:183344)
       at .workLoopSync(address at index.android.bundle:1:183202)
       at .renderRootSync(address at index.android.bundle:1:183086)
       at .performSyncWorkOnRoot(address at index.android.bundle:1:180456)
       at .flushSyncCallbacks(address at index.android.bundle:1:147980)
       at .scheduleUpdateOnFiber(address at index.android.bundle:1:178108)
       at .enqueueSetState(address at index.android.bundle:1:192727)
       at .anonymous(address at index.android.bundle:1:107204)
       at .setWithOnyxState(address at index.android.bundle:1:983054)
       at .sendDataToConnection(address at index.android.bundle:1:818564)
       at .anonymous(address at index.android.bundle:1:819361)
       at .tryCallOne(InternalBytecode.js:53:16)
       at .anonymous(InternalBytecode.js:139:27)
       at .apply((native):0:0)
       at .anonymous(address at index.android.bundle:1:218386)
       at ._callTimer(address at index.android.bundle:1:217285)
       at ._callReactNativeMicrotasksPass(address at index.android.bundle:1:217489)
       at .callReactNativeMicrotasks(address at index.android.bundle:1:219482)
       at .__callReactNativeMicrotasks(address at index.android.bundle:1:124122)
       at .anonymous(address at index.android.bundle:1:123207)
       at .__guard(address at index.android.bundle:1:123963)
       at .flushedQueue(address at index.android.bundle:1:123118)
       at .callFunctionReturnFlushedQueue(address at index.android.bundle:1:122974)

Then running npm run symbolicate:android < stackTrace.txt > android.decoded.stacktrace.txt yielded an error:

TypeError: Line must be greater than or equal to 1, got 0

@kidroca
Copy link
Contributor Author

kidroca commented Jul 16, 2022

Then running npm run symbolicate:android < stackTrace.txt > android.decoded.stacktrace.txt yielded an error:

TypeError: Line must be greater than or equal to 1, got 0

I had the same error, I've updated the description with

Line numbers should start from 1 (not 0)
More details and examples of valid/invalid and decoded stack traces can be seen here: #9293 (comment)

The line at .apply((native):0:0) should be removed
Lines like these should be filtered out

@roryabraham
Copy link
Contributor

roryabraham commented Jul 18, 2022

Cool, that worked 👍

My only other critique is that when we execute this via npm script i.e: npm run android:symbolicate, then we get some default npm stuff in the output that we don't want:

> [email protected] symbolicate:android /Users/roryabraham/Expensidev/App
> npx metro-symbolicate android/app/build/generated/sourcemaps/react/release/index.android.bundle.map

We can workaround that by just adding the --silent flag npm run --silent android:symbolicate < stackTrace.txt > android.decoded.stacktrace.txt, so NAB

Just two things to take note of that we'll need to workaround in the next phase where we hook up decoded stacktraces with the PHP <-> Firebase integration (cc @AndrewGable):

  • Need to remove any lines from the stacktrace that include any zero line numbers. Maybe a regex like this would do:

    ```
    /^.*:(?:0:.*|.*:0)\)$/gm
    ```
    
    image
  • Also want to run the npm run symbolicate:{platform} script with the --silent tag

@roryabraham roryabraham merged commit 4f27bd1 into Expensify:main Jul 18, 2022
@melvin-bot
Copy link

melvin-bot bot commented Jul 18, 2022

Triggered auto assignment to @PauloGasparSv (InternalQA), see https://stackoverflow.com/c/expensify/questions/5042 for more details.

@melvin-bot melvin-bot bot added the Emergency label Jul 18, 2022
@melvin-bot
Copy link

melvin-bot bot commented Jul 18, 2022

@roryabraham looks like this was merged without passing tests. Please add a note explaining why this was done and remove the Emergency label if this is not an emergency.

@roryabraham
Copy link
Contributor

Not an emergency, tests were passing

@OSBotify
Copy link
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@kidroca
Copy link
Contributor Author

kidroca commented Jul 19, 2022

@roryabraham
Another option to filter unwanted shell output might be to define the stuff in ./scripts and not have it in package.json
Or (depending on how the source map file is retrieved) directly run npx metro-symbolicate map-file.map < stacks.txt > result.txt

@kidroca kidroca deleted the kidroca/feature/sourcemaps-alt branch July 19, 2022 16:48
@roryabraham
Copy link
Contributor

Checking this off for internal QA since it was a pass:

image

@OSBotify
Copy link
Contributor

🚀 Deployed to production by @luacmartins in version: 1.1.85-8 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
InternalQA This pull request required internal QA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants