最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

configuration - How can I leave the terminal open in a build dir after debugging in VS Code? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to have the regular terminal re-open into CMake's build dir so I can easily rebuild in the terminal whenever I need to without switching back into the directory. However, whenever I run the debug task, the terminal closes and reopens back into project directory, which is only because that's how I set up my .bashrc (it goes there when I log in). Here are snippets from the relevant VS Code config files. (internalConsoleOptions is set this way so the terminal takes focus instead of the Debug Console.)

launch.json:

    "configurations": [
        {
            "name": "(gdb) Launch",
            "request": "launch",
            "program": "${workspaceFolder}/src/smud",
            "args": ["3000"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/area",
            "preLaunchTask": "Run the makefile",
            "internalConsoleOptions": "neverOpen",
            "postDebugTask": "Return to build"
        }
    ]

tasks.json:

        {
            "type": "shell",
            "label": "Return to build",
            "command": "cd ${workspaceFolder}/src/build; exec $SHELL",
            "options": {
                "cwd": "${workspaceFolder}/src/build"
            },
            "presentation": {
                "reveal": "always",
                "panel": "new",
                "echo": false
            }
        }

This works when I have exec $SHELL by keeping the process running, but then I have the issue that the process never ends. As soon as I kill it, the terminal closes and relaunches back to the working dir. If I don't have exec $SHELL, it immediately resets as if the task isn't even there. Thanks for the help!

I'm trying to have the regular terminal re-open into CMake's build dir so I can easily rebuild in the terminal whenever I need to without switching back into the directory. However, whenever I run the debug task, the terminal closes and reopens back into project directory, which is only because that's how I set up my .bashrc (it goes there when I log in). Here are snippets from the relevant VS Code config files. (internalConsoleOptions is set this way so the terminal takes focus instead of the Debug Console.)

launch.json:

    "configurations": [
        {
            "name": "(gdb) Launch",
            "request": "launch",
            "program": "${workspaceFolder}/src/smud",
            "args": ["3000"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/area",
            "preLaunchTask": "Run the makefile",
            "internalConsoleOptions": "neverOpen",
            "postDebugTask": "Return to build"
        }
    ]

tasks.json:

        {
            "type": "shell",
            "label": "Return to build",
            "command": "cd ${workspaceFolder}/src/build; exec $SHELL",
            "options": {
                "cwd": "${workspaceFolder}/src/build"
            },
            "presentation": {
                "reveal": "always",
                "panel": "new",
                "echo": false
            }
        }

This works when I have exec $SHELL by keeping the process running, but then I have the issue that the process never ends. As soon as I kill it, the terminal closes and relaunches back to the working dir. If I don't have exec $SHELL, it immediately resets as if the task isn't even there. Thanks for the help!

Share Improve this question asked Jan 19 at 14:05 BrainFRZBrainFRZ 4073 silver badges17 bronze badges 1
  • You can create a terminal profile with your relevant directory as the default directory, and then set that terminal profile as the default one in your workspace settings. – gliesefire Commented Jan 26 at 11:29
Add a comment  | 

1 Answer 1

Reset to default 0

(aided by deepseekAI)

Step 1: Modify Your .bashrc Update your.bashrc to conditionally change directories only when not running a VS Code task:

**bash**
# ~/.bashrc
if [ -z "$VSCODE_TASK" ]; then
    cd /path/to/your/project  # Replace with your actual project path
fi

Step 2: Update the Return to build Task in tasks.json Configure the task to set the environment variable VSCODE_TASK and the correct working directory:

**json**
{
    "label": "Return to build",
    "type": "shell",
    "command": "exec $SHELL",
    "options": {
        "cwd": "${workspaceFolder}/src/build",
        "env": {
            "VSCODE_TASK": "1"
        }
    },
    "presentation": {
        "reveal": "always",
        "panel": "new",
        "echo": false
    }
}

Explanation:

.bashrc Change: The VSCODE_TASK environment variable check skips the cd command in your shell startup, preventing the directory reset.

Task Configuration:

  • cwd sets the starting directory to your build folder.
  • env sets VSCODE_TASK to bypass the .bashrc cd.
  • exec $SHELL starts a new shell in the build directory without exiting the task immediately.

This setup ensures the terminal remains in the build directory after debugging, allowing you to rebuild without switching directories manually.

发布评论

评论列表(0)

  1. 暂无评论