Skip to content
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

Arguments in launch.json not being used #20396

Closed
mhdadk opened this issue Dec 18, 2022 · 1 comment
Closed

Arguments in launch.json not being used #20396

mhdadk opened this issue Dec 18, 2022 · 1 comment
Labels
triage-needed Needs assignment to the proper sub-team

Comments

@mhdadk
Copy link

mhdadk commented Dec 18, 2022

Type: Bug

My current workspace directory is path/to/project. Here is what is in this directory:

.vscode/
| launch.json
figures/
scripts/
| test_chdir.py

Here is what is in the launch.json file:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "cwd": "${fileDirname}"
        }
    ]
}

Because I set cwd to "${fileDirname}", then I am expecting that when I run the file scripts/test_chdir.py, the working directory will be scripts. However, that is not the case, and no matter what I do, the working directory is always the workspace directory, i.e. path/to/project. To demo this, the file scripts/test_chdir.py contains:

import os
print(os.getcwd())
assert os.path.isdir("../figures")

When I run this using the “Debug Python file” button, I get the error:

Exception has occurred: AssertionError
exception: no description

I don’t get this error if I cd into the scripts folder and then run python test_chdir.py. In fact, when I change the "program" entry in my launch.json file to be path/to/project/X.py:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "/path/to/project/X.py",
            "console": "integratedTerminal",
            "justMyCode": true,
            "cwd": "${fileDirname}"
        }
    ]
}

which is a file that doesn't exist, I still get the same assertion error above, which means that VS code does not even check the launch.json file. What am I missing here?

Extension version: 2022.20.1
VS Code version: Code 1.74.1 (1ad8d514439d5077d2b0b7ee64d2ce82a9308e5a, 2022-12-14T10:30:51.966Z)
OS version: Windows_NT x64 10.0.22621
Modes:
Sandboxed: No

System Info
Item Value
CPUs 12th Gen Intel(R) Core(TM) i7-12700H (20 x 2688)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off
Load (avg) undefined
Memory (System) 15.72GB (8.23GB free)
Process Argv --crash-reporter-id f3fde2b4-4f86-4e12-b740-e1ffa600c042
Screen Reader no
VM 0%
A/B Experiments
vsliv368:30146709
vsreu685:30147344
python383:30185418
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
pythonvspyl392:30443607
vserr242cf:30382550
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vsdfh931:30280409
vshan820:30294714
vstes263:30335439
vscoreces:30445986
pythondataviewer:30285071
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
cmake_vspar411:30581797
vsaa593:30376534
pythonvs932:30410667
cppdebug:30492333
vsclangdc:30486549
c4g48928:30535728
dsvsc012cf:30540253
azure-dev_surveyone:30548225
pyindex848:30577860
nodejswelcome1:30587005
282f8724:30602487
gswce1:30612156
3d0df643:30613357
f6dab269:30613381
fim-prod:30623723

@mhdadk mhdadk changed the title arguments in launch.json not being used Arguments in launch.json not being used Dec 18, 2022
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Dec 18, 2022
@mhdadk
Copy link
Author

mhdadk commented Dec 19, 2022

I found out what the problem is and its solution. It turns out that whether the "cwd" parameter in the launch.json file will be read or not will depend on how the Python file is run in VS code. From this answer, it seems that if the Python file is run using one of the following actions:

  • Run > Run Without Debugging (or Ctrl+F5)
  • Run > Start Debugging (or F5)

then the "cwd" parameter in the launch.json file will be read, and the correct working directory will be set.

However, if the Python file is run using either the "Run Python File" button or the "Debug Python File" button in the top-right of the editor window, then the "cwd" parameter in the launch.json file will not be read, and the working directory will not be the expected one. According to the answer linked to above, it is possible to force VS code to read the "cwd" parameter in the launch.json file when clicking on the "Debug Python File" button in the top-right of the editor window by setting the "purpose" parameter (more details here) in the launch.json file to "debug-in-terminal", as shown below:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "cwd": "${fileDirname}",
            "purpose": ["debug-in-terminal"]
        }
    ]
}

@mhdadk mhdadk closed this as completed Dec 19, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

1 participant