-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@nrwl/nest VSCode breakpoints no longer working #14708
Comments
Same problem here after migrating a workspace from 15.4.5 to 15.6.3. |
After reverting to Nx v15.6.2 from v15.6.3, breakpoints are working again. Something changed between 15.6.2 to 15.6.3 that breaks debugging with VSCode. |
Reverting to 15.6.2 from 15.6.3 solved for me. The issue is that the paths in webpack are incorrect in 15.6.3 15.6.3 produces:
15.6.2
|
just tested this with 15.7.2 and the issue still persists. |
I am seeing this too with a NestJS application. But I'm not actually sure it's NestJS-specific. I can also replicate it with the following application:
I can hit breakpoints when built/served with This also means I need to keep using the |
@Dzixxx's workaround didn't work for our projects. I added it to my "serve" target and tried with both the I also tried adding |
Problem seems to still persist in |
Problem still persists in the latest (15.8.5) version. It seems that sources array in a generated source map have wrong relative path. As a workaround, the following const { composePlugins, withNx } = require('@nrwl/webpack')
const path = require('path')
// Nx plugins for webpack.
module.exports = composePlugins(withNx(), (config) => {
// Update the webpack config as needed here.
// e.g. `config.plugins.push(new MyPlugin())`
config.output.devtoolModuleFilenameTemplate = function (info) {
const rel = path.relative(process.cwd(), info.absoluteResourcePath)
return `webpack:///./${rel}`
}
return config
}) |
Still a issue |
Bump this is a critical issue |
Bump |
This worked for me. I also had to change project.json configuration adding these 2 lines that might not be present in case of upgrade from an older version:
|
Thank you @gdraganic. With your workaround and the information in @pmosconi's comment, I was able to get this resolved in our workspace on Nx 15.9.2. We're in the middle of migrating from 14 to 15 right now and this bit us (just now realizing 16 came out a few hours ago 😭). Usually during major migrations I will go ahead and generate a "test app" with the newer schematics for each of our application types (Angular, Nest, React, etc.) just to make sure we keep up with any changes to project configurations that migrations may not capture, so thankfully I had already added the For greater convenience (we have multiple node apps), I created the below in const path = require('path');
/*
* Temporary workaround for debugging Node applications being built with webpack and Nx.
* See: https://github.com/nrwl/nx/issues/14708#issuecomment-1457996600
*/
module.exports = function patchNxSourceMapPaths(config) {
config.output.devtoolModuleFilenameTemplate = function (info) {
const rel = path.relative(process.cwd(), info.absoluteResourcePath);
return `webpack:///./${rel}`;
};
return config;
}; And modified all of our const { composePlugins, withNx } = require('@nrwl/webpack');
const patchNxSourceMapPaths = require('../../tools/helpers/patch-nx-source-map-paths');
// Nx plugins for webpack.
module.exports = composePlugins(withNx(), (config) => {
// Update the webpack config as needed here.
// e.g. `config.plugins.push(new MyPlugin())`
patchNxSourceMapPaths(config);
return config;
}); Hopefully this gets addressed soon. |
thanks for the workaround. still an issue in 16.1.4 |
I tried upgrading from 15.9.2 to 16.2.2 and debugger is not even starting. VSCode output window says: |
Continues to fail in 16.3.2. |
Still an issue in |
经过楼上大佬的讨论 目前 仅仅修改vscode的launch.json即可运行debug,但是这仅针对于vscode调试 其他code编辑器未试 nx版本:16.3.x
vscode:launch.json配置其中backend可以替换成nx工作区内各自项目名称
|
Hi guys, |
Also this doesn't work not only with nest, but also with plain TS express application with executor @nx/webpack:webpack. NX 16.5.2 |
Any solution? The VSCode debugger doesn't work with 16.6.0 too. |
You will either need to patch the webpack config with a custom |
Provided solution does not work when using However, it is a workaround for this: {
"version": "0.2.0",
"configurations": [
{
"name": "Run and debug NestJS",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/node_modules/.bin/nx",
"args": ["serve", "backend", "--configuration=development"],
"cwd": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"env": {
"NODE_OPTIONS": "--max-http-header-size=100000",
"TS_NODE_IGNORE": "false",
"TS_NODE_PROJECT": "${workspaceFolder}/apps/backend/tsconfig.json"
},
"console": "integratedTerminal"
}
]
} Of course I have also added following code to my |
the following config worked for me on
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to NestJS",
"request": "attach",
"cwd": "${workspaceFolder}/apps/<my-nest-project>",
"type": "node",
"port": 9229
}
]
}
// // webpack.config.js
const { composePlugins, withNx } = require('@nx/webpack')
const path = require('path')
const { merge } = require('webpack-merge')
// Nx plugins for webpack.
module.exports = composePlugins(
withNx({
target: 'node',
}),
(config, ctx) => {
// Update the webpack config as needed here.
// e.g. `config.plugins.push(new MyPlugin())`
return merge(config, {
output: {
devtoolModuleFilenameTemplate: (info) => {
const rel = path.relative(ctx.context.cwd, info.absoluteResourcePath)
return `webpack:///./${rel}`
},
},
devtool: 'source-map',
})
},
) |
In my case it was much simpler. I am forcing the usage of the I just added {
"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/apps/web-api",
"main": "apps/web-api/src/main.ts",
"tsConfig": "apps/web-api/tsconfig.app.json",
"assets": ["apps/web-api/src/assets"],
"webpackConfig": "apps/web-api/webpack.config.js",
"target": "node",
"compiler": "tsc",
"sourceMap": true
},
"configurations": {
"production": {
"optimization": true,
"extractLicenses": true,
"inspect": false,
"fileReplacements": [
{
"replace": "apps/web-api/src/environments/environment.ts",
"with": "apps/web-api/src/environments/environment.prod.ts"
}
]
}
}
}
}
} Issue gone! I am running Node 20.11.0 and Nx 17.2 By the way, it even works on IntelliJ IDEA and WebStorm debugger, that is what I actually use |
for the love of god its been months why are we still doing a workaround for a basic necessary feature can this be resolved already? |
And also fix multi-application debugging. The first app that gets built is the only one that can be debugged. |
The same happens for node 18.19 and nx 18.0.4 This solution still works |
nx 18.0.7 and node 20.11.1 |
For the last version this config worked for me
|
All this works, but after adding "build" target, the file watcher doesn't restart the server anymore. How do I fix that? |
If anyone is still searching for the solution, in the webpack configuration file of your NestJS application, enable source map generation: const { NxWebpackPlugin } = require('@nx/webpack');
const { join } = require('path');
module.exports = {
output: {
path: join(__dirname, '../../dist/projects/backend'),
},
plugins: [
new NxWebpackPlugin({
target: 'node',
compiler: 'tsc',
main: './src/main.ts',
tsConfig: './tsconfig.app.json',
assets: ['./src/assets'],
optimization: false,
outputHashing: 'none',
sourceMap: true, // <-- Add this
}),
],
}; Create a .vscode folder in the root of the repository. Create a file "launch.json", with the following contents: {
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Server",
"port": 9229,
"restart": true,
"request": "attach",
"type": "node",
"cwd": "${workspaceFolder}/projects/backend"
}
]
}
Adjust the working directory as needed. |
I was trying to get this working using the auto-attach feature of VSCode so that I could use the Checking the sourcemap it seems the problem with mapped file paths is not resolved so I combined @MilanKovacic suggestion with the answer from @nadiagoralski here and it now works. Below is my complete const path = require("node:path")
const { NxWebpackPlugin } = require("@nx/webpack")
/**
In the v17 version of the fix a context-variable with the root path was available.
It may be possible to get that value some other way, but for now this works
**/
const workspaceRoot = path.join(__dirname, "../..")
module.exports = {
output: {
path: path.join(__dirname, "../../dist/warehouse/api"),
devtoolModuleFilenameTemplate: (info) => { // ref: https://webpack.js.org/configuration/output/#outputdevtoolmodulefilenametemplate
const rel = path.relative(workspaceRoot, info.absoluteResourcePath)
return `webpack:///./${rel}`
}
},
module: {
rules: [
{
test: /\.md$/,
type: "asset/source"
}
]
},
plugins: [
new NxWebpackPlugin({
target: "node",
compiler: "tsc",
main: "./src/main.ts",
tsConfig: "./tsconfig.app.json",
assets: ["./src/assets"],
optimization: false,
generatePackageJson: true,
outputHashing: "none",
sourceMap: true // Added source map to output (will not work without this)
})
]
} |
Did you set the "cwd" (current working directory) in your launch.json file?
This is the reason why working directory must be set to the originating folder.
Reason why all of this doesn't work in nx out of the box is this: config.devtool =
options.sourceMap === 'hidden'
? 'hidden-source-map'
: options.sourceMap
? 'source-map'
: false;` Ideally, nx webpack plugin would use a fast source map strategy such as "eval-source-map" in development, and "source-map" in production. |
@MilanKovacic Thanks for the detailed explanation. I was trying to get it working using the "auto attach" feature so that all you needed to do to get debugging running was to use a pre-defined task that basically ran I'm generally not a big fan of having to start the app then attach the debugger as a separate manual step as you may not be able to break on the very first line of code. I'm sure that starting and attaching can be done using a single launch.json configuration, but it seemed even easier if I could simply auto-attach and then use the |
I've accidentally skipped over the "auto" part of the attach, so I was confused why the solution was not working. |
Hi @ndcunningham, is there any progress on implementing this so that debugging works out of the box? |
bump. |
The debugger is still not working without the workaround/solution provided by @rudfoss in Nx |
Nx webpack.config.js const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin')
const { join } = require('path')
module.exports = {
output: {
path: join(__dirname, '../../../dist/backend/apps/api'),
},
plugins: [
new NxAppWebpackPlugin({
target: 'node',
compiler: 'tsc',
main: './src/main.ts',
tsConfig: './tsconfig.app.json',
assets: ['./src/assets'],
generatePackageJson: true,
optimization: false,
sourceMap: true,
outputHashing: 'none',
}),
],
} launch.json {
"version": "0.2.0",
"configurations": [
{
"name": "Debug API",
"type": "node",
"request": "attach",
"port": 9229,
"restart": true,
"cwd": "${workspaceFolder}/backend/apps/api",
"outFiles": ["${workspaceFolder}/dist/**/*.(m|c|)js", "!**/node_modules/**"]
}
]
} |
Got it working like so: .vscode/launch.json: {
"name": "Attach to API (must be running)",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 9229,
"restart": true,
"sourceMaps": true
} apps/api/project.json"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"target": "node",
"compiler": "tsc",
"outputPath": "dist/apps/api",
"main": "apps/api/src/main.ts",
"tsConfig": "apps/api/tsconfig.app.json",
"webpackConfig": "apps/api/webpack.config.js",
"generatePackageJson": true,
"sourceMap": true
},
"configurations": {
"production": {
"optimization": true,
"extractLicenses": true,
"inspect": false
},
"development": {
"optimization": false
}
}
},
"serve": {
"executor": "@nx/js:node",
"defaultConfiguration": "development",
"dependsOn": ["build"],
"options": {
"buildTarget": "api:build",
"runBuildTargetDependencies": false,
"port": 9229
},
"configurations": {
"development": {
"buildTarget": "api:build:development"
},
"production": {
"buildTarget": "api:build:production"
}
}
}, apps/api/tsconfig.app.json {
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/apps/api",
"module": "commonjs",
"types": ["node"],
"emitDecoratorMetadata": true,
"target": "es2021",
"strictNullChecks": true,
"noImplicitAny": true,
"strictBindCallApply": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
} apps/api/webpack.config.jsconst { composePlugins, withNx } = require('@nx/webpack');
const path = require('path');
module.exports = composePlugins(withNx(), (config) => {
config.output.devtoolModuleFilenameTemplate = function (info) {
const rel = path.relative(process.cwd(), info.absoluteResourcePath);
return `webpack:///./${rel}`;
};
config.optimization = {
minimize: false,
};
return config;
}); package.json "scripts": {
"serve:api": "NODE_OPTIONS='inspect' NODE_ENV=development ENVIRONMENT=local yarn nx run api:serve:development",
...
} |
There is a piece of wall next to me with a nice size "my head" imprinted on it.... no matter what combination of the sorcery mentioned so far has worked despite from all accounts everything looking like it should be working 🥺 Both unit tests and app serving fail to connect, however when serving the app it does seem as though the Nx Node processes are connecting..... so it's close (at least it's doing something!) My stack is vscode, [email protected], [email protected], [email protected], [email protected] & nx/[email protected] |
This solution worked for me. I am using VS Code, with [email protected], [email protected], node v22, and @nx/[email protected]. |
solution:Project.jsonset the project.json build option to run in development. By default, --node-env is always in production. screenshot:pasted below to copy-paste:
web.config.jschange webpack.config.js to set sourcemap to true if not production build. screenshot:pasted below to copy-paste:
launch.jsonfinally add launch.json configuration just basic configuration is sufficient. screenshot:pasted below to copy-paste:
|
still an issue...I 'm on latest nx (20.2.1) and its happening. I can get around with the cwd workaround mentioned above, but only if i set auto attach with flags...which I don't want. |
I tried everything in this thread with the various launch.json settings, webpack.config.js changes, and tsconfig.json options. Got close, but nothing worked. [email protected], [email protected], [email protected] My ultimate fix boiled down to two changes in project.json.
project.json
launch.json (for reference)
webpack.config.js (for reference)
|
None of the solutions really work. And even if they did, I find it extremely ugly to have to deal with |
I agree 100%, it is interesting how long i follow this topic already, and it still hasn't been fixed. Although I hear about NRWL |
Just FYI, to make @gdraganic's solution work for monorepo with multiple apps/projects you would do: const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join, relative } = require('path');
const workspaceRoot = join(__dirname, '../../');
module.exports = {
output: {
path: join(__dirname, '../../dist/apps/api-gateway'),
devtoolModuleFilenameTemplate(info) {
const rel = relative(workspaceRoot, info.absoluteResourcePath);
return `webpack:///./${rel}`;
},
},
plugins: [
new NxAppWebpackPlugin({
target: 'node',
compiler: 'tsc',
main: './src/main.ts',
tsConfig: './tsconfig.app.json',
assets: ['./src/assets'],
optimization: false,
outputHashing: 'none',
generatePackageJson: true,
}),
],
}; EDIT: This works without having to write
It's still a bit hacky but it gets the job done for now. Would love to see this fixed on NX's side though! |
Current Behavior
I am getting unbound breakpoints in VSCode when trying to debug Nest with a fresh
npx create-nx-workspace@latest
. This just suddenly started occurring over the past 3 days with no real cause that I can narrow it down to.Though creating a Nest app using @nestjs/cli with
npx @nestjs/cli@latest new
and serving withnest start --debug
with the same launch.json seems to work. It seems to be something specific to Nx.Expected Behavior
Breakpoints should work
GitHub Repo
No response
Steps to Reproduce
npx create-nx-workspace@latest
code . --disable-extensions
nx serve api
and start VSCode debuggerNx Report
Failure Logs
No response
Additional Information
No response
The text was updated successfully, but these errors were encountered: