Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dap subcommand and support basic DAP features #349

Merged
merged 8 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cmd/falco/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ func printHelp(cmd string) {
printTerraformHelp()
case subcommandSimulate:
printSimulateHelp()
case subcommandDAP:
printDAPHelp()
case subcommandStats:
printStatsHelp()
case subcommandTest:
Expand Down Expand Up @@ -48,6 +50,7 @@ Subcommands:
lint : Run lint (default)
stats : Analyze VCL statistics
simulate : Run simulator server with provided VCLs
dap : Launch DAP server to debug VCLs
test : Run local testing for provided VCLs
console : Run terminal console
fmt : Run formatter for provided VCLs
Expand Down Expand Up @@ -93,6 +96,19 @@ Linting with terraform:
`))
}

func printDAPHelp() {
writeln(white, strings.TrimSpace(`
Usage:
falco dap

Flags:
-h, --help : Show this help

This command launches Debug Adapter Protocol server.
Execute this command by using your editor's DAP support.
`))
}

func printSimulateHelp() {
writeln(white, strings.TrimSpace(`
Usage:
Expand Down
7 changes: 7 additions & 0 deletions cmd/falco/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pkg/errors"
"github.com/ysugimoto/falco/config"
"github.com/ysugimoto/falco/console"
"github.com/ysugimoto/falco/dap"
ife "github.com/ysugimoto/falco/interpreter/function/errors"
"github.com/ysugimoto/falco/lexer"
"github.com/ysugimoto/falco/remote"
Expand Down Expand Up @@ -55,6 +56,7 @@ const (
subcommandLint = "lint"
subcommandTerraform = "terraform"
subcommandSimulate = "simulate"
subcommandDAP = "dap"
subcommandStats = "stats"
subcommandTest = "test"
subcommandConsole = "console"
Expand Down Expand Up @@ -105,6 +107,11 @@ func main() {
os.Exit(1)
}
os.Exit(0)
case subcommandDAP:
if err := dap.New(c.Simulator).Run(); err != nil {
os.Exit(1)
}
os.Exit(0)
case subcommandFormat:
// "fmt" command accepts multiple target files
resolvers, err = resolver.NewGlobResolver(c.Commands[1:]...)
Expand Down
39 changes: 39 additions & 0 deletions dap/adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dap

import (
"bufio"
"context"
"io"
"log"
"os"

"github.com/ysugimoto/falco/config"
)

type Adapter struct {
config *config.SimulatorConfig
session *session
}

func New(sc *config.SimulatorConfig) *Adapter {
return &Adapter{
config: sc,
}
}

func (a *Adapter) Run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

a.session = &session{
conn: bufio.NewReadWriter(
bufio.NewReader(os.Stdin),
bufio.NewWriter(os.Stdout),
),
config: a.config,
}

log.SetOutput(io.Discard)

return a.session.start(ctx)
}
72 changes: 72 additions & 0 deletions dap/breakpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dap

import "sync"

type breakpointColl struct {
breakpoints map[string][]breakpoint // map[path][]breakpoint
counter int
mu sync.Mutex
}

type breakpoint struct {
path string
line int
id int
}

func (bpc *breakpointColl) newID() int {
bpc.counter++
return bpc.counter
}

func (bpc *breakpointColl) add(path string, line int) breakpoint {
bpc.mu.Lock()
defer bpc.mu.Unlock()

bp := breakpoint{
path: path,
line: line,
id: bpc.newID(),
}

bpc.breakpoints[path] = append(bpc.breakpoints[path], bp)

return bp
}

func (bpc *breakpointColl) clear(path string) {
bpc.mu.Lock()
defer bpc.mu.Unlock()

delete(bpc.breakpoints, path)
}

func (bpc *breakpointColl) list(path string) []breakpoint {
bpc.mu.Lock()
defer bpc.mu.Unlock()

bps, ok := bpc.breakpoints[path]
if !ok {
return []breakpoint{}
}

return bps
}

func (bpc *breakpointColl) getBreakpoint(path string, line int) *breakpoint {
bpc.mu.Lock()
defer bpc.mu.Unlock()

bps, ok := bpc.breakpoints[path]
if !ok {
return nil
}

for _, bp := range bps {
if bp.line == line {
return &bp
}
}

return nil
}
110 changes: 110 additions & 0 deletions dap/debugger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package dap

import (
"sync"

"github.com/ysugimoto/falco/ast"
"github.com/ysugimoto/falco/interpreter"
)

type Debugger struct {
mode interpreter.DebugState
stateCh <-chan interpreter.DebugState

printFunc func(msg string)
notifyStoppedFunc func(params *notifyStoppedEventParams)

breakpoints *breakpointColl
stacks *stackColl
}

func newDebugger(stateCh <-chan interpreter.DebugState) *Debugger {
return &Debugger{
stateCh: stateCh,
breakpoints: &breakpointColl{
breakpoints: map[string][]breakpoint{},
counter: 0,
mu: sync.Mutex{},
},
stacks: &stackColl{
stacks: []stack{},
counter: 0,
mu: sync.Mutex{},
},
}
}

func (d *Debugger) Run(node ast.Node) interpreter.DebugState {
switch d.mode {
case interpreter.DebugStepIn, interpreter.DebugStepOver:
d.appendStack(node)
d.notifyStoppedFunc(&notifyStoppedEventParams{
reason: "step",
})
return d.waitForNewState()
case interpreter.DebugStepOut:
d.mode = interpreter.DebugStepOver
d.appendStack(node)
d.notifyStoppedFunc(&notifyStoppedEventParams{
reason: "step",
})

return d.waitForNewState()
default:
if bp := d.getBreakpoint(node); bp != nil {
d.mode = interpreter.DebugStepOver
d.appendStack(node)
d.notifyStoppedFunc(&notifyStoppedEventParams{
reason: "breakpoint",
breakpointIDs: []int{bp.id},
})
return d.waitForNewState()
}
return interpreter.DebugPass
}
}

func (d *Debugger) Message(msg string) {
d.printFunc(msg)
}

func (d *Debugger) waitForNewState() interpreter.DebugState {
d.mode = <-d.stateCh

return d.mode
}

func (d *Debugger) getBreakpoint(node ast.Node) *breakpoint {
meta := node.GetMeta()

return d.breakpoints.getBreakpoint(meta.Token.File, meta.Token.Line)
}

func (d *Debugger) clearBreakpoints(path string) {
d.breakpoints.clear(path)
}

func (d *Debugger) setBreakpoint(path string, line int) breakpoint {
return d.breakpoints.add(path, line)
}

func (d *Debugger) listBreakpoints(path string) []int {
bps := d.breakpoints.list(path)

lns := make([]int, 0, len(bps))

for _, bp := range bps {
lns = append(lns, bp.line)
}

return lns
}

func (d *Debugger) appendStack(node ast.Node) {
meta := node.GetMeta()
d.stacks.append(meta.Token.Literal, meta.Token.File, meta.Token.Line)
}

func (d *Debugger) listStacks() []stack {
return d.stacks.list()
}
Loading