From 73611fdbd3916808fd82aafeaa1a2e32164c3596 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Wed, 6 Mar 2024 15:14:14 +0100 Subject: [PATCH] internal/core/adt: guard visual debugger from opening too many tabs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: I5aa1bf6726e10d7da4baa73770f6fd1374a0e790 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1191570 Unity-Result: CUE porcuepine Reviewed-by: Daniel Martí TryBot-Result: CUEcueckoo --- internal/core/adt/debug.go | 33 ++++++++++++++++++++++++++------- internal/core/adt/eval_test.go | 5 ++--- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/internal/core/adt/debug.go b/internal/core/adt/debug.go index 4821a184299..c803ac602b9 100644 --- a/internal/core/adt/debug.go +++ b/internal/core/adt/debug.go @@ -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) @@ -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 @@ -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) diff --git a/internal/core/adt/eval_test.go b/internal/core/adt/eval_test.go index e2f36dd5756..688ab21d13e 100644 --- a/internal/core/adt/eval_test.go +++ b/internal/core/adt/eval_test.go @@ -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 -- @@ -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)