From ccabf866853d36501c34085e2fb0801368e49982 Mon Sep 17 00:00:00 2001
From: Davide Silvestri <davide@baserow.io>
Date: Wed, 13 Apr 2022 07:56:43 +0000
Subject: [PATCH] Improve debugging for frontend and backend

---
 backend/requirements/dev.in            |  3 ++-
 backend/requirements/dev.txt           |  2 ++
 backend/src/baserow/manage.py          | 22 ++++++++++++++++++++++
 docker-compose.dev.yml                 | 12 ++++++++----
 web-frontend/config/nuxt.config.dev.js |  3 +++
 5 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/backend/requirements/dev.in b/backend/requirements/dev.in
index 4f5d3b8e8..2274a9484 100644
--- a/backend/requirements/dev.in
+++ b/backend/requirements/dev.in
@@ -26,4 +26,5 @@ pytest-split==0.6.0
 bandit==1.7.2
 pip-tools==6.5.1
 autopep8==1.5.7
-pytest-unordered==0.4.1
\ No newline at end of file
+pytest-unordered==0.4.1
+debugpy==1.5.1
\ No newline at end of file
diff --git a/backend/requirements/dev.txt b/backend/requirements/dev.txt
index 014ff8d53..0d46ff8b1 100644
--- a/backend/requirements/dev.txt
+++ b/backend/requirements/dev.txt
@@ -44,6 +44,8 @@ click==8.0.4
     #   pip-tools
 coverage==6.2
     # via -r dev.in
+debugpy==1.5.1
+    # via -r dev.in
 django==3.2.12
     # via
     #   -c base.txt
diff --git a/backend/src/baserow/manage.py b/backend/src/baserow/manage.py
index c8a80c71d..b24d73f25 100644
--- a/backend/src/baserow/manage.py
+++ b/backend/src/baserow/manage.py
@@ -1,8 +1,30 @@
 #!/usr/bin/env python
+import os
 import sys
 
 
+def enable_debugger():
+    """Enable the debugger if the environment variable is set."""
+    debugger_enabled = bool(os.environ.get("BASEROW_BACKEND_DEBUGGER_ENABLED"))
+    if debugger_enabled:
+        import debugpy
+
+        debugger_port = int(os.environ.get("BASEROW_BACKEND_DEBUGGER_PORT", 5678))
+        debugpy.listen(("0.0.0.0", debugger_port))  # nosec
+        print(f"Debugger attached! Listening on 0.0.0.0:{debugger_port}")
+    else:
+        print(
+            "Debugger disabled. Set the env variable "
+            "BASEROW_BACKEND_DEBUGGER_ENABLED=True to enable it."
+        )
+
+
 def main():
+    from django.conf import settings
+
+    if settings.DEBUG and os.environ.get("RUN_MAIN"):
+        enable_debugger()
+
     try:
         from django.core.management import execute_from_command_line
     except ImportError as exc:
diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml
index 51f7d7434..2ea927b45 100644
--- a/docker-compose.dev.yml
+++ b/docker-compose.dev.yml
@@ -15,6 +15,9 @@ services:
 
   backend:
     image: baserow_backend_dev:latest
+    environment:
+      - BASEROW_BACKEND_DEBUGGER_ENABLED=${BASEROW_BACKEND_DEBUGGER_ENABLED:-True}
+      - BASEROW_BACKEND_DEBUGGER_PORT=${BASEROW_BACKEND_DEBUGGER_PORT:-5678}
     build:
       dockerfile: ./backend/Dockerfile
       context: .
@@ -26,6 +29,7 @@ services:
         GID: $GID
     ports:
       - "${HOST_PUBLISH_IP:-127.0.0.1}:8000:8000"
+      - "${HOST_PUBLISH_IP:-127.0.0.1}:${BASEROW_BACKEND_DEBUGGER_PORT:-5678}:${BASEROW_BACKEND_DEBUGGER_PORT:-5678}"
     volumes:
       - ./backend:/baserow/backend
       - ./premium/backend/:/baserow/premium/backend
@@ -136,15 +140,15 @@ services:
   mailhog:
     image: mailhog/mailhog
     logging:
-      driver: 'none'  # disable saving logs
+      driver: 'none' # disable saving logs
     ports:
       - "8025:8025" # web ui
     networks:
       local:
 
-  # When switching between dev and local the media files in the media volume will be
-  # owned by different users. Ensure that we chown them to the user appropriate for the
-  # environment here.
+        # When switching between dev and local the media files in the media volume will be
+        # owned by different users. Ensure that we chown them to the user appropriate for the
+        # environment here.
   volume-permissions-fixer:
     image: bash:4.4
     command: chown ${UID:-1000}:${GID:-1000} -R /baserow/media
diff --git a/web-frontend/config/nuxt.config.dev.js b/web-frontend/config/nuxt.config.dev.js
index 607448961..be1871c0c 100644
--- a/web-frontend/config/nuxt.config.dev.js
+++ b/web-frontend/config/nuxt.config.dev.js
@@ -15,6 +15,9 @@ export default Object.assign(base(), {
   build: {
     extend(config, ctx) {
       config.node = { fs: 'empty' }
+      if (ctx.isDev) {
+        config.devtool = ctx.isClient ? 'source-map' : 'inline-source-map'
+      }
     },
     babel: { compact: true },
     plugins: [