Skip to content

Commit 740cd98

Browse files
committed
fix: give a module-tree reference project what mx module-import would
The protected-module path in PackageProject wrote only the units. The reference project is also read for the package's Java and JavaScript sources (DESCRIBE JAVA/JAVASCRIPT ACTION), so marketplace diff and update would report every action of such a module as changed locally (#1441). Write the package's declared files beside it, as mx module-import does. The units are not converted to the project's Mendix version, so refuse a tree saved with a newer Mendix than the project. Split units only out of modules and folders, read the package entry and module name once, and keep the refcache comment true for a reference that stays MPR v2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QiMsnXa9crge2qnSk9S7Zn
1 parent a59ce65 commit 740cd98

6 files changed

Lines changed: 223 additions & 29 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"area": "cmd/mxcli", "date": "2026-11-06", "symptom": "mendixlabs/mxcli#1754: `marketplace install` of a protected (add-on) module shipped as .mxmodule (Menditect MtaPluginModule 11.5.5, Marketplace 214717) failed with \"file is not a database\"; `mx module-import` refuses such a package outright (\"Importing protected module is not supported\", exit 111).", "cause": "The package's project.mpr is not an SQLite .mpr but one BSON document {_ProductVersion, Contents} holding the project as a single tree with every unit nested in its container, so every reader that expected SQLite failed and mx has no import for it.", "file": "cmd/mxcli/marketplace/moduletree.go (split the tree into units at Folders/Documents/DomainModel/ModuleSettings/ModuleSecurity of modules and folders; write them with AddRawUnit; write the declared package files beside the reference; refuse a tree newer than the project), cmd/mxcli/marketplace/scratch.go (PackageProject takes that path for a tree)", "insight": "Replacing `mx module-import` means replacing everything it does, not just the unit write: it also writes the package's declared files next to the .mpr and converts the module to the project's Mendix version. The first cut of mendixlabs/mxcli#1755 did only the units, which installs fine (install copies files from the .mpk) but gives `marketplace diff`/`update` a reference without javasource/javascriptsource — the #1441 'every action changed locally' failure again — and units in the package's schema. Ask which readers consume the reference (refcache.go isModelFile lists them), not only which writer produced it. The unit test asserts Start.java beside the reference, which only the InstallPackageFiles call writes; the conversion is not reproduced (no mx), only a newer tree is refused.", "refs": ["mendixlabs/mxcli#1754", "mendixlabs/mxcli#1755", "mendixlabs/mxcli#1441"], "test": "cmd/mxcli/marketplace/moduletree_test.go"}

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1717

1818
- **`alter workflow` no longer guesses which outcome to drop** (ako/mxcli#808) — `drop X outcome 'V'` with two outcomes of value `V` dropped the first; it is refused as ambiguous now. `check --references` also resolves an activity reference by name before caption, as `exec` does, so its guards no longer go silent on a target captioned like another activity's name.
1919
- **describe → exec keeps a decision whose true branch is empty** (ako/mxcli#1963, mendixlabs/mxcli#1742) — `describe microflow` folded such a decision into `if not(X) then <false branch> end if;`, and executing that stored `not(X)` with the true and false outcomes swapped: the same behaviour, a different model in Studio Pro. It is now printed as stored, `if X then else <false branch> end if;`, which builds Studio Pro's shape. A script written from the old output still means the same thing; an `alter microflow` target written in the folded `if not(X) then` form no longer resolves, and is refused rather than applied.
20-
- **`marketplace install` installs protected (add-on) modules shipped as `.mxmodule`** — such a package carries its model not as an SQLite `.mpr` but as one BSON document holding the module as a tree, so install failed with `file is not a database`, and `mx module-import` refuses protected modules outright ("Importing protected module is not supported", exit 111). The reference project is now built by splitting that tree into units and writing them with the same raw-unit writer the transplant uses; protection settings travel unchanged. Measured on Menditect's MtaPluginModule 11.5.5 (Marketplace 214717) into an 11.15 MPR v2 app: 32 units, `mx check` 0 errors, module listed as *Protected AddOn*, and the plugin starts and serves its endpoint.
20+
- **`marketplace install` installs protected (add-on) modules shipped as `.mxmodule`** — such a package carries its model not as an SQLite `.mpr` but as one BSON document holding the module as a tree, so install failed with `file is not a database`, and `mx module-import` refuses protected modules outright ("Importing protected module is not supported", exit 111). The reference project is now built by splitting that tree into units and writing them with the same raw-unit writer the transplant uses; protection settings travel unchanged. Measured on Menditect's MtaPluginModule 11.5.5 (Marketplace 214717) into an 11.15 MPR v2 app: 32 units, `mx check` 0 errors, module listed as *Protected AddOn*, and the plugin starts and serves its endpoint. The package's declared files (`javasource/`, `javascriptsource/`, …) are written beside the reference project as `mx module-import` would, so `marketplace diff` and `update` do not report every Java or JavaScript action as changed; the units are not converted to the project's Mendix version, so a package saved with a newer Mendix than the project is refused.
2121
- **`test --attach` on Mendix 11.14.0 says why it is refused instead of blaming an entity** — that version's `mxbuild --serve` fails every build after its first, so since ako/mxcli#1917 the attach builds on a fresh serve process, whose first build asks for a runtime restart whatever changed. The refusal claimed "an entity or association changed" and suggested restarting the dev loop, which does not help; it now says the build could not be done incrementally and points to `--local`. The hosting app's pages survive either way. 11.15.0 builds incrementally and attaches as before.
2222
- **`describe microflow` no longer leaves out a note linked to the start event or to another note** (ako/mxcli#1933, mendixlabs/mxcli#1731) — such a note counted as linked, so it was not printed as a free note, and the element it is linked to prints no `@annotation`, so it was not printed at all; re-executing the description deleted it. It is now described as a free note, after a `-- WARNING` line saying the link itself has no MDL form and a rewrite drops it.
2323
- **Rebuilding a described decision no longer adds a merge** (ako/mxcli#1929, mendixlabs/mxcli#1727) — a `case`, `split type` or `if … elsif` one of whose branches returns is described with its own merge as a label (`end case; join shared1; merge shared1;`). Executing that as a fresh create built the decision's merge and a second one for the label, wired merge to merge, and every further describe → drop + create added another (`join junctionN; merge junctionN;`). The label's merge is now the decision's merge when nothing else reaches it, so the round trip keeps one merge.

‎cmd/mxcli/marketplace/moduletree.go‎

Lines changed: 96 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"bytes"
88
"fmt"
99
"io"
10+
"path/filepath"
11+
"strings"
1012

1113
modelsdk "github.com/mendixlabs/mxcli"
1214
"github.com/mendixlabs/mxcli/internal/zipentry"
@@ -28,6 +30,22 @@ import (
2830
// transplant uses. Unit contents are copied as they are, protection settings
2931
// (ModuleSettings.ProtectedModuleType, ExportLevel) included: the result is the
3032
// module as the package ships it, not an unprotected copy.
33+
//
34+
// Two things `mx module-import` does besides writing units are done here too,
35+
// because the reference project is read for more than its units:
36+
//
37+
// - The package's declared files (javasource/, javascriptsource/, ...) are
38+
// written next to the reference .mpr. DESCRIBE JAVA ACTION and DESCRIBE
39+
// JAVASCRIPT ACTION read them, and without them every action of the module
40+
// compares as "changed locally" in `marketplace diff` and `update`
41+
// (mendixlabs/mxcli#1441).
42+
// - The units are NOT migrated. mx converts an imported module to the
43+
// project's Mendix version; these units keep the schema of the version the
44+
// package was saved with. A tree saved with a newer Mendix than the project
45+
// is refused, as mx refuses such an import; an older one is written as it
46+
// is, which is what the measurement above did (11.5.5 into 11.15). A
47+
// comparison against such a reference can therefore also report the
48+
// platform's own migrations of the module.
3149

3250
// sqliteMagic opens every SQLite database file, and so every v1 .mpr.
3351
var sqliteMagic = []byte("SQLite format 3\x00")
@@ -37,6 +55,9 @@ var sqliteMagic = []byte("SQLite format 3\x00")
3755
// carry none of them — measured on an 11.15 MPR v2 project: a Projects$Folder
3856
// unit is {$ID, $Type, Name}, a Projects$ModuleImpl has no Folders, Documents,
3957
// DomainModel, ModuleSettings or ModuleSecurity key.
58+
//
59+
// Only the unit kinds that hold units are split (unitHolders): a document that
60+
// happens to have a property of one of these names keeps it.
4061
var unitContainments = map[string]bool{
4162
"Folders": true,
4263
"Documents": true,
@@ -45,30 +66,73 @@ var unitContainments = map[string]bool{
4566
"ModuleSecurity": true,
4667
}
4768

69+
// unitHolders are the unit types that contain other units in a module.
70+
var unitHolders = map[string]bool{
71+
"Projects$ModuleImpl": true,
72+
"Projects$Folder": true,
73+
}
74+
4875
// treeUnit is one unit cut out of a module tree, ready for AddRawUnit.
4976
type treeUnit struct {
5077
id, containerID, containment, unitType string
5178
contents []byte
5279
}
5380

54-
// IsModuleTreePackage reports whether the package's project.mpr is a module tree
55-
// rather than an SQLite .mpr.
56-
func IsModuleTreePackage(mpkPath string) (bool, error) {
57-
data, err := readZipEntry(mpkPath, packageProjectEntry)
81+
// readModuleTree returns the package's project.mpr and the Mendix version it was
82+
// saved with when it is a module tree, and nil when it is an ordinary .mpr.
83+
func readModuleTree(mpkPath string) (data []byte, productVersion string, err error) {
84+
data, err = readZipEntry(mpkPath, packageProjectEntry)
5885
if err != nil {
59-
return false, err
86+
return nil, "", err
87+
}
88+
productVersion, ok := moduleTreeVersion(data)
89+
if !ok {
90+
return nil, "", nil
6091
}
61-
return isModuleTree(data), nil
92+
return data, productVersion, nil
6293
}
6394

6495
func isModuleTree(data []byte) bool {
96+
_, ok := moduleTreeVersion(data)
97+
return ok
98+
}
99+
100+
// moduleTreeVersion reports the _ProductVersion of a module tree, and false when
101+
// data is not one.
102+
func moduleTreeVersion(data []byte) (string, bool) {
65103
if bytes.HasPrefix(data, sqliteMagic) || len(data) < 5 {
66-
return false
104+
return "", false
67105
}
68106
var head struct {
69107
ProductVersion string `bson:"_ProductVersion"`
70108
}
71-
return bson.Unmarshal(data, &head) == nil && head.ProductVersion != ""
109+
if bson.Unmarshal(data, &head) != nil || head.ProductVersion == "" {
110+
return "", false
111+
}
112+
return head.ProductVersion, true
113+
}
114+
115+
// checkTreeVersion refuses a module tree saved with a newer Mendix than the
116+
// project it is written into: its units may use properties or types that
117+
// version does not have, and nothing here converts them. Only major.minor.patch
118+
// count — _ProductVersion carries a build number (11.5.5.73100) that the
119+
// project's version usually does not.
120+
func checkTreeVersion(treeVersion, projectVersion string) error {
121+
tv, pv := releaseOf(treeVersion), releaseOf(projectVersion)
122+
if tv == "" || pv == "" || !versionLess(pv, tv) {
123+
return nil
124+
}
125+
return fmt.Errorf("the package was saved with Mendix %s, which is newer than the project's %s; "+
126+
"install it from Studio Pro %s or later, or upgrade the project first", tv, pv, tv)
127+
}
128+
129+
// releaseOf keeps the first three components of a dotted version.
130+
func releaseOf(v string) string {
131+
parts := strings.Split(strings.TrimSpace(v), ".")
132+
if len(parts) > 3 {
133+
parts = parts[:3]
134+
}
135+
return strings.Join(parts, ".")
72136
}
73137

74138
// moduleTreeUnits splits the named module of a module tree into units, parents
@@ -104,10 +168,12 @@ func splitUnit(doc bson.D, containerID, containment string, units *[]treeUnit) e
104168
if err != nil {
105169
return err
106170
}
171+
unitType := stringOf(lookup(doc, "$Type"))
172+
holder := unitHolders[unitType]
107173
own := make(bson.D, 0, len(doc))
108174
var children []bson.E
109175
for _, e := range doc {
110-
if unitContainments[e.Key] {
176+
if holder && unitContainments[e.Key] {
111177
children = append(children, e)
112178
continue
113179
}
@@ -121,7 +187,7 @@ func splitUnit(doc bson.D, containerID, containment string, units *[]treeUnit) e
121187
id: id,
122188
containerID: containerID,
123189
containment: containment,
124-
unitType: stringOf(lookup(doc, "$Type")),
190+
unitType: unitType,
125191
contents: encoded,
126192
})
127193
for _, c := range children {
@@ -145,15 +211,26 @@ func splitUnit(doc bson.D, containerID, containment string, units *[]treeUnit) e
145211
return nil
146212
}
147213

148-
// InsertModuleTree writes the package's module into mprPath, a project that does
149-
// not have it yet, attached to that project's project unit. It is the
150-
// module-tree counterpart of `mx module-import` for the reference project.
151-
func InsertModuleTree(mpkPath, mprPath, moduleName string) (int, error) {
152-
data, err := readZipEntry(mpkPath, packageProjectEntry)
153-
if err != nil {
154-
return 0, err
214+
// buildFromModuleTree is the module-tree counterpart of `mx module-import` for
215+
// the reference project: it writes the package's module into mprPath, a project
216+
// that does not have it yet, and the package's declared files next to it.
217+
func buildFromModuleTree(tree []byte, treeVersion, mpkPath, mprPath, moduleName, projectVersion string) error {
218+
if err := checkTreeVersion(treeVersion, projectVersion); err != nil {
219+
return err
155220
}
156-
units, err := moduleTreeUnits(data, moduleName)
221+
if _, err := insertModuleTree(tree, mprPath, moduleName); err != nil {
222+
return err
223+
}
224+
if _, _, err := InstallPackageFiles(mpkPath, filepath.Dir(mprPath)); err != nil {
225+
return fmt.Errorf("write the package's files into the reference project: %w", err)
226+
}
227+
return nil
228+
}
229+
230+
// insertModuleTree writes the tree's module into mprPath, attached to that
231+
// project's project unit, and returns the number of units written.
232+
func insertModuleTree(tree []byte, mprPath, moduleName string) (int, error) {
233+
units, err := moduleTreeUnits(tree, moduleName)
157234
if err != nil {
158235
return 0, err
159236
}
@@ -206,7 +283,7 @@ func idOf(doc bson.D) (string, error) {
206283
return v, nil
207284
}
208285
}
209-
return "", fmt.Errorf("%s without a 16-byte $ID in the module tree", stringOf(lookup(doc, "$Type")))
286+
return "", fmt.Errorf("%s without an $ID (a 16-byte binary or a non-empty string) in the module tree", stringOf(lookup(doc, "$Type")))
210287
}
211288

212289
func readZipEntry(zipPath, entry string) ([]byte, error) {

‎cmd/mxcli/marketplace/moduletree_test.go‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package marketplace
44

55
import (
6+
"os"
7+
"path/filepath"
68
"testing"
79

810
"github.com/mendixlabs/mxcli/mdl/types"
@@ -110,3 +112,117 @@ func TestModuleTreeUnitsUnknownModule(t *testing.T) {
110112
t.Error("want an error for a module the tree does not hold")
111113
}
112114
}
115+
116+
func TestCheckTreeVersion(t *testing.T) {
117+
for _, c := range []struct {
118+
tree, project string
119+
refused bool
120+
}{
121+
{"11.5.5.73100", "11.15.0", false}, // older package: written as is
122+
{"11.5.5.73100", "11.5.5", false}, // same release; the build number does not count
123+
{"11.15.0.80000", "11.15.0.12345", false}, // same release, both with builds
124+
{"11.16.0.1", "11.15.0", true}, // newer than the project: refused
125+
{"12.0.0", "11.15.0", true},
126+
{"", "11.15.0", false},
127+
{"11.16.0", "", false},
128+
} {
129+
err := checkTreeVersion(c.tree, c.project)
130+
if (err != nil) != c.refused {
131+
t.Errorf("checkTreeVersion(%q, %q) = %v, refused want %v", c.tree, c.project, err, c.refused)
132+
}
133+
}
134+
}
135+
136+
// Only modules and folders hold units: a document with a property that happens
137+
// to share a containment's name is one unit and keeps that property.
138+
func TestModuleTreeUnits_DocumentKeepsAPropertyNamedLikeAContainment(t *testing.T) {
139+
doc := bson.D{
140+
{Key: "$ID", Value: gid(7)},
141+
{Key: "$Type", Value: "Constants$Constant"},
142+
{Key: "Documents", Value: bson.A{int32(3), bson.D{{Key: "$ID", Value: gid(8)}, {Key: "$Type", Value: "Some$Part"}}}},
143+
}
144+
var units []treeUnit
145+
if err := splitUnit(doc, "", "Documents", &units); err != nil {
146+
t.Fatal(err)
147+
}
148+
if len(units) != 1 {
149+
t.Fatalf("got %d units, want the document alone", len(units))
150+
}
151+
var own bson.D
152+
if err := bson.Unmarshal(units[0].contents, &own); err != nil {
153+
t.Fatal(err)
154+
}
155+
if lookup(own, "Documents") == nil {
156+
t.Error("the document lost its Documents property")
157+
}
158+
}
159+
160+
const pluginPackageXML = `<?xml version="1.0" encoding="utf-8"?>
161+
<package xmlns="http://www.mendix.com/package/1.0/">
162+
<modelerProject xmlns="http://www.mendix.com/modelerProject/1.0/">
163+
<module name="Plugin" />
164+
<projectFile path="project.mpr" />
165+
<files>
166+
<file path="javasource/plugin/actions/Start.java" />
167+
</files>
168+
</modelerProject>
169+
</package>`
170+
171+
// The reference built from a module tree must carry what `mx module-import`
172+
// would have written beside the units: DESCRIBE JAVA ACTION reads
173+
// javasource/<module>/actions/, and without it every action of the module
174+
// compares as changed (mendixlabs/mxcli#1441).
175+
func TestBuildFromModuleTree_WritesTheModuleAndThePackageFiles(t *testing.T) {
176+
const javaBody = "package plugin.actions; public class Start {}"
177+
mpk := writeZip(t, map[string][]byte{
178+
"package.xml": []byte(pluginPackageXML),
179+
packageProjectEntry: syntheticTree(t),
180+
"javasource/plugin/actions/Start.java": []byte(javaBody),
181+
})
182+
mprPath := copyFixture(t)
183+
184+
tree, version, err := readModuleTree(mpk)
185+
if err != nil || tree == nil {
186+
t.Fatalf("readModuleTree = %v, %v; want the tree", tree != nil, err)
187+
}
188+
if version != "11.0.0.73100" {
189+
t.Errorf("tree version = %q", version)
190+
}
191+
if err := buildFromModuleTree(tree, version, mpk, mprPath, "Plugin", ""); err != nil {
192+
t.Fatalf("buildFromModuleTree: %v", err)
193+
}
194+
195+
got, err := os.ReadFile(filepath.Join(filepath.Dir(mprPath), "javasource", "plugin", "actions", "Start.java"))
196+
if err != nil {
197+
t.Fatalf("the package's Java source was not written next to the reference: %v", err)
198+
}
199+
if string(got) != javaBody {
200+
t.Errorf("Start.java = %q", got)
201+
}
202+
if err := refuseIfPresent(mprPath, "Plugin"); err == nil {
203+
t.Error("the module is not in the reference project")
204+
}
205+
}
206+
207+
func TestBuildFromModuleTree_RefusesANewerPackage(t *testing.T) {
208+
mpk := writeZip(t, map[string][]byte{"package.xml": []byte(pluginPackageXML), packageProjectEntry: syntheticTree(t)})
209+
mprPath := copyFixture(t)
210+
tree, version, err := readModuleTree(mpk)
211+
if err != nil {
212+
t.Fatal(err)
213+
}
214+
if err := buildFromModuleTree(tree, version, mpk, mprPath, "Plugin", "10.24.0"); err == nil {
215+
t.Fatal("a tree saved with 11.0 was written into a 10.24 project")
216+
}
217+
if err := refuseIfPresent(mprPath, "Plugin"); err != nil {
218+
t.Error("the refused module was written anyway")
219+
}
220+
}
221+
222+
func TestReadModuleTree_AnSQLiteProjectIsNotATree(t *testing.T) {
223+
mpk := writeZip(t, map[string][]byte{packageProjectEntry: append([]byte("SQLite format 3\x00"), make([]byte, 100)...)})
224+
tree, _, err := readModuleTree(mpk)
225+
if err != nil || tree != nil {
226+
t.Fatalf("readModuleTree = %v, %v; want no tree and no error", tree != nil, err)
227+
}
228+
}

‎cmd/mxcli/marketplace/refcache.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ func publishToCache(buildDir, cacheDir string) error {
299299
// — a difference that shows up as findings that come and go. Extend this filter
300300
// in the same commit and bump refEntryFormat, or store the whole tree again.
301301
//
302-
// mprcontents/ is kept even though `mx module-import` always collapses the
303-
// reference to MPR v1: the cost is nothing when it is absent, and the failure if
304-
// that ever changes is an unreadable model rather than a slower run.
302+
// mprcontents/ is kept: `mx module-import` collapses the reference to MPR v1,
303+
// but a reference built from a protected module's tree (moduletree.go) skips
304+
// module-import and keeps whatever format the blank project was created in.
305305
func isModelFile(rel string) bool {
306306
for _, dir := range []string{"mprcontents", "javascriptsource", "javasource"} {
307307
if rel == dir || strings.HasPrefix(rel, dir+string(filepath.Separator)) {

0 commit comments

Comments
 (0)