|
| 1 | +# How do I debug a SAFE app? |
| 2 | + |
| 3 | +## I'm using Visual Studio |
| 4 | +In order to debug Server code from Visual Studio, we need set the correct URLs in the project's debug properties. |
| 5 | + |
| 6 | +### Debugging the Server |
| 7 | + |
| 8 | +#### 1. Configure launch settings |
| 9 | +You can do this through the Server project's **Properties/Debug** editor or by editing the `launchSettings.json` file in `src/Server/Properties` |
| 10 | + |
| 11 | +After selecting the debug profile that you wish to edit (**IIS Express** or **Server**), you will need to set the **App URL** field to `http://localhost:5000` and **Launch browser** field to `http://localhost:8080`. The process is [very similar](https://docs.microsoft.com/en-us/visualstudio/mac/launch-settings?view=vsmac-2019#configure-the-start-url) for VS Mac. |
| 12 | + |
| 13 | +Once this is done, you can expect your `launchSettings.json` file to look something like this: |
| 14 | +```json |
| 15 | +{ |
| 16 | + "iisSettings": { |
| 17 | + "windowsAuthentication": false, |
| 18 | + "anonymousAuthentication": true, |
| 19 | + "iisExpress": { |
| 20 | + "applicationUrl": "http://localhost:5000/", |
| 21 | + "sslPort": 44330 |
| 22 | + } |
| 23 | + }, |
| 24 | + "profiles": { |
| 25 | + "IIS Express": { |
| 26 | + "commandName": "IISExpress", |
| 27 | + "launchBrowser": true, |
| 28 | + "launchUrl": "http://localhost:8080/", |
| 29 | + "environmentVariables": { |
| 30 | + "ASPNETCORE_ENVIRONMENT": "Development" |
| 31 | + } |
| 32 | + }, |
| 33 | + "Server": { |
| 34 | + "commandName": "Project", |
| 35 | + "launchBrowser": true, |
| 36 | + "launchUrl": "http://localhost:8080", |
| 37 | + "environmentVariables": { |
| 38 | + "ASPNETCORE_ENVIRONMENT": "Development" |
| 39 | + }, |
| 40 | + "applicationUrl": "http://localhost:5000" |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +#### 2. Start the Client |
| 47 | +Since you will be running the server directly through Visual Studio, you cannot use a FAKE script to start the application. Launch the Client directly by running the following command in `src/Client` |
| 48 | + |
| 49 | +```html |
| 50 | +dotnet fable watch -o output -s --run npx vite |
| 51 | +``` |
| 52 | + |
| 53 | +#### 3. Debug the Server |
| 54 | +Set the server as your Startup project, either using the drop-down menu at the top of the IDE or by right clicking on the project itself and selecting **Set as Startup Project**. Select the profile that you set up earlier and wish to launch from the drop-down at the top of the IDE. Either press the Play button at the top of the IDE or hit F5 on your keyboard to start the Server debugging and launch a browser pointing at the website. |
| 55 | + |
| 56 | +### Debugging the Client |
| 57 | +Although we write our client-side code using F#, it is being converted into JavaScript at runtime by Fable and executed in the browser. |
| 58 | +However, we can still debug it via the magic of source mapping. If you are using Visual Studio, you cannot directly connect to the browser debugger. You can, however, debug your client F# code using the browser's development tools. |
| 59 | + |
| 60 | +#### 1. Set breakpoints in Client code |
| 61 | +The exact instructions will depend on your browser, but essentially it simply involves: |
| 62 | + |
| 63 | +* Opening the Developer tools panel (usually by hitting F12). |
| 64 | +* Finding the F# file you want to add breakpoints to in the source of the website. |
| 65 | +* Add breakpoints to it in your browser inspector. |
| 66 | + |
| 67 | +## I'm using VS Code |
| 68 | +VS Code allows "full stack" debugging i.e. both the client and server. Prerequisites that you should install: |
| 69 | + |
| 70 | +### Install Prerequisites |
| 71 | + |
| 72 | +* **Install** either [Google Chrome](https://www.google.com/chrome/) or [Microsoft Edge](https://www.microsoft.com/en-us/edge): Enables client-side debugging. |
| 73 | +* **Configure your browser** with the following extensions: |
| 74 | + * [Redux Dev Tools](https://chromewebstore.google.com/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=en): Provides improved debugging support in Chrome with Elmish and access to Redux debugging. |
| 75 | + * [React Developer Tools](https://chromewebstore.google.com/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi): Provides access to React debugging in Chrome. |
| 76 | +* **Configure VS Code** with the following extensions: |
| 77 | + * [Ionide](https://marketplace.visualstudio.com/items?itemName=Ionide.Ionide-fsharp): Provides F# support to Code. |
| 78 | + * [C#](https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp): Provides .NET Core debugging support. |
| 79 | + |
| 80 | +### Debug the Server |
| 81 | + |
| 82 | +1. Click the debug icon on the left hand side, or hit `ctrl+shift+d` to open the debug pane. |
| 83 | +2. Hit the `Run and Debug` button |
| 84 | +3. In the bar with the play error, where it says "No Configurations", use the dropdown to select ".NET 5 and .NET Core". In the dialog that pops up, select "Server.Fsproj: Server" |
| 85 | +4. Hit F5 |
| 86 | + |
| 87 | +The server is now running. You can use the bar at the top of your screen to pause, restart or kill the debugger |
| 88 | + |
| 89 | +### Debug the Client |
| 90 | + |
| 91 | +1. Start the Client by running `dotnet fable watch -o output -s --run npx vite` from `<repo root>/src/Client/`. |
| 92 | +2. Open the Command Palettek using `ctrl+shift+p` and run `Debug: Open Link`. |
| 93 | +3. When prompted for a url, type `http://localhost:8080/`. This will launch a browser which is pointed at the URL and connect the debugger to it. |
| 94 | +4. You can now set breakpoints in your F# code by opening files via the "Loaded Scrips" tab in the debugger; setting breakpoints in files opened from disk does NOT work. |
| 95 | + |
| 96 | +> If you find that your breakpoints aren't being hit, try stopping the Client, disconnecting the debugger and re-launching them both. |
| 97 | +
|
| 98 | +> To find out more about the VS Code debugger, see [here](https://code.visualstudio.com/docs/editor/debugging). |
0 commit comments