fix(0130): #library/#framework collection recurses into nested namespaces
extractLibraries/extractFrameworks walked the merged root plus exactly one namespace_decl level, so a #library reached through two or more aliased imports never made it to the AOT link line or the JIT dlopen list. Both walks now recurse over namespace_decl children. Regression: examples/1617-modules-library-nested-namespace.sx binds libpcap (not in the compiler's loaded images, so the JIT cannot mask the miss via RTLD_DEFAULT) behind two aliased imports.
This commit is contained in:
10
examples/1617-modules-library-nested-namespace.sx
Normal file
10
examples/1617-modules-library-nested-namespace.sx
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
// Regression (issue 0130): a #library declared in a module reached through
|
||||||
|
// TWO aliased imports must still be linked (AOT) / dlopened (JIT).
|
||||||
|
#import "modules/std.sx";
|
||||||
|
b :: #import "1617-modules-library-nested-namespace/b.sx";
|
||||||
|
|
||||||
|
main :: () -> i32 {
|
||||||
|
v := b.version_via_b();
|
||||||
|
print("pcap version non-empty: {}\n", v.len > 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
5
examples/1617-modules-library-nested-namespace/b.sx
Normal file
5
examples/1617-modules-library-nested-namespace/b.sx
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
// Middle module: aliases c.sx, putting its #library one namespace deep.
|
||||||
|
#import "modules/std.sx";
|
||||||
|
c :: #import "c.sx";
|
||||||
|
|
||||||
|
version_via_b :: () -> string { return c.pcap_version(); }
|
||||||
11
examples/1617-modules-library-nested-namespace/c.sx
Normal file
11
examples/1617-modules-library-nested-namespace/c.sx
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// Innermost module: owns the #library and its foreign fn.
|
||||||
|
#import "modules/std.sx";
|
||||||
|
|
||||||
|
pcaplib :: #library "pcap";
|
||||||
|
pcap_lib_version :: () -> ?cstring #foreign pcaplib "pcap_lib_version";
|
||||||
|
|
||||||
|
pcap_version :: () -> string {
|
||||||
|
p := pcap_lib_version();
|
||||||
|
if p == null { return ""; }
|
||||||
|
return from_cstring(p!);
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
0
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pcap version non-empty: true
|
||||||
116
issues/0130-library-decl-nested-namespace-dropped.md
Normal file
116
issues/0130-library-decl-nested-namespace-dropped.md
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
# RESOLVED — 0130: `#library` declared behind two aliased imports is dropped — no `-l` flag, no JIT dlopen
|
||||||
|
|
||||||
|
> **RESOLVED** (2026-06-12). Root cause as filed: `extractLibraries`
|
||||||
|
> and `extractFrameworks` (src/main.zig) walked the merged root's
|
||||||
|
> decls plus exactly ONE `namespace_decl` level, while aliased imports
|
||||||
|
> nest namespaces arbitrarily deep — a `#library`/`#framework` two or
|
||||||
|
> more aliases down never reached the AOT link args or the JIT dlopen
|
||||||
|
> loop. Both walks are now recursive over `namespace_decl` children
|
||||||
|
> (same `seen`-set dedup as before). Regression test:
|
||||||
|
> `examples/1617-modules-library-nested-namespace.sx` (+ its module
|
||||||
|
> dir) — `main → b :: #import → c :: #import` where c.sx declares
|
||||||
|
> `#library "pcap"`; libpcap is NOT in the compiler process's loaded
|
||||||
|
> images (unlike libz/libbz2/libsqlite3, which CoreServices/LLVM pull
|
||||||
|
> in and which mask the bug under `sx run`), so the pre-fix JIT fails
|
||||||
|
> symbol materialization and the pre-fix AOT link dies with undefined
|
||||||
|
> `_pcap_lib_version`. Gates: zig build test 426/426,
|
||||||
|
> tests/run_examples.sh 605/605, distribution repo `make test` 21/21
|
||||||
|
> at its HEAD plus a successful `make build` of its P5.2 branch state
|
||||||
|
> (dist.sx → ops → db → sqlite, the original failing chain).
|
||||||
|
|
||||||
|
## Symptom
|
||||||
|
|
||||||
|
A `#library` declaration in a module that is reached through TWO (or
|
||||||
|
more) levels of aliased `#import` never makes it into the build's
|
||||||
|
library list: `sx build` emits no `-l<name>` on the link line (link
|
||||||
|
fails with `Undefined symbols` for every `#foreign` fn of that
|
||||||
|
library), and `sx run` skips the dlopen of that library (the JIT then
|
||||||
|
resolves the foreign symbols only if some already-loaded image happens
|
||||||
|
to export them).
|
||||||
|
|
||||||
|
- Observed: `main → b :: #import "b.sx" → c :: #import "c.sx"` where
|
||||||
|
c.sx declares `zlib :: #library "z"` → link line ends `-lc` (no
|
||||||
|
`-lz`), `ld: symbol(s) not found: _zlibVersion`.
|
||||||
|
- Expected: every `#library` reachable through the import graph is
|
||||||
|
linked (AOT) / dlopened (JIT), regardless of import depth or
|
||||||
|
aliasing.
|
||||||
|
- Control: aliasing c.sx DIRECTLY from main (one namespace level)
|
||||||
|
produces `-lz` and links fine. A plain (unaliased) `#import` of c.sx
|
||||||
|
from a module that main aliases also works — the merged decls sit at
|
||||||
|
one namespace level.
|
||||||
|
|
||||||
|
## Reproduction
|
||||||
|
|
||||||
|
Three files in one directory; build `a.sx`.
|
||||||
|
|
||||||
|
```sx
|
||||||
|
// c.sx — declares the library + a foreign fn
|
||||||
|
#import "modules/std.sx";
|
||||||
|
|
||||||
|
zlib :: #library "z";
|
||||||
|
zlibVersion :: () -> ?cstring #foreign zlib "zlibVersion";
|
||||||
|
|
||||||
|
zver :: () -> string {
|
||||||
|
p := zlibVersion();
|
||||||
|
if p == null { return ""; }
|
||||||
|
return from_cstring(p!);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```sx
|
||||||
|
// b.sx — first namespace level
|
||||||
|
#import "modules/std.sx";
|
||||||
|
c :: #import "c.sx";
|
||||||
|
|
||||||
|
ver_via_b :: () -> string { return c.zver(); }
|
||||||
|
```
|
||||||
|
|
||||||
|
```sx
|
||||||
|
// a.sx — main; c.sx's library now sits two namespace levels deep
|
||||||
|
#import "modules/std.sx";
|
||||||
|
b :: #import "b.sx";
|
||||||
|
|
||||||
|
main :: () -> i32 {
|
||||||
|
print("zlib {}\n", b.ver_via_b());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sx build -o a a.sx
|
||||||
|
Undefined symbols for architecture arm64:
|
||||||
|
"_zlibVersion", referenced from: ...
|
||||||
|
error: linking failed
|
||||||
|
```
|
||||||
|
|
||||||
|
Replacing a.sx's import with `c :: #import "c.sx"` (and calling
|
||||||
|
`c.zver()` directly) links and prints the zlib version — same code,
|
||||||
|
one namespace level less.
|
||||||
|
|
||||||
|
Found in the distribution repo the first time a product chain nested
|
||||||
|
the SQLite bindings two aliases deep: `dist.sx → ops :: #import
|
||||||
|
"release/ops.sx" → db :: #import "../repo/db.sx" → #import
|
||||||
|
"../db/sqlite.sx"` loses `-lsqlite3` even though the bindings compile
|
||||||
|
fine (the foreign wrappers ARE in main.o; only the link flag is gone).
|
||||||
|
|
||||||
|
## Investigation prompt
|
||||||
|
|
||||||
|
> In /Users/agra/projects/sx: `extractLibraries` (src/main.zig, ~line
|
||||||
|
> 877) walks `root.data.root.decls` and exactly ONE level of
|
||||||
|
> `namespace_decl` children. Aliased imports lower to nested
|
||||||
|
> `namespace_decl` nodes, so a `#library` (or, same pattern,
|
||||||
|
> `#framework` — see `extractFrameworks` right below it, ~line 904)
|
||||||
|
> sitting two or more namespace levels deep is never collected. Both
|
||||||
|
> consumers are affected: the AOT link args (`link(...)` in
|
||||||
|
> src/target.zig receives this list) and the JIT dlopen loop
|
||||||
|
> (src/main.zig ~line 274). Fix: make the walk recursive over
|
||||||
|
> `namespace_decl` (a small explicit stack or recursive helper over
|
||||||
|
> `ns.decls`), dedup as today via the `seen` set; apply the same to
|
||||||
|
> `extractFrameworks`. Verify with the three-file libz repro from
|
||||||
|
> issues/0130-library-decl-nested-namespace-dropped.md: `sx build`
|
||||||
|
> must emit `-lz` and link, `sx run` must dlopen libz; add an
|
||||||
|
> examples/ regression mirroring the repro (one library decl behind
|
||||||
|
> two aliased imports) that fails on pre-fix master. Then re-run the
|
||||||
|
> distribution repo's `make test` (which now links SQLite through
|
||||||
|
> dist.sx's ops→db→sqlite chain) to confirm the original failure is
|
||||||
|
> gone.
|
||||||
72
src/main.zig
72
src/main.zig
@@ -877,54 +877,50 @@ fn hasTopLevelRun(root: *const sx.ast.Node) bool {
|
|||||||
fn extractLibraries(allocator: std.mem.Allocator, root: *const sx.ast.Node) ![]const []const u8 {
|
fn extractLibraries(allocator: std.mem.Allocator, root: *const sx.ast.Node) ![]const []const u8 {
|
||||||
var libs = std.ArrayList([]const u8).empty;
|
var libs = std.ArrayList([]const u8).empty;
|
||||||
var seen = std.StringHashMap(void).init(allocator);
|
var seen = std.StringHashMap(void).init(allocator);
|
||||||
const addLib = struct {
|
// Aliased imports lower to namespace_decl nodes and NEST when a
|
||||||
fn f(l: *std.ArrayList([]const u8), s: *std.StringHashMap(void), a: std.mem.Allocator, name: []const u8) !void {
|
// namespaced module aliases its own imports, so the walk must recurse —
|
||||||
if (s.contains(name)) return;
|
// a `#library` at any namespace depth belongs on the link line / in the
|
||||||
try s.put(name, {});
|
// JIT dlopen list.
|
||||||
try l.append(a, name);
|
const walker = struct {
|
||||||
}
|
fn walk(l: *std.ArrayList([]const u8), s: *std.StringHashMap(void), a: std.mem.Allocator, decls: []const *sx.ast.Node) !void {
|
||||||
}.f;
|
for (decls) |d| {
|
||||||
for (root.data.root.decls) |decl| {
|
switch (d.data) {
|
||||||
switch (decl.data) {
|
.library_decl => |ld| {
|
||||||
.library_decl => |ld| try addLib(&libs, &seen, allocator, ld.lib_name),
|
if (s.contains(ld.lib_name)) continue;
|
||||||
.namespace_decl => |ns| {
|
try s.put(ld.lib_name, {});
|
||||||
for (ns.decls) |nd| {
|
try l.append(a, ld.lib_name);
|
||||||
switch (nd.data) {
|
},
|
||||||
.library_decl => |ld| try addLib(&libs, &seen, allocator, ld.lib_name),
|
.namespace_decl => |ns| try walk(l, s, a, ns.decls),
|
||||||
else => {},
|
else => {},
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
else => {},
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
try walker.walk(&libs, &seen, allocator, root.data.root.decls);
|
||||||
return try libs.toOwnedSlice(allocator);
|
return try libs.toOwnedSlice(allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extractFrameworks(allocator: std.mem.Allocator, root: *const sx.ast.Node) ![]const []const u8 {
|
fn extractFrameworks(allocator: std.mem.Allocator, root: *const sx.ast.Node) ![]const []const u8 {
|
||||||
var fws = std.ArrayList([]const u8).empty;
|
var fws = std.ArrayList([]const u8).empty;
|
||||||
var seen = std.StringHashMap(void).init(allocator);
|
var seen = std.StringHashMap(void).init(allocator);
|
||||||
const addFw = struct {
|
// Same nested-namespace recursion as extractLibraries: `#framework`
|
||||||
fn f(l: *std.ArrayList([]const u8), s: *std.StringHashMap(void), a: std.mem.Allocator, name: []const u8) !void {
|
// declarations behind multiple aliased imports must still be linked.
|
||||||
if (s.contains(name)) return;
|
const walker = struct {
|
||||||
try s.put(name, {});
|
fn walk(l: *std.ArrayList([]const u8), s: *std.StringHashMap(void), a: std.mem.Allocator, decls: []const *sx.ast.Node) !void {
|
||||||
try l.append(a, name);
|
for (decls) |d| {
|
||||||
}
|
switch (d.data) {
|
||||||
}.f;
|
.framework_decl => |fd| {
|
||||||
for (root.data.root.decls) |decl| {
|
if (s.contains(fd.name)) continue;
|
||||||
switch (decl.data) {
|
try s.put(fd.name, {});
|
||||||
.framework_decl => |fd| try addFw(&fws, &seen, allocator, fd.name),
|
try l.append(a, fd.name);
|
||||||
.namespace_decl => |ns| {
|
},
|
||||||
for (ns.decls) |nd| {
|
.namespace_decl => |ns| try walk(l, s, a, ns.decls),
|
||||||
switch (nd.data) {
|
else => {},
|
||||||
.framework_decl => |fd| try addFw(&fws, &seen, allocator, fd.name),
|
|
||||||
else => {},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
else => {},
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
try walker.walk(&fws, &seen, allocator, root.data.root.decls);
|
||||||
return try fws.toOwnedSlice(allocator);
|
return try fws.toOwnedSlice(allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user