skip to Main Content

I am trying to write a launch.json config to run a python program called myProg. By design, myProg needs to be run from a folder containing a file named myInput.py; My workflow is that I open myInput.py, put some breakpoints there, and launch myProg through the following launch configuration:

{
  "name": "my launcher",
  "type": "python",
  "request": "launch",
  "program": "/home/user/.local/bin/myProg",
  "args": [
      "install",
      "--someOption=myOption",
      "--someOtherOption=myOtherOption"
  ],
  "cwd": "${fileDirname}",
  "console": "integratedTerminal",
  "justMyCode": false
}

This config works as long as I have myInput.py (or any other file from the folder containing myInput.py) open and in focus in the editor when I click "start debugging", because I have set "cwd" to be "${fileDirname}", i.e. the folder containing the current file.

This is what I want to achieve: I want to get an error when any file other than myInput.py is in focus when I "start debugging".

These are the parts of the behavior that I want to change:

1- Currently when a file from a different folder is selected, the code runs until it reaches the part when it needs to load myInput.py, then it raises an exception and fails. What I want in this case is "not running at all", instead giving me an error, message or similar.

2- Currently when a file from the same folder as myInput.py is selected, the code runs fine. I would prefer it to "not run" and instead give me an error/message that the correct file is not selected.

Is there a way to achieve what I want (item 1 is more important)? In short, I want to have a condition based on which it is decided whether to run the program or not.

Assume that I cannot change "myProg" or how it works.

3

Answers


  1. You can use the extension Command Variable and the command extension.commandvariable.file.fileAsKey

    If the current file does not contain /myInput.py choose a non existing command DoNotRun

    {
      "version": "2.0.0",
      "tasks": [
        {
          "name": "my launcher",
          "type": "python",
          "request": "launch",
          "program": "${input:chooseCmd}",
          "args": [
              "install",
              "--someOption=myOption",
              "--someOtherOption=myOtherOption"
          ],
          "cwd": "${fileDirname}",
          "console": "integratedTerminal",
          "justMyCode": false
        }
      ],
      "inputs": [
        {
          "id": "chooseCmd",
          "type": "command",
          "command": "extension.commandvariable.file.fileAsKey",
          "args": {
            "/myInput.py": "/home/user/.local/bin/myProg",
            "@default": "/home/user/.local/bin/DoNotRun"
          }
        }
      ]
    }
    
    Login or Signup to reply.
  2. If i were you i would try to specify the cwd in launch.json differently, so it looks for myInput.py file from the {workspaceFolder} instead from file dirname. Then it would look for your file from the folder you have opened in vscode not from path of the file you are currently editing.

    {
      "name": "my launcher",
      "type": "python",
      "request": "launch",
      "program": "/home/user/.local/bin/myProg",
      "args": [
          "install",
          "--someOption=myOption",
          "--someOtherOption=myOtherOption"
      ],
      "cwd": "${workspaceFolder}/path to input.py/myInput.py",
      "console": "integratedTerminal",
      "justMyCode": false
    }
    
    Login or Signup to reply.
  3. This sounds like an XY problem. Just set cwd in the launch config to ${workspaceFolder}/<path to directory containing myInput.py>. Even the fact that it’s "by design" that your program needs to be run from that directory might be an XY problem. But there’s not enough info to tell.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search