Skip to content

Source mapping is offset when using switch statements #1863

Closed

Description

@richardsimko

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

7.86.0

Framework Version

No response

Link to Sentry event

https://fl101.sentry.io/issues/4715851450/events/4f6786772d4045d4b57a56cab225e50e/

SDK Setup

N/A

Steps to Reproduce

https://github.com/richardsimko/sentry-sourcemap-bug

  1. yarn install
  2. yarn run tsc
  3. yarn run sentry-cli sourcemaps inject ./build
  4. yarn run sentry-cli sourcemaps resolve build/test.js.map -l 6 (To simulate the lookup from one of the errors)

This happens in the live Sentry environment as well, the stack trace in the linked event are also off by 2 lines.

Expected Result

Source line is line 7 in test.ts

Actual Result

Source line is line 5 in test.ts

source map path: "build/test.js.map"
source map type: regular
lookup line: 5, column: 0:
  name: not found
  source file: "/test.ts"
  source line: 5
  source column: 13
  minified line: 4
  minified column: 15
  source code:
    console.log("test2");
    
    switch (process.env.TEST) {
      case "test":
        throw new Error("test");
      default:
        throw new Error("default");

Activity

  1. richardsimko commented on Dec 11, 2023

    @richardsimko
    Author

    @lforst Thanks for transferring the issue. Although I'm not sure if it's a sentry-cli issue though since the problem appears in the stack traces we send to Sentry as well, I was just presuming the same logic is used in the CLI and that's the reason why the issue appears here as well.

  2. lforst commented on Dec 11, 2023

    @lforst

    @richardsimko the SDK merely sends raw stack traces. If it is resolved wrongly there are 3 possibilities:

    • The CLI sourcemaps inject command messed up the source map/generated file.
    • The CLI sourcemaps resolve command is not working correctly.
    • The Processing in Sentry's backend is wrong.
    • tsc is generating faulty source maps.

    Your repro also seems a bit off. If I try to reproduce, everything seems to resolve fine. Can you share in a bit more detail what exactly you would expect?

    The only thing I noticed is that we may have an off-by-one in the cli output.

  3. richardsimko commented on Dec 11, 2023

    @richardsimko
    Author

    Thanks for the info, then I see why you moved it!

    It resolves fine, but the resolved line is off by 2 after the switch statement. For example:

    sentry-cli sourcemaps resolve build/test.js.map -l 6 (https://github.com/richardsimko/sentry-sourcemap-bug/blob/28129d9eeb2fb30560a3036d205f839b57606ac2/build/test.js#L6)

    and this resolves to line 5 (https://github.com/richardsimko/sentry-sourcemap-bug/blob/28129d9eeb2fb30560a3036d205f839b57606ac2/test.ts#L5)

    Trying it with -l 3 resolves to line 2, as you say then it's off by one.

    I'm not sure it's caused by switch specifically I just noticed in our production code that was the common denominator for the off-by-2 issues.

    I initially thought this was an issue with Node 20 as I've experienced similar offset sourcemaps before but I tried with Node 19 and it has the same issue.

    I pushed the build output from my tsc+sentry-cli inject to hopefully make it easier to reproduce.

  4. lforst commented on Dec 19, 2023

    @lforst

    @szokeasaurusrex When you have time

  5. richardsimko commented on Dec 21, 2023

    @richardsimko
    Author

    FYI I'm using Mac OS, perhaps that could be the source of the issue?

  6. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Dec 21, 2023
  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Dec 21, 2023
  8. lforst commented on Dec 21, 2023

    @lforst

    @richardsimko likely not. We are all using MacOS at Sentry and don't have this issue.

  9. richardsimko commented on Dec 21, 2023

    @richardsimko
    Author

    Got it, thanks!

  10. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Dec 21, 2023
  11. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Dec 21, 2023
  12. szokeasaurusrex commented on Dec 28, 2023

    @szokeasaurusrex
    Member

    I investigated this issue, and it appears that all of the Sentry's CLI source map resolving functionality is being performed by the rust-sourcemap library.

    The reason why the line numbers in the output are off-by-one is because the rust-sourcemap library internally uses zero-indexed line and column numbers. The Sentry CLI converts the line and column numbers provided by the user to zero-indexed line and column numbers before passing the line/column numbers to the rust-sourcemap library. However, the CLI also uses the zero-indexed line/column numbers in the output, rather than converting them to one-indexed values. The use of zero-indexed line numbers explain the off-by-one error observed outside the switch statement, since the CLI is outputting the correct zero-indexed line numbers.

    However, as you noted, the lines were off-by-two in the switch statement, so even if we consider the output line numbers to be zero-indexed, the zero-indexed line numbers would still be off-by-one. Since the sourcemap resolution occurs within the rust-sourcemap library, this off-by-one error is likely caused by a rust-sourcemap bug. So, I will open an issue in that repository so we can get the switch statement bug fixed.

    We should, however, be able to fix the off-by-one error directly here in the CLI codebase by converting the zero-indexed line and column values back into one-indexed values.

  13. szokeasaurusrex commented on Dec 28, 2023

    @szokeasaurusrex
    Member

    After some more investigation, I now believe that the only bug here is the off-by-one error in the Sentry CLI output. The off-by-two behavior you observed in the switch statement can likely be fully explained by the off-by-error in the CLI output and by the sourcemap resolver's intended behavior.

    Explanation

    The sourcemap resolver, at a high level, works as follows:

    1. First, the resolver locates the token nearest to the provided line and column in the minified file.
    2. The resolver then locates the same token in the source file.
    3. Lastly, the resolver outputs the zero-indexed (line, column) locations of the token in both the source and minified files.

    In your example command, you provided only a line argument with -l 6. Whenever there is no column provided, such as in your example, the CLI assumes you are searching from the beginning of the line, i.e. from (one-indexed) column 1. In other words, the following two commands are completely equivalent:

    • sentry-cli sourcemaps resolve build/test.js.map -l 6
    • sentry-cli sourcemaps resolve build/test.js.map -l 6 -c 1

    When you run the command sentry-cli sourcemaps resolve build/test.js.map -l 6, the CLI will convert your provided line number and implicit column number (1) to the zero-indexed (line, column) coordinates $\left(5, 0\right)$ (going forward, for clarity, I use the convention $\left(x, y\right)$ to denote the code location at zero-indexed line $x$ and column $y$).

    Next, the resolver locates the token nearest to $\left(5, 0\right)$ in the minified file. This search is performed in both directions, and therefore the nearest token is the colon at $\left(4, 15\right)$ (at the end of the case "test": line). Notice here that the closest token is not necessarily on the same line as the line passed to the command, and in this case, the closest token is on an earlier line. The colon token is only one character (a newline) away from the search position $\left(5, 0\right)$, whereas the next token after the search location is the throw statement eight space characters away.

    The resolver then uses the sourcemap to locate the same colon token in the original source file. The resolver should identify the colon token's location in the source file as $\left(5, 13\right)$.

    The CLI lastly should output the coordinates $\left(5, 13\right)$ and $\left(4, 15\right)$ as the locations of the token in the source and minified files, respectively. These coordinates match the output you observed when you ran the CLI, indicating that the CLI is working correctly (except, of course, for the fact that we should be outputting one-indexed rather than zero-indexed line and column numbers).

    Verifying this explanation

    To verify that the above explanation is correct, we can provide an explicit column argument, in addition to -l 6, so that the nearest token is actually on (one-indexed) line 6. If we add the -c 9 argument, the closest token will be the throw statement on line 6, since the token is located at exactly (one-indexed) column 8 of line 9. The full command is therefore the following:

    $ sentry-cli sourcemaps resolve build/test.js.map -l 6 -c 9

    When we run the command, the CLI first converts our one-indexed line and column values to the zero-indexed values $\left(5, 8\right)$. Then, the sourcemap resolver searches for the nearest token to $\left(5, 8\right)$ in the minified file, which will of course be the throw token located at exactly $\left(5, 8\right)$. The resolver then uses the sourcemap to find the same throw token in the source file; it should locate the token at $\left(6, 4\right)$ (which corresponds to one-indexed line 7, column 5). Then, the CLI should output these coordinates.

    If we run the command, we should observe the following output, which matches our expectations:

    source map path: "build/test.js.map"
    source map type: regular
    lookup line: 5, column: 8:
      name: not found
      source file: "/test.ts"
      source line: 6
      source column: 4
      minified line: 5
      minified column: 8
      source code:
        
        switch (process.env.TEST) {
          case "test":
            throw new Error("test");
          default:
            throw new Error("default");
        }
    

    Summary and next steps

    It seems the off-by-two error you are observing in the switch statements is explained by the fact that the sourcemap resolver searches for the closest token to the provided location, which may be on a different line, and the fact that the CLI outputs zero-indexed line and column numbers.

    We should fix the off-by-one error in the CLI output, since this is likely not the desired behavior, given that the input arguments are interpreted as one-indexed line and column numbers. If the input values are one-indexed, the output values should also be one-indexed, especially since the convention is to use one-indexed line and column numbers.

    Lastly, users should be aware that the sourcemap resolver works by identifying the nearest token to the provided location, which may be on a different line than the line number passed as an argument to the CLI. We should likely improve the command output from the CLI so that it is clearer to users what the CLI is actually doing. Perhaps, this could be accomplished by outputting the nearest token that was found, so that if the resolver was looking at a different token than the user intended, they can adjust the arguments they pass to the CLI.

    I hope this explanation was clear. If you have questions about anything @richardsimko, please reach out to me!

  14. lforst commented on Dec 29, 2023

    @lforst

    Nice investigation @szokeasaurusrex. On a related note, I feel like we should unship the resolve command from the CLI. It doesn't really have anything to do with Sentry and there are arguably tools out there that do this way better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Bug

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions