Skip to content

Commit 7e557e1

Browse files
committed
wit/bindgen: sort packages topologically by dependency before selecting WIT world
Warn if there are multiple WIT worlds that match the world option. Fixes #356.
1 parent 111960c commit 7e557e1

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

wit/bindgen/generator.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,39 @@ func newGenerator(res *wit.Resolve, opts ...Option) (*generator, error) {
163163
g.opts.cmPackage = cmPackage
164164
}
165165
g.res = res
166-
for _, g.world = range res.Worlds {
167-
if g.world.Match(g.opts.world) {
168-
break
169-
}
170-
// otherwise choose the last world
166+
167+
// Sort WIT packages topologically
168+
packages := slices.Clone(res.Packages)
169+
slices.SortFunc(packages, wit.ComparePackages)
170+
slices.Reverse(packages)
171+
172+
// Select WIT world
173+
var first *wit.World
174+
for _, pkg := range packages {
175+
pkg.Worlds.All()(func(_ string, w *wit.World) bool {
176+
if first == nil {
177+
first = w
178+
}
179+
if w.Match(g.opts.world) {
180+
name := w.Package.Name
181+
name.Extension = w.Name
182+
if g.world == nil {
183+
g.opts.logger.Infof("WIT world: %s", name.String())
184+
g.world = w
185+
} else {
186+
g.opts.logger.Warnf("Warning: ambiguous WIT world: %s", name.String())
187+
}
188+
}
189+
return true
190+
})
191+
}
192+
193+
// Fall back to first world
194+
if g.world == nil {
195+
g.world = first
171196
}
197+
198+
// Instantiate a wasm-tools instance.
172199
g.wasmTools, err = wasmtools.New(context.Background())
173200
if err != nil {
174201
return nil, err

0 commit comments

Comments
 (0)