【问题标题】:How to run a command in Visual Studio Code with launch.json如何使用 launch.json 在 Visual Studio Code 中运行命令
【发布时间】:2017-05-07 21:23:10
【问题描述】:

在使用.vscode/launch.json 调试项目时,有没有办法执行ssh 命令?

例如:ssh -i xxxxx

或者是否可以创建一个可以从 F1 命令面板弹出窗口运行的命令?类似RunCustomCommandxx

【问题讨论】:

标签: visual-studio-code


【解决方案1】:

您可以在tasks.json 文件中定义task,并在launch.json 中将其指定为preLaunchTask,该任务将在调试开始之前执行。

例子:

在tasks.json中

对于 0.1.0 版:

{
    "version": "0.1.0",
    "tasks": [{
        "taskName": "echotest",
        "command": "echo", // Could be any other shell command
        "args": ["test"],
        "isShellCommand": true
    }]
}

对于 2.0.0 版(更新和推荐):

{
    "version": "2.0.0",
    "tasks": [{
        "label": "echotest",
        "command": "echo", // Could be any other shell command
        "args": ["test"],
        "type": "shell"
    }]
}

在launch.json中:

{
    "configurations": [
        {
            // ...
            "preLaunchTask": "echotest", // The name of the task defined above
            // ...
        }
    ]   
}

任务文档:https://code.visualstudio.com/docs/editor/tasks

启动配置:https://code.visualstudio.com/docs/editor/debugging#_launch-configurations

【讨论】:

  • 嗨 Saravana,感谢您的回复,我收到一个错误:无法启动外部程序 ssh -i ~/.ssh/some_key -L 3307:127.0.0.1:3306 forge@xxx.203.107。 80 . spawn ssh -i ~/.ssh/some_key -L 3307:127.0.0.1:3306 forge@xxx.203.100.80 ENOENT
  • 确保 ssh 在您的路径中。或者在你的命令中给出绝对路径。
  • @user3466947,Address already in use 说的还不够吗?用ssh连接机器,试试netstat -lnpt看看进程是不是already using the address
  • 您使用哪种类型的启动配置?我拥有的那些(例如python)需要一个“程序”路径......我想我可以将它设置为一个虚拟的python文件。但是,我什至不想使用调试器,只需运行我的任务即可。
  • 我尝试在 GNU\Linux 中设置环境变量。该任务按预期启动,但我认为环境变量没有传递给launch.json。我做错了什么还是我想做的事情是不可能的? stackoverflow.com/questions/51317136/…
【解决方案2】:

格式改变了。 Visual Studio Code 可以为您创建tasks.json 文件。在您的 launch.json 文件和任何配置对象中,只需定义 preLaunchTask 以强制自动创建 tasks.json 模板文件:

{
  "version": "0.2.0",
  "configurations": [
      {
        "type": "node",
        "request": "launch",
        "name": "launch program",
        "skipFiles": [ "<node_internals>/**"],
        "preLaunchTask": "myShellCommand",
        "program": "${workspaceFolder}/test.js"
      }
  ]
}

如果您没有配置文件tasks.json

  • 启动调试器。 Visual Studio Code 会提示你:“找不到任务 myShellCommand"
  • 选择配置任务创建tasks.json文件 来自模板其他

然后将为您创建tasks.json 模板文件。将您的命令添加到command,并将您在preLaunchTask 中输入的名称添加到label

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "myShellCommand",
      "type": "shell",
      "command": "echo goodfood"
    }
  ]
}

【讨论】:

    【解决方案3】:

    对我来说,我只需要一个环境变量,这是不同的。你不想要这个任务,因为(至少对我来说)启动器运行时它不起作用。

    感谢here,在我的启动器 (launch.json) 条目中,我让它像这样工作:

    "environment": [{
        "name": "ENVIRONMENT_VARIABLE_NAME",
        "value": "${workspaceFolder}/lib" //Set to whatever value you want.
    }],
    

    【讨论】:

      【解决方案4】:

      我的配置版本允许只运行定义的任务并继续(在我的情况下,任务是运行当前打开的Groovy 文件):

      "configurations": [
          {
              "name": "Launch groovy",
              "request": "launch",
              "type": "coreclr",
              "preLaunchTask": "groovy",
              "program": "cmd.exe",
              "args": ["/c"],
              "cwd": "${workspaceFolder}",
              "console": "internalConsole",
              "internalConsoleOptions": "neverOpen"
          }
      ]
      

      还有任务:

      "tasks": [
          {
              "label": "groovy",
              "type": "shell",
              "command": "groovy ${file}"
          }
      ]
      

      【讨论】:

      • 我这样做了,我的任务命令运行了,但是它没有继续启动 python 文件调试。知道为什么吗?
      • 就我而言,"cwd": "${workspaceFolder}" 参数起到了作用。
      【解决方案5】:

      适用于正在搜索如何运行 Flutter 构建命令的 Flutter 开发人员。 在tasks.json中添加

      "tasks": [
              {
                  "type": "flutter",
                  "command": "flutter",
                  "args": [
                      "pub",
                      "run",
                      "build_runner",
                      "build",
                      "--delete-conflicting-outputs"
                  ],
                  "problemMatcher": [
                      "$dart-build_runner"
                  ],
                  "group": "build",
                  "label": "flutter: flutter pub run build_runner build --delete-conflicting-outputs"
              },
      ]
      

      然后从 vscode 你可以尝试“运行任务”它会告诉你flutter: flutter pub run build_runner build --delete-conflicting-outputs

      这样你就不需要记住和输入终端源代码生成/构建命令

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-11
        • 1970-01-01
        • 2016-06-04
        • 2016-04-16
        • 2020-10-07
        • 2018-05-19
        • 2019-07-31
        • 1970-01-01
        相关资源
        最近更新 更多