Skip to content

Commit 19383ae

Browse files
committed
wit: reverse topological sort of packages
This allows packages that do not depend on anything to sort first. Candidly, I’m not sure why this works.
1 parent b98bdef commit 19383ae

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

wit/resolve.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,18 +1622,19 @@ func DependsOn(node Node, p *Package) bool {
16221622
}
16231623

16241624
func comparePackages(a, b *Package) int {
1625+
// fmt.Fprintln(os.Stderr, "comparing "+b.Name.String()+" to "+a.Name.String())
16251626
switch {
16261627
case a == b:
16271628
return 0
1628-
case DependsOn(b, a):
1629-
// println(b.Name.String() + " depends on " + a.Name.String())
1630-
return 1
16311629
case DependsOn(a, b):
1632-
// println(a.Name.String() + " depends on " + b.Name.String())
1630+
// fmt.Fprintln(os.Stderr, a.Name.String()+" depends on "+b.Name.String())
1631+
return 1
1632+
case DependsOn(b, a):
1633+
// fmt.Fprintln(os.Stderr, b.Name.String()+" depends on "+a.Name.String())
16331634
return -1
16341635
}
1635-
// println(a.Name.String() + " does not depend on " + b.Name.String())
1636-
return strings.Compare(a.Name.String(), b.Name.String())
1636+
// fmt.Fprintln(os.Stderr, a.Name.String()+" does not depend on "+b.Name.String())
1637+
return -1 * strings.Compare(a.Name.String(), b.Name.String())
16371638
}
16381639

16391640
// Stability represents the version or feature-gated stability of a given feature.

wit/wit.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,25 @@ func (r *Resolve) WIT(ctx Node, _ string) string {
5757
// Sort packages topologically by dependency
5858
packages := slices.Clone(r.Packages)
5959
slices.SortFunc(packages, comparePackages)
60+
slices.Reverse(packages)
6061

6162
var b strings.Builder
6263
var hasContent bool
63-
for i, p := range packages {
64+
i := 0
65+
for _, p := range packages {
6466
var name string
6567
if i != 0 {
6668
// Write subsequent packages with explicit name, which renders the package WIT with nested braces.
6769
name = p.Name.WIT(p, "")
6870
}
6971
wit := p.WIT(ctx, name)
70-
if wit != "" {
72+
if len(packages) == 1 || strings.Count(wit, "\n") > 1 {
7173
if hasContent {
7274
b.WriteString("\n")
7375
}
7476
hasContent = true
7577
b.WriteString(wit)
78+
i++
7679
}
7780
}
7881
return b.String()
@@ -1087,10 +1090,10 @@ func (p *Package) WIT(ctx Node, name string) string {
10871090
if multi {
10881091
b.WriteString("}\n")
10891092
}
1090-
// Return empty string in multi-package mode if package has no contents
1091-
if multi && i == 0 {
1092-
return ""
1093-
}
1093+
// Return empty string if package has no contents
1094+
// if i == 0 {
1095+
// return ""
1096+
// }
10941097
return b.String()
10951098
}
10961099

0 commit comments

Comments
 (0)