|
| 1 | +package dap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/docker/buildx/build" |
| 11 | + "github.com/docker/cli/cli-plugins/metadata" |
| 12 | + "github.com/google/go-dap" |
| 13 | + "github.com/google/shlex" |
| 14 | + "github.com/pkg/errors" |
| 15 | +) |
| 16 | + |
| 17 | +func (d *Adapter) Evaluate(ctx Context, req *dap.EvaluateRequest, resp *dap.EvaluateResponse) error { |
| 18 | + if req.Arguments.Context != "repl" { |
| 19 | + return errors.Errorf("unsupported evaluate context: %s", req.Arguments.Context) |
| 20 | + } |
| 21 | + |
| 22 | + args, err := shlex.Split(req.Arguments.Expression) |
| 23 | + if err != nil { |
| 24 | + return errors.Wrapf(err, "cannot parse expression") |
| 25 | + } |
| 26 | + |
| 27 | + if len(args) == 0 { |
| 28 | + return nil |
| 29 | + } |
| 30 | + |
| 31 | + switch arg0 := args[0]; arg0 { |
| 32 | + case "exec": |
| 33 | + if !d.supportsExec { |
| 34 | + return errors.New("dap client does not support runInTerminalRequest") |
| 35 | + } |
| 36 | + |
| 37 | + t := d.getCurrentThread() |
| 38 | + if t == nil { |
| 39 | + return errors.New("no paused thread for exec command") |
| 40 | + } |
| 41 | + |
| 42 | + argv := args[1:] |
| 43 | + if err := t.Exec(ctx, argv, resp); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + return nil |
| 47 | + default: |
| 48 | + return errors.Errorf("unknown evalute command: %q", arg0) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (t *thread) Exec(ctx Context, args []string, eresp *dap.EvaluateResponse) (retErr error) { |
| 53 | + cfg := &build.InvokeConfig{Tty: true} |
| 54 | + if len(cfg.Entrypoint) == 0 && len(cfg.Cmd) == 0 { |
| 55 | + cfg.Entrypoint = []string{"/bin/sh"} // launch shell by default |
| 56 | + cfg.Cmd = []string{} |
| 57 | + cfg.NoCmd = false |
| 58 | + } |
| 59 | + |
| 60 | + ctr, err := build.NewContainer(ctx, t.rCtx, cfg) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + defer func() { |
| 65 | + if retErr != nil { |
| 66 | + ctr.Cancel() |
| 67 | + } |
| 68 | + }() |
| 69 | + |
| 70 | + dir, err := os.MkdirTemp("", "buildx-dap-exec") |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + defer func() { |
| 75 | + if retErr != nil { |
| 76 | + os.RemoveAll(dir) |
| 77 | + } |
| 78 | + }() |
| 79 | + |
| 80 | + socketPath := filepath.Join(dir, "s.sock") |
| 81 | + l, err := net.Listen("unix", socketPath) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + go func() { |
| 87 | + defer os.RemoveAll(dir) |
| 88 | + t.runExec(l, ctr, cfg) |
| 89 | + }() |
| 90 | + |
| 91 | + // TODO: this should work in standalone mode too. |
| 92 | + docker := os.Getenv(metadata.ReexecEnvvar) |
| 93 | + req := &dap.RunInTerminalRequest{ |
| 94 | + Request: dap.Request{ |
| 95 | + Command: "runInTerminal", |
| 96 | + }, |
| 97 | + Arguments: dap.RunInTerminalRequestArguments{ |
| 98 | + Kind: "integrated", |
| 99 | + Args: []string{docker, "buildx", "dap", "attach", socketPath}, |
| 100 | + Env: map[string]any{ |
| 101 | + "BUILDX_EXPERIMENTAL": "1", |
| 102 | + }, |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + resp := ctx.Request(req) |
| 107 | + if !resp.GetResponse().Success { |
| 108 | + return errors.New(resp.GetResponse().Message) |
| 109 | + } |
| 110 | + |
| 111 | + eresp.Body.Result = fmt.Sprintf("Started process attached to %s.", socketPath) |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (t *thread) runExec(l net.Listener, ctr *build.Container, cfg *build.InvokeConfig) { |
| 116 | + defer l.Close() |
| 117 | + defer ctr.Cancel() |
| 118 | + |
| 119 | + conn, err := l.Accept() |
| 120 | + if err != nil { |
| 121 | + return |
| 122 | + } |
| 123 | + defer conn.Close() |
| 124 | + |
| 125 | + // start a background goroutine to politely refuse any subsequent connections. |
| 126 | + go func() { |
| 127 | + for { |
| 128 | + conn, err := l.Accept() |
| 129 | + if err != nil { |
| 130 | + return |
| 131 | + } |
| 132 | + fmt.Fprint(conn, "Error: Already connected to exec instance.") |
| 133 | + conn.Close() |
| 134 | + } |
| 135 | + }() |
| 136 | + ctr.Exec(context.Background(), cfg, conn, conn, conn) |
| 137 | +} |
0 commit comments