Breakpoints in vscode debugger often fail for a React application - reactjs

My React application is written in Typescript and I debug it using Chrome. After a fresh start of the application everything works fine. I can set breakpoints and the debugger stops at them.
The problems appear when I change source code. Existing breakpoints are moved to a wrong line and when I try to set new breakpoints they cannot be resolved. I have to close Chrome and re-open it to make things work again. My launch config is:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Shell GUI",
"url": "https://localhost:3001",
"webRoot": "${workspaceFolder}/src",
"userDataDir": "${workspaceRoot}/.vscode/chrome",
"sourceMaps": true,
"sourceMapPathOverrides": {
"webpack:///build/*": "${webRoot}/*"
}
}
]
}
I tried to fix the issue by specifying source map overrides, but that has not improved the situation at all. And if I haven't created a production build yet then there's no build folder at all. To me it looks as if Chrome doesn't get code changes (assuming here that breakpoints are evaluated by Chrome).
What do I have to change to make debugging working correctly, even after source code changes?

Unfortunately this is a known issue with create-react-app at the moment:
There's a link to track the issue here: https://github.com/facebook/create-react-app/issues/6074

Related

Debugging a react app shows main.chunk.js instead of sources

First of all, I'd like to admit that I'm relatively new to react, node, npm and etc. And I learn it when I have free time. I saw similar topic but answers there don't work.
I use Visual Studio code. I created react app using create-react-app cli command.
To start debugging I use 'npm start' command and then I start debugger in visual studio code with following launch config:
{
"type": "msedge",
"request": "launch",
"name": "Launch Edge against localhost",
"url": "https://localhost:3000/",
"webRoot": "${workspaceFolder}",
}
It works as expected - breakpoints work, VS code shows sources. But if I do any change in the sources and save it VS code starts showing main.chunk.js instead of my sources. main.chunk.js is understandable but I believe it can be fixed.
I have spent couple of weeks on this issue and you are my last hope :)
I guess after recompiling changed source file something breaks. Seems like it can't find sources or map to source file. I tried to find something that can point to sources. I found sourceMapPathOverrides property of launch config and set it to:
"sourceMapPathOverrides": {
"/src/*": "${workspaceRoot}/src/*"
}
But it didn't help.
I thought it might be related to src folder in the solution. I tried to change "webRoot" to "${workspaceFolder}/src" but it didn't help.
I tried to set root folder in the browser's dev tools as advised somewhere here.
Using .scripts in VS code debug console I checked loaded scripts, and it says that main.chunk.js is mapped to App.js.
When I do a change in App.js instead of main.chunk.js it maps hot-update.js file to App.js.
And looks like main.chunk.js isn't mapped to App.js anymore.
So how can I fix it?)

Can't set breakpoints on NPM Link'ed library

I learn React JavaScript and now I have this problem
I Fork the notistack library in GitHub then download my fork with Git Desktop so the project are on Windows 10 here D:/git/notistack.
After following npm-link doc it all work ok I can debug run the notistack library typescript project in VScode.
I "npm link" on my notistack library and "npm link notistack" in my ReactJs project all standard procedure and I can debug run the library ok.
I make changes and rebuild notistack library and I see it's working ok.
But when I set up launch.json like this, with the runtimeArgs, that suppose to enable debugging I can't make breakpoints work in the Library.
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Edge",
"request": "launch",
"type": "pwa-msedge",
"url": "https://localhost:6545",
"webRoot": "${workspaceFolder}",
"runtimeArgs": [
"--preserve-symlinks"
],
}
]
}
I set breakpoints in the ReactJs project node_module/notistack library but VSCode is setting them as unbound breakpoints.
I suspekt it has something to do with that that notistack library is a Typescript project maybe and I link to a ReactJs project. any idea?
Please advice what I need to check and do?
I looked this up, and saw some possible fixes,
Did you try disabling this setting in VSCode?
"debug.javascript.usePreview": false
Try these properties to your launch.json file
{
"trace": true,
"sourceMaps": true,
"webRoot": "${workspaceFolder}/src",
}
Restarting VSCode or Downgrading the version
Run -> Disable All Breakpoints, then Enable All Breakpoints
None of the solutions I saw for this problem worked for me. I am a windows user ; I precise cause it works without this solution, on linux, for my colleagues.
So I tried to found a configuration that works, the important parameter is outFiles :
"outFiles": [
"${workspaceFolder}/**/*.js",
"**/my-npm-linked-library/**/*.js"
"!**/node_modules/**",
]
The second line of outFiles array is the most important. You can adapt the path to one who match better with the project you work on.
The order of the paths is important, here "!**/node_modules/**" is the last one cause we don't want to add "**/my-npm-linked-library/node_modules/*.js" in our outFiles.
! Important note ! : You must remove "--preserve-symlinks" and "--preserve-symlinks-main" inside runtimeArgs parameter. My understanding about that is limited, but it doesn't work whith these options.
Try adding the --preerve-symlinks-main to the runtimeArgs. It may solve the problem.
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Edge",
"request": "launch",
"type": "pwa-msedge",
"url": "https://localhost:6545",
"webRoot": "${workspaceFolder}",
"runtimeArgs": [
"--preserve-symlinks",
"--preserve-symlinks-main"
],
}
]
}
If you wanna read the docs before implementing, here is the link: https://nodejs.org/api/cli.html#cli_preserve_symlinks_main
This can be a difficult question to answer without an example repo to view your setup. For example we don't know if you're trying to debug something for SSR or Client.
First question, are you debugging this module because it's not showing up in your App? If that's the case, and you're using Webpack to compile you may need to try resolve.symlinks: true.
Generally speaking, I try to debug my code via the software I'm using to view the compiled code. For React projects, that's usually a Browser. For stuff like Unit tests, debugging within VSCode is handy. The below suggestions are for debugging via the Browser.
For SSR
Any node_modules should show up in your Sources panel, just as they would on your file system.
Here's an article on setting Chrome up to debug Server code.
Basically, start your Server with the --inspect flag.
In an empty Browser tab go to chrome://inspect
If the Server was started with --inspect, it'll be listening for a debugging session to connect, and you should see your Server listed under the Remote Target section.
Some articles suggest clicking on the item listed under Remote Target, but I just use that as an indicator that my Server is listening. Instead I click on the Open dedicated DevTools for Node. Doing that has the same result, with the benefit of not having to reopen a debugging window if your Server restarts via something like nodemon.
Navigate to the Sources tab, and you can search for a specific source file or module to place a breakpoint in.
For Client
You'll have to ensure that your compiler (Webpack, Rollup, Parcel - whatever you use), has source maps set up correctly. Also, compilers may have default settings to strip out any inlined debugger; statements, so you'll need to look into that and disable that when building for Local.
If your source maps are set up, you should be able to go to the Sources tab (in Chrome's Devtools) and search for the file you want to debug and place some breakpoints.
If source maps aren't set up, you most likely have a giant bundle file with all your node_modules and source files all compiled together (which could be why your breakpoints aren't firing currently).
In this case, you can try adding a debugger; line within your node_modules file, and see if the debugger stops now. Don't forget to reset the node_modules file after this testing step, it was purely for debugging and shouldn't remain of course.

Debugging a React Component Library in one vscode window while symlinked to a host application

Summary
I have a unique situation where we have created a (for lack of better term) "micro-ui" as a React component. The UI receives props so it can dynamically configure itself to work within several of our larger applications. Due to the nature of it being used in several applications, we have made the decision to distribute it as a component library that exports a single component. The implementation of it works really well, but the debugging has become a bit difficult/non-existent.
Currently we are using npm link to develop the dependency application locally, while running it within the host application. This has worked well, but we have had some issues with source mapping and getting breakpoints to catch. We have solved the source mapping problem with some Webpack configurations, but the breakpoints still won't catch within the linked dependency application. Currently the debugger is attached to the running host application, and the dependency application is simply linked as a local dependency. In an ideal world I would be able to find a solution that allows me to place a breakpoint either in the host application or the dependency application, and it would catch within the debugger.
Attempts So Far
My current attempt is to debug directly within vscode, and I think I am almost there.
So far I have tried several versions of a launch.json file, and have settled on one that works really well when attaching the host application (shown below).
As you can see, I have attempted to add mapping to the sourceMapOverrides key to see if I can explicitly point the node_modules folder for the dependency to the build folder in the root of my dependency project. I have tried this mapping a couple of different ways, but it doesn't catch the break point within the linked application. In addition there are no obvious errors that are thrown, so I am unable to tell if my configuration is invalid. I am certain that I am missing something obvious, but after searching all day I can't seem to find a good solution.
Thanks all in advance for your time and help, and please let me know if you need additional information.
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}",
"runtimeArgs": ["--preserve-symlinks"],
"sourceMapPathOverrides": {
"${webRoot}/node_modules/<dependency_project>/build": "/Users/<USER>/<path_to_dependency_project>/build",
}
}
]
}

Debugging standalone Typescript file in React project using VScode

I've a React project created using create react app. Sometimes I want to play with typescript code in a separate file and debug it.
VSCode requires the generated .js folder to be set in the debug configuration, otherwise the following message appears.
Cannot launch program 'File.ts' setting the 'outFiles' attribute might help.
Or if set to "outFiles": ["${fileDirname}/*.js"]:
Cannot launch program 'File.ts' because corresponding JavaScript cannot be found.
I can't find where the project is generating the .js files. It seems create react app uses webpack, which should use ts-loader. It generates a bundle.js file, but I can't even find it in the workspace folder either.
For reference, my full launch.json. Any help is appreciated. Thanks!
{
"version": "0.2.0",
"configurations": [
// Not working
{
"type": "node",
"request": "launch",
"name": "Debug File",
"program": "${file}",
"outFiles": ["${fileDirname}/*.js"]
},
{
"type": "node",
"name": "Run tests",
"request": "launch",
"args": [
"test",
"--runInBand"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/react-scripts",
"protocol": "inspector"
},
{
"name": "Chrome",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000",
"webRoot": "${workspaceRoot}/src"
}
]
}
You can either build the project as a .ts project and support the legacy js files, or you can include the typescript files output folder so you compile the .ts into .js, and the .js module will be included in your React app.
But you cannot just drop a ts file in a js project and expect React to make that connection for you.
To keep your workspace organized, I recommend move the ts to a separate folder, init that folder as a ts project and include it back in your React project as a library (even if it's a single file)
You can then use testing frameworks such as mocha + chai and jest to test it. Both mocha and jest can test your .ts directly without requiring your to compile it (they're doing it for you, using packages such as jest-ts)
Lerna or Yarn workspaces can help with the linkage, and vs-code can be easily configured to support multiple projects (packages) in the workspace. including for testing.

Not able to use GoogleApi with Cake2.X

I have been trying to use Google API in my 2.X CakePHP project for a couple of days now, and I seem not to be able to use it. The method I used to load it into my test server was installing it in local with composer (I changed my composer.json to include the library which looks like this).
{
"name": "cakephp/cakephp",
"description": "The CakePHP framework",
"type": "library",
"keywords": ["framework"],
"homepage": "http://cakephp.org",
"license": "MIT",
"authors": [
{
"name": "CakePHP Community",
"homepage": "https://github.com/cakephp/cakephp/graphs /contributors"
}
],
"support": {
"issues": "https://github.com/cakephp/cakephp/issues",
"forum": "http://stackoverflow.com/tags/cakephp",
"irc": "irc://irc.freenode.org/cakephp",
"source": "https://github.com/cakephp/cakephp"
},
"require": {
"php": ">=5.2.8",
"ext-mcrypt": "*",
"google/apiclient": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"cakephp/debug_kit" : "2.2.*"
},
"bin": [
"lib/Cake/Console/cake"
]
}
After that I uploaded my whole project using SFTP to my server, and even though I am running a simple script it would show the error "Google_Client class not found". My attemp looks like this:
dd(new Google_Client());
exit();
My output is as follows:
'Class 'Google_Client' not found'
'/var/www/html/app/Controller/AgendaEventosController.php'
I have tried with composer dumpautoload and reuploading the composer autoload to my server via ftp and also tried to import the autoload like this:
App::import("Vendor/Google/apiclient/src/Google","autoload.php"
Can anyone point a better approach to my problem? (also, for what it may be worth, my server's php version is 5.5.9 and my local enviroment -which I used to install the dependency- is 7.2, but I don't think this may be an issue)
As an aditional note: the README file states:
Finally, be sure to include the autoloader:
```php
require_once '/path/to/your-project/vendor/autoload.php';
It may also be worth mentioning I ran the composer install command in the root folder (not in the app folder).
But I am confused on where I should add this line, or even if I should add it, since CakePHP autoloader fires automatically (as fas as I know).

Resources