Skip to content

Commit 728414a

Browse files
MendixMauclaude
andcommitted
fix: install protected modules shipped as .mxmodule (closes #1754)
A protected module package carries its model as one BSON tree instead of an SQLite .mpr, so install failed with "file is not a database" and mx module-import refuses it. Split the tree into units and write them into the reference project with the raw-unit writer; protection settings are copied unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f3c4d53 commit 728414a

4 files changed

Lines changed: 356 additions & 0 deletions

File tree

‎CHANGELOG.md‎

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

1414
### Fixed
1515

16+
- **`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.
1617
- **`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.
1718
- **`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.
1819
- **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.
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package marketplace
4+
5+
import (
6+
"archive/zip"
7+
"bytes"
8+
"fmt"
9+
"io"
10+
11+
modelsdk "github.com/mendixlabs/mxcli"
12+
"github.com/mendixlabs/mxcli/internal/zipentry"
13+
"github.com/mendixlabs/mxcli/mdl/types"
14+
"go.mongodb.org/mongo-driver/v2/bson"
15+
)
16+
17+
// A module package normally carries its model as a v1 .mpr — an SQLite
18+
// database. Packages exported as protected (add-on) modules, delivered as
19+
// .mxmodule, carry something else under the same name: one BSON document,
20+
// {_ProductVersion, Contents}, where Contents is the whole project as a single
21+
// tree with every unit nested inside its container. Measured on Menditect's
22+
// MtaPluginModule 11.5.5 (Marketplace 214717): `marketplace install` failed on
23+
// it with "file is not a database", and `mx module-import` refuses it outright
24+
// ("Importing protected module is not supported", exit 111).
25+
//
26+
// The tree holds exactly the units a .mpr would, so it is split back into them
27+
// and written into the reference project with the same raw-unit writer the
28+
// transplant uses. Unit contents are copied as they are, protection settings
29+
// (ModuleSettings.ProtectedModuleType, ExportLevel) included: the result is the
30+
// module as the package ships it, not an unprotected copy.
31+
32+
// sqliteMagic opens every SQLite database file, and so every v1 .mpr.
33+
var sqliteMagic = []byte("SQLite format 3\x00")
34+
35+
// unitContainments are the properties under which a unit holds other units.
36+
// They are stored as separate units in a .mpr, and the parent's own contents
37+
// carry none of them — measured on an 11.15 MPR v2 project: a Projects$Folder
38+
// unit is {$ID, $Type, Name}, a Projects$ModuleImpl has no Folders, Documents,
39+
// DomainModel, ModuleSettings or ModuleSecurity key.
40+
var unitContainments = map[string]bool{
41+
"Folders": true,
42+
"Documents": true,
43+
"DomainModel": true,
44+
"ModuleSettings": true,
45+
"ModuleSecurity": true,
46+
}
47+
48+
// treeUnit is one unit cut out of a module tree, ready for AddRawUnit.
49+
type treeUnit struct {
50+
id, containerID, containment, unitType string
51+
contents []byte
52+
}
53+
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)
58+
if err != nil {
59+
return false, err
60+
}
61+
return isModuleTree(data), nil
62+
}
63+
64+
func isModuleTree(data []byte) bool {
65+
if bytes.HasPrefix(data, sqliteMagic) || len(data) < 5 {
66+
return false
67+
}
68+
var head struct {
69+
ProductVersion string `bson:"_ProductVersion"`
70+
}
71+
return bson.Unmarshal(data, &head) == nil && head.ProductVersion != ""
72+
}
73+
74+
// moduleTreeUnits splits the named module of a module tree into units, parents
75+
// before children. The module unit's containerID is left empty: the caller
76+
// attaches it to its own project unit.
77+
func moduleTreeUnits(data []byte, moduleName string) ([]treeUnit, error) {
78+
var root bson.D
79+
if err := bson.Unmarshal(data, &root); err != nil {
80+
return nil, fmt.Errorf("decode module tree: %w", err)
81+
}
82+
contents, ok := lookup(root, "Contents").(bson.D)
83+
if !ok {
84+
return nil, fmt.Errorf("module tree has no Contents document")
85+
}
86+
modules, _ := lookup(contents, "Modules").(bson.A)
87+
for _, m := range modules {
88+
mod, ok := m.(bson.D)
89+
if !ok || !equalFold(stringOf(lookup(mod, "Name")), moduleName) {
90+
continue
91+
}
92+
var units []treeUnit
93+
if err := splitUnit(mod, "", projectContainment, &units); err != nil {
94+
return nil, err
95+
}
96+
return units, nil
97+
}
98+
return nil, fmt.Errorf("module %q not found in the package's module tree", moduleName)
99+
}
100+
101+
// splitUnit appends doc as a unit, then recurses into the units it contains.
102+
func splitUnit(doc bson.D, containerID, containment string, units *[]treeUnit) error {
103+
id, err := idOf(doc)
104+
if err != nil {
105+
return err
106+
}
107+
own := make(bson.D, 0, len(doc))
108+
var children []bson.E
109+
for _, e := range doc {
110+
if unitContainments[e.Key] {
111+
children = append(children, e)
112+
continue
113+
}
114+
own = append(own, e)
115+
}
116+
encoded, err := bson.Marshal(own)
117+
if err != nil {
118+
return fmt.Errorf("encode unit %s: %w", id, err)
119+
}
120+
*units = append(*units, treeUnit{
121+
id: id,
122+
containerID: containerID,
123+
containment: containment,
124+
unitType: stringOf(lookup(doc, "$Type")),
125+
contents: encoded,
126+
})
127+
for _, c := range children {
128+
switch v := c.Value.(type) {
129+
case bson.D:
130+
if err := splitUnit(v, id, c.Key, units); err != nil {
131+
return err
132+
}
133+
case bson.A:
134+
// A Mendix list stores its storage-kind marker as element 0; the
135+
// units are the documents after it.
136+
for _, item := range v {
137+
if child, ok := item.(bson.D); ok {
138+
if err := splitUnit(child, id, c.Key, units); err != nil {
139+
return err
140+
}
141+
}
142+
}
143+
}
144+
}
145+
return nil
146+
}
147+
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
155+
}
156+
units, err := moduleTreeUnits(data, moduleName)
157+
if err != nil {
158+
return 0, err
159+
}
160+
projectID, err := projectUnitID(mprPath)
161+
if err != nil {
162+
return 0, err
163+
}
164+
if err := refuseIfPresent(mprPath, moduleName); err != nil {
165+
return 0, err
166+
}
167+
writer, err := modelsdk.OpenForWriting(mprPath)
168+
if err != nil {
169+
return 0, fmt.Errorf("open %s for writing: %w", mprPath, err)
170+
}
171+
defer writer.Disconnect()
172+
for i, u := range units {
173+
container := u.containerID
174+
if container == "" {
175+
container = projectID
176+
}
177+
if err := writer.AddRawUnit(u.id, container, u.containment, u.unitType, u.contents); err != nil {
178+
return i, fmt.Errorf("write unit %s (%s): %w", u.id, u.unitType, err)
179+
}
180+
}
181+
return len(units), nil
182+
}
183+
184+
func lookup(doc bson.D, key string) any {
185+
for _, e := range doc {
186+
if e.Key == key {
187+
return e.Value
188+
}
189+
}
190+
return nil
191+
}
192+
193+
func stringOf(v any) string {
194+
s, _ := v.(string)
195+
return s
196+
}
197+
198+
func idOf(doc bson.D) (string, error) {
199+
switch v := lookup(doc, "$ID").(type) {
200+
case bson.Binary:
201+
if len(v.Data) == 16 {
202+
return types.BlobToUUID(v.Data), nil
203+
}
204+
case string:
205+
if v != "" {
206+
return v, nil
207+
}
208+
}
209+
return "", fmt.Errorf("%s without a 16-byte $ID in the module tree", stringOf(lookup(doc, "$Type")))
210+
}
211+
212+
func readZipEntry(zipPath, entry string) ([]byte, error) {
213+
zr, err := zip.OpenReader(zipPath)
214+
if err != nil {
215+
return nil, err
216+
}
217+
defer zr.Close()
218+
for _, f := range zr.File {
219+
if f.Name != entry {
220+
continue
221+
}
222+
rc, err := zipentry.Open(f)
223+
if err != nil {
224+
return nil, err
225+
}
226+
defer rc.Close()
227+
return io.ReadAll(rc)
228+
}
229+
return nil, fmt.Errorf("no %s in %s", entry, zipPath)
230+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package marketplace
4+
5+
import (
6+
"testing"
7+
8+
"github.com/mendixlabs/mxcli/mdl/types"
9+
"go.mongodb.org/mongo-driver/v2/bson"
10+
)
11+
12+
func gid(last byte) bson.Binary {
13+
b := make([]byte, 16)
14+
b[15] = last
15+
return bson.Binary{Subtype: 0, Data: b}
16+
}
17+
18+
// syntheticTree has the shape of a protected module package's project.mpr: one
19+
// BSON document with the project as a tree and every unit nested in its
20+
// container, lists led by their storage-kind marker.
21+
func syntheticTree(t *testing.T) []byte {
22+
t.Helper()
23+
module := bson.D{
24+
{Key: "$ID", Value: gid(1)},
25+
{Key: "$Type", Value: "Projects$ModuleImpl"},
26+
{Key: "Documents", Value: bson.A{int32(3)}},
27+
{Key: "DomainModel", Value: bson.D{{Key: "$ID", Value: gid(2)}, {Key: "$Type", Value: "DomainModels$DomainModel"}, {Key: "Entities", Value: bson.A{int32(3)}}}},
28+
{Key: "Folders", Value: bson.A{int32(3), bson.D{
29+
{Key: "$ID", Value: gid(3)},
30+
{Key: "$Type", Value: "Projects$Folder"},
31+
{Key: "Documents", Value: bson.A{int32(3), bson.D{{Key: "$ID", Value: gid(4)}, {Key: "$Type", Value: "Constants$Constant"}, {Key: "Name", Value: "C"}}}},
32+
{Key: "Folders", Value: bson.A{int32(3)}},
33+
{Key: "Name", Value: "USE_ME"},
34+
}}},
35+
{Key: "ModuleSecurity", Value: bson.D{{Key: "$ID", Value: gid(5)}, {Key: "$Type", Value: "Security$ModuleSecurity"}}},
36+
{Key: "ModuleSettings", Value: bson.D{{Key: "$ID", Value: gid(6)}, {Key: "$Type", Value: "Projects$ModuleSettings"}, {Key: "ProtectedModuleType", Value: "AddOn"}}},
37+
{Key: "Name", Value: "Plugin"},
38+
}
39+
root := bson.D{
40+
{Key: "_ProductVersion", Value: "11.0.0.73100"},
41+
{Key: "Contents", Value: bson.D{
42+
{Key: "$ID", Value: gid(9)},
43+
{Key: "$Type", Value: "Projects$Project"},
44+
{Key: "Modules", Value: bson.A{int32(3), module}},
45+
}},
46+
}
47+
data, err := bson.Marshal(root)
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
return data
52+
}
53+
54+
func TestIsModuleTree(t *testing.T) {
55+
if !isModuleTree(syntheticTree(t)) {
56+
t.Error("a module tree was not recognised")
57+
}
58+
if isModuleTree(append([]byte("SQLite format 3\x00"), make([]byte, 100)...)) {
59+
t.Error("an SQLite .mpr was taken for a module tree")
60+
}
61+
}
62+
63+
func TestModuleTreeUnits(t *testing.T) {
64+
units, err := moduleTreeUnits(syntheticTree(t), "plugin")
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
type want struct{ id, container, containment, unitType string }
69+
id := func(b byte) string { return types.BlobToUUID(gid(b).Data) }
70+
expect := []want{
71+
{id(1), "", "Modules", "Projects$ModuleImpl"},
72+
{id(2), id(1), "DomainModel", "DomainModels$DomainModel"},
73+
{id(3), id(1), "Folders", "Projects$Folder"},
74+
{id(4), id(3), "Documents", "Constants$Constant"},
75+
{id(5), id(1), "ModuleSecurity", "Security$ModuleSecurity"},
76+
{id(6), id(1), "ModuleSettings", "Projects$ModuleSettings"},
77+
}
78+
if len(units) != len(expect) {
79+
t.Fatalf("got %d units, want %d", len(units), len(expect))
80+
}
81+
for i, w := range expect {
82+
u := units[i]
83+
if u.id != w.id || u.containerID != w.container || u.containment != w.containment || u.unitType != w.unitType {
84+
t.Errorf("unit %d = {%s %s %s %s}, want %+v", i, u.id, u.containerID, u.containment, u.unitType, w)
85+
}
86+
}
87+
88+
// A unit's own contents carry none of its child units, as in a .mpr.
89+
var folder bson.D
90+
if err := bson.Unmarshal(units[2].contents, &folder); err != nil {
91+
t.Fatal(err)
92+
}
93+
for _, e := range folder {
94+
if unitContainments[e.Key] {
95+
t.Errorf("folder unit still holds %s", e.Key)
96+
}
97+
}
98+
// Protection travels with the module, untouched.
99+
var settings bson.D
100+
if err := bson.Unmarshal(units[5].contents, &settings); err != nil {
101+
t.Fatal(err)
102+
}
103+
if stringOf(lookup(settings, "ProtectedModuleType")) != "AddOn" {
104+
t.Error("ProtectedModuleType was not kept")
105+
}
106+
}
107+
108+
func TestModuleTreeUnitsUnknownModule(t *testing.T) {
109+
if _, err := moduleTreeUnits(syntheticTree(t), "Other"); err == nil {
110+
t.Error("want an error for a module the tree does not hold")
111+
}
112+
}

‎cmd/mxcli/marketplace/scratch.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ func PackageProject(ctx context.Context, mpkPath, mendixVersion, workDir string,
182182
}
183183
}
184184

185+
// A protected module ships its model as a module tree, which mx refuses to
186+
// import; split it into units and write those instead (moduletree.go).
187+
if tree, err := IsModuleTreePackage(mpkPath); err == nil && tree {
188+
name, err := ModuleNameInPackage(mpkPath)
189+
if err != nil {
190+
return "", err
191+
}
192+
if _, err := InsertModuleTree(mpkPath, mprPath, name); err != nil {
193+
return "", fmt.Errorf("load the package's module tree: %w", err)
194+
}
195+
return mprPath, nil
196+
}
197+
185198
// mx refuses a theme module (Atlas_Core, Atlas_Web_Content, Conversational
186199
// UI); makeImportable clears the flag that gates the refusal, on a copy.
187200
importable, err := makeImportable(mpkPath, workDir)

0 commit comments

Comments
 (0)