【发布时间】:2021-06-15 14:44:55
【问题描述】:
我正在尝试在 Docker 容器中运行的 Typescript 代码库上使用 attach 模式在 VsCode 中设置调试器。当我运行我的 docker 容器并通过 VsCode 附加调试器时,我能够命中断点,但它们总是以编译后的 Javascript 代码而不是 Typescript 代码结束。
从图中可以看出,代码是一个无限循环内的简单日志语句。
index.ts
console.log('Hello world');
while(true) {
console.log('a')
}
在使用 Docker 进行设置之前,我检查了 docs 并在本地尝试了调试器,在 Typescript 文件上命中断点没有问题。以下是有关设置的更多信息:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Launch Program",
"port": 9229,
"restart": true,
"address": "localhost",
"remoteRoot": "./",
"localRoot": "${workspaceFolder}",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"sourceMaps": true
}
]
}
docker-compose.yml
version: '3.8'
services:
nodeserver:
command: nodemon --inspect=0.0.0.0:9229 ./dist/index.js
build:
context: ./
dockerfile: ./build/Dockerfile
ports:
- '3000:3000'
- '9229:9229'
Dockerfile
FROM node:15-alpine3.11 as production
WORKDIR /opt/project
COPY package.json .
RUN yarn global add typescript
RUN yarn global add nodemon
RUN yarn install
COPY src src
COPY tsconfig.json .
RUN tsc
tsconfig.json
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"strict": true, /* Enable all strict type-checking options. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
我已经使用 nodemon 和常规节点尝试了多种设置,但这些设置都无法击中断点并将结果指向 Typescript 文件。是否可以在附加到进程时执行此操作?
【问题讨论】:
标签: typescript docker debugging visual-studio-code