You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Source mapping is offset when using switch statements #1863
@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.
@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.
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.
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.
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:
First, the resolver locates the token nearest to the provided line and column in the minified file.
The resolver then locates the same token in the source file.
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:
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:
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!
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.
Activity
richardsimko commentedon Dec 11, 2023
@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.
lforst commentedon Dec 11, 2023
@richardsimko the SDK merely sends raw stack traces. If it is resolved wrongly there are 3 possibilities:
sourcemaps injectcommand messed up the source map/generated file.sourcemaps resolvecommand is not working correctly.tscis 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.
richardsimko commentedon Dec 11, 2023
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 3resolves to line 2, as you say then it's off by one.I'm not sure it's caused by
switchspecifically 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 injectto hopefully make it easier to reproduce.lforst commentedon Dec 19, 2023
@szokeasaurusrex When you have time
richardsimko commentedon Dec 21, 2023
FYI I'm using Mac OS, perhaps that could be the source of the issue?
lforst commentedon Dec 21, 2023
@richardsimko likely not. We are all using MacOS at Sentry and don't have this issue.
richardsimko commentedon Dec 21, 2023
Got it, thanks!
szokeasaurusrex commentedon Dec 28, 2023
I investigated this issue, and it appears that all of the Sentry's CLI source map resolving functionality is being performed by the
rust-sourcemaplibrary.The reason why the line numbers in the output are off-by-one is because the
rust-sourcemaplibrary 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 therust-sourcemaplibrary. 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-sourcemaplibrary, this off-by-one error is likely caused by arust-sourcemapbug. 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.
szokeasaurusrex commentedon Dec 28, 2023
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:
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 6sentry-cli sourcemaps resolve build/test.js.map -l 6 -c 1When you run the command$\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$ ).
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) coordinatesNext, 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 $\left(5, 0\right)$ , whereas the next token after the search location is 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 positionthrowstatement 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 9argument, the closest token will be thethrowstatement on line 6, since the token is located at exactly (one-indexed) column 8 of line 9. The full command is therefore the following: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 $\left(5, 8\right)$ . The resolver then uses the sourcemap to find the same $\left(6, 4\right)$ (which corresponds to one-indexed line 7, column 5). Then, the CLI should output these coordinates.
throwtoken located at exactlythrowtoken in the source file; it should locate the token atIf we run the command, we should observe the following output, which matches our expectations:
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!
lforst commentedon Dec 29, 2023
Nice investigation @szokeasaurusrex. On a related note, I feel like we should unship the
resolvecommand 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.feat(sourcemaps): Improve `sourcemaps resolve` command output (#1880)