Skip to content

Commit

Permalink
internal/core/adt: guard visual debugger from opening too many tabs
Browse files Browse the repository at this point in the history
Add some guards to avoid opening too many browser tabs when
debugging and switching from TestX to TestEval(Alpha).

We both require an explict flag for opening tabs and have a maximum
count for how many tabs can be opened.

Issue #2884

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I5aa1bf6726e10d7da4baa73770f6fd1374a0e790
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1191570
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
mpvl committed Apr 12, 2024
1 parent 03de5ae commit 73611fd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
33 changes: 26 additions & 7 deletions internal/core/adt/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,34 @@ func RecordDebugGraph(ctx *OpContext, v *Vertex, name string) {
}
}

var (
// DebugDeps enables dependency tracking for debugging purposes.
// It is off by default, as it adds a significant overhead.
//
// TODO: hook this init CUE_DEBUG, once we have set this up as a single
// environment variable. For instance, CUE_DEBUG=matchdeps=1.
DebugDeps = false

OpenGraphs = false

// MaxGraphs is the maximum number of debug graphs to be opened. To avoid
// confusion, a panic will be raised if this number is exceeded.
MaxGraphs = 10

numberOpened = 0
)

// OpenNodeGraph takes a given mermaid graph and opens it in the system default
// browser.
func OpenNodeGraph(title, path, code, out, graph string) {
if !OpenGraphs {
return
}
if numberOpened > MaxGraphs {
panic("too many debug graphs opened")
}
numberOpened++

err := os.MkdirAll(path, 0755)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -214,13 +239,6 @@ type ccDep struct {
taskID int
}

// DebugDeps enables dependency tracking for debugging purposes.
// It is off by default, as it adds a significant overhead.
//
// TODO: hook this init CUE_DEBUG, once we have set this up as a single
// environment variable. For instance, CUE_DEBUG=matchdeps=1.
var DebugDeps = false

func (c *closeContext) addDependent(kind depKind, dependant *closeContext) *ccDep {
if !DebugDeps {
return nil
Expand Down Expand Up @@ -643,6 +661,7 @@ func (m *mermaidContext) pstr(cc *closeContext) string {
addFlag(cc.isClosed, 'c')
addFlag(cc.isClosedOnce, 'C')
addFlag(cc.hasEllipsis, 'o')
flags.WriteByte(cc.arcType.String()[0])
io.Copy(w, flags)

w.WriteString(close)
Expand Down
5 changes: 2 additions & 3 deletions internal/core/adt/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,10 @@ func TestX(t *testing.T) {
verbosity = 1 // comment to turn logging off.

adt.DebugDeps = true
// adt.OpenGraphs = true

var version internal.EvaluatorVersion
version = internal.DevVersion // comment to use default implementation.
openGraph := true
openGraph = false

in := `
-- cue.mod/module.cue --
Expand Down Expand Up @@ -242,7 +241,7 @@ module: "mod.test"
adt.Verbosity = 0

out := debug.NodeString(r, v, nil)
if openGraph {
if adt.OpenGraphs {
for p, g := range ctx.ErrorGraphs {
path := filepath.Join(".debug/TestX", p)
adt.OpenNodeGraph("TestX", path, in, out, g)
Expand Down

0 comments on commit 73611fd

Please sign in to comment.