node.jstypescriptkubernetesvisual-studio-codegoogle-cloud-code

Configure VSCode and Cloud Code to debug TypeScript Node application in Kubernetes


I am trying to figure out if it is possible to use Cloud Code in VSCode to debug a TypeScript Node application in Kubernetes remotely?

I used Cloud Code regularly to debug JS Node applications in Kubernetes, but I am having hard time to configure launch.json to do the same for TypeScript Node app. Sources for that is non-existent and at this point I am not even sure if this is possible.

Here is the launch.json file I configured. Also you can see a setting for local debugging which works fine:

{
"configurations": [
    {
        "name": "Kubernetes: Run/Debug",
        "type": "cloudcode.kubernetes",
        "request": "launch",
        "skaffoldConfig": "${workspaceFolder}\\skaffold.yaml",
        "watch": false,
        "cleanUp": false,
        "portForward": true,
        "imageRegistry": "zzz.common.repositories.zzz.zzz"
    },
    {
        "type": "node",
        "request": "launch",
        "name": "Local Debug",
        "runtimeExecutable": "node",
        "runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
        "args": ["${workspaceRoot}/lcs/src/index.ts"],
        "cwd": "${workspaceRoot}/lcs",
        "internalConsoleOptions": "openOnSessionStart",
        "env": { "NODE_ENV": "development" },
        "skipFiles": ["<node_internals>/**", "node_modules/**"]
      }
]}

In my tsconfig.json I have "sourceMap": true. I assume I need to map my dist folder in Kubernetes (that is where I compile TS files to JS files) to src folder (which contains original TS files). Problem is I couldn't find any documentation to do that.

Here is my docker file so you can see that I am putting the compiled files into ./dist folder:

FROM node:19-alpine as builder
WORKDIR /app
COPY package*.json ./
COPY tsconfig.json ./
RUN npm install
COPY . .
RUN npm run build


FROM node:19-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY --from=builder /app/dist ./dist

EXPOSE 3000
CMD [ "node", "./dist/index.js" ]

Solution

  • I figured it out. It needed a bit more configuration in launch.json. Specifically debug section with sourceFileMap. Here is the final result if anybody else has trouble making it work:

    {
        "configurations": [
            {
                "name": "Kubernetes: Run/Debug",
                "type": "cloudcode.kubernetes",
                "request": "launch",
                "skaffoldConfig": "${workspaceFolder}\\skaffold.yaml",
                "watch": false,
                "cleanUp": false,
                "portForward": true,
                "imageRegistry": "zzz.common.repositories.zzz.zzz",
                "debug": [
                    {
                        "image": "zzz.common.repositories.zzz.zzz/zzz-lcs/k8s",
                        "containerName": "lcs",
                        "sourceFileMap": {
                            "${workspaceFolder}\\lcs\\dist": "/app/dist"
                        }
                    }
                ]
            },
            {
                "type": "node",
                "request": "launch",
                "name": "Local Debug",
                "runtimeExecutable": "node",
                "runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
                "args": ["${workspaceRoot}/lcs/src/index.ts"],
                "cwd": "${workspaceRoot}/lcs",
                "internalConsoleOptions": "openOnSessionStart",
                "env": { "NODE_ENV": "development" },
                "skipFiles": ["<node_internals>/**", "node_modules/**"]
              }
        ]
    }
    

    Note that my files are located in my local inside /lcs/app/src and my build put them in ./lcs/dist