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
CRA2 recommended config hits random break points in VS Code #5319
it('should find the breakpoint',()=>{constfoo='bar'console.log({ foo })expect(foo).toBe('bar')})it('should find the other breakpoint',()=>{constfoo='bar'console.log({ foo })expect(foo).toBe('bar')})
Expected Behavior
Execution stops on line 13
Actual Behavior
Execution stops on line 6 or 7, it's inconsistent.
This sounds like a sourcemapping problem that causing the breakpoint to be set on the wrong line. Adding debugger should also make the VS Code debugger break. I'll add debugging this to my backlog.
Question @ryancogswell Can you set breakpoints with Chrome DevTools using their Node.js debugging? Breakpoint from the gutter.
I'm not a VSCode user but I was troubleshooting this issue two weeks ago for devs on my team (on an ejected create-react-app build) and I couldn't understand why it worked with react-scripts (prior to v2 release) but didn't on mine. I configured the launch.json like this for CRA (which worked):
But on my build it didn't work. After a lot of side-trails and meandering through VSCode and Jest issue threads I finally got it working by downgrading my project's version of Jest from 23.4.x to 22.2.0, and it started working.
@arizonatribe Thank you for taking the time to figure this out and share it. I lost two days trying to fix this and the one thing I failed to try is downgrading Jest.
@auchenberg Do you think I should open an issue with Jest?
tl;dr: a workaround is that breakpoints set after launching work.
I can repro this, I tried downgrading jest in the test repo, but it doesn't make a difference for me? Could one of you share a test repo where it does work with jest downgraded? Or if it's easier just set "trace": true in your launch config, run it again, and share the log file.
Here's the problem: In a transpiling scenario you can use the outFiles parameter to point vscode towards the output sourcemaps so it can preload them and resolve your breakpoints to their real locations. But in this scenario, the sourcemaps are generated in memory and don't exist on disk.
So you set a breakpoint in a .js file. VS Code doesn't know whether it's a bp in a script with no sourcemaps, or a transpiled script that will have sourcemaps at runtime. Since it's a .js file, we guess and set breakpoints for that path and line. Anyone using .ts or something else won't see an issue.
Then, jest loads the transpiled script and gives it the same path as the file on disk. It's a match, and your breakpoint hits. But the line numbers don't match between the two scripts, so you've stopped on the wrong line. At the same time, vscode has loaded the sourcemap so any breakpoints you set at this point will bind correctly.
If this is really new in Jest then they might have changed how they name the loaded scripts. If they can name them differently from the source files on disk, that would fix this. Maybe we should provide an option to disable this optimistic bp placement but it should "just work"...
But this scenario where sourcemaps can't be preloaded from disk has always been imperfect in vscode. We would still have a race to load the sourcemap and set the BP before the line of code with the BP executes. Chrome gives us a way to break every time a script loads, giving us a chance to catch up and load sourcemaps. Node doesn't support that unfortunately. We've experimented with guessing the name of the script at runtime, setting a BP for line 1, then checking whether it has sourcemaps. That would help in this scenario but is complex and relies on the name of the script at runtime matching the path on disk.
I don't believe that part can be customized unless you eject . When the app is ejected, the babelrc: false and configFile: false code are removed.
I know ejecting is not a solution (maintaining an ejected create-react-app based build eats up a good portion of my time and makes me develop an irrational dislike for jest, webpack and and the whole ecosystem of plugin/loader authors that can never stay in-sync), but you might be able to verify locally whether it works with a Jest downgrade to v22 and/or by setting that prop locally in the node_modules/react-scripts/config/jest/babelTransform.js.
Then you should be able to re-launch the test script in VSCode to see if it's hitting those breakpoints correctly or not.
If that setting is the trick to getting it working (and especially if it works with Jest v23) maybe they'll accept a PR for changing the babelTransform.js file.
Ok I was able to create a project with create-react-app@1, and I do see that it sets retainLines in node_modules/babel-jest/build/index.js. I think that bringing that back is the best fix. I'll try to do a PR for babel-jest.
It seems that retainLines was removed because of a Babel bug that gives bad output with that option. jestjs/jest#5326 The upstream bug has been open for awhile: babel/babel#7275
I think there are a couple things vscode could do to make this scenario work better but they are not great and I don't want to add more options or modes.
At the moment, setting retainLines: true in node_modules/react-scripts/config/jest/babelTransform.js is a good local workaround, assuming your code doesn't trigger the babel bug.
I'd appreciate any other suggestions, in the meantime I'll think about what vscode can do here...
@ianschmitz - unfortunately I don't think this will help because I've already tested every possible devtool option (see #6296 (comment)) and none of them fixed the problem described in #6296 where the webpack sourcemap content doesn't match the original source files in node_modules.
But regardless I'll definitely try the new release to see if some other commit helps.
Reacted by Lauri Harpf, Mark Adamson, Daniel Katz, Steven Ulibarri, DreamAwakening, Stephen Sullivan, Aaron Levin, Dylan Landry, Adam Edison, Jonathan Häberle and 3 more
Activity
ryan-mars commentedon Oct 5, 2018
For added information...
Using the Chrome inspector works fine.
Steps to reproduce:
package.jsondebuggerstatement to line 13npm run test:debugchrome://inspectdebuggerpackage.jsonsnippet:I'm not sure if this is a VS Code bug or a problem with CRA's recommended
launch.jsonsettings.gaearon commentedon Oct 5, 2018
@auchenberg Can you please look at this?
auchenberg commentedon Oct 6, 2018
This sounds like a sourcemapping problem that causing the breakpoint to be set on the wrong line. Adding
debuggershould also make the VS Code debugger break. I'll add debugging this to my backlog.Question @ryancogswell Can you set breakpoints with Chrome DevTools using their Node.js debugging? Breakpoint from the gutter.
arizonatribe commentedon Oct 9, 2018
I'm not a VSCode user but I was troubleshooting this issue two weeks ago for devs on my team (on an ejected create-react-app build) and I couldn't understand why it worked with
react-scripts(prior to v2 release) but didn't on mine. I configured thelaunch.jsonlike this for CRA (which worked):But on my build it didn't work. After a lot of side-trails and meandering through VSCode and Jest issue threads I finally got it working by downgrading my project's version of Jest from 23.4.x to 22.2.0, and it started working.
ryan-mars commentedon Oct 9, 2018
@arizonatribe Thank you for taking the time to figure this out and share it. I lost two days trying to fix this and the one thing I failed to try is downgrading Jest.
@auchenberg Do you think I should open an issue with Jest?
roblourens commentedon Oct 9, 2018
tl;dr: a workaround is that breakpoints set after launching work.
I can repro this, I tried downgrading jest in the test repo, but it doesn't make a difference for me? Could one of you share a test repo where it does work with jest downgraded? Or if it's easier just set
"trace": truein your launch config, run it again, and share the log file.Here's the problem: In a transpiling scenario you can use the
outFilesparameter to point vscode towards the output sourcemaps so it can preload them and resolve your breakpoints to their real locations. But in this scenario, the sourcemaps are generated in memory and don't exist on disk.So you set a breakpoint in a .js file. VS Code doesn't know whether it's a bp in a script with no sourcemaps, or a transpiled script that will have sourcemaps at runtime. Since it's a .js file, we guess and set breakpoints for that path and line. Anyone using .ts or something else won't see an issue.
Then, jest loads the transpiled script and gives it the same path as the file on disk. It's a match, and your breakpoint hits. But the line numbers don't match between the two scripts, so you've stopped on the wrong line. At the same time, vscode has loaded the sourcemap so any breakpoints you set at this point will bind correctly.
If this is really new in Jest then they might have changed how they name the loaded scripts. If they can name them differently from the source files on disk, that would fix this. Maybe we should provide an option to disable this optimistic bp placement but it should "just work"...
But this scenario where sourcemaps can't be preloaded from disk has always been imperfect in vscode. We would still have a race to load the sourcemap and set the BP before the line of code with the BP executes. Chrome gives us a way to break every time a script loads, giving us a chance to catch up and load sourcemaps. Node doesn't support that unfortunately. We've experimented with guessing the name of the script at runtime, setting a BP for line 1, then checking whether it has sourcemaps. That would help in this scenario but is complex and relies on the name of the script at runtime matching the path on disk.
Yikes sorry for the wall of text...
arizonatribe commentedon Oct 10, 2018
I'm not sure I can share the code repo without permission, but I'll check and see if it's okay to share the logs.
I forgot one other thing I did was set
reatainLinestotrueandsourceMapsto "inline" in the Babel config (but only for the Babeltestenv)roblourens commentedon Oct 10, 2018
That sounds extremely likely to help, how do you do that? I'm not clear on how you override config in a CRA app
arizonatribe commentedon Oct 10, 2018
I don't believe that part can be customized unless you eject . When the app is ejected, the
babelrc: falseandconfigFile: falsecode are removed.I know ejecting is not a solution (maintaining an ejected create-react-app based build eats up a good portion of my time and makes me develop an irrational dislike for jest, webpack and and the whole ecosystem of plugin/loader authors that can never stay in-sync), but you might be able to verify locally whether it works with a Jest downgrade to v22 and/or by setting that prop locally in the
node_modules/react-scripts/config/jest/babelTransform.js.Then you should be able to re-launch the test script in VSCode to see if it's hitting those breakpoints correctly or not.
If that setting is the trick to getting it working (and especially if it works with Jest v23) maybe they'll accept a PR for changing the
babelTransform.jsfile.roblourens commentedon Oct 10, 2018
Ok I was able to create a project with create-react-app@1, and I do see that it sets retainLines in
node_modules/babel-jest/build/index.js. I think that bringing that back is the best fix. I'll try to do a PR for babel-jest.roblourens commentedon Oct 10, 2018
It seems that retainLines was removed because of a Babel bug that gives bad output with that option. jestjs/jest#5326 The upstream bug has been open for awhile: babel/babel#7275
I think there are a couple things vscode could do to make this scenario work better but they are not great and I don't want to add more options or modes.
At the moment, setting
retainLines: trueinnode_modules/react-scripts/config/jest/babelTransform.jsis a good local workaround, assuming your code doesn't trigger the babel bug.I'd appreciate any other suggestions, in the meantime I'll think about what vscode can do here...
ryan-mars commentedon Oct 13, 2018
Not sure if this is the right thing to do here but I submitted babel/babel#8868
I have no idea what I'm doing.
37 remaining items
justingrant commentedon Feb 7, 2019
Adding unnecessary comment to get stalebot to re-open this issue.
ianschmitz commentedon Feb 10, 2019
We will be releasing a new version including the change made in #5060 shortly. I hope this may improve some of these issues you're experiencing.
justingrant commentedon Feb 10, 2019
@ianschmitz - unfortunately I don't think this will help because I've already tested every possible devtool option (see #6296 (comment)) and none of them fixed the problem described in #6296 where the webpack sourcemap content doesn't match the original source files in node_modules.
But regardless I'll definitely try the new release to see if some other commit helps.
johnrazeur commentedon Feb 21, 2019
Has solved the problem for me !
ryan-mars commentedon Feb 21, 2019
@johnrazeur You put those in
.vscode/launch.json?johnrazeur commentedon Feb 21, 2019
@ryanwmarsh yes
ryan-mars commentedon Feb 21, 2019
@johnrazeur I just tested it and created a PR. Thank you!
roblourens commentedon Feb 22, 2019
What does
"env": { "CI": "true" },do?codenamekt commentedon Mar 12, 2019
This works great. Is this where we spam the PR with 👍 @ryanwmarsh
danielkatz commentedon Mar 14, 2019
Thanks for the solution!
For me
disableOptimisticBPs: truealone did the trick.mvgiacomello commentedon Jul 6, 2019
For whoever is having issues debugging Webdriver.IO, the
"disableOptimisticBPs": truein the launch file does the trick. Example project here.