Problem description
I have a NestJS application that is built in Docker container. I was able to successfully setup debugging. Also, when I adjust the code while app is running, the app is recompiled and restarted and incorporated changes are reflected in behavior of the app.
However, as I do the changes in code, it seems as if source maps are not updated. So, after adding/deleting few lines of code in VS Code, I cannot correctly setup breakpoints and step into the changed code.
See the following two pictures that illustrate my problem:
Picture 1
This shows the correct behavior in VS Code after fresh start of my app. After stepping over line 281, variable content was written to console.
Picture 2
On the second picture I added few lines of code. Now I clicked on line 281 with the first console.log command to add a breakpoint. However, the breakpoint was added to line 288. And after stepping over first console.log, I landed on line 294.
As you can see in the console output, changes in the code were reflected correctly. Only the behavior of breakpoints, as well as step-by-step debugging functionality is broken. After I stop the app by CTRL+C and start it again, everything is refreshed and working. That is, until I make some other changes.
My setup
npm command I use to start my app
nest build && docker-compose -f docker-compose.dev.yaml up --build
docker-compose.dev.yaml
version: '3'
services:
api:
build:
context: .
dockerfile: Dockerfile.dev
image: registry.gitlab.com/...:api-latest
container_name: api
ports:
- "3000:3000"
- "9229:9229"
volumes:
- C:\projects\..path..\src:/app/src
- .\logs:/app/logs
environment:
VARIOUS_VARIABLES
restart: unless-stopped
networks:
- app-network
Dockerfile.dev
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
ADD nest-cli.json .
ADD tsconfig.json .
ADD tsconfig.build.json .
ADD ./.vscode/launch.json ./.vscode/
#ADD . .
EXPOSE 3000
#VSC debug
EXPOSE 9229
CMD ["npm", "run", "start:debug"]
nest-cli.json
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": ["**/*.hbs"],
"watchAssets": true
}
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
},
"watchOptions": {
"watchFile": "priorityPollingInterval",
"watchDirectory": "dynamicPriorityPolling"
}
}
tsconfig.build.json
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
VS Code launch.json I use for debugging
"version": "0.2.0",
"configurations": [
{
"name": "Docker: Attach to Node",
"type": "node",
"request": "attach",
"port": 9229,
"address": "localhost",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app",
"protocol": "inspector",
"restart": true,
"sourceMaps": true,
}
I have looked into several topics on Stackoverflow, for example
Debug in VS Code a Node Typescript app running in Docker
Debug the nestjs project in vscode using docker does not work
Debugging nest.js application with vscode
In many posts I looked in, problem was debugging not working at all which is not my case. But I could have just overlook an existing solution. If anyone could help me with solving this, I would really appreciate it.