Debugging python in docker container using debugpy and vs code results in timeout/connection refused - python-3.x

I'm trying to setup native debugging for a python script running in docker for Visual Studio Code using debugpy. Ideally I'd like to just F5 and be on my way (including a build phase if needed). Currently I'm bouncing between a timeout caused from debugpy.listen(5678) inlined within the VS code editor itself (Exception has occurred: RuntimeError timed out waiting for adapter to connect) or a connection refused.
I created a launch.json from the documentation provided by microsoft:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Integration (test)",
"type": "python",
"request": "attach",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/test",
"remoteRoot": "/test"
}
],
"port": 5678,
"host": "127.0.0.1"
}
]
}
building the image looks like this so far:
Dockerfile
FROM python:3.7-slim-buster as base
RUN apt-get -y update; apt-get install -y vim git cmake
WORKDIR /
RUN mkdir .cache src in out config log
COPY requirements.txt .
RUN pip install -r requirements.txt; rm requirements.txt
#! TODO: config folder needs to be a mapped volume so they can change creds without rebuild
WORKDIR /src
COPY test ../test
COPY config ../config
COPY src/ .
#? D E B U G I M A G E
FROM base as debug
RUN pip install debugpy
CMD python -m debugpy --listen 0.0.0.0:5678 ../test/edu.employer._test.py
#! P R O D U C T I O N I M A G E
# FROM base as prod
# CMD [ "python", "/test/edu.employer._test.py" ]
Some examples I found try to simply things with a docker-compose.yaml, but I'm unsure if i need one at this point.
docker-compose.yaml
services:
tester:
container_name: tester
image: employer/test:1.0.0
build:
context: .
target: debug
dockerfile: test/edu.employer._test.Dockerfile
volumes:
- ./out:/out
- ./.cache:/.cache
- ./log:/log
ports:
- 5678:5678
which I based off a the CLI command: docker run -it -v $(pwd)/out:/out -v $(pwd)/.cache:/.cache -v $(pwd)/log:/log employer/test:1.0.0;
"critical" parts of my script just listen and wait for the bugger:
from __future__ import absolute_import
# Standard
import os
import sys
# 3rd Party
import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()
# 1st Party. NOTE: All source files are in /src, so we can add that path here for testing
# and batch import all integrations files. Not very clean however
sys.path.insert(0, os.path.join('/', 'src'))
import integrations as ints

You have to configure the debugger with: debugpy.listen(("0.0.0.0", 5678)).
This happens because, by default, debugpy is listening on localhost. If you have your docker container on another host you have to add 0.0.0.0.

Turns out I needed to create a tasks.json file and provide the details on running the image...
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"dockerRun": {
"image": "employer/test:1.0.0"
// "env": {
// "FLASK_APP": "path_to/flask_entry_point.py"
// }
},
"python": {
"args": [],
"file": "/test/edu.employer._test.py"
}
}
]
}
and define a preLaunchTask:
{
"name": "Docker: Python",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}/test",
"remoteRoot": "/test"
}
],
//"projectType": "django"
}
}

Related

Unknown error while debuggin rust application in VS Code

I am trying to debug a fairly large rust project in VS code.
The launch.json has this:
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'rpfm_ui'",
"cargo": {
"args": [
"build",
"--bin=rpfm_ui",
"--package=rpfm_ui"
],
"filter": {
"name": "rpfm_ui",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
But when I try to run the application I get the following
Finished dev [unoptimized + debuginfo] target(s) in 9.53s
Raw artifacts:
{
fileName: 'c:\\Users\\ole_k\\Desktop\\rpfm-master\\target\\debug\\rpfm_ui.exe',
name: 'rpfm_ui',
kind: 'bin'
}
Filtered artifacts:
{
fileName: 'c:\\Users\\ole_k\\Desktop\\rpfm-master\\target\\debug\\rpfm_ui.exe',
name: 'rpfm_ui',
kind: 'bin'
}
configuration: {
type: 'lldb',
request: 'launch',
name: "Debug executable 'rpfm_ui'",
args: [],
cwd: '${workspaceFolder}',
relativePathBase: 'c:\\Users\\ole_k\\Desktop\\rpfm-master',
program: 'c:\\Users\\ole_k\\Desktop\\rpfm-master\\target\\debug\\rpfm_ui.exe',
sourceLanguages: [ 'rust' ]
}
Listening on port 49771
[adapter\src\terminal.rs:99] FreeConsole() = 1
[adapter\src\terminal.rs:100] AttachConsole(pid) = 1
[adapter\src\terminal.rs:104] FreeConsole() = 1
[2020-06-27T20:43:04Z ERROR codelldb::debug_session] process launch failed: unknown error
Debug adapter exit code=0, signal=null.
I have also seen this:
PS C:\Users\ole_k\Desktop\rpfm-master> & 'c:\Users\ole_k.vscode\extensions\vadimcn.vscode-lldb-1.5.3\adapter\codelldb.exe' 'terminal-agent' '--port=49628'
Error: Os { code: 10061, kind: ConnectionRefused, message: "No connection could be made because the target machine actively refused it." }
[2020-06-27T20:29:08Z ERROR codelldb::debug_session] process launch failed: unknown error
If I run the application from the terminal inside vs code (cargo run --bin rpfm_ui) it works.
There are some external dependencies which are in folders outside of the root folder.
I can debug other projects in the solution which share a lot of the code, but not the external dependencies.
I can debug other projects.
I am running as administrator.
Any ideas on how to resolve the issue?

How to fix Jest "No Tests Found" on windows 10?

I am trying to use Jest on my windows 10 desktop computer, but it keeps telling me that there are no tests found. On my windows 10 laptop, it works just fine. Here is the output I am getting on my desktop:
C:\app> jest
No tests found
In C:\app
25163 files checked.
testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 743 matches
testPathIgnorePatterns: \\node_modules\\ - 25163 matches
Pattern: "" - 0 matches
In my package.json file, my jest config looks like this:
"jest": {
"collectCoverageFrom": [
"app/**/*.{js,jsx}",
"!app/**/*.test.{js,jsx}",
"!app/*/RbGenerated*/*.{js,jsx}",
"!app/app.js"
],
"coverageThreshold": {
"global": {
"statements": 98,
"branches": 91,
"functions": 98,
"lines": 98
}
},
"moduleDirectories": [
"node_modules",
"app",
"common"
],
"moduleNameMapper": {
".*\\.(css|less|styl|scss|sass)$": "<rootDir>/internals/mocks/cssModule.js",
".*\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/internals/mocks/image.js"
},
"setupTestFrameworkScriptFile": "<rootDir>/internals/testing/test-bundler.js"
}
I am using node 8.1.4 and jest v20.0.4
Any ideas on how to get jest to locate my tests?
I am not 100% sure its the same issue. But what solved it for me was to get rid of watchman (I added it in on path for another project that used relay). Try to run with --no-watchman (or set watchman: false in jest config)
Seeing this issue with Jest 24.8.0. It seems if you add --runTestsByPath it will correctly handle forward/backspaces,
There is a discussion of the issue https://github.com/microsoft/vscode-recipes/issues/205#issuecomment-533645097, with the following suggested VSCode debug configuration
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--runTestsByPath", // This ensures the next line is treated as a path
"${relativeFile}", // This path may contain backslashes on windows
"--config",
"jest.config.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
For anyone attempting to find out how to fix this issue, this was a bug in Jest that was fixed in v22.
Changelog:
https://github.com/facebook/jest/blob/master/CHANGELOG.md (PR #5054)
If I run the console command
jest test/components/checkBox/treezCheckBox.test.js
the tests in that file are found and executed.
If I instead run the console command
jest test\components\checkBox\treezCheckBox.test.js
I get the error
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In D:\treezjs
814 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 44 matches
testPathIgnorePatterns: \\node_modules\\ - 814 matches
testRegex: - 0 matches
Pattern: test\components\checkBox\treezCheckBox.test.js - 0 matches
=> It seems to be important if forward or backward slashes are used.
Using doubled backward slashes works:
jest test\\components\\checkBox\\treezCheckBox.test.js
If you use a vscode launch configuration with a file path variable ${file}, the resulting system command unfortunately contains single "\" as separator.
Also see discussion and linked issues at https://github.com/Microsoft/vscode/issues/40256
(Last statement is outdated; ${relativeFile} also uses "\".)
Work around: Use a debug extension (e.g. Daddy Jest) instead of a custom launch configuration.
I have removed -- --watch from package.json where I wrote "test" : "jest -- --watch"

VSCode/Win10 - Cannot find runtime 'node' on PATH

To reproduce:
Use my Windows 10 PC to open VSCode
Create a Hello World js file
Launch as node application from the VSCode Debugger view
Produces error: Cannot find runtime 'node' on PATH
Given:
My launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Build",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/build.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": false
}
]
}
I have moved nodejs to within 2048 characters of my PATH variable.
I have restarted my computer.
I have restarted VSCode.
From both CMD and the Integrated Terminal (sysnative/cmd):
PATH contains D:/Program Files/nodejs
where node returns D:/Program Files/nodejs/node.exe
From the Google Developer Console:
process.env.PATH contains D:/Program Files/nodejs
Running the following code produces D:/Program Files/nodejs/node.exe:
(function(){
const cp = require('child_process');
const env = Object.assign({}, process.env, {
ATOM_SHELL_INTERNAL_RUN_AS_NODE: '1',
ELECTRON_NO_ATTACH_CONSOLE: '1'
});
const result = cp.spawnSync('where', ['node'], {
detached: true,
stdio: ['ignore', 'pipe', process.stderr],
env,
encoding: 'utf8'
});
console.log(result.stdout);
})();
Additional
Also .NET-Core applications behave identically - terminal and cmd works, dotnet is on the path, but Launch from VSCode debugger view fails to find CLI Tools on the path.
Attaching to an existing dotnet process produces a different error:
Please set up the launch configuration file for your application. command 'csharp.listProcess' not found
Not sure if related, but F12 for jumping to declaration is unresponsive.
Update
I've been doing some debugging, and it looks like the following code produces Command Failed: echo test:
require('child_process')
.execSync('echo test', {cwd: workspaceRoot, env: process.env});
Under the hood, it winds up calling
require('child_process')
.spawnSync('cmd', ['/s', '/c', '"echo test"'], {cwd: workspaceRoot, env: process.env});
The command it builds under the hood is C:\Windows\System32\cmd.exe /s /c "echo test" which I tested and does indeed print test.
The spawnSync call reveals that the exit code was 3221225477.
In fact, every time I use child_process to execute something via cmd, the exit code is 3221225477. I can get spawnSync to start other processes aside from cmd, though. This works:
require('child_process')
.spawnSync('node', ['build.js'], {cwd: workspaceRoot, env: process.env});

How to install a nodejs cms like pencilblue on uberspace

I would like to have the pencilblue nodejs cms with mongodb installed on my uberspace account. Which steps do I have to take?
As I found it hard figuring out how to do it, here is how I finally succeeded. Most of it is relevant for nodeJS installations other than pencilblue as well.
First you need to create an account on uberspace.de.
Open your terminal and login into your uberspace console with ssh:
ssh {account}#{server}.uberspace.de
Enter the password you created with the creation of the account.
Create the service directory:
uberspace-setup-svscan
Create the mongo database:
uberspace-setup-mongodb
Create folder for database data:
mkdir data
cd data
mkdir db
Start db:
mongod --dbpath data/db/
You will get some login data. I suggest you write it down somewhere:
Hostname: localhost
Portnum#: {dbPort}
Username: {account}_mongoadmin
Password: {dbPassword}
To connect to the db via shell you may use:
mongo admin --port {dbPort} -u {account}_mongoadmin -p)
Configure npm:
cat > ~/.npmrc <<__EOF__
prefix = $HOME
umask = 077
__EOF__
Install pencilblue-cli:
npm install pencilblue-cli
Change to html-folder and create a .htaccess file (you could do this with your ftp-client as well):
RewriteEngine On
RewriteRule ^(.*) http://localhost:8080/$1 [P]
Now if you want to use github:
Create a new repository on github.
Open a new terminal window and clone pencilblue cms in a local folder on your machine:
git clone git#github.com:pencilblue/pencilblue.git pencilblue
cd pencilblue
git remote set-url origin git#github.com:{yourGitName}/{yourRepoName}.git
git add .
git commit -m "Initial commit."
Setup ssh on uberspace:
Go back to your uberspace console.
ssh-keygen -t rsa -b 4096 -C "{yourEmailAddress}"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
Copy the whole key that is printed out and paste it in github under settings/SSH keys.
Clone the new repo in uberspace console:
git clone git#github.com:{yourGitName}/{yourRepoName}.git cms
cd cms
Create a config.js either with vim config.js or upload it with ftp:
module.exports = {
"siteName": "{yourSiteName}",
"siteRoot": "http://{account}.{server}.uberspace.de/",
"sitePort": {
8080
},
"logging": {
"level": "info"
},
"db": {
"type": "mongo",
"servers": [
"mongodb://{account}_mongoadmin:{dbPassword}#127.0.0.1:{dbPort}/"
],
"name": "admin",
"writeConcern": 1
},
"cache": {
"fake": false,
"host": "localhost",
"port": 6379
},
"settings": {
"use_memory": false,
"use_cache": false
},
"templates": {
"use_memory": true,
"use_cache": false
},
"plugins": {
"caching": {
"use_memory": false,
"use_cache": false
}
},
"registry": {
"type": "mongo"
},
"session": {
"storage": "mongo"
},
"media": {
"provider": "mongo",
"max_upload_size": 6291456
},
"cluster": {
"workers": 1,
"self_managed": true
},
"siteIP": "0.0.0.0"
};
Install node_modules:
npm install
Create a service that starts the server:
uberspace-setup-service pbservice node ~/cms/pencilblue.js
Start the service:
svc -u ~/service/pbservice
Now you can go to the page on http://{account}.{server}.uberspace.de
(To start the service (hint: u = up):
svc -u ~/service/pbservice
To stop the service (hint: d = down):
svc -d ~/service/pbservice
To reload the service (hint: h = HUP):
svc -h ~/service/pbservice
To restart the service (hint: du = down, up):
svc -du ~/service/pbservice
To remove the service:
cd ~/service/pbservice
rm ~/service/pbservice
svc -dx . log
rm -rf ~/etc/run-pbservice)

How to use nodemon with JSX?

I can compile and run my JSX app with one command:
jsx app.jsx | node
But I also want my server to automatically restart every time I modify app.jsx. I can do that with nodemon, but I can't quite figure out how to get nodemon to run my script through the JSX compiler beforehand.
I've got a nodemon.json file set up like this:
{
"execMap": {
"js": "node",
"jsx": "jsx {{filename}} | node"
},
"ext": "js jsx",
"ignore": [
".hg",
"node_modules",
".idea"
],
"verbose": true
}
But when I run nodemon it tells me:
8 Feb 21:58:48 - [nodemon] starting `jsx app.jsx | node`
8 Feb 21:58:48 - [nodemon] child pid: 10976
'\"jsx app.jsx | node\"' is not recognized as an internal or external command,
operable program or batch file.
Which is odd, because that command works verbatim when I paste it directly into my terminal.
Is there any way I get nodemon to run my JSX files?
It seems nodemon is attempting to run a program with the name you provide, rather than executing a shell.
Create a jsx.sh file with this content:
#!/bin/sh
jsx "$1" | node
Then chmod +x jsx.sh, and put this in your nodemon.json:
{
"execMap": {
"js": "node",
"jsx": "./jsx.sh"
},
"ext": "js jsx",
"ignore": [
".hg",
"node_modules",
".idea"
],
"verbose": true
}
* not tested
OR you can just locate the jsx command in your ./node_modules/.bin directory and run it off that instead:
{
script: "client.js",
options: {
execMap: {
"js": "node",
"jsx": "./node_modules/.bin/jsx \"$1\" | node"
},
ext: "js jsx",
callback: function (nodemon) {
nodemon.on("log", function (event) {
console.log(event.colour);
});
},
ignore: [
"node_modules/**/*.js",
"public/js/**",
"lib/api/**",
]
}
}
If you're on Windows (like me) you can create a .bat instead of a .sh like FakeRainBrigand suggests
#echo off
jsx %1 | node
This file has to be in the same directory as nodemon.json and package.json -- paths don't seem to work in the execMap for whatever reason.
Also, an even easier solution is to just not use any JSX in your main/server script, install node-jsx and then require your JSX files as needed.

Resources