Search code examples
vscode-debuggerdenoworking-directory

Why doesn't `console.log(Deno.cwd())` print anything in the debugger?


I have a simple path/to/test.ts script:

console.log(Deno.cwd())

It runs fine with deno run --allow-read .\test.ts and returns path/to as expected. However using the debugger it returns nothing before exiting.

Why is that? Here is the content of launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "request": "launch",
            "name": "Deno",
            "type": "node",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "runtimeExecutable": "deno",
            "runtimeArgs": [
                "run",
                "--allow-read"
            ],
            "attachSimplePort": 9229
        }
    ]
}

I try ${fileDirname}, ${workspaceFolder} and ${cwd} value for the cwd key but it doesn't help.

See more: Visual Studio Code Variables Reference
Also asked on GitHub


Solution

  • The debugger is a separate process with its own I/O streams (e.g. console).

    When debugging a program, any data that's emitted to one of the program's I/O streams (e.g. stdout via console.log) will not be forwarded to the debugger's console until after the debugger connects to the program and creates a pipe to that stream. That connection process takes time, and almost certainly won't happen until after the single console.log statement already finishes in the example program that you showed.

    Deno's run command supports an argument to delay execution until after a debugger connects (added in denoland/deno#17001) — this argument is --inspect-wait.

    From the manual:

    Other runtime flags

    More flags which affect the execution environment.

    (snip)
    
    --inspect-wait=<HOST:PORT>   activate inspector on host:port and wait for ...
    
    (snip)
    

    Include it in the array of args in configurations.runtimeArgs in ./.vscode/launch.json:

    {
      "configurations": [
        {
          "runtimeArgs": [
            "run",
            "--inspect-wait", // include this argument for debugging
            "--allow-read"
          ]
        }
      ]
    }
    

    After adding that argument, I can run your example reproduction files in a VS Code workspace using the debugger and I see this in the debugger console:

    /Users/deno/.deno/bin/deno run --inspect-wait --allow-read ./test.ts
    /Users/deno/so-76891244                                                test.ts:1
    

    Here are the software versions I used to reproduce the issue:

    so-76891244 % code --version
    1.81.1
    6c3e3dba23e8fadc360aed75ce363ba185c49794
    arm64
    
    so-76891244 % deno --version
    deno 1.36.1 (release, aarch64-apple-darwin)
    v8 11.6.189.12
    typescript 5.1.6
    

    Edit: It looks like a Deno core team member just responded at your GitHub issue with a summarized version of the same info.