From 0daa25b5f83e9e219eba0f7f0e7f729d08f335b2 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 05:30:55 +0000 Subject: [PATCH 01/13] feat(check): MDL097 flags returns T as $R when a path ends the flow without $R MDL003 exempts the AS clause because the end event returns $R, which holds only while $R has a value where the flow ends. A path off the end with $R never defined is CE0109 on MxBuild (CE0108 when $R is defined only in a branch or loop); a top-level declare is clean. Measured on 11.16.1 over 15 flows, microflow and nanoflow. Guarded by bodyReturns and lastStmtIsReturn. A sweep of every .mdl in the repo (1658 flows, 684 with an AS clause) finds two hits, both correct. docs-site tutorial snippet fixed: it declared an AS variable it never defined. Refs ako/mxcli#1258. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011oK82tETa46YQmMS58pQjL --- ...6-11-17-return-variable-never-defined.json | 1 + .../write-microflows/reference/pitfalls.md | 22 ++++ CHANGELOG.md | 1 + docs-site/src/tutorial/skills.md | 1 + mdl/executor/validate_microflow.go | 4 + mdl/executor/validate_nanoflow.go | 4 + .../validate_return_variable_defined.go | 80 ++++++++++++ .../validate_return_variable_defined_test.go | 123 ++++++++++++++++++ 8 files changed, 236 insertions(+) create mode 100644 .claude/skills/fix-issue/findings/mdl-executor/2026-11-17-return-variable-never-defined.json create mode 100644 mdl/executor/validate_return_variable_defined.go create mode 100644 mdl/executor/validate_return_variable_defined_test.go diff --git a/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-return-variable-never-defined.json b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-return-variable-never-defined.json new file mode 100644 index 0000000000..ee95c9840f --- /dev/null +++ b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-return-variable-never-defined.json @@ -0,0 +1 @@ +{"area": "mdl/executor", "date": "2026-11-17", "refs": ["ako/mxcli#1258"], "ce": ["CE0109", "CE0108", "CE0067"], "symptom": "`create microflow M.F ($B: Boolean) returns Boolean as $R begin if $B then return true; end if; end;` passed `mxcli check` and `exec`, then MxBuild reported CE0109 \"Undefined variable 'R'\" at the End event. Same in a nanoflow.", "file": "mdl/executor/validate_return_variable_defined.go checkReturnVariableDefined (MDL097), called from validate_microflow.go validate() after the MDL003 block and from validate_nanoflow.go. Test: mdl/executor/validate_return_variable_defined_test.go", "cause": "MDL003 (missing return on a non-void flow) exempts `returns T as $R`, on the reasoning that the End event returns $R whether or not the body writes a RETURN. That is true only while $R has a value where the flow ends; an undefined $R is CE0109 and one defined only inside a branch or loop is CE0108.", "insight": "**An exemption written from one measured case is a rule with an unstated precondition; re-measure the boundary before widening or narrowing it.** The 11.13.0 measurement behind the AS-clause exemption had $R assigned, so it held; the parity fixture left $R undefined. Measured on 11.16.1, one `mx check -j`, 15 flows microflow and nanoflow: undefined + a path off the end = CE0109; defined only in a nested body = CE0108; declared at the top level = clean whether or not the body falls off the end (even declared AFTER an if that returns); undefined but every path returns = clean; no AS clause = CE0067 (MDL003, unchanged). So the rule is about where $R is defined, not about return statements. Guarded by both bodyReturns and lastStmtIsReturn so a named join or raise error stays quiet. Sweep of every .mdl in the repo (1658 flows, 684 with an AS clause): 2 hits, both correct: the f07 fixture and bug-tests/captrack-19-return-in-loop-as-clause.fail.mdl, whose own header independently records the same CE0109 for an unassigned $Done. A third hit came from the skill-example gate, not the sweep: docs-site/src/tutorial/skills.md:83 showed `returns Boolean as $result` with an empty body, which fails the build; fixed there. Control: stubbing the check fails the five positive cases and the @excluded test."} diff --git a/.claude/skills/mendix/write-microflows/reference/pitfalls.md b/.claude/skills/mendix/write-microflows/reference/pitfalls.md index 14036f380e..fa42c49fde 100644 --- a/.claude/skills/mendix/write-microflows/reference/pitfalls.md +++ b/.claude/skills/mendix/write-microflows/reference/pitfalls.md @@ -578,6 +578,28 @@ A regex that ends in a lone backslash (`'\'`) builds and fails every time it run (`secondsBetween`, `minutesBetween`, …) return the absolute difference, whichever date comes first — compare the dates for the direction. +### 21. `returns T as $R` Needs `$R` Defined Where the Flow Ends + +The `as $R` clause makes the end event return `$R`, so the body need not spell out a +`return` — but only while `$R` has a value on the path that reaches the end. Declare it at +the top level of the flow, or end every path with a `return`. Measured on MxBuild 11.16.1, +microflow and nanoflow alike: + +- `$R` never defined and a path falls off the end — CE0109 "Undefined variable 'R'" (MDL097). +- `$R` defined only inside an `if` branch or loop — CE0108, out of scope at the end event (MDL097). +- `$R` declared at the top level — clean, even when the body falls off the end. + +```mdl +mdl 1; +create microflow Mod.IsEven ($N: Integer) returns Boolean as $Even +begin + declare $Even Boolean = false; -- top level: always has a value at the end + if $N mod 2 = 0 then + set $Even = true; + end if; +end; +``` + ## Implicit Variable Creation (CE0111 Duplicate Variable) These statements **implicitly create a new variable** with the name on the left side: diff --git a/CHANGELOG.md b/CHANGELOG.md index 53774a4e4b..69320beb8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **`check` rejects `returns T as $R` when a path ends the flow without `$R`** (ako/mxcli#1258) — MDL003 exempts the AS clause because the end event returns `$R`, which holds only while `$R` has a value there. A flow with a path off its end and no top-level `$R` passed `check` and `exec` and failed the build with CE0109 "Undefined variable" (or CE0108 when `$R` is defined only inside a branch or loop). New MDL097, microflow and nanoflow. Measured on MxBuild 11.16.1. - **A `create or modify microflow` that inserts statements can run again** (ako/mxcli#2311) — the insert drew the connectors at the new statements' ends with vectors of its own, not as the script's `@curve` states. The next run of the same script read them as redrawn and refused (MDL-V1-REBUILD). The connectors are now drawn as the script states them, and the second run reports Unchanged. A splice refusal's advice now says to make a deliberate drop + create a one-time run, then keep the flow's `create or modify`, because a drop + create rewrites the flow every time. - **`alter navigation … insert` can run again** (ako/mxcli#2312) — re-running a script that inserts a menu item added it a second time and reported "Altered". An item the list already holds, as the statement builds it, is now already there, and the second run reports Unchanged. An item with the same caption but another action or other sub-items is refused, naming `replace`. The same applies to `alter menu`. - **`Visible: roles (…) and ` stores the roles and the condition** (ako/mxcli#2319) — the combined form parsed as one expression, so the whole text, `roles (…)` included, was stored as the visibility expression and the role list stayed empty: an invalid expression, and the role restriction lost, while check and exec passed. The leading `roles (…) and` is now split into the module roles and the condition, as Studio Pro stores them. A `roles (…)` anywhere else in an expression is refused (MDL-WIDGET54). diff --git a/docs-site/src/tutorial/skills.md b/docs-site/src/tutorial/skills.md index e4ee243b3f..deaa22b24e 100644 --- a/docs-site/src/tutorial/skills.md +++ b/docs-site/src/tutorial/skills.md @@ -85,6 +85,7 @@ CREATE MICROFLOW Module.Name( $param: Module.Entity ) RETURNS Boolean AS $result BEGIN + DECLARE $result Boolean = false; -- activities here END; ``` diff --git a/mdl/executor/validate_microflow.go b/mdl/executor/validate_microflow.go index 4bdf43b6a8..3137b44b65 100644 --- a/mdl/executor/validate_microflow.go +++ b/mdl/executor/validate_microflow.go @@ -163,6 +163,10 @@ func (v *microflowValidator) validate(body []ast.MicroflowStatement) { } } + // MDL097: the AS clause's exemption above holds only while $Var has a value + // where the flow ends. See validate_return_variable_defined.go. + v.checkReturnVariableDefined(body) + // Check 3: variable scope — detect variables declared inside branches but used after v.checkBranchScoping(body) diff --git a/mdl/executor/validate_nanoflow.go b/mdl/executor/validate_nanoflow.go index 3bdbb3e6cb..2ed50322df 100644 --- a/mdl/executor/validate_nanoflow.go +++ b/mdl/executor/validate_nanoflow.go @@ -65,6 +65,10 @@ func validateNanoflowWith(stmt *ast.CreateNanoflowStmt, voids *voidCodeActions) // MDL-FLOW05: a nanoflow's flow is built by the same builder, so a statement // after a return is dead there too (mendixlabs/mxcli#1491). v.checkUnreachableStatements(stmt.Body) + // MDL097: `returns T as $R` with a path off the end and no $R. Measured on + // 11.16.1 in a nanoflow as in a microflow: CE0109 when $R is undefined, clean + // when it is declared at the top level. + v.checkReturnVariableDefined(stmt.Body) // MDL049 / MDL094: a bare association path returned, or a loop over a path. // Measured on 11.12.1, a nanoflow's return is CE0117 as a microflow's is, // and its loop is built from the same AST (mendixlabs/mxcli#199). diff --git a/mdl/executor/validate_return_variable_defined.go b/mdl/executor/validate_return_variable_defined.go new file mode 100644 index 0000000000..e2b8f1aea5 --- /dev/null +++ b/mdl/executor/validate_return_variable_defined.go @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: Apache-2.0 + +package executor + +import ( + "fmt" + + "github.com/mendixlabs/mxcli/mdl/ast" + "github.com/mendixlabs/mxcli/mdl/linter" +) + +// returnVariableDefinedRule is MDL097: `returns T as $R` whose body can run off +// the end while $R holds no value. +const returnVariableDefinedRule = "MDL097" + +// checkReturnVariableDefined flags a microflow or nanoflow declared +// `returns T as $R` that has a path off its end and never gives $R a value. +// +// MDL003 exempts the AS clause, because the final End event returns `$R` +// whether or not the body spells a RETURN out. That holds only while `$R` exists +// where the flow ends. Measured on MxBuild 11.16.1, one `mx check` over 15 flows, +// microflow and nanoflow alike: +// +// $R never defined, a path falls off the end CE0109 "Undefined variable 'R'" at the End event +// $R defined only inside a branch or a loop CE0108 "defined but not in scope" at the End event +// $R declared at the top level 0 errors, whether the body falls off the end or not +// $R never defined, every path returns 0 errors: an explicit RETURN wins +// +// "Defined at the top level" is any statement of the flow's own body, in any +// position: declaring $R after an `if` that returns is clean too. A parameter of +// the same name defines it. A name only a nested body defines — an `if` branch, a +// loop body, an error handler — is out of scope at the End event, which is why +// those two cases report differently. +// +// The rule stays quiet unless the flow really can fall off the end, by both +// predicates the repo already has: bodyReturns (the MDL003 one) and +// lastStmtIsReturn (the builder's, which also counts a named join and a `raise +// error`). It never fires on a flow with no AS clause, which is MDL003's. +func (v *microflowValidator) checkReturnVariableDefined(body []ast.MicroflowStatement) { + if v.skipCEGapRules() || v.returnType == nil || v.returnType.Type.Kind == ast.TypeVoid || v.returnType.Variable == "" { + return + } + name := v.returnType.Variable + if bodyReturns(body) || lastStmtIsReturn(body) { + return + } + for _, p := range v.params { + if p.Name == name { + return + } + } + for _, s := range body { + for _, p := range statementProducedVars(s) { + if p.name == name && !p.loop { + return + } + } + } + + var problem string + if containsName(FlowDefinedVariables(body), name) { + problem = fmt.Sprintf("$%s is defined only inside a branch or loop, so it is out of scope where the %s ends "+ + "and not every path returns — the build fails CE0108", name, v.docType) + } else { + problem = fmt.Sprintf("returns %s as $%s, but $%s is never defined and not every path returns — "+ + "the build fails CE0109 \"Undefined variable '%s'\" at the end event", returnTypeString(v.returnType), name, name, name) + } + v.addViolation(returnVariableDefinedRule, linter.SeverityError, problem, + fmt.Sprintf("Declare $%s at the top level of the %s (`declare $%s %s = …;`), or end every path with a return.", + name, v.docType, name, returnTypeString(v.returnType))) +} + +func containsName(names []string, name string) bool { + for _, n := range names { + if n == name { + return true + } + } + return false +} diff --git a/mdl/executor/validate_return_variable_defined_test.go b/mdl/executor/validate_return_variable_defined_test.go new file mode 100644 index 0000000000..eeff819d71 --- /dev/null +++ b/mdl/executor/validate_return_variable_defined_test.go @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: Apache-2.0 + +package executor + +import ( + "strings" + "testing" + + "github.com/mendixlabs/mxcli/mdl/ast" + "github.com/mendixlabs/mxcli/mdl/linter" + "github.com/mendixlabs/mxcli/mdl/visitor" +) + +// MDL097: `returns T as $R` whose body can run off the end without ever giving +// $R a value. MDL003 exempts the AS clause because the End event returns $R, +// which is right only while $R exists on that path. +// +// Measured on MxBuild 11.16.1, one `mx check -j` over 15 flows (microflow and +// nanoflow alike): +// +// $R never defined, a path falls off the end CE0109 Undefined variable 'R' (End event) +// $R defined only inside a branch or loop CE0108 defined but not in scope (End event) +// $R declared at the top level 0 errors, falling off the end or not +// $R never defined, every path returns 0 errors +// no AS clause, a path falls off the end CE0067 (MDL003's, unchanged) +func returnVarViolations(t *testing.T, src string) []linter.Violation { + t.Helper() + prog, errs := visitor.Build("mdl 1;\n" + src) + if len(errs) > 0 { + t.Fatalf("parse: %v", errs) + } + var out []linter.Violation + for _, st := range prog.Statements { + switch s := st.(type) { + case *ast.CreateMicroflowStmt: + out = append(out, ValidateMicroflow(s)...) + case *ast.CreateNanoflowStmt: + out = append(out, ValidateNanoflow(s)...) + } + } + return out +} + +func TestReturnVariableDefined_MDL097(t *testing.T) { + const hdr = "create microflow Mr.F ($B: Boolean) " + cases := []struct { + name string + src string + want string // "" = no MDL097; otherwise a substring of the message + }{ + {"never defined, falls off", hdr + "returns Integer as $R begin declare $X Integer = 1; end;", "never defined"}, + {"never defined, if without else", hdr + "returns Integer as $R begin if $B then return 1; end if; end;", "never defined"}, + {"never defined, object", hdr + "returns Mr.Obj as $R begin declare $X Integer = 1; end;", "never defined"}, + {"defined only inside a branch", hdr + "returns Integer as $R begin if $B then declare $R Integer = 1; end if; end;", "inside a branch"}, + {"declared at the top, falls off", hdr + "returns Integer as $R begin declare $R Integer = 0; if $B then set $R = 1; end if; end;", ""}, + {"declared at the top, if returns", hdr + "returns Integer as $R begin declare $R Integer = 0; if $B then return 1; end if; end;", ""}, + {"declared after the if", hdr + "returns Integer as $R begin if $B then return 1; end if; declare $R Integer = 5; end;", ""}, + {"never defined, every path returns", hdr + "returns Integer as $R begin if $B then return 1; else return 2; end if; end;", ""}, + {"created at the top", hdr + "returns Mr.Obj as $R begin $R = create Mr.Obj (Name = 'x'); end;", ""}, + {"no AS clause is MDL003's, not this rule", hdr + "returns Integer begin declare $X Integer = 1; end;", ""}, + {"returns nothing", hdr + "begin declare $X Integer = 1; end;", ""}, + {"nanoflow never defined", "create nanoflow Mr.N ($B: Boolean) returns Integer as $R begin declare $X Integer = 1; end;", "never defined"}, + {"nanoflow declared at the top", "create nanoflow Mr.N ($B: Boolean) returns Integer as $R begin declare $R Integer = 0; end;", ""}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + var got *linter.Violation + for _, v := range returnVarViolations(t, c.src) { + if v.RuleID == "MDL097" { + v := v + got = &v + } + } + switch { + case c.want == "" && got != nil: + t.Fatalf("unexpected MDL097: %s", got.Message) + case c.want != "" && got == nil: + t.Fatalf("expected MDL097 containing %q, got none", c.want) + case c.want != "" && !strings.Contains(got.Message, c.want): + t.Fatalf("MDL097 message %q does not contain %q", got.Message, c.want) + } + }) + } +} + +// A parameter of the same name defines $R, and an @excluded document is not +// built, so neither is reported. +func TestReturnVariableDefined_MDL097_Exemptions(t *testing.T) { + param := returnVarViolations(t, "create microflow Mr.P ($R: Integer) returns Integer as $R begin declare $X Integer = 1; end;") + for _, v := range param { + if v.RuleID == "MDL097" { + t.Errorf("a parameter named like the return variable defines it: %s", v.Message) + } + } +} + +// mxbuild does not check an @excluded document, so a flow stashed there with a +// broken return is not reported (the rule stands down like MDL061-063). +func TestReturnVariableDefined_MDL097_Excluded(t *testing.T) { + for _, kind := range []string{"microflow", "nanoflow"} { + src := "@excluded\ncreate " + kind + " Mr.X ($B: Boolean) returns Integer as $R begin declare $X Integer = 1; end;" + var any097 bool + for _, v := range returnVarViolations(t, src) { + if v.RuleID == "MDL097" { + any097 = true + } + } + if any097 { + t.Errorf("an @excluded %s must not be reported", kind) + } + // Control: the same flow without @excluded is reported. + ctl := returnVarViolations(t, strings.TrimPrefix(src, "@excluded\n")) + found := false + for _, v := range ctl { + if v.RuleID == "MDL097" { + found = true + } + } + if !found { + t.Errorf("control: the same %s without @excluded must be reported", kind) + } + } +} From 3992cd74aba6f7d4ab20b312573c7ab6edca7ff4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:06:07 +0000 Subject: [PATCH 02/13] ci: Bump actions/upload-artifact from 4 to 7 Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 4 to 7. - [Release notes](https://github.com/actions/upload-artifact/releases) - [Commits](https://github.com/actions/upload-artifact/compare/v4...v7) --- updated-dependencies: - dependency-name: actions/upload-artifact dependency-version: '7' dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/nightly.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index db1b05e837..2cf9dbf790 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -345,7 +345,7 @@ jobs: - name: Upload the trend if: always() - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v7 with: name: agent-benchmark-trend path: | From 655b70b759eddec794bf8c91421abfcab6451acf Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 07:19:21 +0000 Subject: [PATCH 03/13] feat(check): MDL098 flags an expression that reads a variable nothing defines return $Nope + 1 with $Nope defined nowhere passed check and exec, then failed the build with CE0109. MDL098 joins the $names an expression reads (new exprcheck.VariableReferences) with the names the flow defines (params plus every statement's producers). $currentUser, $currentSession, $latestError, $latestHttpResponse, $latestSoapFault, $currentIndex and $currentObject count as defined everywhere: measured on MxBuild 11.16.1, the first five are accepted in every position, the last two only in their own context (the rule is looser on purpose). A sweep of every .mdl in the repo (1658 flows) finds three hits, all correct. Refs ako/mxcli#1258. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011oK82tETa46YQmMS58pQjL --- ...1-17-undefined-variable-in-expression.json | 1 + CHANGELOG.md | 1 + mdl/executor/validate_microflow.go | 7 ++ mdl/executor/validate_nanoflow.go | 1 + mdl/executor/validate_undefined_variable.go | 76 ++++++++++++ .../validate_undefined_variable_test.go | 108 ++++++++++++++++++ mdl/exprcheck/variable_refs.go | 58 ++++++++++ mdl/exprcheck/variable_refs_test.go | 41 +++++++ 8 files changed, 293 insertions(+) create mode 100644 .claude/skills/fix-issue/findings/mdl-executor/2026-11-17-undefined-variable-in-expression.json create mode 100644 mdl/executor/validate_undefined_variable.go create mode 100644 mdl/executor/validate_undefined_variable_test.go create mode 100644 mdl/exprcheck/variable_refs.go create mode 100644 mdl/exprcheck/variable_refs_test.go diff --git a/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-undefined-variable-in-expression.json b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-undefined-variable-in-expression.json new file mode 100644 index 0000000000..1b07ced402 --- /dev/null +++ b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-undefined-variable-in-expression.json @@ -0,0 +1 @@ +{"area": "mdl/executor", "date": "2026-11-17", "refs": ["ako/mxcli#1258"], "ce": ["CE0109"], "symptom": "`return $Nope + 1;` with `$Nope` defined nowhere passed `mxcli check -p` and `exec`, then MxBuild reported CE0109 \"Undefined variable 'Nope'\" at the End event.", "file": "mdl/executor/validate_undefined_variable.go (MDL098: initDefinedVariables, checkUndefinedVariables, implicitVariables), hooked into checkExprFunctions in validate_microflow.go and initialised in validate() and validateNanoflowWith; mdl/exprcheck/variable_refs.go VariableReferences. Tests: validate_undefined_variable_test.go, variable_refs_test.go", "cause": "Nothing compared the `$name`s an expression reads with the names the flow defines. MDL-LISTOP01 did it for a filter/find predicate only, and MDL063 collects definitions for duplicates, so both halves existed and were never joined for ordinary expression slots.", "insight": "**The risky half of an undefined-variable rule is the implicit names, and the answer is a measurement, not a list from memory.** One `mx check -j` over 55 flows (11 candidate names x plain flow, loop body, error handler, filter predicate, after a loop) on MxBuild 11.16.1: $currentUser, $currentSession, $latestError, $latestHttpResponse and $latestSoapFault are accepted in EVERY position (even $latestError in a flow with no handler); $currentIndex and the loop iterator only inside the loop; $currentObject only inside a filter predicate; $latestJavaException and a made-up $IteratorItem are CE0109 like $Nope; the iterator after its loop is CE0108. The rule treats $currentIndex and $currentObject as defined everywhere, stricter than MxBuild on purpose: a missed report costs less than a false one, because exec refuses what check reports. Producers come from FlowDefinedVariables (params plus every statement's declare/create/retrieve/OutputVariable at any depth); a read of the AST for fields that look like producers found none missed (ExternalObjectStmt.Variable is consumed). Names compare case-insensitively since whether Mendix distinguishes $count from $Count is unmeasured. Sweep of every .mdl in the repo (1658 flows): 3 hits, all correct: the f05 fixture and two negative tests (bug-tests/ledger-17 and ledger-21 .fail.mdl) that `set $Bad = …` without declaring it and then `return $Bad`. Control: stubbing the check fails all 11 positive cases and the @excluded control."} diff --git a/CHANGELOG.md b/CHANGELOG.md index db1c4a63a6..e87c573a76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **`check` rejects an expression that reads a variable nothing defines** (ako/mxcli#1258) — `return $Nope + 1;` with no `$Nope` anywhere passed `check` and `exec` and failed the build with CE0109 "Undefined variable". New MDL098, for the expressions of return, if, declare, set, create/change members, log, show message and validation feedback, in microflows and nanoflows. A name counts as defined if a parameter or any statement of the flow produces it, or it is one MxBuild accepts with no declaration (`$currentUser`, `$currentSession`, `$latestError`, `$latestHttpResponse`, `$latestSoapFault`, `$currentIndex`, `$currentObject`). Measured on MxBuild 11.16.1. - **`fix widgets` and `fix design-properties` use the project's own Mendix version of `mx`** (ako/mxcli#2321) — they took the version as a preference, so on macOS an `mx` on PATH or another Studio Pro install ran, and `mx update-widgets` refused with "Project is in different Mendix version". They now take an `mx` of exactly the project's version. On Linux that version is downloaded when it is missing. On macOS and Windows, where no matching `mx` is installed, they say which version is needed and how to pass it with `--mx`. - **Parallel `mxcli exec` runs on one project no longer lose each other's writes** (ako/mxcli#2326) — measured with 8 parallel scripts: every process reported success and 2 to 6 of the 16 entities were missing (the reporter saw all of them fail instead). A script, or a `-c`, that writes now holds the project's lock (`.mxcli/write.lock`) for its whole run. A second writer waits up to 60 seconds, then changes nothing and exits 75, naming the holder. A lock left by a crashed run is taken over. Read-only runs take no lock. With the lock, all 16 entities land in every round. - **`run restart` names the secret it cannot replay** (ako/mxcli#2313) — a restart refused because of a literal secret printed only a generic `--db-password @env:` hint. The `--constant X=@env:VAR` references are recorded and replayed, so the hint read as if they were not. The refusal now lists each flag stored redacted, such as `--constant Helpdesk.OpenRouterKey` or `--db-password`, and says that the values given as references are replayed. diff --git a/mdl/executor/validate_microflow.go b/mdl/executor/validate_microflow.go index 4bdf43b6a8..c509e0e925 100644 --- a/mdl/executor/validate_microflow.go +++ b/mdl/executor/validate_microflow.go @@ -99,6 +99,11 @@ type microflowValidator struct { // excluded marks an @excluded document. mxbuild does not check one, so the // #893 rules stand down for it — see skipCEGapRules. excluded bool + // definedVars is every name the flow defines, lower-cased, and + // undefinedReported the names MDL098 has already reported (see + // validate_undefined_variable.go). Both are nil until initDefinedVariables. + definedVars map[string]bool + undefinedReported map[string]bool // fragment marks the body of an `alter microflow|nanoflow` operation: a // piece of a stored flow, whose notes `@annotation(existing: '…')` can // attach to (ako/mxcli#1695). Anything else states a whole flow. @@ -123,6 +128,7 @@ func (v *microflowValidator) addViolation(ruleID string, severity linter.Severit // validate runs all checks on the microflow body. func (v *microflowValidator) validate(body []ast.MicroflowStatement) { + v.initDefinedVariables(body) v.checkListOperationIterator(body) v.checkRetrieveLimitOneAsList(body) v.checkRetrieveListAsObject(body) @@ -626,6 +632,7 @@ func (v *microflowValidator) checkExprFunctions(label string, expr ast.Expressio if src == "" { return } + v.checkUndefinedVariables(label, src) for _, u := range exprcheck.UnknownFunctionCalls(src) { var suggestion string if mendixAggregateFuncs[strings.ToLower(u.Name)] { diff --git a/mdl/executor/validate_nanoflow.go b/mdl/executor/validate_nanoflow.go index 3bdbb3e6cb..2808b41007 100644 --- a/mdl/executor/validate_nanoflow.go +++ b/mdl/executor/validate_nanoflow.go @@ -37,6 +37,7 @@ func validateNanoflowWith(stmt *ast.CreateNanoflowStmt, voids *voidCodeActions) excluded: stmt.Excluded, voids: voids, } + v.initDefinedVariables(stmt.Body) v.walkExprFunctions(stmt.Body) v.walkAnnotations(stmt.Body) // MDL063 holds for a nanoflow too: its variable names are as flat as a diff --git a/mdl/executor/validate_undefined_variable.go b/mdl/executor/validate_undefined_variable.go new file mode 100644 index 0000000000..d0f98a503d --- /dev/null +++ b/mdl/executor/validate_undefined_variable.go @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: Apache-2.0 + +package executor + +import ( + "fmt" + "strings" + + "github.com/mendixlabs/mxcli/mdl/ast" + "github.com/mendixlabs/mxcli/mdl/exprcheck" + "github.com/mendixlabs/mxcli/mdl/linter" +) + +// undefinedVariableRule is MDL098: an expression reads `$name` and nothing in the +// flow defines it. +const undefinedVariableRule = "MDL098" + +// implicitVariables are the names MxBuild accepts with no declaration, lower-cased. +// +// Measured on 11.16.1, one `mx check -j` over 55 flows (11 names x plain flow, +// loop body, error handler, filter predicate, after a loop): $currentUser, +// $currentSession, $latestError, $latestHttpResponse and $latestSoapFault are +// accepted in every position, $currentIndex and the loop iterator inside the +// loop, and $currentObject inside a filter predicate. $latestJavaException and a +// made-up $IteratorItem are undefined like any other name. +// +// The rule treats $currentIndex and $currentObject as defined everywhere. MxBuild +// is stricter (CE0109 outside the loop or predicate), but a missed report costs +// less than a false one: exec refuses what check reports. +var implicitVariables = map[string]bool{ + "currentuser": true, "currentsession": true, + "latesterror": true, "latesthttpresponse": true, "latestsoapfault": true, + "currentindex": true, "currentobject": true, +} + +// initDefinedVariables records every name the flow defines: the parameters and +// every variable a statement produces, at any depth, loop iterators included. +// Where a name is defined is not asked — a name defined only in a branch, or used +// before it is declared, is CE0108 / CE0109 for other reasons that other checks +// (MDL-SCOPE, MDL052) own. This one is only for a name defined NOWHERE. +// +// Names compare case-insensitively: whether Mendix distinguishes `$count` from +// `$Count` is unmeasured, and treating them as one can only silence a report. +func (v *microflowValidator) initDefinedVariables(body []ast.MicroflowStatement) { + v.definedVars = map[string]bool{} + for _, p := range v.params { + v.definedVars[strings.ToLower(p.Name)] = true + } + for _, n := range FlowDefinedVariables(body) { + v.definedVars[strings.ToLower(n)] = true + } + if v.returnType != nil && v.returnType.Variable != "" { + // The `as $R` name is the end event's own variable (MDL097 owns it). + v.definedVars[strings.ToLower(v.returnType.Variable)] = true + } + v.undefinedReported = map[string]bool{} +} + +// checkUndefinedVariables reports each `$name` an expression reads that the flow +// does not define, once per name per flow. label says where the expression is. +func (v *microflowValidator) checkUndefinedVariables(label, src string) { + if v.definedVars == nil || v.skipCEGapRules() { + return + } + for _, r := range exprcheck.VariableReferences(src) { + key := strings.ToLower(r.Name) + if v.definedVars[key] || implicitVariables[key] || v.undefinedReported[key] { + continue + } + v.undefinedReported[key] = true + v.addViolation(undefinedVariableRule, linter.SeverityError, + fmt.Sprintf("%s reads $%s, which nothing in this %s defines — the build fails CE0109 \"Undefined variable '%s'\"", + label, r.Name, v.docType, r.Name), + fmt.Sprintf("Declare $%s (`declare $%s = …;`), take it as a parameter, or check the spelling.", r.Name, r.Name)) + } +} diff --git a/mdl/executor/validate_undefined_variable_test.go b/mdl/executor/validate_undefined_variable_test.go new file mode 100644 index 0000000000..32e15d9735 --- /dev/null +++ b/mdl/executor/validate_undefined_variable_test.go @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: Apache-2.0 + +package executor + +import ( + "strings" + "testing" + + "github.com/mendixlabs/mxcli/mdl/ast" + "github.com/mendixlabs/mxcli/mdl/linter" + "github.com/mendixlabs/mxcli/mdl/visitor" +) + +// MDL098: a `$name` used in an expression that nothing in the flow defines. +// +// Measured on MxBuild 11.16.1, one `mx check -j` over 55 flows: an undefined +// name is CE0109 "Undefined variable" at the activity or end event. MxBuild +// accepts a fixed set of names with no declaration — $currentUser, +// $currentSession, $latestError, $latestHttpResponse and $latestSoapFault in +// every position, $currentIndex and the loop iterator inside the loop, and +// $currentObject inside a filter predicate. $latestJavaException and a made-up +// $IteratorItem are undefined like any other name. +func undefinedVarViolations(t *testing.T, src string) []linter.Violation { + t.Helper() + prog, errs := visitor.Build("mdl 1;\n" + src) + if len(errs) > 0 { + t.Fatalf("parse: %v", errs) + } + var out []linter.Violation + for _, st := range prog.Statements { + var vs []linter.Violation + switch s := st.(type) { + case *ast.CreateMicroflowStmt: + vs = ValidateMicroflow(s) + case *ast.CreateNanoflowStmt: + vs = ValidateNanoflow(s) + } + for _, v := range vs { + if v.RuleID == "MDL098" { + out = append(out, v) + } + } + } + return out +} + +func TestUndefinedVariable_MDL098(t *testing.T) { + const hdr = "create microflow Mr.F ($B: Boolean, $O: Mr.Obj, $L: list of Mr.Obj) " + cases := []struct { + name string + src string + want string // "" = no MDL098; else the undefined name expected in the message + }{ + {"return", hdr + "returns Integer begin return $Nope + 1; end;", "Nope"}, + {"if condition", hdr + "begin if $Nope then declare $X Integer = 1; end if; end;", "Nope"}, + {"declare value", hdr + "begin declare $X Integer = $Nope; end;", "Nope"}, + {"set value", hdr + "begin declare $X Integer = 0; set $X = $Nope; end;", "Nope"}, + {"attribute path", hdr + "returns String begin return $Nope/Name; end;", "Nope"}, + {"create member value", hdr + "begin $N = create Mr.Obj (Name = $Nope); end;", "Nope"}, + {"change member value", hdr + "begin change $O (Name = $Nope); end;", "Nope"}, + {"log message", hdr + "begin log info node 'X' $Nope; end;", "Nope"}, + {"nanoflow", "create nanoflow Mr.N ($B: Boolean) returns Integer begin return $Nope; end;", "Nope"}, + {"latestJavaException is not implicit", hdr + "begin declare $X Boolean = ($latestJavaException = empty); end;", "latestJavaException"}, + {"made-up iterator name", hdr + "begin loop $Item in $L begin declare $T Boolean = ($IteratorItem = empty); end loop; end;", "IteratorItem"}, + + {"parameter", hdr + "returns Boolean begin return $B; end;", ""}, + {"parameter attribute", hdr + "returns String begin return $O/Name; end;", ""}, + {"declared variable", hdr + "returns Integer begin declare $X Integer = 1; return $X + 1; end;", ""}, + {"defined only in a branch is not this rule's", hdr + "returns Integer begin if $B then declare $X Integer = 1; end if; return $X; end;", ""}, + {"created object", hdr + "returns String begin $N = create Mr.Obj (Name = 'a'); return $N/Name; end;", ""}, + {"retrieved list", hdr + "returns Integer begin retrieve $R from Mr.Obj; return length($R); end;", ""}, + {"loop iterator in the loop", hdr + "begin loop $Item in $L begin declare $T String = $Item/Name; end loop; end;", ""}, + {"currentUser", hdr + "begin declare $T Boolean = ($currentUser = empty); end;", ""}, + {"currentSession", hdr + "begin declare $T Boolean = ($currentSession = empty); end;", ""}, + {"latestError", hdr + "begin declare $T Boolean = ($latestError = empty); end;", ""}, + {"latestHttpResponse", hdr + "begin declare $T Boolean = ($latestHttpResponse = empty); end;", ""}, + {"latestSoapFault", hdr + "begin declare $T Boolean = ($latestSoapFault = empty); end;", ""}, + {"currentIndex", hdr + "begin loop $Item in $L begin declare $T Integer = $currentIndex; end loop; end;", ""}, + {"currentObject", hdr + "begin $F = filter($L, $currentObject/Name = 'a'); end;", ""}, + {"name case differs, stays quiet", hdr + "returns Integer begin declare $Count Integer = 1; return $count; end;", ""}, + {"no variable at all", hdr + "returns Integer begin return 1 + 2; end;", ""}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + got := undefinedVarViolations(t, c.src) + switch { + case c.want == "" && len(got) > 0: + t.Fatalf("unexpected MDL098: %s", got[0].Message) + case c.want != "" && len(got) == 0: + t.Fatalf("expected MDL098 for $%s, got none", c.want) + case c.want != "" && !strings.Contains(got[0].Message, "$"+c.want): + t.Fatalf("MDL098 message %q does not name $%s", got[0].Message, c.want) + } + }) + } +} + +// An @excluded document is not built, so a name left undefined in one is not +// reported; the same flow without the annotation is (the control). +func TestUndefinedVariable_MDL098_Excluded(t *testing.T) { + src := "create microflow Mr.X () returns Integer begin return $Nope; end;" + if got := undefinedVarViolations(t, "@excluded\n"+src); len(got) != 0 { + t.Errorf("an @excluded microflow must not be reported: %s", got[0].Message) + } + if got := undefinedVarViolations(t, src); len(got) == 0 { + t.Errorf("control: the same microflow without @excluded must be reported") + } +} diff --git a/mdl/exprcheck/variable_refs.go b/mdl/exprcheck/variable_refs.go new file mode 100644 index 0000000000..e13be5f681 --- /dev/null +++ b/mdl/exprcheck/variable_refs.go @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: Apache-2.0 + +package exprcheck + +import "strings" + +// VarRef is one `$name` reference found in an expression. +type VarRef struct { + Name string // without the `$` + Line int + Column int +} + +// VariableReferences parses a Mendix expression and returns every `$name` it +// reads, in source order and with repeats: the variable of a `$name` and of a +// `$name/Attr/Path`. A bare identifier (`Status`, as a filter predicate writes an +// attribute of the item under test) is not a `$` reference and is left out, and +// so is an XPath path that starts at an entity rather than at a variable. +func VariableReferences(src string) []VarRef { + if strings.TrimSpace(src) == "" { + return nil + } + root, _ := (&parserImpl{}).Parse(src, Context{}) + if root == nil { + return nil + } + var out []VarRef + walkVars(root, &out) + return out +} + +func walkVars(e RobustExpr, out *[]VarRef) { + switch n := e.(type) { + case *VariableExpr: + if !n.Bare && n.Name != "" { + *out = append(*out, VarRef{Name: n.Name, Line: n.Pos().Line, Column: n.Pos().Column}) + } + case *AttributePathExpr: + if n.Variable != "" { + *out = append(*out, VarRef{Name: n.Variable, Line: n.Pos().Line, Column: n.Pos().Column}) + } + case *CallExpr: + for _, a := range n.Args { + walkVars(a, out) + } + case *BinExpr: + walkVars(n.L, out) + walkVars(n.R, out) + case *UnaryExpr: + walkVars(n.Operand, out) + case *ParenExpr: + walkVars(n.Inner, out) + case *IfThenElseExpr: + walkVars(n.Cond, out) + walkVars(n.Then, out) + walkVars(n.Else, out) + } +} diff --git a/mdl/exprcheck/variable_refs_test.go b/mdl/exprcheck/variable_refs_test.go new file mode 100644 index 0000000000..f3828556f4 --- /dev/null +++ b/mdl/exprcheck/variable_refs_test.go @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: Apache-2.0 + +package exprcheck + +import ( + "reflect" + "testing" +) + +func TestVariableReferences(t *testing.T) { + cases := []struct { + src string + want []string + }{ + {"$Nope + 1", []string{"Nope"}}, + {"$a + $b * $a", []string{"a", "b", "a"}}, + {"$Order/Status = 'Open'", []string{"Order"}}, + {"if $Flag then $X else $Y", []string{"Flag", "X", "Y"}}, + {"length($S) > $Max", []string{"S", "Max"}}, + {"not($Done)", []string{"Done"}}, + {"($A)", []string{"A"}}, + // No variable at all. + {"'text'", nil}, + {"1 + 2", nil}, + {"[%CurrentDateTime%]", nil}, + {"Mod.Enum.Value", nil}, + {"", nil}, + // A bare identifier is not a `$` reference: a predicate uses one for an + // attribute of the item under test. + {"Status = 'Open'", nil}, + } + for _, c := range cases { + var got []string + for _, r := range VariableReferences(c.src) { + got = append(got, r.Name) + } + if !reflect.DeepEqual(got, c.want) { + t.Errorf("%q: got %v, want %v", c.src, got, c.want) + } + } +} From 8ffb81a64348a46e0c6e4bd1a7e0189ac65c5cd5 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 09:13:37 +0000 Subject: [PATCH 04/13] test(bench): check hot-apply's two commands against a floor, not another step Nightly #116 failed in TestRun_RuntimeSessionRunsArmsInTurnAndAlwaysStops: "hot-apply took 72 ms against 108 ms for a one-command step, want the two commands summed". The check compared one sample of a two-command step with one sample of a one-command step, ~30 ms a command, so a stall in the one-command step's single run failed it. The candidate's two hot-apply commands now sleep 300 ms more each (the fake's slowOn takes "a|b" alternatives), and the step must take at least the 600 ms they sum to: a stalled runner can only raise it, while a step that timed one command alone reads ~330. Control: with runStep keeping only the last command's wall time, it fails with "hot-apply took 335 ms". Modelling stalls in the fake (a 30% chance per command of up to 300 ms), the old check failed 3 of 12 runs with the nightly's message, the new one 0 of 12. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GeAhBjRBm5vN2ik3ddvVWW --- scripts/agentbench/bench_test.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/scripts/agentbench/bench_test.go b/scripts/agentbench/bench_test.go index 5eefc2a459..7c9e9f0db5 100644 --- a/scripts/agentbench/bench_test.go +++ b/scripts/agentbench/bench_test.go @@ -22,7 +22,7 @@ import ( // scheduler's jitter is small beside it, exits 0 and reports fixed counts, // except: // -// slowOn a command line containing this sleeps first +// slowOn a command line containing this sleeps first; "a|b" slows either // failOn a command line containing this exits non-zero // writeOn a command line containing this rewrites a unit file with new bytes func fakeMxcli(t *testing.T, name, slowOn, failOn, writeOn string) string { @@ -43,6 +43,14 @@ const fakeBase, fakeSlow = "0.03", "0.25" func fakeMxcliScript(t *testing.T, name, slowOn, failOn, writeOn, callLog, base, slow string) string { t.Helper() + slowWhen := "false" + if slowOn != "" { + var alts []string + for _, alt := range strings.Split(slowOn, "|") { + alts = append(alts, fmt.Sprintf("match %q", alt)) + } + slowWhen = strings.Join(alts, " || ") + } script := fmt.Sprintf(`#!/bin/sh if [ "$1" = version ]; then echo "mxcli version %[1]s"; exit 0; fi match() { [ -n "$1" ] && case "$ARGS" in *"$1"*) return 0;; esac; return 1; } @@ -59,14 +67,14 @@ if [ -n "$outdir" ]; then mkdir -p "$outdir/mprcontents" && echo mpr > "$outdir/App.mpr" && echo unit > "$outdir/mprcontents/unit.mxunit" fi sleep %[6]s -if match %[2]q; then sleep %[7]s; fi +if %[2]s; then sleep %[7]s; fi if match %[3]q; then echo "unknown statement" >&2; exit 3; fi if match %[4]q; then date +%%s%%N >> "$(dirname "$project")/mprcontents/unit.mxunit"; fi if [ -n "$MXCLI_STATS_FILE" ]; then echo '{"unit_file_reads":7,"writes_offered":2,"writes_written":1}' >> "$MXCLI_STATS_FILE" fi echo ok -`, name, slowOn, failOn, writeOn, callLog, base, slow) +`, name, slowWhen, failOn, writeOn, callLog, base, slow) path := filepath.Join(t.TempDir(), name) if err := writeExecutable(path, []byte(script)); err != nil { t.Fatal(err) @@ -296,7 +304,9 @@ func TestCopyProject_LeavesTheCachesBehind(t *testing.T) { func TestRun_RuntimeSessionRunsArmsInTurnAndAlwaysStops(t *testing.T) { t.Parallel() callLog := filepath.Join(t.TempDir(), "calls.log") - candidate := fakeMxcliLogging(t, "candidate", "", "", "", callLog) + // Both of hot-apply's commands sleep 300 ms more in the candidate, so the + // step can be checked against a floor that a stalled runner only raises. + candidate := fakeMxcliScript(t, "candidate", "single-edit.mdl|run wait", "", "", callLog, fakeBase, "0.3") baseline := fakeMxcliLogging(t, "baseline", "", "entity-edit.mdl", "", callLog) cfg := testConfig(t, candidate, baseline) cfg.sizes, cfg.runtimeVersion, cfg.runtimeReps, cfg.pageCheck = nil, "11.13.0", 1, true @@ -353,9 +363,12 @@ func TestRun_RuntimeSessionRunsArmsInTurnAndAlwaysStops(t *testing.T) { if st := stepNamed(t, s, "hot-apply"); st.Baseline == nil { t.Error("hot-apply ran before the baseline failed and has no baseline") - } else if one := stepNamed(t, s, "test-attach").Candidate.Wall.Median; st.Candidate.Wall.Median < 1.3*one { - // Two commands, the edit and the wait, measured as one step. - t.Errorf("hot-apply took %.0f ms against %.0f ms for a one-command step, want the two commands summed", st.Candidate.Wall.Median, one) + } else if got := st.Candidate.Wall.Median; got < 600 { + // Two commands, the edit and the wait, measured as one step: their two + // 300 ms delays sum to 600, where either alone is about 330. Comparing + // against another step's single sample failed on the nightly when that + // sample stalled (72 ms against 108). + t.Errorf("hot-apply took %.0f ms, want at least the 600 ms its two commands' delays sum to", got) } for _, name := range []string{"apply-entity", "stop", "test-cold"} { if stepNamed(t, s, name).Baseline != nil { From cc9a3dba522f7f6c9f316e6bfa449335b4dccec4 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 09:13:39 +0000 Subject: [PATCH 05/13] findings: agentbench hot-apply timing check compared single samples Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GeAhBjRBm5vN2ik3ddvVWW --- ...1-17-agentbench-hot-apply-timing-compares-single-samples.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 .claude/skills/fix-issue/findings/other/2026-11-17-agentbench-hot-apply-timing-compares-single-samples.json diff --git a/.claude/skills/fix-issue/findings/other/2026-11-17-agentbench-hot-apply-timing-compares-single-samples.json b/.claude/skills/fix-issue/findings/other/2026-11-17-agentbench-hot-apply-timing-compares-single-samples.json new file mode 100644 index 0000000000..39a7c5a52b --- /dev/null +++ b/.claude/skills/fix-issue/findings/other/2026-11-17-agentbench-hot-apply-timing-compares-single-samples.json @@ -0,0 +1 @@ +{"area":"other","date":"2026-11-17","symptom":"mendixlabs/mxcli push CI (Build, Test & Lint #2233 on main, b7dc2f8) failed in build-and-test's Test step, while the identical tree passed twice on ako/mxcli; the fork's nightly #116 had failed the day before in unit tests with `TestRun_RuntimeSessionRunsArmsInTurnAndAlwaysStops: hot-apply took 72 ms against 108 ms for a one-command step, want the two commands summed`","cause":"A third wall-clock assertion in scripts/agentbench's own tests, not covered by the earlier fix (2026-11-15-agentbench-tests-flake-etxtbsy-and-timing): it compared one sample of the two-command hot-apply step with one sample of the one-command test-attach step (runtimeReps 1, ~30 ms a fake command), so one stall in test-attach's run failed it","file":"`scripts/agentbench/bench_test.go` (`TestRun_RuntimeSessionRunsArmsInTurnAndAlwaysStops`; fakeMxcliScript's slowOn takes `a|b` alternatives)","insight":"A timing assertion that compares two measured samples is a race between them; one that checks a measured time against a floor built from known injected delays cannot be failed by a stall, because noise only adds time. So the candidate's two hot-apply commands each sleep 300 ms more and the step must be at least 600 ms (one command alone reads ~330). When fixing one flaky timing test, grep the package for every other comparison of measured times (Wall, Median, ratio) — the first fix left this one behind. Control: runStep keeping only the last command's wall time fails it at 335 ms. Stall model in the fake (30% chance per command of up to 300 ms, via go test -overlay): old check 3 of 12 failed with the nightly's message, new 0 of 12. The upstream failure's log needs sign-in, so its test is inferred: the fork's 60 CI runs over the same 36 hours had no other failure","refs":["mendixlabs/mxcli Build, Test & Lint run 44036694201","ako/mxcli nightly run 43836035285"],"test":"scripts/agentbench/bench_test.go"} From 859f1586804e7493e817eec4e93466fbacb2edb5 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 09:15:08 +0000 Subject: [PATCH 06/13] docs(changelog): add the entries for #2338 and #2339 Both merged without a CHANGELOG line, which this repo keeps per fix: the replace() function-table fix (#2338) and the declare/set/return type check (#2339). Refs ako/mxcli#1258, mendixlabs/mxcli#2308. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011oK82tETa46YQmMS58pQjL --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17a82766e9..6c03bbf41e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **`check` rejects `replace(…)` — it is not a Mendix expression function** (mendixlabs/mxcli#2308, ako/mxcli#1258) — the function table listed `replace(String, String, String)`, so `check` stayed silent, `mxcli syntax microflow.expressions.functions` advertised it, and the build failed with CE0117 (Mendix CLI 2: CE7811 "Unknown function 'replace'"). The string replacers are `replaceAll` and `replaceFirst`; `check` now reports MDL044 and suggests `replaceAll()`. The did-you-mean for `replace` also no longer flips between `replaceAll` and `replaceFirst` from one run to the next. Measured on MxBuild 11.16.1. +- **`check` rejects a value of the wrong type in a `declare`, `set` or `return`** (ako/mxcli#1258) — `declare $X Integer = 'abc';`, `set $X = 'abc';` and a `return` of the wrong type in a flow with a return type passed `check` and `exec` and failed the build with CE0117 (Mendix CLI 2: CE0844). The value is now checked against the declared primitive type with the rule already used for call arguments: Integer and Long pass into each other and widen to Decimal, every other primitive mismatch is reported as E009. `empty` into a Boolean, enumerations, objects, lists and values of unknown kind stay silent, and a computed Decimal into an Integer or Long by `declare` or `set` is left to MDL041. Measured on MxBuild 11.16.1 over 216 microflows. - **`check` rejects an expression that reads a variable nothing defines** (ako/mxcli#1258) — `return $Nope + 1;` with no `$Nope` anywhere passed `check` and `exec` and failed the build with CE0109 "Undefined variable". New MDL098, for the expressions of return, if, declare, set, create/change members, log, show message and validation feedback, in microflows and nanoflows. A name counts as defined if a parameter or any statement of the flow produces it, or it is one MxBuild accepts with no declaration (`$currentUser`, `$currentSession`, `$latestError`, `$latestHttpResponse`, `$latestSoapFault`, `$currentIndex`, `$currentObject`). Measured on MxBuild 11.16.1. - **`check` rejects `returns T as $R` when a path ends the flow without `$R`** (ako/mxcli#1258) — MDL003 exempts the AS clause because the end event returns `$R`, which holds only while `$R` has a value there. A flow with a path off its end and no top-level `$R` passed `check` and `exec` and failed the build with CE0109 "Undefined variable" (or CE0108 when `$R` is defined only inside a branch or loop). New MDL097, microflow and nanoflow. Measured on MxBuild 11.16.1. - **`fix widgets` and `fix design-properties` use the project's own Mendix version of `mx`** (ako/mxcli#2321) — they took the version as a preference, so on macOS an `mx` on PATH or another Studio Pro install ran, and `mx update-widgets` refused with "Project is in different Mendix version". They now take an `mx` of exactly the project's version. On Linux that version is downloaded when it is missing. On macOS and Windows, where no matching `mx` is installed, they say which version is needed and how to pass it with `--mx`. From 0390650761f3f95ad8c1e08d14779a05bc4b2fdd Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 10:28:48 +0000 Subject: [PATCH 07/13] fix(check): Visible: roles on a tab page is stored, so do not refuse it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit tabPageStoredProps did not list visibleroles, so under mdl 1 check refused Visible: roles (…) on a tab page as 'cannot be stored' (MDL-WIDGET44), and a headerless script was warned it was not written. The builder writes it, describe prints it back and MxBuild 11.16.1 accepts an own-module role on a tab page. Refs ako/mxcli#1258, mendixlabs/mxcli#1460. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011oK82tETa46YQmMS58pQjL --- ...6-11-17-tabpage-visible-roles-refused.json | 1 + CHANGELOG.md | 1 + mdl/executor/tabpage_visible_roles_test.go | 81 +++++++++++++++++++ mdl/executor/validate_tabpage_props.go | 9 ++- 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 .claude/skills/fix-issue/findings/mdl-executor/2026-11-17-tabpage-visible-roles-refused.json create mode 100644 mdl/executor/tabpage_visible_roles_test.go diff --git a/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-tabpage-visible-roles-refused.json b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-tabpage-visible-roles-refused.json new file mode 100644 index 0000000000..618b41ad7c --- /dev/null +++ b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-tabpage-visible-roles-refused.json @@ -0,0 +1 @@ +{"area": "mdl/executor", "date": "2026-11-17", "refs": ["ako/mxcli#1258", "mendixlabs/mxcli#1460", "mendixlabs/mxcli#1465"], "ce": ["CE0149"], "symptom": "`tabpage tp (Caption: 'a', Visible: roles (Wd.Viewer))` was refused by `check` under mdl 1 (MDL-WIDGET44 \"`VisibleRoles` cannot be stored on a tab page\") and warned about headerless (MDL-V1-TABPAGEPROP \"not written\"), though the roles are written, describe prints them back and MxBuild 11.16.1 accepts an own-module role on a tab page.", "file": "mdl/executor/validate_tabpage_props.go tabPageStoredProps (added visibleroles). Test: mdl/executor/tabpage_visible_roles_test.go", "cause": "tabPageStoredProps is a hand-kept allowlist of the keys buildTabPageV3 writes. #1460 made the builder write VisibleRoles through childVisibility, and #1465 added the refusal for every known widget property the list did not name; the two never met, so the new key was refused as dropped.", "insight": "**A refusal built from an allowlist of what a builder writes goes stale the day the builder learns a new key, and it fails closed: it blocks a feature that works.** The test that settles it is two-sided: build the AST through the real builder and assert the stored field (TestTabPage_VisibleRolesIsWritten passed before the fix, which is what proved the refusal was the wrong half), then assert check does not refuse (failed before, with the reported message). Found while measuring a different gap: Visible: roles naming another module's role is CE0149 on a container, dynamic text, layout-grid row, navigation-list item, snippet container and tab page alike (own-module role clean everywhere), but the tab page case could not even be reached under mdl 1 because of this refusal. The cross-module check is a separate change. The message also named VisibleRoles when it sat beside a real offender (CaptionParams); that goes with the fix since the key is no longer listed. Badge keys were checked and are not affected: they are not page-widget properties, so tabPageDroppedProps never reaches them."} diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c03bbf41e..e1ef6a26c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **`Visible: roles (…)` on a tab page is no longer refused** (ako/mxcli#1258) — under `mdl 1;` `check` refused it as "cannot be stored on a tab page" (MDL-WIDGET44), and a headerless script was warned it was not written (MDL-V1-TABPAGEPROP). Both were wrong: the roles are stored, `describe` prints them back, and MxBuild accepts a role of the tab page's own module. The property was missing from the tab page's list of stored keys. - **`check` rejects `replace(…)` — it is not a Mendix expression function** (mendixlabs/mxcli#2308, ako/mxcli#1258) — the function table listed `replace(String, String, String)`, so `check` stayed silent, `mxcli syntax microflow.expressions.functions` advertised it, and the build failed with CE0117 (Mendix CLI 2: CE7811 "Unknown function 'replace'"). The string replacers are `replaceAll` and `replaceFirst`; `check` now reports MDL044 and suggests `replaceAll()`. The did-you-mean for `replace` also no longer flips between `replaceAll` and `replaceFirst` from one run to the next. Measured on MxBuild 11.16.1. - **`check` rejects a value of the wrong type in a `declare`, `set` or `return`** (ako/mxcli#1258) — `declare $X Integer = 'abc';`, `set $X = 'abc';` and a `return` of the wrong type in a flow with a return type passed `check` and `exec` and failed the build with CE0117 (Mendix CLI 2: CE0844). The value is now checked against the declared primitive type with the rule already used for call arguments: Integer and Long pass into each other and widen to Decimal, every other primitive mismatch is reported as E009. `empty` into a Boolean, enumerations, objects, lists and values of unknown kind stay silent, and a computed Decimal into an Integer or Long by `declare` or `set` is left to MDL041. Measured on MxBuild 11.16.1 over 216 microflows. - **`check` rejects an expression that reads a variable nothing defines** (ako/mxcli#1258) — `return $Nope + 1;` with no `$Nope` anywhere passed `check` and `exec` and failed the build with CE0109 "Undefined variable". New MDL098, for the expressions of return, if, declare, set, create/change members, log, show message and validation feedback, in microflows and nanoflows. A name counts as defined if a parameter or any statement of the flow produces it, or it is one MxBuild accepts with no declaration (`$currentUser`, `$currentSession`, `$latestError`, `$latestHttpResponse`, `$latestSoapFault`, `$currentIndex`, `$currentObject`). Measured on MxBuild 11.16.1. diff --git a/mdl/executor/tabpage_visible_roles_test.go b/mdl/executor/tabpage_visible_roles_test.go new file mode 100644 index 0000000000..cce7143723 --- /dev/null +++ b/mdl/executor/tabpage_visible_roles_test.go @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: Apache-2.0 + +package executor + +import ( + "reflect" + "strings" + "testing" +) + +// `Visible: roles (…)` on a tab page is stored. buildTabPageV3 applies it through +// childVisibility (mendixlabs/mxcli#1460), describe prints it back, and MxBuild +// 11.16.1 accepts an own-module role on a tab page (a role of another module is +// CE0149, which is its own check). But tabPageStoredProps did not list +// `visibleroles`, so `check` refused the property as "cannot be stored on a tab +// page" (MDL-WIDGET44) under mdl 1, and a headerless script was warned it was +// not written when it was. +func TestTabPage_VisibleRolesIsWritten(t *testing.T) { + _, tc := tabContainerOf(t, "", ` + tabpage tpA (Caption: 'A', Visible: roles (M.Viewer)) { dynamictext t1 (Content: 'A') } + tabpage tpB (Caption: 'B', Visible: roles (M.Viewer, M.Other)) { dynamictext t2 (Content: 'B') } + tabpage tpC (Caption: 'C', Visible: false, Visible: roles (M.Viewer)) { dynamictext t3 (Content: 'C') } + tabpage tpD (Caption: 'D') { dynamictext t4 (Content: 'D') } +`) + got := buildTabs(t, visibleWhenPB("M.Job"), tc) + want := [][]string{{"M.Viewer"}, {"M.Viewer", "M.Other"}, {"M.Viewer"}, nil} + for i, w := range want { + cv := got.TabPages[i].ConditionalVisibility + if w == nil { + if cv != nil { + t.Errorf("tab page %d: no Visible: but got a setting %+v", i, cv) + } + continue + } + if cv == nil || !reflect.DeepEqual(cv.ModuleRoles, w) { + t.Errorf("tab page %d: ModuleRoles = %+v, want %v", i, cv, w) + } + } + // The expression beside the roles is kept (tpC). + if cv := got.TabPages[2].ConditionalVisibility; cv == nil || cv.Expression != "false" { + t.Errorf("tpC: Expression = %+v, want false beside the roles", cv) + } +} + +// check must not refuse what the builder writes, under mdl 1 or without a header. +func TestCheck_TabPageVisibleRolesNotRefused(t *testing.T) { + const tabs = ` + tabpage tpA (Caption: 'A', Visible: roles (M.Viewer)) { dynamictext t1 (Content: 'A') } + tabpage tpB (Caption: 'B', Visible: false, Visible: roles (M.Viewer, M.Other)) { dynamictext t2 (Content: 'B') } +` + for _, header := range []string{"mdl 1;\n", ""} { + prog, _ := tabContainerOf(t, header, tabs) + if vs := tabPageViolations(prog); len(vs) != 0 { + t.Errorf("header %q: Visible: roles on a tab page was reported: %+v", header, vs) + } + } +} + +// Controls: what a tab page really cannot store is still refused, with or +// without roles beside it. +func TestCheck_TabPageUnstorablePropsStillRefusedBesideRoles(t *testing.T) { + prog, _ := tabContainerOf(t, "mdl 1;\n", ` + tabpage tpA (Caption: 'Due {1}', CaptionParams: ({1} = Title), Visible: roles (M.Viewer)) { dynamictext t1 (Content: 'A') } +`) + vs := tabPageViolations(prog) + if len(vs) != 1 { + t.Fatalf("got %d violations, want 1 for CaptionParams: %+v", len(vs), vs) + } + if got := vs[0].Message; !containsAll(got, "CaptionParams") || containsAll(got, "VisibleRoles") { + t.Errorf("message should name CaptionParams and not VisibleRoles: %q", got) + } +} + +func containsAll(s string, subs ...string) bool { + for _, sub := range subs { + if !strings.Contains(s, sub) { + return false + } + } + return true +} diff --git a/mdl/executor/validate_tabpage_props.go b/mdl/executor/validate_tabpage_props.go index da03a245a7..15eb330954 100644 --- a/mdl/executor/validate_tabpage_props.go +++ b/mdl/executor/validate_tabpage_props.go @@ -32,9 +32,16 @@ var tabPagePropDropped = langver.Change{ New: "a refusal (MDL-WIDGET44), with nothing written", } -// tabPageStoredProps are the keys buildTabPageV3 writes. +// tabPageStoredProps are the keys buildTabPageV3 writes. `visibleroles` is the +// roles of `Visible: roles (…)`, applied by childVisibility +// (mendixlabs/mxcli#1460): it was missing here, so check refused — under mdl 1 — +// or warned "not written" — headerless — about a property that is written, and +// that MxBuild accepts for a role of the tab page's own module. The badge keys +// are not listed because they are not page-widget properties at all +// (isTabPageBadgeProp), so tabPageDroppedProps never reaches them. var tabPageStoredProps = map[string]bool{ "caption": true, "visible": true, "visibleif": true, "visiblewhen": true, + "visibleroles": true, } // tabPageDroppedProps lists, sorted, the keys on a tab page that the page From a6cdc5ebbcb9e32222289c427813cf1cc741af6a Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 10:46:45 +0000 Subject: [PATCH 08/13] feat(check): MDL-GRANT04 flags Visible: roles naming a role of another module A widget's role-based visibility can only name module roles of its own page, snippet or layout. A role of another module passed check --references and exec, and the build reported CE0149 'reselect roles'. MDL-GRANT04 is MDL-GRANT01's sibling for a widget's roles: it needs no project and runs in the shared pre-flight pass. Measured on MxBuild 11.16.1 over 17 documents (container, dynamic text, layout-grid row, navigation-list item, tab page, snippet, layout, alter page insert): a foreign role is CE0149, an own-module role is clean, and viewing access is irrelevant. Refs ako/mxcli#1258, mendixlabs/mxcli#1460. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011oK82tETa46YQmMS58pQjL --- ...2026-11-17-visible-roles-cross-module.json | 1 + .claude/skills/mendix/create-page/SKILL.md | 17 ++- CHANGELOG.md | 1 + docs-site/src/language/pages.md | 6 +- .../visible-roles-cross-module.fail.mdl | 27 ++++ .../bug-tests/visible-roles-cross-module.mdl | 25 ++++ mdl/executor/validate_program.go | 4 + mdl/executor/validate_visible_roles_module.go | 89 +++++++++++++ .../validate_visible_roles_module_test.go | 117 ++++++++++++++++++ 9 files changed, 279 insertions(+), 8 deletions(-) create mode 100644 .claude/skills/fix-issue/findings/mdl-executor/2026-11-17-visible-roles-cross-module.json create mode 100644 mdl-examples/bug-tests/visible-roles-cross-module.fail.mdl create mode 100644 mdl-examples/bug-tests/visible-roles-cross-module.mdl create mode 100644 mdl/executor/validate_visible_roles_module.go create mode 100644 mdl/executor/validate_visible_roles_module_test.go diff --git a/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-visible-roles-cross-module.json b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-visible-roles-cross-module.json new file mode 100644 index 0000000000..12e6e34847 --- /dev/null +++ b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-17-visible-roles-cross-module.json @@ -0,0 +1 @@ +{"area": "mdl/executor", "date": "2026-11-17", "refs": ["ako/mxcli#1258", "mendixlabs/mxcli#1460"], "ce": ["CE0149"], "symptom": "`create page Wd.P (…) { container c (Visible: roles (Administration.Administrator)) {…} }` passed `mxcli check --references` and `exec`; `mx check` then reported CE0149 \"Visibility needs to be updated, reselect roles in properties\". A role of the page's own module, viewable or not, was clean.", "file": "mdl/executor/validate_visible_roles_module.go ValidateVisibleRolesModule (MDL-GRANT04), called from validate_program.go next to ValidateGrantRoles. Tests: validate_visible_roles_module_test.go; fixtures mdl-examples/bug-tests/visible-roles-cross-module(.fail).mdl", "cause": "validate_grant_refs.go (widgetVisibleRoles) only checks that each role EXISTS; nothing compared the role's module with the document's. MDL-GRANT01 already did that for document grants (CE0148) and was never extended to a widget's role list, the same mistake one property over.", "insight": "**When a rule exists for one place a Mendix reference list is stored (document grants: CE0148), ask for every other place that stores the same kind of list before calling it done: the same-module restriction is a property of how Mendix stores module roles, not of GRANT.** Measured on MxBuild 11.16.1, one `mx check -j` over 17 documents: container, dynamic text, layout-grid row, navigation-list item, snippet container, layout container, tab page and a widget inserted by alter page are each CE0149 for a foreign role (Administration.Administrator or a role of a user-created module) and clean for an own-module one; an own+foreign list is CE0149; viewing access is irrelevant. The report's first guess was page access; the measurement said module only. Tab pages were unreachable until the separate MDL-WIDGET44 false refusal of `Visible: roles` on a tab page was fixed. Case-insensitive module compare (unmeasured whether Mendix distinguishes) and unqualified roles skipped, so a miss rather than a false report. Sweep of every .mdl in the repo: 0 hits, but only 8 scripts mention `roles (` and one uses Visible: roles (an own-module role), so the sweep is weak evidence here; the measurement is the evidence. Control: stubbing the rule fails all 11 positive cases, the one-violation test and the ValidateProgram wiring test; the positive-control script builds with 0 errors on MxBuild."} diff --git a/.claude/skills/mendix/create-page/SKILL.md b/.claude/skills/mendix/create-page/SKILL.md index e788d8bb98..cef0ad548d 100644 --- a/.claude/skills/mendix/create-page/SKILL.md +++ b/.claude/skills/mendix/create-page/SKILL.md @@ -15,12 +15,12 @@ description: "CREATE PAGE syntax reference — parameters, variables, layouts, a | 47-222 | Syntax | | 223-293 | Basic Examples | | 294-318 | Modifying Existing Pages | -| 319-435 | Conditional Visibility and Editability | -| 436-605 | Known Limitations | -| 606-614 | Tips | -| 615-641 | Related Commands | -| 642-666 | PLUGGABLEWIDGET Escape Hatch | -| 667-670 | See Also | +| 319-440 | Conditional Visibility and Editability | +| 441-610 | Known Limitations | +| 611-619 | Tips | +| 620-646 | Related Commands | +| 647-671 | PLUGGABLEWIDGET Escape Hatch | +| 672-675 | See Also | # CREATE PAGE - MDL Syntax Guide @@ -389,6 +389,11 @@ create page MyModule.Request_Review (title: 'Review', layout: Atlas_Core.Atlas_D }; ``` +The roles must be roles of the page's, snippet's or layout's OWN module: a role of +another module (`Administration.Administrator` on a page in `MyModule`) builds with +CE0149 "reselect roles", and `check` refuses it (MDL-GRANT04). Whether the page may +be viewed by the role does not matter, only the role's module. + `visible: roles (Mod.Role) and ` is the same two settings in one line (both apply). `roles (…)` anywhere else in an expression is refused (MDL-WIDGET54): Mendix expressions have no roles function. diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c03bbf41e..a7ad2e8734 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **`check` rejects `Visible: roles (…)` naming a role of another module** (ako/mxcli#1258) — a widget can only be shown to the module roles of its own page, snippet or layout. A role of another module (`Administration.Administrator` on a page in `Wd`, or a role of any other module) passed `check --references` and `exec`, and the build reported CE0149 "Visibility needs to be updated, reselect roles in properties". New MDL-GRANT04, the sibling of MDL-GRANT01 (CE0148 for document grants): it needs no project, and covers a container, dynamic text, layout-grid row, navigation-list item, tab page, snippet, layout, and a widget inserted by `alter page`. Whether the role may view the page does not matter, only its module. Measured on MxBuild 11.16.1 over 17 documents. - **`check` rejects `replace(…)` — it is not a Mendix expression function** (mendixlabs/mxcli#2308, ako/mxcli#1258) — the function table listed `replace(String, String, String)`, so `check` stayed silent, `mxcli syntax microflow.expressions.functions` advertised it, and the build failed with CE0117 (Mendix CLI 2: CE7811 "Unknown function 'replace'"). The string replacers are `replaceAll` and `replaceFirst`; `check` now reports MDL044 and suggests `replaceAll()`. The did-you-mean for `replace` also no longer flips between `replaceAll` and `replaceFirst` from one run to the next. Measured on MxBuild 11.16.1. - **`check` rejects a value of the wrong type in a `declare`, `set` or `return`** (ako/mxcli#1258) — `declare $X Integer = 'abc';`, `set $X = 'abc';` and a `return` of the wrong type in a flow with a return type passed `check` and `exec` and failed the build with CE0117 (Mendix CLI 2: CE0844). The value is now checked against the declared primitive type with the rule already used for call arguments: Integer and Long pass into each other and widen to Decimal, every other primitive mismatch is reported as E009. `empty` into a Boolean, enumerations, objects, lists and values of unknown kind stay silent, and a computed Decimal into an Integer or Long by `declare` or `set` is left to MDL041. Measured on MxBuild 11.16.1 over 216 microflows. - **`check` rejects an expression that reads a variable nothing defines** (ako/mxcli#1258) — `return $Nope + 1;` with no `$Nope` anywhere passed `check` and `exec` and failed the build with CE0109 "Undefined variable". New MDL098, for the expressions of return, if, declare, set, create/change members, log, show message and validation feedback, in microflows and nanoflows. A name counts as defined if a parameter or any statement of the flow produces it, or it is one MxBuild accepts with no declaration (`$currentUser`, `$currentSession`, `$latestError`, `$latestHttpResponse`, `$latestSoapFault`, `$currentIndex`, `$currentObject`). Measured on MxBuild 11.16.1. diff --git a/docs-site/src/language/pages.md b/docs-site/src/language/pages.md index 8faeac0039..a9c5ff7561 100644 --- a/docs-site/src/language/pages.md +++ b/docs-site/src/language/pages.md @@ -131,8 +131,10 @@ create page MyModule.Request_Review (Title: 'Review', Layout: Atlas_Core.Atlas_D }; ``` -It is accepted on widgets, tab pages and navigation-list items. `mxcli check ---references` refuses a role the project (or the script, above the statement) +It is accepted on widgets, tab pages and navigation-list items. The roles must be +roles of the page's own module: a role of another module builds with CE0149 +("reselect roles") and `mxcli check` refuses it (MDL-GRANT04), whether or not the +role may view the page. `mxcli check --references` refuses a role the project (or the script, above the statement) does not define. `alter page … set Visible` changes only the condition and keeps the stored roles. diff --git a/mdl-examples/bug-tests/visible-roles-cross-module.fail.mdl b/mdl-examples/bug-tests/visible-roles-cross-module.fail.mdl new file mode 100644 index 0000000000..5e5f73e66e --- /dev/null +++ b/mdl-examples/bug-tests/visible-roles-cross-module.fail.mdl @@ -0,0 +1,27 @@ +mdl 1; +-- NEGATIVE TEST — `mxcli check` must REFUSE this file (MDL-GRANT04), with no project. +-- +-- A widget's `Visible: roles (…)` can name only module roles of its OWN +-- module. A role of another module passed `mxcli check --references` and `exec`, +-- and the build then reported +-- +-- [CE0149] "Visibility needs to be updated, reselect roles in properties." +-- +-- Measured on MxBuild 11.16.1 over 17 documents, one `mx check -j`: a container, +-- a dynamic text, a layout-grid row, a navigation-list item, a tab page, a snippet +-- container, a layout container and a widget inserted by `alter page` are each +-- CE0149 for Administration.Administrator or for a role of a user-created module, +-- and clean for a role of their own module. A list that mixes the two is CE0149. +-- Whether the page may be VIEWED by the role does not matter, only the role's module. +-- +-- The positive control, whose shapes must stay clean, is beside this file as +-- visible-roles-cross-module.mdl. + +create module Wd; +create module role Wd.Viewer; + +-- The one foreign role: the other three containers on this page are fine. +create page Wd.Granted (Title: 'G', Layout: Atlas_Core.Atlas_Default) { + container cOwn (Visible: roles (Wd.Viewer)) { dynamictext t1 (Content: 'a') } + container cOtherModule (Visible: roles (Administration.Administrator)) { dynamictext t2 (Content: 'b') } +}; diff --git a/mdl-examples/bug-tests/visible-roles-cross-module.mdl b/mdl-examples/bug-tests/visible-roles-cross-module.mdl new file mode 100644 index 0000000000..aa583cc054 --- /dev/null +++ b/mdl-examples/bug-tests/visible-roles-cross-module.mdl @@ -0,0 +1,25 @@ +mdl 1; +-- Positive control for visible-roles-cross-module.fail.mdl: a role of the +-- widget's own module is clean on every widget shape, whether or not the role may +-- view the page. `mxcli check` must pass this file. + +create module Wd; +create module role Wd.Viewer; +create module role Wd.Other; + +create page Wd.Granted (Title: 'G', Layout: Atlas_Core.Atlas_Default) { + container cOwnGranted (Visible: roles (Wd.Viewer)) { dynamictext t1 (Content: 'a') } + container cOwnNotGranted (Visible: roles (Wd.Other)) { dynamictext t2 (Content: 'b') } + container cTwoOwn (Visible: roles (Wd.Viewer, Wd.Other)) { dynamictext t3 (Content: 'c') } + navigationlist nl { + item (Action: close page, Visible: roles (Wd.Viewer)) { dynamictext t4 (Content: 'd') } + } + layoutgrid g { + row (Visible: roles (Wd.Viewer)) { column (desktopwidth: 12) { dynamictext t5 (Content: 'e') } } + } +}; +grant view on page Wd.Granted to Wd.Viewer; + +create snippet Wd.S { + container c (Visible: roles (Wd.Viewer)) { dynamictext t (Content: 'a') } +}; diff --git a/mdl/executor/validate_program.go b/mdl/executor/validate_program.go index fc22662bc6..378a5ad242 100644 --- a/mdl/executor/validate_program.go +++ b/mdl/executor/validate_program.go @@ -348,6 +348,10 @@ func ValidateProgram(prog *ast.Program, projectPath string) []linter.Violation { // under --references, where it would only fire with -p (#836). violations = append(violations, ValidateGrantRoles(prog)...) + // MDL-GRANT04: the same mistake in a widget's `Visible: roles (…)` — a role of + // another module than the document's is CE0149. Also needs no project. + violations = append(violations, ValidateVisibleRolesModule(prog)...) + // MDL-GRANT03: a role given a constrained and an unconstrained rule on one // entity in this script — rules add up, so the constraint limits nothing // (ako/mxcli#2320). The overlap with the project's rules is reported under diff --git a/mdl/executor/validate_visible_roles_module.go b/mdl/executor/validate_visible_roles_module.go new file mode 100644 index 0000000000..17a9d0d326 --- /dev/null +++ b/mdl/executor/validate_visible_roles_module.go @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: Apache-2.0 + +package executor + +import ( + "fmt" + "strings" + + "github.com/mendixlabs/mxcli/mdl/ast" + "github.com/mendixlabs/mxcli/mdl/linter" +) + +// visibleRolesModuleRule is MDL-GRANT04: `Visible: roles (…)` naming a module role +// of another module than the document the widget is in. It is MDL-GRANT01's +// sibling — CE0148 for a document grant, CE0149 for a widget's visibility. +const visibleRolesModuleRule = "MDL-GRANT04" + +// ValidateVisibleRolesModule reports a widget whose `Visible: roles (…)` names a +// role of a module other than its page's, snippet's or layout's own. +// +// Mendix stores a widget's role-based visibility as references to module roles of +// the document's own module only, so a role of another module builds with CE0149 +// "Visibility needs to be updated, reselect roles in properties". Measured on +// MxBuild 11.16.1 over 17 documents, one `mx check -j`: a container, a dynamic +// text, a layout-grid row, a navigation-list item, a tab page, a snippet +// container, a layout container and a widget inserted by `alter page` are each +// CE0149 for Administration.Administrator or a role of a user-created module, and +// clean for a role of their own module; a list mixing an own and a foreign role is +// CE0149. Whether the page may be VIEWED by the role does not matter: only the +// role's module does. +// +// `check` passed it and `exec` wrote it, so the first sign was the build. Like +// MDL-GRANT01 it needs no project — both module names are in the script, the +// document's from its own statement — so it runs in the pre-flight pass `check` +// and `exec` share, not only under --references. +// +// Names compare without regard to letter case: whether Mendix distinguishes +// `Wd` from `wd` is unmeasured, and treating them as one module can only silence a +// report. A role written without a module is left to the reference check, which +// cannot resolve it either. +func ValidateVisibleRolesModule(prog *ast.Program) []linter.Violation { + var out []linter.Violation + for _, stmt := range prog.Statements { + module, kind, ok := visibleRolesDocument(stmt) + if !ok { + continue + } + forEachWidget(stmt, func(w *ast.WidgetV3, where string) { + var foreign []string + for _, r := range widgetVisibleRoles(w) { + if qn, ok := splitQualified(r); ok && !strings.EqualFold(qn.Module, module) { + foreign = append(foreign, r) + } + } + if len(foreign) == 0 { + return + } + out = append(out, linter.Violation{ + RuleID: visibleRolesModuleRule, + Severity: linter.SeverityError, + Message: fmt.Sprintf("%s: Visible: roles names %s, a role of another module than the %s's own module %q — "+ + "a widget can only be shown to its own module's roles; Mendix reports CE0149 \"Visibility needs to be "+ + "updated, reselect roles in properties\"", where, strings.Join(foreign, ", "), kind, module), + Suggestion: fmt.Sprintf("Use a role of module %s (e.g. %s.) and map the user role to it "+ + "(`alter user role add %s.`).", module, module, module), + Location: linter.Location{Module: module, DocumentType: kind}, + }) + }) + } + return out +} + +// visibleRolesDocument is the module and kind of the document a statement's +// widgets belong to, for the statements forEachWidget walks. The module is taken +// from the statement's own qualified name, so the check works before the document +// exists — it may be created earlier in the same script. +func visibleRolesDocument(stmt ast.Statement) (module, kind string, ok bool) { + switch s := stmt.(type) { + case *ast.CreatePageStmtV3: + return s.Name.Module, "page", true + case *ast.CreateSnippetStmtV3: + return s.Name.Module, "snippet", true + case *ast.CreateLayoutStmt: + return s.Name.Module, "layout", true + case *ast.AlterPageStmt: + return s.PageName.Module, strings.ToLower(s.ContainerType), true + } + return "", "", false +} diff --git a/mdl/executor/validate_visible_roles_module_test.go b/mdl/executor/validate_visible_roles_module_test.go new file mode 100644 index 0000000000..390749678b --- /dev/null +++ b/mdl/executor/validate_visible_roles_module_test.go @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: Apache-2.0 + +package executor + +import ( + "strings" + "testing" + + "github.com/mendixlabs/mxcli/mdl/linter" + "github.com/mendixlabs/mxcli/mdl/visitor" +) + +// MDL-GRANT04: `Visible: roles (…)` naming a role of another module than the +// document's. Measured on MxBuild 11.16.1 over 17 documents, one `mx check -j`: +// +// container, dynamic text, layout-grid row, navigation-list item, snippet +// container, layout container, tab page, a widget inserted by `alter page`: +// a role of ANOTHER module (Administration.Administrator, or a role of a +// user-created module) is CE0149 "Visibility needs to be updated, reselect +// roles in properties"; a role of the document's own module is clean. +// a list mixing an own role and a foreign one: CE0149. +// +// The same shape as MDL-GRANT01 (CE0148 for a document grant), and like it needs +// no project: both names are in the script. +func visibleRolesModuleViolations(t *testing.T, src string) []linter.Violation { + t.Helper() + prog, errs := visitor.Build("mdl 1;\n" + src) + if len(errs) > 0 { + t.Fatalf("parse: %v\n%s", errs, src) + } + var out []linter.Violation + for _, v := range ValidateVisibleRolesModule(prog) { + if v.RuleID == "MDL-GRANT04" { + out = append(out, v) + } + } + return out +} + +func TestVisibleRolesModule_MDLGrant04(t *testing.T) { + const page = "create page Wd.P (Title: 'T', Layout: Atlas_Core.Atlas_Default) { " + cases := []struct { + name string + src string + want string // "" = clean; else a substring of the message + }{ + {"container", page + "container c (Visible: roles (Administration.Administrator)) { dynamictext t (Content: 'a') } };", "Administration.Administrator"}, + {"dynamic text", page + "dynamictext t (Content: 'a', Visible: roles (Administration.Administrator)) };", "Administration.Administrator"}, + {"layout grid row", page + "layoutgrid g { row (Visible: roles (Administration.Administrator)) { column (desktopwidth: 12) { dynamictext t (Content: 'a') } } } };", "Administration.Administrator"}, + {"navigation list item", page + "navigationlist nl { item (Action: close page, Visible: roles (Administration.Administrator)) { dynamictext t (Content: 'a') } } };", "Administration.Administrator"}, + {"tab page", page + "tabcontainer tabs { tabpage tp (Caption: 'a', Visible: roles (Administration.Administrator)) { dynamictext t (Content: 'a') } } };", "Administration.Administrator"}, + {"role of a user-created module", page + "container c (Visible: roles (Wd2.Other)) { dynamictext t (Content: 'a') } };", "Wd2.Other"}, + {"own role beside a foreign one", page + "container c (Visible: roles (Wd.Viewer, Administration.Administrator)) { dynamictext t (Content: 'a') } };", "Administration.Administrator"}, + {"roles beside an expression", page + "container c (Visible: false, Visible: roles (Administration.Administrator)) { dynamictext t (Content: 'a') } };", "Administration.Administrator"}, + {"snippet", "create snippet Wd.S { container c (Visible: roles (Administration.Administrator)) { dynamictext t (Content: 'a') } };", "Administration.Administrator"}, + {"layout", "create layout Wd.L (layouttype: 'Default') { container c (Visible: roles (Administration.Administrator)) { dynamictext t (Content: 'a') } placeholder Main };", "Administration.Administrator"}, + {"alter page insert", "alter page Wd.P { insert after base { container ac (Visible: roles (Administration.Administrator)) { dynamictext t (Content: 'a') } } };", "Administration.Administrator"}, + + {"own role", page + "container c (Visible: roles (Wd.Viewer)) { dynamictext t (Content: 'a') } };", ""}, + {"own roles, two", page + "container c (Visible: roles (Wd.Viewer, Wd.Other)) { dynamictext t (Content: 'a') } };", ""}, + {"own role, other letter case", page + "container c (Visible: roles (wd.Viewer)) { dynamictext t (Content: 'a') } };", ""}, + {"own role on a tab page", page + "tabcontainer tabs { tabpage tp (Caption: 'a', Visible: roles (Wd.Viewer)) { dynamictext t (Content: 'a') } } };", ""}, + {"own role in a snippet", "create snippet Wd.S { container c (Visible: roles (Wd.Viewer)) { dynamictext t (Content: 'a') } };", ""}, + {"own role in an alter page insert", "alter page Wd.P { insert after base { container ac (Visible: roles (Wd.Viewer)) { dynamictext t (Content: 'a') } } };", ""}, + {"no roles", page + "container c (Visible: false) { dynamictext t (Content: 'a') } };", ""}, + {"no visibility", page + "container c { dynamictext t (Content: 'a') } };", ""}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + got := visibleRolesModuleViolations(t, c.src) + switch { + case c.want == "" && len(got) > 0: + t.Fatalf("unexpected MDL-GRANT04: %s", got[0].Message) + case c.want != "" && len(got) == 0: + t.Fatalf("expected MDL-GRANT04 naming %s, got none", c.want) + case c.want != "" && !strings.Contains(got[0].Message, c.want): + t.Fatalf("message %q does not name %s", got[0].Message, c.want) + } + }) + } +} + +// One violation per widget, however many foreign roles it names, and the message +// names each of them. +func TestVisibleRolesModule_OneViolationPerWidget(t *testing.T) { + got := visibleRolesModuleViolations(t, + "create page Wd.P (Title: 'T', Layout: Atlas_Core.Atlas_Default) { container c (Visible: roles (Administration.Administrator, Wd2.Other, Wd.Viewer)) { dynamictext t (Content: 'a') } };") + if len(got) != 1 { + t.Fatalf("got %d violations, want 1: %+v", len(got), got) + } + for _, want := range []string{"Administration.Administrator", "Wd2.Other"} { + if !strings.Contains(got[0].Message, want) { + t.Errorf("message %q does not name %s", got[0].Message, want) + } + } + if strings.Contains(got[0].Message, "Wd.Viewer") { + t.Errorf("message names the own-module role Wd.Viewer: %q", got[0].Message) + } +} + +// The rule must be part of the pre-flight pass that `check` and `exec` both run, +// not just a function nobody calls. +func TestVisibleRolesModule_RunsInValidateProgram(t *testing.T) { + prog, errs := visitor.Build("mdl 1;\ncreate page Wd.P (Title: 'T', Layout: Atlas_Core.Atlas_Default) { container c (Visible: roles (Administration.Administrator)) { dynamictext t (Content: 'a') } };") + if len(errs) > 0 { + t.Fatal(errs) + } + found := false + for _, v := range ValidateProgram(prog, "") { + if v.RuleID == "MDL-GRANT04" { + found = true + } + } + if !found { + t.Fatal("ValidateProgram did not report MDL-GRANT04") + } +} From e906487e19979af769ba2a56afc3ee41c11b3fff Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 11:48:56 +0000 Subject: [PATCH 09/13] Carry test results into the traceability report (#2340) report traceability read the --junit results into its evidence and then reduced each requirement's tests to their names, so the report was identical with and without --junit. Requirements now carry testResults (name and status), and the table shows and marks a failing test. Closes #2340 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011oK82qrr1WnXxq1eRn7qRJ --- ...1-17-traceability-drops-junit-results.json | 1 + CHANGELOG.md | 1 + cmd/mxcli/projectreport/traceability.go | 55 +++++++++- .../traceability_junit_2340_test.go | 103 ++++++++++++++++++ 4 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 .claude/skills/fix-issue/findings/cmd-mxcli/2026-11-17-traceability-drops-junit-results.json create mode 100644 cmd/mxcli/projectreport/traceability_junit_2340_test.go diff --git a/.claude/skills/fix-issue/findings/cmd-mxcli/2026-11-17-traceability-drops-junit-results.json b/.claude/skills/fix-issue/findings/cmd-mxcli/2026-11-17-traceability-drops-junit-results.json new file mode 100644 index 0000000000..61ff5215be --- /dev/null +++ b/.claude/skills/fix-issue/findings/cmd-mxcli/2026-11-17-traceability-drops-junit-results.json @@ -0,0 +1 @@ +{"area": "cmd/mxcli", "date": "2026-11-17", "refs": ["ako/mxcli#2340", "mendixlabs/mxcli#1877"], "ce": [], "symptom": "`mxcli report traceability --format json --junit junit.xml` produced output byte-identical to the run without --junit; each requirement's `tests` stayed a list of names, while `report tests --junit` on the same file showed 10 passed / 1 failed.", "file": "cmd/mxcli/projectreport/traceability.go TraceRequirement.TestResults / TraceTestResult / testsCell / testsClass. Test: cmd/mxcli/projectreport/traceability_junit_2340_test.go", "cause": "The traceability command did read the JUnit file into the evidence (CollectTests carries each TestInfo.Status), but CollectTraceability reduced each requirement's reaching tests to their names, so the results never reached the report.", "insight": "**A flag that is accepted, documented and plumbed can still be dropped at the last projection.** The evidence path was correct end to end up to `tr.Tests = append(tr.Tests, t.Name)`. The fix keeps `tests` (names) for existing readers and adds `testResults` [{name, status}] — 'not run' where no result was read — and the rendered table shows 'name (failed)' with the cell marked bad, so 'built, test failing' no longer reads as 'built and proven'."} diff --git a/CHANGELOG.md b/CHANGELOG.md index db1c4a63a6..ea80085d98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **`report traceability --junit` shows each requirement's test results** (ako/mxcli#2340, mendixlabs/mxcli#1877) — the results were read and then dropped, so the report was byte-identical with and without `--junit`. Each requirement now has `testResults` (name and passed, failed, error, skipped or not run), and the table marks a failing test, so "built, test failing" no longer reads as "built and proven". - **`fix widgets` and `fix design-properties` use the project's own Mendix version of `mx`** (ako/mxcli#2321) — they took the version as a preference, so on macOS an `mx` on PATH or another Studio Pro install ran, and `mx update-widgets` refused with "Project is in different Mendix version". They now take an `mx` of exactly the project's version. On Linux that version is downloaded when it is missing. On macOS and Windows, where no matching `mx` is installed, they say which version is needed and how to pass it with `--mx`. - **Parallel `mxcli exec` runs on one project no longer lose each other's writes** (ako/mxcli#2326) — measured with 8 parallel scripts: every process reported success and 2 to 6 of the 16 entities were missing (the reporter saw all of them fail instead). A script, or a `-c`, that writes now holds the project's lock (`.mxcli/write.lock`) for its whole run. A second writer waits up to 60 seconds, then changes nothing and exits 75, naming the holder. A lock left by a crashed run is taken over. Read-only runs take no lock. With the lock, all 16 entities land in every round. - **`run restart` names the secret it cannot replay** (ako/mxcli#2313) — a restart refused because of a literal secret printed only a generic `--db-password @env:` hint. The `--constant X=@env:VAR` references are recorded and replayed, so the hint read as if they were not. The refusal now lists each flag stored redacted, such as `--constant Helpdesk.OpenRouterKey` or `--db-password`, and says that the values given as references are replayed. diff --git a/cmd/mxcli/projectreport/traceability.go b/cmd/mxcli/projectreport/traceability.go index 326a1a5e5e..d8c3bed4d8 100644 --- a/cmd/mxcli/projectreport/traceability.go +++ b/cmd/mxcli/projectreport/traceability.go @@ -90,9 +90,20 @@ type TraceRequirement struct { // footprint is the microflows that create or change its entity. FootprintApprox bool `json:"footprintApprox,omitempty"` Tests []string `json:"tests"` - Journeys []string `json:"journeys"` - Decisions []string `json:"decisions"` - Questions []string `json:"questions"` + // TestResults are Tests with the result each had in the JUnit file read + // (--junit or --run): passed, failed, error, skipped, or "not run" when + // none was read or the file has no result for it (ako/mxcli#2340). Tests + // keeps the names alone, for readers of the older field. + TestResults []TraceTestResult `json:"testResults,omitempty"` + Journeys []string `json:"journeys"` + Decisions []string `json:"decisions"` + Questions []string `json:"questions"` +} + +// TraceTestResult is one test that reaches a requirement, with its result. +type TraceTestResult struct { + Name string `json:"name"` + Status string `json:"status"` } // TraceQuestion is an open question that bears on one or more requirements. @@ -304,6 +315,11 @@ func CollectTraceability(q Querier, in TraceabilityInput) (*TraceabilityData, er for _, t := range in.Evidence.Tests { if intersects(t.Reached, fp) { tr.Tests = append(tr.Tests, t.Name) + status := t.Status + if status == "" { + status = "not run" + } + tr.TestResults = append(tr.TestResults, TraceTestResult{Name: t.Name, Status: status}) } } for _, j := range in.Evidence.Journeys { @@ -525,9 +541,9 @@ func RenderTraceability(env *Envelope, ann *Annotations) (Doc, error) { if impl == "" { impl = r.FootprintNote } - t.Rows = append(t.Rows, []string{title, status, impl, strings.Join(r.Tests, "; "), strings.Join(r.Journeys, "; "), + t.Rows = append(t.Rows, []string{title, status, impl, testsCell(r), strings.Join(r.Journeys, "; "), strings.Join(r.Decisions, "; "), strings.Join(r.Questions, "; ")}) - t.Classes = append(t.Classes, []string{"", statusClass(r.Status), "", proofClass(r), proofClass(r), "", ""}) + t.Classes = append(t.Classes, []string{"", statusClass(r.Status), "", testsClass(r), proofClass(r), "", ""}) } doc.Sections = append(doc.Sections, Section{ID: fmt.Sprintf("m%d", i+1), Title: m.Name, Blocks: []Block{t}}) } @@ -559,3 +575,32 @@ func bulletsOrNone(items []string) Block { } return Bullets{Items: items} } + +// testsCell lists a requirement's tests with the result of each that ran, +// so "built, test failing" does not read as "built and proven" +// (ako/mxcli#2340). A test with no result is listed by name alone. +func testsCell(r TraceRequirement) string { + if len(r.TestResults) == 0 { + return strings.Join(r.Tests, "; ") + } + parts := make([]string, 0, len(r.TestResults)) + for _, t := range r.TestResults { + if t.Status == "" || t.Status == "not run" { + parts = append(parts, t.Name) + continue + } + parts = append(parts, t.Name+" ("+t.Status+")") + } + return strings.Join(parts, "; ") +} + +// testsClass is the tests cell's class: "bad" when a test reaching the +// requirement failed or errored, else proofClass's. +func testsClass(r TraceRequirement) string { + for _, t := range r.TestResults { + if t.Status == "failed" || t.Status == "error" { + return "bad" + } + } + return proofClass(r) +} diff --git a/cmd/mxcli/projectreport/traceability_junit_2340_test.go b/cmd/mxcli/projectreport/traceability_junit_2340_test.go new file mode 100644 index 0000000000..64d02b7bd7 --- /dev/null +++ b/cmd/mxcli/projectreport/traceability_junit_2340_test.go @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: Apache-2.0 + +package projectreport + +import ( + "reflect" + "strings" + "testing" + + "github.com/mendixlabs/mxcli/cmd/mxcli/brain" +) + +// ako/mxcli#2340 (mendixlabs/mxcli#1877): `report traceability --junit` read +// the results and dropped them — each requirement's tests stayed a list of +// names, so the JSON with and without --junit was byte-identical, and "built, +// test failing" read like "built and proven". A requirement's tests now carry +// their result. +func TestTraceability_TestsCarryTheirResult_Issue2340(t *testing.T) { + f := newFixture(t) + f.module("Game", "") + f.microflow("Game.ACT_RemoveScore", true, "") + f.microflow("Game.ACT_AddScore", true, "") + in := TraceabilityInput{ + Requirements: []brain.RequirementResult{ + req("69d67f", "01-scores", "Remove a high score", true, anchor("@Game.ACT_RemoveScore", "microflow", brain.Resolved)), + req("70a1b2", "01-scores", "Add a high score", true, anchor("@Game.ACT_AddScore", "microflow", brain.Resolved)), + }, + Evidence: &TestsData{Tests: []TestInfo{ + {Name: "A removed high score is gone", Status: "failed", Message: "expected 0, got 1", Reached: []string{"Game.ACT_RemoveScore"}}, + {Name: "A score is added", Status: "passed", Reached: []string{"Game.ACT_AddScore"}}, + }}, + } + d, err := CollectTraceability(f.cat, in) + if err != nil { + t.Fatal(err) + } + got := map[string]TraceRequirement{} + for _, m := range d.Milestones { + for _, r := range m.Requirements { + got[r.ID] = r + } + } + if want := []TraceTestResult{{Name: "A removed high score is gone", Status: "failed"}}; !reflect.DeepEqual(got["69d67f"].TestResults, want) { + t.Errorf("69d67f test results = %+v, want %+v", got["69d67f"].TestResults, want) + } + if want := []TraceTestResult{{Name: "A score is added", Status: "passed"}}; !reflect.DeepEqual(got["70a1b2"].TestResults, want) { + t.Errorf("70a1b2 test results = %+v, want %+v", got["70a1b2"].TestResults, want) + } + // The names stay where they were, for readers of the old field. + if !reflect.DeepEqual(got["69d67f"].Tests, []string{"A removed high score is gone"}) { + t.Errorf("tests = %v", got["69d67f"].Tests) + } + // The rendered table says which test fails. + env, err := NewEnvelope("traceability", "Game", "2026-11-17", "test", d, nil, nil, nil) + if err != nil { + t.Fatal(err) + } + doc, err := RenderTraceability(env, nil) + if err != nil { + t.Fatal(err) + } + var cell string + for _, s := range doc.Sections { + for _, b := range s.Blocks { + tb, ok := b.(Table) + if !ok { + continue + } + for _, row := range tb.Rows { + for _, c := range row { + if strings.Contains(c, "A removed high score is gone") { + cell = c + } + } + } + } + } + if !strings.Contains(cell, "failed") { + t.Errorf("rendered tests cell = %q, want the failing result shown", cell) + } +} + +// CONTROL: without results (no --junit) the tests are "not run", and the +// table shows the names alone. +func TestTraceability_TestsWithoutResults_Issue2340(t *testing.T) { + f := newFixture(t) + f.module("Game", "") + f.microflow("Game.ACT_RemoveScore", true, "") + in := TraceabilityInput{ + Requirements: []brain.RequirementResult{ + req("69d67f", "01-scores", "Remove a high score", true, anchor("@Game.ACT_RemoveScore", "microflow", brain.Resolved)), + }, + Evidence: &TestsData{Tests: []TestInfo{{Name: "A removed high score is gone", Status: "not run", Reached: []string{"Game.ACT_RemoveScore"}}}}, + } + d, err := CollectTraceability(f.cat, in) + if err != nil { + t.Fatal(err) + } + r := d.Milestones[0].Requirements[0] + if len(r.TestResults) != 1 || r.TestResults[0].Status != "not run" { + t.Errorf("test results = %+v", r.TestResults) + } +} From ef42c71b485a545839f6a6dd238ad5398dc0ea8f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 11:50:15 +0000 Subject: [PATCH 10/13] Say a Mendix 9 runtime has no OQL action instead of blaming flags (#2341) A 9.x runtime has no preview_execute_oql admin action, and mxcli oql's error sent the user to restart with live-preview flags, which cannot help. When both OQL routes are missing, the runtime's version is read (M2EE about), and below Mendix 10 the error says OQL over the admin API needs 10+ and names the version. 10+ keeps the dev-flags hint. Refs #2341 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011oK82qrr1WnXxq1eRn7qRJ --- ...026-11-17-oql-mendix9-misleading-hint.json | 1 + CHANGELOG.md | 1 + cmd/mxcli/docker/oql.go | 55 +++++++++++++++ cmd/mxcli/docker/oql_old_runtime_2341_test.go | 67 +++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 .claude/skills/fix-issue/findings/cmd-mxcli/2026-11-17-oql-mendix9-misleading-hint.json create mode 100644 cmd/mxcli/docker/oql_old_runtime_2341_test.go diff --git a/.claude/skills/fix-issue/findings/cmd-mxcli/2026-11-17-oql-mendix9-misleading-hint.json b/.claude/skills/fix-issue/findings/cmd-mxcli/2026-11-17-oql-mendix9-misleading-hint.json new file mode 100644 index 0000000000..d140470fd5 --- /dev/null +++ b/.claude/skills/fix-issue/findings/cmd-mxcli/2026-11-17-oql-mendix9-misleading-hint.json @@ -0,0 +1 @@ +{"area": "cmd/mxcli", "date": "2026-11-17", "refs": ["ako/mxcli#2341", "mendixlabs/mxcli#1879"], "ce": [], "symptom": "`mxcli oql` against a Mendix 9.24.11 runtime: 'OQL error: Action not found. -- the running app does not expose the OQL preview endpoint, which needs the live-preview dev flags at boot … upgrade mxcli …'. The 9.24 runtime answers preview_execute_oql with {\"result\":-5,\"message\":\"Action not found.\"} whatever the flags; 10.24 answers it.", "file": "cmd/mxcli/docker/oql.go ExecuteOQL / runtimeVersion / majorVersion. Test: cmd/mxcli/docker/oql_old_runtime_2341_test.go", "cause": "'Action not found' on both OQL routes was read as one cause only — the live-preview flags off — but a runtime before Mendix 10 has no such action at all.", "insight": "**An absent admin action has two causes, and the runtime can say which: ask it its version (M2EE `about`) before advising.** Only when both routes are missing is the version read; below 10 the error says OQL over the admin API needs Mendix 10+ and names the runtime's version, and points at the database; 10+ keeps the dev-flags hint (the control). Not run against a live 9.x runtime: the test replays the reporter's measured responses through a fake admin server. The issue's second ask, OQL-to-SQL over the app database for 9.x, is a feature and is not built."} diff --git a/CHANGELOG.md b/CHANGELOG.md index ea80085d98..9d273422bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +- **`mxcli oql` against a Mendix 9 runtime says why it cannot work** (ako/mxcli#2341, mendixlabs/mxcli#1879) — a 9.x runtime has no `preview_execute_oql` action, and the error sent the user to restart with live-preview flags, which cannot help. When the action is missing, the runtime's version is now read, and below Mendix 10 the error says OQL over the admin API needs 10 or later and names that version. - **`report traceability --junit` shows each requirement's test results** (ako/mxcli#2340, mendixlabs/mxcli#1877) — the results were read and then dropped, so the report was byte-identical with and without `--junit`. Each requirement now has `testResults` (name and passed, failed, error, skipped or not run), and the table marks a failing test, so "built, test failing" no longer reads as "built and proven". - **`fix widgets` and `fix design-properties` use the project's own Mendix version of `mx`** (ako/mxcli#2321) — they took the version as a preference, so on macOS an `mx` on PATH or another Studio Pro install ran, and `mx update-widgets` refused with "Project is in different Mendix version". They now take an `mx` of exactly the project's version. On Linux that version is downloaded when it is missing. On macOS and Windows, where no matching `mx` is installed, they say which version is needed and how to pass it with `--mx`. - **Parallel `mxcli exec` runs on one project no longer lose each other's writes** (ako/mxcli#2326) — measured with 8 parallel scripts: every process reported success and 2 to 6 of the 16 entities were missing (the reporter saw all of them fail instead). A script, or a `-c`, that writes now holds the project's lock (`.mxcli/write.lock`) for its whole run. A second writer waits up to 60 seconds, then changes nothing and exits 75, naming the holder. A lock left by a crashed run is taken over. Read-only runs take no lock. With the lock, all 16 entities land in every round. diff --git a/cmd/mxcli/docker/oql.go b/cmd/mxcli/docker/oql.go index f0d955cd05..fa1d0a3bfc 100644 --- a/cmd/mxcli/docker/oql.go +++ b/cmd/mxcli/docker/oql.go @@ -51,6 +51,61 @@ type OQLResult struct { // binds to 127.0.0.1 inside the container and is unreachable from DinD). // Set Direct=true to connect via HTTP directly (when the admin API is reachable). func ExecuteOQL(opts OQLOptions, query string) (*OQLResult, error) { + res, err := executeOQL(opts, query) + if err != nil && strings.Contains(strings.ToLower(err.Error()), "action not found") { + if v := runtimeVersion(m2eeOptionsFor(opts)); v != "" && majorVersion(v) > 0 && majorVersion(v) < 10 { + return nil, fmt.Errorf("OQL over the admin API needs Mendix 10 or later; this runtime is %s, which has no "+ + "preview_execute_oql action, so no flag or restart makes it answer. Read its data from its database "+ + "instead (tables are named module$entity)", v) + } + } + return res, err +} + +// m2eeOptionsFor is the admin connection ExecuteOQL uses. +func m2eeOptionsFor(opts OQLOptions) M2EEOptions { + return M2EEOptions{ + Host: opts.Host, + Port: opts.Port, + Token: opts.Token, + ProjectPath: opts.ProjectPath, + Direct: opts.Direct, + Timeout: 10 * time.Second, + } +} + +// runtimeVersion is the running runtime's version from the M2EE `about` +// action, or "" when it cannot be read. Asked only once an OQL route is +// missing: a Mendix 9 runtime has no preview_execute_oql, and the dev-flags +// advice the missing action otherwise gets cannot help there +// (ako/mxcli#2341, mendixlabs/mxcli#1879). +func runtimeVersion(opts M2EEOptions) string { + resp, err := CallM2EE(opts, "about", nil) + if err != nil || resp.M2EEError() != "" { + return "" + } + var about struct { + Version string `json:"version"` + } + if json.Unmarshal(resp.RawFeedback, &about) != nil { + return "" + } + return about.Version +} + +// majorVersion is the leading number of a version string, 0 when it has none. +func majorVersion(v string) int { + n := 0 + for _, c := range v { + if c < '0' || c > '9' { + break + } + n = n*10 + int(c-'0') + } + return n +} + +func executeOQL(opts OQLOptions, query string) (*OQLResult, error) { m2eeOpts := M2EEOptions{ Host: opts.Host, Port: opts.Port, diff --git a/cmd/mxcli/docker/oql_old_runtime_2341_test.go b/cmd/mxcli/docker/oql_old_runtime_2341_test.go new file mode 100644 index 0000000000..33a715d9be --- /dev/null +++ b/cmd/mxcli/docker/oql_old_runtime_2341_test.go @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: Apache-2.0 + +package docker + +import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// ako/mxcli#2341 (mendixlabs/mxcli#1879): a Mendix 9.24 runtime has no +// preview_execute_oql admin action, so `mxcli oql` answered "Action not found +// -- … needs the live-preview dev flags at boot …", sending the user to +// restart with flags that cannot help: the action does not exist in 9.x. When +// both routes are missing, the runtime is asked its version, and a runtime +// before Mendix 10 is named as the reason. + +func oqlServerWithVersion(t *testing.T, version string) *httptest.Server { + t.Helper() + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + var req struct { + Action string `json:"action"` + } + _ = json.Unmarshal(body, &req) + if req.Action == "about" { + _ = json.NewEncoder(w).Encode(map[string]any{"result": 0, + "feedback": map[string]any{"name": "Mendix Business Server", "version": version}}) + return + } + _ = json.NewEncoder(w).Encode(map[string]any{"result": -5, "message": "Action not found."}) + })) +} + +func TestExecuteOQL_Mendix9RuntimeSaysSo_Issue2341(t *testing.T) { + server := oqlServerWithVersion(t, "9.24.11.19193") + defer server.Close() + host, port := parseTestServerAddr(t, server.URL) + _, err := ExecuteOQL(OQLOptions{Host: host, Port: port, Token: "x", Direct: true}, "SELECT COUNT(*) AS n FROM System.Session") + if err == nil { + t.Fatal("expected an error") + } + msg := err.Error() + for _, want := range []string{"9.24.11", "Mendix 10"} { + if !strings.Contains(msg, want) { + t.Errorf("error lacks %q:\n%s", want, msg) + } + } + if strings.Contains(msg, "dev flags") || strings.Contains(msg, "upgrade mxcli") { + t.Errorf("error still sends the user to restart with flags:\n%s", msg) + } +} + +// CONTROL: on a Mendix 10+ runtime the action missing does mean the +// live-preview flags are off, and the message keeps saying so. +func TestExecuteOQL_Mendix10WithoutFlagsKeepsTheHint_Issue2341(t *testing.T) { + server := oqlServerWithVersion(t, "10.24.28.129343") + defer server.Close() + host, port := parseTestServerAddr(t, server.URL) + _, err := ExecuteOQL(OQLOptions{Host: host, Port: port, Token: "x", Direct: true}, "SELECT 1") + if err == nil || !strings.Contains(err.Error(), "live-preview dev flags") { + t.Errorf("error = %v, want the dev-flags hint", err) + } +} From dc7aaf875b7a1d8e1166bc4af2b1b9d37b950a2c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 11:52:03 +0000 Subject: [PATCH 11/13] Document JDBC drivers and DuckDB extensions in a Java action (#2314) The java-dependencies skill now has a section on a library that registers itself with the JVM: DriverManager does not see a userlib driver (instantiate the driver class), DuckDB refuses a community extension file (install it once, bundle the installed file and its .info, LOAD by name), and only a real build compiles Java. Reported, not measured here. Closes #2314 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011oK82qrr1WnXxq1eRn7qRJ --- .../skills/mendix/java-dependencies/SKILL.md | 48 ++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/.claude/skills/mendix/java-dependencies/SKILL.md b/.claude/skills/mendix/java-dependencies/SKILL.md index 45ab77c49e..5c5dcd1f6c 100644 --- a/.claude/skills/mendix/java-dependencies/SKILL.md +++ b/.claude/skills/mendix/java-dependencies/SKILL.md @@ -10,17 +10,18 @@ description: "Add, update, remove and audit a module's Maven/JAR dependencies in | lines | section | |---|---| -| 34-44 | When to Use This Skill | -| 45-56 | Listing Dependencies | -| 57-65 | Describing a Dependency | -| 66-103 | Adding a Dependency | -| 104-116 | Updating a Dependency | -| 117-131 | Managing Transitive Exclusions | -| 132-139 | Removing a Dependency | -| 140-155 | Jackson Version Gotcha | -| 156-166 | After Adding a Dependency | -| 167-177 | Coordinate Format | -| 178-181 | Related Skills | +| 35-45 | When to Use This Skill | +| 46-57 | Listing Dependencies | +| 58-66 | Describing a Dependency | +| 67-104 | Adding a Dependency | +| 105-117 | Updating a Dependency | +| 118-132 | Managing Transitive Exclusions | +| 133-140 | Removing a Dependency | +| 141-156 | Jackson Version Gotcha | +| 157-181 | A JDBC Driver or Native Library in the Runtime | +| 182-192 | After Adding a Dependency | +| 193-203 | Coordinate Format | +| 204-207 | Related Skills | # Managing Maven / JAR Dependencies @@ -153,6 +154,31 @@ Pin to the version the Mendix runtime bundles to avoid classloader conflicts: ls ~/.mxcli/mxbuild//runtime/bundles/ | grep jackson ``` +## A JDBC Driver or Native Library in the Runtime + +A library that registers itself with the JVM can be on the class path and still +not be found from a Java action. Reported building a SPARQL action on DuckDB +(`org.duckdb:duckdb_jdbc` 2.0.0.1) in Mendix 11.16 (ako/mxcli#2314; not +measured here): + +1. **`DriverManager` does not see a userlib driver.** `DriverManager.getConnection("jdbc:duckdb:")` + fails with `No suitable driver found`: the jar sits in the app's own class + loader, which `DriverManager` does not search. Instantiate the driver class + instead — `new org.duckdb.DuckDBDriver().connect("jdbc:duckdb:", new java.util.Properties())` — + the same for any JDBC driver a dependency brings. +2. **DuckDB verifies an extension file against its core signing key.** A core + extension installs from a bundled file (`INSTALL ''`); a community one + (`rdf`) is refused ("doesn't have a valid signature"), loading it by path is + refused too, and the failed `INSTALL` closes the statement. What works: run + `INSTALL rdf FROM community` once on a machine with network access, bundle the + installed `rdf.duckdb_extension` with its `.info` file, copy both into + `/v//` (e.g. `v2.0.0/linux_amd64/`) + at runtime, then `LOAD rdf` by name. +3. **Only a real build compiles Java.** `mxcli check` and `mxcli docker check` + (`mx check`) do not, so a mistake in the body shows in `mxcli test --local` or + `run --local` (`mxbuild --target=deploy`). `import com.mendix.core.Core;` is + added for you when the code uses `Core`; any other class needs its import line. + ## After Adding a Dependency MDL writes the `Projects$JarDependency` entry into the `.mpr` file. Gradle From 6741e64ba353701c91a5aa08ff441545565af373 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 14:16:35 +0000 Subject: [PATCH 12/13] =?UTF-8?q?feat(scripts):=20diag-coverage=20?= =?UTF-8?q?=E2=80=94=20which=20of=20Mendix's=20check=20diagnostics=20mxcli?= =?UTF-8?q?=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit make diag-coverage reads the diagnostic code IDs from the installed MxBuild's Mendix.Modeler.Texts.dll (plain ASCII PO resources, no monodis needed), compares them with the codes named in the string literals of non-test Go code and in the Starlark lint rules, and ranks the codes mxcli does not name by how many bug-fix records hit them. IDs only: none of Mendix's message text is read out or stored. go/scanner reads the literals, so a code in a comment or a test does not count. covered-as.tsv declares the checks that raise a code without naming it (CE0067 by MDL003, CE0844 by E009). The proposal's hand-quoted '~6%' is replaced by a pointer to the computed figure. Refs ako/mxcli#1258. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011oK82tETa46YQmMS58pQjL --- Makefile | 9 +- .../PROPOSAL_check_diagnostics_catalog.md | 19 +- scripts/diagcoverage/coverage.go | 360 ++++++++++++++++++ scripts/diagcoverage/coverage_test.go | 285 ++++++++++++++ scripts/diagcoverage/covered-as.tsv | 11 + scripts/diagcoverage/main.go | 154 ++++++++ 6 files changed, 832 insertions(+), 6 deletions(-) create mode 100644 scripts/diagcoverage/coverage.go create mode 100644 scripts/diagcoverage/coverage_test.go create mode 100644 scripts/diagcoverage/covered-as.tsv create mode 100644 scripts/diagcoverage/main.go diff --git a/Makefile b/Makefile index d988211b58..198d44bdca 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ GO_BUILD_FLAGS = -trimpath # Clean version for VS Code extension (must be valid semver: major.minor.patch) VSCE_VERSION = $(shell echo "$(VERSION)" | sed 's/^v//; s/-.*//' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$$' || echo "0.0.0") -.PHONY: build build-debug size release clean test ci-local test-mdl check-mdl check-skill-mdl check-conformance conformance-shrink gen-migration-reference check-migration-reference check-skill-pack-js check-findings check-wiki-pages digest-status check-tunnel-deps check-test-timeouts check-widget-versions test-integration test-integration-executor test-integration-roundtrip test-integration-parity test-integration-upgrade test-integration-other grammar completions sync-skills sync-skill-packs sync-commands sync-mods sync-lint-rules sync-changelog sync-all docs documentation docs-site docs-serve vscode-ext vscode-install source-tree sbom sbom-report lint lint-go lint-ts fmt fmt-check vet skill-index check-skill-index bench bench-trend +.PHONY: build build-debug size release clean test ci-local test-mdl check-mdl check-skill-mdl check-conformance conformance-shrink gen-migration-reference check-migration-reference check-skill-pack-js check-findings check-wiki-pages digest-status diag-coverage check-tunnel-deps check-test-timeouts check-widget-versions test-integration test-integration-executor test-integration-roundtrip test-integration-parity test-integration-upgrade test-integration-other grammar completions sync-skills sync-skill-packs sync-commands sync-mods sync-lint-rules sync-changelog sync-all docs documentation docs-site docs-serve vscode-ext vscode-install source-tree sbom sbom-report lint lint-go lint-ts fmt fmt-check vet skill-index check-skill-index bench bench-trend # Helper: copy file only if content differs (avoids mtime updates that invalidate go build cache) # Usage: $(call copy-if-changed,src,dst) @@ -356,6 +356,13 @@ check-skill-pack-js: done; \ [ $$found -eq 1 ] || echo " (no skill-pack JS tests)" +# Which of Mendix's consistency diagnostics (the codes mx check can raise) mxcli's check +# names, ranked by how often bug reports hit the ones it does not. Needs a MxBuild +# installed (`mxcli setup mxbuild -p app.mpr`); reads code IDs only. Advisory. +# make diag-coverage ARGS="--top 50" make diag-coverage ARGS=--all +diag-coverage: + @go run ./scripts/diagcoverage $(ARGS) + # How far docs-wiki/bug-patterns/ has fallen behind the findings it digests. # Advisory, always exits 0 — see the header in the script for why. digest-status: diff --git a/docs/11-proposals/PROPOSAL_check_diagnostics_catalog.md b/docs/11-proposals/PROPOSAL_check_diagnostics_catalog.md index e0a99f6882..81c6bbabb3 100644 --- a/docs/11-proposals/PROPOSAL_check_diagnostics_catalog.md +++ b/docs/11-proposals/PROPOSAL_check_diagnostics_catalog.md @@ -60,9 +60,14 @@ Present identically in 11.6.3 and 11.12.1. Subsystem split (by source file): Microflows 364, Domain model 275, Pages/Widgets 248, Integration/REST/OData 129, Security 29, Workflows 15, Navigation 7. -**mxcli already does ~6% of this by hand.** The source references **~77–81 distinct -`CE`/`CW` codes** across ~24 `validate_*.go` files and ~30 lint rules — proof the approach -works; the catalog just turns "port as we hit them" into an ordered backlog of ~1240 left. +**mxcli already names a share of this by hand, and that share is computed, not quoted.** +`make diag-coverage` (`scripts/diagcoverage/`) reads the code IDs from the installed MxBuild's +`Mendix.Modeler.Texts.dll`, compares them with the codes named in mxcli's message strings and +lint rules, and ranks the codes it does not name by how many bug-fix records +(`.claude/skills/fix-issue/findings/`) hit them. A figure written here would be stale the next +time anyone adds a check. "Named" is a proxy for "covered": a check that does not name its +code is declared in `scripts/diagcoverage/covered-as.tsv`, with the fixture that shows it. +The approach works; the catalog turns "port as we hit them" into an ordered backlog. **Is there a cheaper Mendix-native shortcut?** Not established. `mx check` (consistency only, no build) was measured **once, at ~22 s** (project size and warm/cold state not @@ -190,6 +195,7 @@ button, or a URL."* | File | Change | |------|--------| +| `scripts/diagcoverage/` | New (done): `make diag-coverage`, per-code coverage from the installed MxBuild, IDs only, ranked by bug-record frequency. No monodis. | | `scripts/extract-diagnostics-catalog.sh` | New: the reproducible extractor (stream DLL → `monodis --mresources` → parse+tier). Committed here. | | `sdk/versions/diagnostics-{version}.json` | Generated catalog data asset per version (IP decision pending). | | `mdl/linter/rules/`, `mdl/executor/validate_*.go` | Port Tier-A codes, prioritised by frequency; cite the `CE`/`CW` code in each. | @@ -220,8 +226,11 @@ button, or a URL."* our own paraphrased messages**; (b) generate the full catalog **locally at build time** from the user's own licensed mxbuild (never committed); (c) confirm redistribution is permitted. Default: **(b)** — commit the extractor, not the corpus. -2. **`monodis` dependency.** The extractor needs `mono-utils`. Alternatives: `ilspycmd` - (dotnet tool) or a small pure-Go PE manifest-resource reader to drop the mono dep. +2. **`monodis` dependency.** The message extractor needs `mono-utils`. Alternatives: + `ilspycmd` (dotnet tool) or a small pure-Go PE manifest-resource reader to drop the mono + dep. The code IDs alone need none of it: the PO resources are plain ASCII in the DLL, so + `scripts/diagcoverage` finds them by scanning the bytes. Only the message text and the + tiering still need the extractor. 3. **Tiering is heuristic.** The A/B/C/D split is keyword+subsystem-based; the real per-rule trigger predicate lives in Mendix's `.cs` (the `LOCATION` breadcrumb) and must be reverse-engineered per rule. The catalog gives the *target + message*, not the diff --git a/scripts/diagcoverage/coverage.go b/scripts/diagcoverage/coverage.go new file mode 100644 index 0000000000..99eade64ef --- /dev/null +++ b/scripts/diagcoverage/coverage.go @@ -0,0 +1,360 @@ +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "go/scanner" + "go/token" + "io" + "io/fs" + "os" + "path/filepath" + "regexp" + "sort" + "strconv" + "strings" +) + +// codeRE is a Mendix consistency-diagnostic code: CE (error), CW (warning) or CI +// (deprecation / information) and four digits. +var codeRE = regexp.MustCompile(`C[EWI][0-9]{4}`) + +// CatalogCodes returns every diagnostic code that appears in Mendix's text +// resources (Mendix.Modeler.Texts.dll), sorted and unique. +// +// The resources are PO files embedded as plain ASCII, one per locale, so the IDs +// are found by scanning the bytes: no monodis, no .NET. Only the IDs are kept. The +// message text next to each code is Mendix's, and the proposal that motivates this +// (docs/11-proposals/PROPOSAL_check_diagnostics_catalog.md) is explicit that it is +// not ours to copy into the repository. +func CatalogCodes(dll []byte) []string { + seen := map[string]bool{} + for _, m := range codeRE.FindAll(dll, -1) { + seen[string(m)] = true + } + return sortedKeys(seen) +} + +// sourceRoots are the directories whose code can name a diagnostic to a user. +var sourceRoots = []string{"mdl", "modelsdk", "cmd", "sdk", "api"} + +var skipDirs = map[string]bool{"testdata": true, "node_modules": true, "vendor": true, ".git": true} + +// maxFilesPerCode caps how many files are remembered for a code, for the report. +const maxFilesPerCode = 3 + +// SourceCodes returns the codes mxcli NAMES, each with up to three of the files that +// name it: a code that appears inside a string literal of non-test Go code, or +// anywhere in a Starlark lint rule. +// +// Naming is a proxy for covering, not the same thing, and it errs both ways: +// a check that does not name its code (MDL003 raises what MxBuild calls CE0067) is +// missed here, which is what covered-as.tsv is for; and a code named in a message +// may be only partly detected. Comments, tests and docs are deliberately not read — +// a code in a comment is a note about a measurement, not a diagnostic a user sees. +// go/scanner reads string literals and skips comments, so the two cannot be mixed up. +func SourceCodes(repo string) (map[string][]string, error) { + out := map[string][]string{} + add := func(code, path string) { + files := out[code] + for _, f := range files { + if f == path { + return + } + } + if len(files) < maxFilesPerCode { + out[code] = append(files, path) + } + } + for _, root := range sourceRoots { + dir := filepath.Join(repo, root) + if _, err := os.Stat(dir); err != nil { + continue + } + err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() { + if skipDirs[d.Name()] { + return filepath.SkipDir + } + return nil + } + rel, _ := filepath.Rel(repo, path) + switch { + case strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "_test.go"): + src, err := os.ReadFile(path) + if err != nil { + return err + } + for _, c := range stringLiteralCodes(path, src) { + add(c, rel) + } + case strings.HasSuffix(path, ".star"): + src, err := os.ReadFile(path) + if err != nil { + return err + } + for _, c := range codeRE.FindAllString(string(src), -1) { + add(c, rel) + } + } + return nil + }) + if err != nil { + return nil, err + } + } + return out, nil +} + +// stringLiteralCodes returns the diagnostic codes inside the string literals of one +// Go file. A file that does not scan cleanly still yields what it could. +func stringLiteralCodes(path string, src []byte) []string { + fset := token.NewFileSet() + file := fset.AddFile(path, fset.Base(), len(src)) + var s scanner.Scanner + s.Init(file, src, nil, 0) // mode 0: comments are skipped + var out []string + for { + _, tok, lit := s.Scan() + if tok == token.EOF { + break + } + if tok == token.STRING { + out = append(out, codeRE.FindAllString(lit, -1)...) + } + } + return out +} + +// FindingCounts counts, per code, the finding records that name it in their `ce` +// field — the diagnostics real bug reports have hit. It returns the counts and how +// many finding files it read. A finding file holds one JSON object (older shards hold +// several, one per line); both read. +func FindingCounts(dir string) (map[string]int, int, error) { + counts := map[string]int{} + files := 0 + err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() || !(strings.HasSuffix(path, ".json") || strings.HasSuffix(path, ".jsonl")) { + return nil + } + f, err := os.Open(path) + if err != nil { + return err + } + defer f.Close() + files++ + dec := json.NewDecoder(f) + for { + var rec struct { + CE []any `json:"ce"` + } + if err := dec.Decode(&rec); err == io.EOF { + break + } else if err != nil { + break // a record that does not decode is not a reason to stop counting + } + seen := map[string]bool{} + for _, e := range rec.CE { + for _, c := range codeRE.FindAllString(fmt.Sprint(e), -1) { + seen[c] = true // once per record, however often it repeats + } + } + for c := range seen { + counts[c]++ + } + } + return nil + }) + return counts, files, err +} + +// exactCodeRE matches a whole line field that is a code and nothing else. +var exactCodeRE = regexp.MustCompile(`^C[EWI][0-9]{4}$`) + +// LoadCoveredAs reads the codes mxcli covers under another name: a check that +// raises what MxBuild reports under this code but does not say so in its message. +// Lines are `CODEcovered bynote`; blank lines and #-comments are skipped. +func LoadCoveredAs(path string) (map[string]string, error) { + f, err := os.Open(path) + if err != nil { + if os.IsNotExist(err) { + return map[string]string{}, nil + } + return nil, err + } + defer f.Close() + out := map[string]string{} + sc := bufio.NewScanner(f) + for n := 1; sc.Scan(); n++ { + line := strings.TrimSpace(sc.Text()) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + cols := strings.Split(line, "\t") + if len(cols) < 2 || !exactCodeRE.MatchString(cols[0]) || strings.TrimSpace(cols[1]) == "" { + return nil, fmt.Errorf("%s:%d: want `CODEcovered by[note]`, got %q", path, n, line) + } + out[cols[0]] = strings.TrimSpace(cols[1]) + } + return out, sc.Err() +} + +// Status of a catalog code. +const ( + StatusNamed = "named" // a message string or lint rule of mxcli names it + StatusCoveredAs = "covered-as" // covered under another name (covered-as.tsv) + StatusNotNamed = "not-named" +) + +// Gap is a code real bug reports have hit that mxcli does not name. +type Gap struct { + Code string `json:"code"` + Findings int `json:"findings"` +} + +// Report is the coverage of the catalog. +type Report struct { + Version string `json:"mxbuildVersion,omitempty"` + Catalog int `json:"catalog"` + ByPrefix map[string]int `json:"byPrefix"` + Named int `json:"named"` + CoveredAs int `json:"coveredAs"` + NotNamed int `json:"notNamed"` + NamedNotIn []string `json:"namedButNotInCatalog,omitempty"` + FindingFiles int `json:"findingFiles"` + FindingCodes int `json:"findingCodes"` + FindingsOff []string `json:"findingCodesNotInCatalog,omitempty"` + Gaps []Gap `json:"fieldGaps"` + Status map[string]string `json:"-"` + FindingCounts map[string]int `json:"-"` +} + +// Build classifies every catalog code and ranks the field-observed gaps by how many +// finding records name them (ties broken by code, so the order is stable). +func Build(catalog []string, named map[string][]string, coveredAs map[string]string, findings map[string]int, findingFiles int, version string) *Report { + r := &Report{ + Version: version, + Catalog: len(catalog), + ByPrefix: map[string]int{}, + FindingFiles: findingFiles, + FindingCodes: len(findings), + Status: map[string]string{}, + FindingCounts: findings, + } + inCatalog := map[string]bool{} + for _, c := range catalog { + inCatalog[c] = true + r.ByPrefix[c[:2]]++ + switch { + case len(named[c]) > 0: + r.Status[c] = StatusNamed + r.Named++ + case coveredAs[c] != "": + r.Status[c] = StatusCoveredAs + r.CoveredAs++ + default: + r.Status[c] = StatusNotNamed + r.NotNamed++ + } + } + for c := range named { + if !inCatalog[c] { + r.NamedNotIn = append(r.NamedNotIn, c) + } + } + sort.Strings(r.NamedNotIn) + for c, n := range findings { + switch { + case !inCatalog[c]: + r.FindingsOff = append(r.FindingsOff, c) + case r.Status[c] == StatusNotNamed: + r.Gaps = append(r.Gaps, Gap{Code: c, Findings: n}) + } + } + sort.Strings(r.FindingsOff) + sort.Slice(r.Gaps, func(i, j int) bool { + if r.Gaps[i].Findings != r.Gaps[j].Findings { + return r.Gaps[i].Findings > r.Gaps[j].Findings + } + return r.Gaps[i].Code < r.Gaps[j].Code + }) + return r +} + +// versionPattern pulls the MxBuild version out of .../mxbuild//modeler/.... +var versionPattern = regexp.MustCompile(`mxbuild[/\\]([0-9][0-9.]*)[/\\]modeler`) + +// textsDLL is the resource assembly, relative to a MxBuild's modeler directory. +const textsDLL = "Mendix.Modeler.Texts.dll" + +// ResolveDLL finds the text assembly to read: an explicit --dll, the --version +// MxBuild that `mxcli setup mxbuild` installed under ~/.mxcli/mxbuild, or the newest +// one installed. The version is "" when the path does not say. +func ResolveDLL(dll, version, home string) (path, ver string, err error) { + if dll != "" { + if _, err := os.Stat(dll); err != nil { + return "", "", err + } + return dll, versionOf(dll), nil + } + base := filepath.Join(home, ".mxcli", "mxbuild") + if version != "" { + p := filepath.Join(base, version, "modeler", textsDLL) + if _, err := os.Stat(p); err != nil { + return "", "", fmt.Errorf("no %s for MxBuild %s under %s — run `mxcli setup mxbuild --version %s`", textsDLL, version, base, version) + } + return p, version, nil + } + entries, _ := os.ReadDir(base) + var versions []string + for _, e := range entries { + if e.IsDir() { + if _, err := os.Stat(filepath.Join(base, e.Name(), "modeler", textsDLL)); err == nil { + versions = append(versions, e.Name()) + } + } + } + if len(versions) == 0 { + return "", "", fmt.Errorf("no MxBuild with %s under %s — run `mxcli setup mxbuild -p ` (or pass --dll)", textsDLL, base) + } + sort.Slice(versions, func(i, j int) bool { return versionLess(versions[j], versions[i]) }) // newest first + return filepath.Join(base, versions[0], "modeler", textsDLL), versions[0], nil +} + +func versionOf(path string) string { + if m := versionPattern.FindStringSubmatch(path); m != nil { + return m[1] + } + return "" +} + +// versionLess orders dotted version numbers numerically (11.9.0 < 11.16.1). +func versionLess(a, b string) bool { + pa, pb := strings.Split(a, "."), strings.Split(b, ".") + for i := 0; i < len(pa) && i < len(pb); i++ { + x, _ := strconv.Atoi(pa[i]) + y, _ := strconv.Atoi(pb[i]) + if x != y { + return x < y + } + } + return len(pa) < len(pb) +} + +func sortedKeys(m map[string]bool) []string { + out := make([]string, 0, len(m)) + for k := range m { + out = append(out, k) + } + sort.Strings(out) + return out +} diff --git a/scripts/diagcoverage/coverage_test.go b/scripts/diagcoverage/coverage_test.go new file mode 100644 index 0000000000..728ce551a8 --- /dev/null +++ b/scripts/diagcoverage/coverage_test.go @@ -0,0 +1,285 @@ +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "bytes" + "encoding/json" + "os" + "path/filepath" + "reflect" + "strings" + "testing" +) + +func write(t *testing.T, path, content string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { + t.Fatal(err) + } +} + +func TestCatalogCodes(t *testing.T) { + // Codes sit in the middle of unrelated bytes, repeat (one PO per locale) and one + // near-miss is not a code. + dll := []byte("MZ\x00\x01msgctxt \"CE0149\"\x00\xffCW0007 and CE0149 again\x00CI0001\x00CE12 CX0001 ce0001 CE00012") + got := CatalogCodes(dll) + want := []string{"CE0001", "CE0149", "CI0001", "CW0007"} + // CE00012 holds CE0001 as a prefix: a code is four digits, and what follows is not + // checked, which is why the catalog is read from PO text and not from arbitrary data. + if !reflect.DeepEqual(got, want) { + t.Errorf("CatalogCodes = %v, want %v", got, want) + } +} + +func TestStringLiteralCodes(t *testing.T) { + src := "package p\n" + + "// CE0001 in a line comment\n" + + "/* CE0002 in a block comment */\n" + + "var a = \"fails CE0003 at build\"\n" + + "var b = `raw CE0004 and CW0005`\n" + + "func f() { _ = \"x\" + \"CE0006\" } // trailing CE0007\n" + + "const CE0008 = 1\n" + got := stringLiteralCodes("p.go", []byte(src)) + want := []string{"CE0003", "CE0004", "CW0005", "CE0006"} + if !reflect.DeepEqual(got, want) { + t.Errorf("stringLiteralCodes = %v, want %v (comments and identifiers are not diagnostics)", got, want) + } +} + +func TestSourceCodes(t *testing.T) { + repo := t.TempDir() + write(t, filepath.Join(repo, "mdl/executor/a.go"), "package executor\nvar m = \"CE0111 duplicate\"\n") + write(t, filepath.Join(repo, "mdl/executor/a_test.go"), "package executor\nvar m = \"CE0222 only in a test\"\n") + write(t, filepath.Join(repo, "mdl/executor/b.go"), "package executor\n// CE0333 is only a note\nvar x = 1\n") + write(t, filepath.Join(repo, "cmd/mxcli/lint-rules/r.star"), "# rule\nMSG = 'CE0444 something'\n") + write(t, filepath.Join(repo, "mdl/testdata/skip.go"), "package p\nvar m = \"CE0555\"\n") + write(t, filepath.Join(repo, "docs/not-a-root.go"), "package p\nvar m = \"CE0666\"\n") + for _, f := range []string{"c1", "c2", "c3", "c4", "c5"} { // more files than are remembered + write(t, filepath.Join(repo, "sdk", f+".go"), "package sdk\nvar m = \"CE0777\"\n") + } + got, err := SourceCodes(repo) + if err != nil { + t.Fatal(err) + } + for _, c := range []string{"CE0111", "CE0444", "CE0777"} { + if len(got[c]) == 0 { + t.Errorf("%s is named by the source and was not found", c) + } + } + for _, c := range []string{"CE0222", "CE0333", "CE0555", "CE0666"} { + if len(got[c]) != 0 { + t.Errorf("%s must not count: test file, comment, testdata and non-root files are not read (%v)", c, got[c]) + } + } + if n := len(got["CE0777"]); n != maxFilesPerCode { + t.Errorf("CE0777 remembers %d files, want the cap of %d", n, maxFilesPerCode) + } +} + +func TestFindingCounts(t *testing.T) { + dir := t.TempDir() + write(t, filepath.Join(dir, "a/one.json"), `{"ce": ["CE0117", "CE0117 again", "CE1613"], "symptom": "x"}`+"\n") + write(t, filepath.Join(dir, "a/two.json"), `{"ce": ["CE0117"]}`+"\n") + write(t, filepath.Join(dir, "b/old.jsonl"), `{"ce": ["CE0001"]}`+"\n"+`{"ce": ["CE0001", "CE0002"]}`+"\n") + write(t, filepath.Join(dir, "b/none.json"), `{"ce": []}`+"\n") + write(t, filepath.Join(dir, "b/bad.json"), `not json`+"\n") + write(t, filepath.Join(dir, "README.md"), "CE9999 is not a record\n") + counts, files, err := FindingCounts(dir) + if err != nil { + t.Fatal(err) + } + want := map[string]int{"CE0117": 2, "CE1613": 1, "CE0001": 2, "CE0002": 1} + if !reflect.DeepEqual(counts, want) { + t.Errorf("counts = %v, want %v (a record names a code once however often it repeats)", counts, want) + } + if files != 5 { + t.Errorf("files read = %d, want 5 (.json and .jsonl only)", files) + } +} + +func TestLoadCoveredAs(t *testing.T) { + dir := t.TempDir() + good := filepath.Join(dir, "good.tsv") + write(t, good, "# comment\n\nCE0067\tMDL003\tmissing return\nCE0844\tE009\n") + got, err := LoadCoveredAs(good) + if err != nil { + t.Fatal(err) + } + if want := map[string]string{"CE0067": "MDL003", "CE0844": "E009"}; !reflect.DeepEqual(got, want) { + t.Errorf("LoadCoveredAs = %v, want %v", got, want) + } + bad := filepath.Join(dir, "bad.tsv") + write(t, bad, "CE67\tMDL003\n") + if _, err := LoadCoveredAs(bad); err == nil || !strings.Contains(err.Error(), "bad.tsv:1") { + t.Errorf("a malformed line must be refused with its location, got %v", err) + } + missing, err := LoadCoveredAs(filepath.Join(dir, "nope.tsv")) + if err != nil || len(missing) != 0 { + t.Errorf("a missing file is an empty mapping, got %v, %v", missing, err) + } +} + +func TestBuild(t *testing.T) { + catalog := []string{"CE0001", "CE0002", "CE0003", "CE0004", "CE0005", "CW0006", "CI0007"} + named := map[string][]string{"CE0001": {"a.go"}, "CE9999": {"b.go"}} // CE9999: not in the catalog + covered := map[string]string{"CE0002": "MDL003"} + findings := map[string]int{ + "CE0001": 50, // named: not a gap however often it was hit + "CE0002": 40, // covered-as: not a gap + "CE0004": 5, "CE0003": 5, "CE0005": 9, // gaps: by count, ties by code + "CE8888": 3, // not in the catalog + } + r := Build(catalog, named, covered, findings, 7, "11.16.1") + if r.Catalog != 7 || r.Named != 1 || r.CoveredAs != 1 || r.NotNamed != 5 { + t.Errorf("counts = catalog %d named %d covered %d not-named %d", r.Catalog, r.Named, r.CoveredAs, r.NotNamed) + } + if r.ByPrefix["CE"] != 5 || r.ByPrefix["CW"] != 1 || r.ByPrefix["CI"] != 1 { + t.Errorf("ByPrefix = %v", r.ByPrefix) + } + want := []Gap{{"CE0005", 9}, {"CE0003", 5}, {"CE0004", 5}} + if !reflect.DeepEqual(r.Gaps, want) { + t.Errorf("Gaps = %v, want %v", r.Gaps, want) + } + if !reflect.DeepEqual(r.NamedNotIn, []string{"CE9999"}) || !reflect.DeepEqual(r.FindingsOff, []string{"CE8888"}) { + t.Errorf("not-in-catalog = %v / %v", r.NamedNotIn, r.FindingsOff) + } + if r.Status["CE0001"] != StatusNamed || r.Status["CE0002"] != StatusCoveredAs || r.Status["CE0003"] != StatusNotNamed { + t.Errorf("Status = %v", r.Status) + } +} + +func TestVersionLessIsNumeric(t *testing.T) { + // Lexically "11.9.0" > "11.16.1"; as versions it is older. + for _, c := range []struct { + a, b string + less bool + }{{"11.9.0", "11.16.1", true}, {"11.16.1", "11.9.0", false}, {"11.14.0", "11.14.0", false}, {"10.24.0", "11.0.0", true}, {"11.1", "11.1.1", true}} { + if got := versionLess(c.a, c.b); got != c.less { + t.Errorf("versionLess(%q, %q) = %v, want %v", c.a, c.b, got, c.less) + } + } +} + +func fakeHome(t *testing.T, versions ...string) string { + t.Helper() + home := t.TempDir() + for _, v := range versions { + write(t, filepath.Join(home, ".mxcli/mxbuild", v, "modeler", textsDLL), "MZ CE0001 CE0002 CW0003\n") + } + return home +} + +func TestResolveDLL(t *testing.T) { + home := fakeHome(t, "11.9.0", "11.16.1", "11.14.0") + p, v, err := ResolveDLL("", "", home) + if err != nil || v != "11.16.1" || !strings.Contains(p, "11.16.1") { + t.Errorf("newest = %q %q %v, want 11.16.1", p, v, err) + } + if _, v, err := ResolveDLL("", "11.14.0", home); err != nil || v != "11.14.0" { + t.Errorf("explicit version = %q %v", v, err) + } + if _, _, err := ResolveDLL("", "10.1.0", home); err == nil || !strings.Contains(err.Error(), "mxcli setup mxbuild") { + t.Errorf("a missing version must say how to install it, got %v", err) + } + if _, _, err := ResolveDLL("", "", t.TempDir()); err == nil || !strings.Contains(err.Error(), "mxcli setup mxbuild") { + t.Errorf("no MxBuild at all must say how to install one, got %v", err) + } + explicit := filepath.Join(home, ".mxcli/mxbuild/11.9.0/modeler", textsDLL) + if p, v, err := ResolveDLL(explicit, "", home); err != nil || p != explicit || v != "11.9.0" { + t.Errorf("explicit --dll = %q %q %v", p, v, err) + } +} + +// fakeRepo is a repository root with one named code, one finding-only gap and one +// covered-as code, against a DLL that holds those and an unrelated one. +func fakeRepo(t *testing.T) string { + t.Helper() + repo := t.TempDir() + write(t, filepath.Join(repo, "go.mod"), "module x\n") + write(t, filepath.Join(repo, "mdl/a.go"), "package mdl\nvar m = \"CE0001\"\n") + write(t, filepath.Join(repo, ".claude/skills/fix-issue/findings/a/f.json"), `{"ce":["CE0002","CE0001"]}`+"\n") + write(t, filepath.Join(repo, "scripts/diagcoverage/covered-as.tsv"), "CE0003\tMDL003\tnote\n") + return repo +} + +func TestRunReport(t *testing.T) { + t.Setenv("HOME", fakeHome(t, "11.16.1")) + repo := fakeRepo(t) + var out, errOut bytes.Buffer + if err := run([]string{"--repo", repo}, &out, &errOut); err != nil { + t.Fatal(err, errOut.String()) + } + text := out.String() + for _, want := range []string{"MxBuild 11.16.1", "catalog", "3 codes", "CE0002", "named is a proxy"} { + if !strings.Contains(text, want) { + t.Errorf("report lacks %q:\n%s", want, text) + } + } + // CE0001 is named and CE0003 covered-as, so neither is a gap; CE0002 is. + gaps := text[strings.Index(text, "by how many records"):] + if strings.Contains(gaps, "CE0001 ") || strings.Contains(gaps, "CE0003 ") || !strings.Contains(gaps, "CE0002") { + t.Errorf("gap list wrong:\n%s", gaps) + } +} + +func TestRunAllAndJSON(t *testing.T) { + t.Setenv("HOME", fakeHome(t, "11.16.1")) + repo := fakeRepo(t) + var out bytes.Buffer + if err := run([]string{"--repo", repo, "--all"}, &out, os.Stderr); err != nil { + t.Fatal(err) + } + want := "code\tstatus\tfindings\nCE0001\tnamed\t1\nCE0002\tnot-named\t1\nCW0003\tnot-named\t0\n" + // CW0003 is in the fake DLL; CE0003 is not, so it is not listed. + if out.String() != want { + t.Errorf("--all =\n%q\nwant\n%q", out.String(), want) + } + out.Reset() + if err := run([]string{"--repo", repo, "--json"}, &out, os.Stderr); err != nil { + t.Fatal(err) + } + var r Report + if err := json.Unmarshal(out.Bytes(), &r); err != nil { + t.Fatalf("--json is not JSON: %v", err) + } + if r.Catalog != 3 || r.Named != 1 || len(r.Gaps) != 1 || r.Gaps[0].Code != "CE0002" { + t.Errorf("json report = %+v", r) + } +} + +func TestRunRefusesANonRepo(t *testing.T) { + t.Setenv("HOME", fakeHome(t, "11.16.1")) + err := run([]string{"--repo", t.TempDir()}, &bytes.Buffer{}, &bytes.Buffer{}) + if err == nil || !strings.Contains(err.Error(), "go.mod") { + t.Errorf("a directory that is not the repository must be refused, got %v", err) + } +} + +// The real repository, as a smoke test: the scan finds a sensible number of codes and +// does not fail on any file in it. The exact number is not asserted, because it +// changes with every check anyone adds. +func TestSourceCodesOnThisRepository(t *testing.T) { + root, err := filepath.Abs("../..") + if err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(root, "go.mod")); err != nil { + t.Skip("not run from inside the repository") + } + got, err := SourceCodes(root) + if err != nil { + t.Fatal(err) + } + if len(got) < 100 { + t.Errorf("only %d codes named by this repository's source; the scan is broken or the checks went", len(got)) + } + for _, c := range []string{"CE0111", "CE0117"} { // long-standing checks + if len(got[c]) == 0 { + t.Errorf("%s is not found in this repository's source", c) + } + } +} diff --git a/scripts/diagcoverage/covered-as.tsv b/scripts/diagcoverage/covered-as.tsv new file mode 100644 index 0000000000..4645cb89d5 --- /dev/null +++ b/scripts/diagcoverage/covered-as.tsv @@ -0,0 +1,11 @@ +# Codes mxcli covers under another name: a check that raises what MxBuild reports +# under this code but does not say so in its message, so diag-coverage cannot see it. +# Each line is `CODEcovered bynote`. Add one only with evidence: a measured +# fixture that MxBuild rejects with this code and mxcli rejects too. +# +# CE0067 "The 'Return value' property is required": a flow with a return type and no +# `as $R` whose path runs off the end (measured on MxBuild 11.16.1). +CE0067 MDL003 missing return on a non-void flow; the message names no CE code +# CE0844 "The expression is of type X but should be of type Y" is Mendix CLI 2's name +# for what MxBuild 11.16.1 reports as CE0117: a declare, set or return of the wrong type. +CE0844 E009 declare/set/return type check (checkTargetKind); MxBuild reports it as CE0117 diff --git a/scripts/diagcoverage/main.go b/scripts/diagcoverage/main.go new file mode 100644 index 0000000000..dcbfb1c901 --- /dev/null +++ b/scripts/diagcoverage/main.go @@ -0,0 +1,154 @@ +// SPDX-License-Identifier: Apache-2.0 + +// diag-coverage reports which of the diagnostics Mendix's own consistency check can +// raise (`mx check`, Studio Pro's Errors pane) mxcli's `check` names, ranked by how +// often real bug reports have hit the ones it does not. +// +// go run ./scripts/diagcoverage # newest MxBuild installed +// go run ./scripts/diagcoverage --version 11.16.1 --top 50 +// go run ./scripts/diagcoverage --all > codes.tsv # every catalog code, one line each +// make diag-coverage ARGS="--top 50" +// +// It needs a MxBuild installed (`mxcli setup mxbuild -p app.mpr`) for its text +// resource, and reads only the code IDs from it. Nothing of Mendix's message text is +// printed or stored. See docs/11-proposals/PROPOSAL_check_diagnostics_catalog.md. +// +// "Named" is a proxy, not a proof of coverage; see SourceCodes. The number is +// computed, never quoted: a figure copied into prose is stale the next time anyone +// adds a check. +package main + +import ( + "encoding/json" + "flag" + "fmt" + "io" + "os" + "path/filepath" + "sort" + "strings" +) + +func main() { + if err := run(os.Args[1:], os.Stdout, os.Stderr); err != nil { + fmt.Fprintln(os.Stderr, "diag-coverage:", err) + os.Exit(1) + } +} + +func run(args []string, stdout, stderr io.Writer) error { + fs := flag.NewFlagSet("diag-coverage", flag.ContinueOnError) + fs.SetOutput(stderr) + dll := fs.String("dll", "", "path to Mendix.Modeler.Texts.dll (default: from --version or the newest MxBuild installed)") + version := fs.String("version", "", "MxBuild version installed under ~/.mxcli/mxbuild (default: newest)") + repo := fs.String("repo", ".", "mxcli repository root") + findings := fs.String("findings", "", "finding records directory (default: /.claude/skills/fix-issue/findings)") + coveredAs := fs.String("covered-as", "", "codes covered under another name (default: /scripts/diagcoverage/covered-as.tsv)") + top := fs.Int("top", 30, "how many field-observed gaps to list (0 = all)") + all := fs.Bool("all", false, "print every catalog code as TSV: code, status, findings") + asJSON := fs.Bool("json", false, "print the report as JSON") + if err := fs.Parse(args); err != nil { + return err + } + + if _, err := os.Stat(filepath.Join(*repo, "go.mod")); err != nil { + return fmt.Errorf("%s does not look like the mxcli repository (no go.mod); pass --repo", *repo) + } + if *findings == "" { + *findings = filepath.Join(*repo, ".claude", "skills", "fix-issue", "findings") + } + if *coveredAs == "" { + *coveredAs = filepath.Join(*repo, "scripts", "diagcoverage", "covered-as.tsv") + } + home, err := os.UserHomeDir() + if err != nil { + return err + } + dllPath, ver, err := ResolveDLL(*dll, *version, home) + if err != nil { + return err + } + raw, err := os.ReadFile(dllPath) + if err != nil { + return err + } + named, err := SourceCodes(*repo) + if err != nil { + return err + } + counts, files, err := FindingCounts(*findings) + if err != nil { + return err + } + extras, err := LoadCoveredAs(*coveredAs) + if err != nil { + return err + } + r := Build(CatalogCodes(raw), named, extras, counts, files, ver) + + switch { + case *asJSON: + enc := json.NewEncoder(stdout) + enc.SetIndent("", " ") + return enc.Encode(r) + case *all: + printAll(stdout, r) + default: + printReport(stdout, r, *top) + } + return nil +} + +func printAll(w io.Writer, r *Report) { + codes := make([]string, 0, len(r.Status)) + for c := range r.Status { + codes = append(codes, c) + } + sort.Strings(codes) + fmt.Fprintln(w, "code\tstatus\tfindings") + for _, c := range codes { + fmt.Fprintf(w, "%s\t%s\t%d\n", c, r.Status[c], r.FindingCounts[c]) + } +} + +func printReport(w io.Writer, r *Report, top int) { + src := "Mendix.Modeler.Texts.dll" + if r.Version != "" { + src += " of MxBuild " + r.Version + } + fmt.Fprintf(w, "diagnostics coverage — %s\n\n", src) + fmt.Fprintf(w, " catalog %5d codes (CE %d, CW %d, CI %d)\n", r.Catalog, r.ByPrefix["CE"], r.ByPrefix["CW"], r.ByPrefix["CI"]) + fmt.Fprintf(w, " named %5d %.1f%% named by a message string of non-test Go code or a lint rule\n", r.Named, pct(r.Named, r.Catalog)) + fmt.Fprintf(w, " covered-as %5d covered under another name (scripts/diagcoverage/covered-as.tsv)\n", r.CoveredAs) + fmt.Fprintf(w, " not named %5d\n", r.NotNamed) + if len(r.NamedNotIn) > 0 { + fmt.Fprintf(w, "\n named but not in this catalog (another version's code, or a typo): %s\n", strings.Join(r.NamedNotIn, " ")) + } + fmt.Fprintf(w, "\nfield-observed codes mxcli does not name — %d finding records name %d distinct codes;\n", r.FindingFiles, r.FindingCodes) + fmt.Fprintf(w, "%d of them are in this catalog and not named, by how many records name each:\n\n", len(r.Gaps)) + n := len(r.Gaps) + if top > 0 && top < n { + n = top + } + for _, g := range r.Gaps[:n] { + fmt.Fprintf(w, " %s %4d\n", g.Code, g.Findings) + } + if n < len(r.Gaps) { + fmt.Fprintf(w, " … %d more (--top 0 for all)\n", len(r.Gaps)-n) + } + if len(r.FindingsOff) > 0 { + fmt.Fprintf(w, "\n %d codes in the finding records are not in this catalog (another MxBuild version?): %s\n", len(r.FindingsOff), strings.Join(r.FindingsOff, " ")) + } + fmt.Fprintln(w, "\nnamed is a proxy for covered: a check that does not name its code shows as a gap (add it to covered-as.tsv),") + fmt.Fprintln(w, "and a code named in a message may be only partly detected. --all lists every code.") + fmt.Fprintln(w, "The finding records are fixes, so they over-represent codes mxcli already learned about: a code") + fmt.Fprintln(w, "nobody reported is invisible in the ranking. To find those, run broken scripts through mx check") + fmt.Fprintln(w, "(mdl-examples/check-gap/run-parity.sh) and add what it catches that check does not.") +} + +func pct(n, d int) float64 { + if d == 0 { + return 0 + } + return 100 * float64(n) / float64(d) +} From d1c88769a51bcd841f7d6d16f5028dd2f870b3a6 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 14:29:52 +0000 Subject: [PATCH 13/13] test(diagcoverage): isolate the home directory with testutil.SetHome A bare Setenv("HOME") leaves the developer's real ~/.mxcli in use on Windows, where os.UserHomeDir reads USERPROFILE (#1256). TestNoBareHomeSetenv caught it. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011oK82tETa46YQmMS58pQjL --- scripts/diagcoverage/coverage_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/diagcoverage/coverage_test.go b/scripts/diagcoverage/coverage_test.go index 728ce551a8..f515685a85 100644 --- a/scripts/diagcoverage/coverage_test.go +++ b/scripts/diagcoverage/coverage_test.go @@ -10,6 +10,8 @@ import ( "reflect" "strings" "testing" + + "github.com/mendixlabs/mxcli/internal/testutil" ) func write(t *testing.T, path, content string) { @@ -207,7 +209,7 @@ func fakeRepo(t *testing.T) string { } func TestRunReport(t *testing.T) { - t.Setenv("HOME", fakeHome(t, "11.16.1")) + testutil.SetHome(t, fakeHome(t, "11.16.1")) repo := fakeRepo(t) var out, errOut bytes.Buffer if err := run([]string{"--repo", repo}, &out, &errOut); err != nil { @@ -227,7 +229,7 @@ func TestRunReport(t *testing.T) { } func TestRunAllAndJSON(t *testing.T) { - t.Setenv("HOME", fakeHome(t, "11.16.1")) + testutil.SetHome(t, fakeHome(t, "11.16.1")) repo := fakeRepo(t) var out bytes.Buffer if err := run([]string{"--repo", repo, "--all"}, &out, os.Stderr); err != nil { @@ -252,7 +254,7 @@ func TestRunAllAndJSON(t *testing.T) { } func TestRunRefusesANonRepo(t *testing.T) { - t.Setenv("HOME", fakeHome(t, "11.16.1")) + testutil.SetHome(t, fakeHome(t, "11.16.1")) err := run([]string{"--repo", t.TempDir()}, &bytes.Buffer{}, &bytes.Buffer{}) if err == nil || !strings.Contains(err.Error(), "go.mod") { t.Errorf("a directory that is not the repository must be refused, got %v", err)