如何使用vscode实现goland类似的本地编译开发机运行的功能

94 阅读1分钟

背景

由于最近尝试使用trae,发现AI协助写代码非常nice,但是远程调试的时候不如goland方便,所以研究了下如何在trae/vscode上实现类似goland本地编译项目,自动部署到远程开发机,然后基于本地代码debug的功能

goland功能展示

image.png

vscode适配

如果下面两个配置文件你不知道是做什么的,请自行google或者求助AI学习下

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "${project_name}-remote",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "${workspaceFolder}/apiserver", // 使用本地代码路径
            "port": 12345,
            "host": "10.37.14.157",
            "cwd": "${workspaceFolder}/apiserver", // 使用本地工作目录
            "preLaunchTask": "build-and-deploy",
            "postDebugTask": "cleanup-remote",
            "env": {
                "GOARCH": "amd64",
                "GOOS": "linux"
            }
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        // 本地编译项目
        {
            "label": "build",
            "type": "shell",
            "command": "bash",
            "args": [
                "-c",
                "cd ${本地项目目录} && GOOS=linux GOARCH=amd64 go build -o ${项目名} ./cmd/"
            ],
            "options": {
                "cwd": "${本地项目目录}"
            },
            "problemMatcher": [],
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        },
        // 上传编译好的二进制文件到开发机
        {
            "label": "deploy",
            "type": "shell",
            "command": "bash",
            "args": [
                "-c",
                "ssh -o \"User=root\" 10.37.14.157 \"rm -f ${开发机二进制文件路径}\" && scp -o \"User=root\" ${本地编译文件路径} root@10.37.14.157:${开发机二进制文件路径}\"
            ],
            "dependsOn": ["build"],
            "problemMatcher": [],
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        },
        // 清理本地的二进制文件
        {
            "label": "clean-local",
            "type": "shell",
            "command": "bash",
            "args": [
                "-c",
                "rm -f  ${本地编译文件路径}"
            ],
            "dependsOn": ["deploy"],
            "problemMatcher": [],
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        },
        // 添加可执行权限
        {
            "label": "setup-remote",
            "type": "shell",
            "command": "bash",
            "args": [
                "-c",
                "ssh -o \"User=root\" 10.37.14.157 \"cd ${开发机二进制文件目录} && chmod +x ${开发机二进制文件路径}\""
            ],
            "dependsOn": ["deploy"],
            "problemMatcher": [],
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        },
        // 开启dlv端口
        {
            "label": "start-debug",
            "type": "shell",
            "command": "bash",
            "args": [
                "-c",
                "ssh -o \"User=root\" 10.37.14.157 \"cd ${开发机二进制文件目录} && PATH=\\\"\\$PATH:/usr/local/go/bin\\\" nohup /usr/local/bin/dlv --listen=:12345 --headless=true --api-version=2 --accept-multiclient exec ${开发机二进制文件路径} > /dev/null 2>&1 &\""
            ],
            "dependsOn": ["setup-remote"],
            "problemMatcher": [],
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        },
        {
            "label": "build-and-deploy",
            "dependsOn": ["build", "deploy", "clean-local", "setup-remote", "start-debug"]
        },
        // kill运行的服务
        {
            "label": "cleanup-remote",
            "type": "shell",
            "command": "bash",
            "args": [
                "-c",
                "ssh -o \"User=root\" 10.37.14.157 \"pkill -9 ${项目名}; pkill -9 dlv\""
            ],
            "problemMatcher": [],
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        }
    ]
}