Using Replay to fix my first Replay bug
Tags: debugging, javascript, react, opensource
Published: 2022-02-04 19:34:38

---
title: Using Replay to fix my first Replay bug
published: true
description: How I used the debugging tool Replay to debug and fix my first issue on the Replay team.
tags: debugging, javascript, React, OSS
//cover_image: https://direct_url_to_image.jpg
---

I joined Replay in January as a Community Lead and was excited to hit the ground running. 

I had the chance to contribute to the [open source devtools repo](https://github.com/RecordReplay/devtools) with my first bug fix. Merging that first pull request is a rite of passage with any new company, and this was a fun one because I gained real world experience using Replay to debug Replay! 

To be honest, I was a little nervous. I have software development experience but hadn’t pushed production code in over a year. But, dogfooding your own product is important, and if Replay is going to help developers of all experience levels demystify debugging, the best place to start was with myself!

These are the steps I used to debug the issue, which can serve as a good framework for approaching many types of bugs.


### Reproduce the issue

Jason our CEO first identified and reported the bug in this [GitHub issue](https://github.com/RecordReplay/devtools/issues/4939). Like with all our issues, he included [a replay of the bug](https://app.replay.io/recording/b26df7ad-6a7a-4770-86da-ab841929c159?point=26610521419020861461449900973622165&time=13404&hasFrames=false#). The replay is public, so feel free to follow along!

If you haven’t used Replay before, it lets you record a browser session to produce a shareable replay for collaborative debugging. The replay isn’t just a video — everything from the browser is recorded so you can inspect ✨ all the things ✨ . The [‘Why Replay?’ guide](https://docs.replay.io/docs/why-replay-12b64a4c3b5d461981f1b498fc055d56) in our docs is a good overview if you’re not familiar.

> 💡 A reproducible example of a bug is required for debugging. Trying to debug without one is almost always impossible because you don’t have insight into what’s happening with your code.

Right away I could see the issue in the [Viewer](https://docs.replay.io/docs/viewer-26591deb256c473a946d0f64abb67859). 

<a href="https://www.loom.com/share/8469cd618f884253ba333615c5e570ca"> <p>Open Viewer bug - Watch Video</p> <img style="max-width:300px;" src="https://cdn.loom.com/sessions/thumbnails/8469cd618f884253ba333615c5e570ca-with-play.gif"> </a>

 When using the Command Palette and selecting Open Viewer, the side panel doesn’t switch the way it should, and the layout gets — to use the technical term — borked. 

This is a less common method for switching views. Typically you’d use the Viewer/DevTools toggle in the upper right-hand corner, which doesn’t result in this issue.


### Understand the expected behavior

With a reproducible example, I can move on with debugging. At this point I don’t even have a developer environment set up for this codebase, and I don’t need one! I can do all my debugging right inside the replay.


> 💡 When debugging an issue, you need to understand what working looks like before you can fix what’s broken.

Replay includes the code from the browser. We’ve also [added source maps](https://docs.replay.io/docs/adding-source-maps-1923e679c1e4411db1bda29536eb1e31) to see how the browser code maps back to our actual source code. 

I knew this bug didn’t occur when using the toggle to switch views. So that means there must be a difference between the working code and the code causing the bug. Again, I’m brand new to this codebase, so I need to understand what the working code looks like.

![ViewToggle.tsx](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7iharfz634vtyxvhkpr.png)

With Replay, I’m able to see all of our components like I would in a code editor or IDE. Comments can be added directly to lines of code, and Jason helpfully highlighted where we are handling the toggle switch. By clicking on his comment, I was able to jump right into the `ViewToggle.tsx` component file I needed to understand the logical flow of the view switch.

Within this file, I can see there are `setViewMode()` and `setPrimarySelectedPanel()` functions that are executed when the toggle is selected. There is also some logic that checks the current panel in view before updating. 

Now I know what the working code looks like — time to investigate how using the Command Palette to switch views is different.


### Isolate the issue

Within the replay, I’m also able to see what code executed and evaluate expressions from any point in the time of the recording. This was helpful because I could see that the `executeCommand()` function in the `CommandPalette.tsx` component file triggered one time. I can also see the values of the `shownCommands` array and the value of `activeIndex` at the time the function executed. This leads me to the `open_viewer` command, our culprit.

> 💡 When debugging, it’s tempting to try a lot of different things if you don’t know where to start. This can lead to frustration and use up a lot of your time. Instead, try to isolate the code that actually executed as closely to the triggering event as possible.

<a href="https://www.loom.com/share/5d961698d8ae461cac6db62eb29ec50f"> <p>executeCommand() - Watch Video</p> <img style="max-width:300px;" src="https://cdn.loom.com/sessions/thumbnails/5d961698d8ae461cac6db62eb29ec50f-with-play.gif"> </a>


### Identify the root cause

We are so close now! Now that we’ve isolated the code executing when the bug occurs, let’s compare it to the code we already know actually works.

> 💡 Identifying the root cause means finding the primary reason why the code is not working. Sometimes we try to solve for side effects instead of the root cause. Try to trace the bug all the way down to its true source.

![executeCommand()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkiq6wmfccyi64pth5zn.png)

This is the `executeCommand` function, which we know is taking in the `open_viewer` key. On line 375 from the replay we see that we’re dispatching `setViewMode()`, but there is no `setPrimaryPanel()` . Caught you! 🐛🔍


### Implement the solution

All the collaboration on how to implement a solution for the bug happened right in the replay. Ultimately, we decided to move the `setSelectedPrimaryPanel()` logic inside `setViewMode()` so they always happen at the same time, regardless of the source of the change. You can check out the implementation in [the pull request here](https://github.com/RecordReplay/devtools/pull/5203).

![Screenshot of replay comment discussion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5e5wmdbpbs6vwiffidz.png)


### Bonus: Identify a related bug

While investigating the sidebar issue, I noticed that there is another difference between the working and non-working code. Inside `ViewToggle.tsx`, there is logic that delays the dispatch of `setViewMode()` to allow for the animation of the toggle.  This does not happen when we switch views with the Command Palette.

![handleToggle()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wqvktk4m22p7hm0uxlf.png)

In the Viewer, I was able to see that the view in the layout updates before the animation completes. This is a separate bug that can also be addressed by better organizing how we handle changes to the view mode and having a single source of truth for updates. Check out the [GitHub issue here](https://github.com/RecordReplay/devtools/issues/5202) for how we plan to approach this fix.

This is a great example of how a standard methodology for approaching debugging can help you better understand your code overall and even identify other areas of improvement.


### Learn more

You can learn more about using Replay to debug your own application by checking out our documentation. The [‘Debugging a replay’ guide](https://docs.replay.io/docs/debugging-a-replay-1c18f02c9f1d455188e3f202ef5f5c08) walks through the main debugging functionality, and if you’re interested in our open source work, check out [Contributing to Replay](https://docs.replay.io/docs/contributing-to-replay-711d4d8b10fb497aba2a7c06583b26a6).

This post was originally [published on the Replay blog](https://medium.com/replay-io/using-replay-to-fix-my-first-replay-bug-8bdfb1799f65).