shape-runtime 0.3.2

Bytecode compiler, builtins, and runtime infrastructure for Shape
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
//! Module loading and management for Shape
//!
//! This module handles loading, compiling, and caching Shape modules
//! from both the standard library and user-defined sources.

mod cache;
mod loading;
mod resolution;
#[cfg(all(test, feature = "deep-tests"))]
mod resolution_deep_tests;
mod resolver;

use crate::project::{DependencySpec, ProjectRoot, find_project_root, normalize_package_identity};
use shape_ast::ast::{AnnotationDef, FunctionDef, ImportStmt, Program};
use shape_ast::error::{Result, ShapeError};
use shape_ast::parser::parse_program;
use shape_value::KindedSlot;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use cache::ModuleCache;
pub use resolver::{
    FilesystemResolver, InMemoryResolver, ModuleCode, ModuleResolver, ResolvedModuleArtifact,
};

include!(concat!(env!("OUT_DIR"), "/embedded_stdlib_modules.rs"));

/// Known stdlib module leaf names that live under `std::core::`.
///
/// When a bare-name import like `"file"` fails to resolve, we check this list
/// and suggest the canonical `std::core::file` path in the error message.
const KNOWN_STDLIB_LEAF_NAMES: &[&str] = &[
    "file", "json", "http", "crypto", "env", "toml", "yaml", "xml", "compress", "archive",
    "unicode", "csv", "msgpack", "regex", "parallel", "time", "io", "set", "state", "transport",
    "remote",
];

/// If `module_path` is a single-segment name (no `::`) that matches a known stdlib
/// module, return a migration hint string. Otherwise return `None`.
pub fn bare_name_migration_hint(module_path: &str) -> Option<String> {
    // Only trigger for single-segment paths (no `::` separator).
    if module_path.contains("::") {
        return None;
    }
    if KNOWN_STDLIB_LEAF_NAMES.contains(&module_path) {
        let canonical = format!("std::core::{}", module_path);
        Some(format!(
            "Module '{}' not found. Did you mean '{}'?\n  Hint: use {}",
            module_path, canonical, canonical
        ))
    } else {
        None
    }
}

/// A compiled module ready for execution
#[derive(Debug, Clone)]
pub struct Module {
    pub name: String,
    pub path: String,
    pub exports: HashMap<String, Export>,
    pub ast: Program,
}

impl Module {
    /// Get an exported item by name
    pub fn get_export(&self, name: &str) -> Option<&Export> {
        self.exports.get(name)
    }

    /// Get all export names
    pub fn export_names(&self) -> Vec<&str> {
        self.exports.keys().map(|s| s.as_str()).collect()
    }
}

/// An exported item from a module
///
/// Per ADR-006 §2.7.1.2, `Export::Value` is a GENERIC_CARRIER single-
/// value site: the kind of an exported value is not statically known
/// at the module-loader layer (different exports from the same module
/// can carry different `NativeKind`s), so the carrier is `KindedSlot`.
#[derive(Debug, Clone)]
pub enum Export {
    Function(Arc<FunctionDef>),
    TypeAlias(Arc<shape_ast::ast::TypeAliasDef>),
    Annotation(Arc<AnnotationDef>),
    Value(KindedSlot),
}

// Re-export shared module resolution types from shape-ast so that existing
// consumers (`shape-vm`, `shape-lsp`, etc.) can continue to import them from
// `shape_runtime::module_loader::*` without changes.
pub use shape_ast::module_utils::{ModuleExportKind, ModuleExportSymbol};

/// Collect exported symbols from a parsed module AST.
///
/// Delegates to the canonical shared implementation in `shape_ast::module_utils`.
pub fn collect_exported_symbols(program: &Program) -> Result<Vec<ModuleExportSymbol>> {
    shape_ast::module_utils::collect_exported_symbols(program)
}

/// Collect exported function names from module source using canonical
/// module-loader export semantics.
///
/// This keeps extension module namespace behavior (`use mod; mod.fn(...)`)
/// aligned with normal module loading and avoids ad-hoc export parsing.
pub fn collect_exported_function_names_from_source(
    module_path: &str,
    source: &str,
) -> Result<Vec<String>> {
    let ast = parse_program(source).map_err(|e| ShapeError::ModuleError {
        message: format!("Failed to parse module source '{}': {}", module_path, e),
        module_path: None,
    })?;

    let module = loading::compile_module(module_path, ast)?;
    let mut names: Vec<String> = module
        .exports
        .into_iter()
        .filter_map(|(name, export)| match export {
            Export::Function(_) => Some(name),
            _ => None,
        })
        .collect();
    names.sort();
    names.dedup();
    Ok(names)
}

/// Module loader manages loading and caching of modules
pub struct ModuleLoader {
    /// Standard library modules (built-in)
    stdlib_path: PathBuf,
    /// User module search paths
    module_paths: Vec<PathBuf>,
    /// Active project root used to attribute loaded filesystem modules.
    current_project_root: Option<PathBuf>,
    /// Module cache and dependency tracking
    cache: ModuleCache,
    /// Resolved dependency paths (name -> local path).
    /// Populated by the dependency resolver after resolving shape.toml deps.
    dependency_paths: HashMap<String, PathBuf>,
    /// Extension-provided in-memory modules (highest priority).
    extension_resolver: InMemoryResolver,
    /// Bundle-provided in-memory modules (between extension and embedded stdlib).
    bundle_resolver: InMemoryResolver,
    /// Embedded stdlib in-memory modules (before filesystem fallback).
    embedded_stdlib_resolver: InMemoryResolver,
    /// Optional keychain for verifying module signatures.
    keychain: Option<crate::crypto::Keychain>,
    /// Optional external blob store for lazy-fetching content-addressed blobs
    /// that are not found in the inline blob cache.
    blob_store: Option<Arc<dyn crate::blob_store::BlobStore>>,
}

impl ModuleLoader {
    /// Create a new module loader
    pub fn new() -> Self {
        let mut loader = Self {
            stdlib_path: Self::default_stdlib_path(),
            module_paths: Self::default_module_paths(),
            current_project_root: None,
            cache: ModuleCache::new(),
            dependency_paths: HashMap::new(),
            extension_resolver: InMemoryResolver::default(),
            bundle_resolver: InMemoryResolver::default(),
            embedded_stdlib_resolver: InMemoryResolver::default(),
            keychain: None,
            blob_store: None,
        };

        // Add paths from SHAPE_PATH environment variable
        if let Ok(shape_path) = std::env::var("SHAPE_PATH") {
            for path in shape_path.split(':') {
                loader.add_module_path(PathBuf::from(path));
            }
        }

        for (module_path, source) in EMBEDDED_STDLIB_MODULES {
            loader.register_embedded_stdlib_module(
                (*module_path).to_string(),
                ModuleCode::Source(Arc::from(*source)),
            );
        }

        loader
    }

    /// Clone loader configuration (search paths + resolver payloads) without cache state.
    pub fn clone_without_cache(&self) -> Self {
        Self {
            stdlib_path: self.stdlib_path.clone(),
            module_paths: self.module_paths.clone(),
            current_project_root: self.current_project_root.clone(),
            cache: ModuleCache::new(),
            dependency_paths: self.dependency_paths.clone(),
            extension_resolver: self.extension_resolver.clone(),
            bundle_resolver: self.bundle_resolver.clone(),
            embedded_stdlib_resolver: self.embedded_stdlib_resolver.clone(),
            keychain: None,
            blob_store: self.blob_store.clone(),
        }
    }

    /// Get the canonical stdlib path.
    fn default_stdlib_path() -> PathBuf {
        crate::stdlib_metadata::default_stdlib_path()
    }

    /// Get default module search paths
    fn default_module_paths() -> Vec<PathBuf> {
        let mut paths = vec![];

        // Current directory
        paths.push(PathBuf::from("."));

        // Project-specific paths
        paths.push(PathBuf::from(".shape"));
        paths.push(PathBuf::from("shape_modules"));
        paths.push(PathBuf::from("modules"));

        // User home directory paths
        if let Some(home) = dirs::home_dir() {
            paths.push(home.join(".shape/modules"));
            paths.push(home.join(".local/share/shape/modules"));
        }

        // System-wide paths
        paths.push(PathBuf::from("/usr/local/share/shape/modules"));
        paths.push(PathBuf::from("/usr/share/shape/modules"));

        paths
    }

    /// Add a module search path
    pub fn add_module_path(&mut self, path: PathBuf) {
        if !self.module_paths.contains(&path) {
            self.module_paths.push(path);
        }
    }

    /// Set the project root and prepend its configured module paths
    ///
    /// Inserts the project root directory itself plus any extra paths
    /// (typically resolved from shape.toml [modules].paths) at the
    /// front of the search list so project modules take priority.
    pub fn set_project_root(&mut self, root: &std::path::Path, extra_paths: &[PathBuf]) {
        let root_buf = root.to_path_buf();
        self.current_project_root = Some(root_buf.clone());
        // Insert project root first, then extra paths, all at front
        let mut to_prepend = vec![root_buf];
        to_prepend.extend(extra_paths.iter().cloned());
        // Remove duplicates from existing paths, then prepend
        self.module_paths.retain(|p| !to_prepend.contains(p));
        to_prepend.extend(self.module_paths.drain(..));
        self.module_paths = to_prepend;
    }

    /// Configure module paths and dependency paths from workspace/file context.
    pub fn configure_for_context(&mut self, current_file: &Path, workspace_root: Option<&Path>) {
        if let Some(project) = resolve_project_root(current_file, workspace_root) {
            let module_paths = project.resolved_module_paths();
            self.set_project_root(&project.root_path, &module_paths);
            self.set_dependency_paths(resolve_path_dependencies(&project));
        }
    }

    /// Configure module loader for context and register declared extension artifacts.
    ///
    /// This is the canonical context setup path for tooling (LSP/CLI) so
    /// extension module namespaces are resolved through the same loader.
    ///
    /// `extension_schema_cache` is the caller-owned schema cache consulted by
    /// [`crate::extension_context::register_declared_extensions_in_loader`] —
    /// callers that need cross-request reuse (e.g. the LSP) should pass a
    /// long-lived cache; one-shot callers may pass a fresh one.
    pub fn configure_for_context_with_source(
        &mut self,
        current_file: &Path,
        workspace_root: Option<&Path>,
        current_source: Option<&str>,
        extension_schema_cache: &crate::extension_context::ExtensionModuleSchemaCache,
    ) {
        self.configure_for_context(current_file, workspace_root);

        // Fallback: if no shape.toml was found, try frontmatter [dependencies]
        if self.dependency_paths.is_empty() {
            if let Some(source) = current_source {
                let (project, _rest) = crate::frontmatter::parse_frontmatter(source);
                if let Some(config) = project {
                    if !config.dependencies.is_empty() {
                        let root = ProjectRoot {
                            root_path: current_file
                                .parent()
                                .unwrap_or(Path::new("."))
                                .to_path_buf(),
                            config,
                        };
                        let module_paths = root.resolved_module_paths();
                        self.set_project_root(&root.root_path, &module_paths);
                        self.set_dependency_paths(resolve_path_dependencies(&root));
                    }
                }
            }
        }

        crate::extension_context::register_declared_extensions_in_loader(
            self,
            Some(current_file),
            workspace_root,
            current_source,
            extension_schema_cache,
        );
    }

    /// Register resolved dependency paths from `[dependencies]` in shape.toml.
    ///
    /// Each entry maps a package name to its resolved local path. When a module
    /// import matches a dependency name, the loader searches that path first.
    /// If a dependency path points to a `.shapec` bundle file, the bundle is
    /// loaded and its modules are registered in the bundle resolver.
    pub fn set_dependency_paths(&mut self, deps: HashMap<String, PathBuf>) {
        let mut regular_deps = HashMap::new();

        for (name, path) in deps {
            if path.extension().and_then(|e| e.to_str()) == Some("shapec") && path.is_file() {
                // Load the bundle and register its modules
                match crate::package_bundle::PackageBundle::read_from_file(&path) {
                    Ok(bundle) => {
                        self.load_bundle(&bundle, Some(&name));
                    }
                    Err(e) => {
                        eprintln!(
                            "Warning: failed to load bundle dependency '{}' from '{}': {}",
                            name,
                            path.display(),
                            e
                        );
                        // Fall back to treating it as a regular path
                        regular_deps.insert(name, path);
                    }
                }
            } else {
                regular_deps.insert(name, path);
            }
        }

        self.dependency_paths = regular_deps;
    }

    /// Register an extension-provided in-memory module artifact.
    pub fn register_extension_module(&mut self, module_path: impl Into<String>, code: ModuleCode) {
        self.extension_resolver.register(module_path, code);
    }

    /// Register an embedded stdlib in-memory module artifact.
    pub fn register_embedded_stdlib_module(
        &mut self,
        module_path: impl Into<String>,
        code: ModuleCode,
    ) {
        self.embedded_stdlib_resolver.register(module_path, code);
    }

    /// Register modules from a package bundle, optionally prefixed with a dependency name.
    ///
    /// If the bundle contains content-addressed manifests (v2+), those are
    /// registered as `ContentAddressed` modules. Otherwise, legacy compiled
    /// modules are registered as `Compiled`.
    pub fn load_bundle(
        &mut self,
        bundle: &crate::package_bundle::PackageBundle,
        prefix: Option<&str>,
    ) {
        // Register content-addressed modules from manifests (v2 bundles).
        for manifest in &bundle.manifests {
            let path = if let Some(prefix) = prefix {
                format!("{}::{}", prefix, manifest.name)
            } else {
                manifest.name.clone()
            };

            // Collect all blobs referenced by this manifest, including
            // transitive dependencies from the dependency closure.
            let mut module_blobs = HashMap::new();
            for hash in manifest.exports.values() {
                if let Some(data) = bundle.blob_store.get(hash) {
                    module_blobs.insert(*hash, data.clone());
                }
                // Also include transitive dependencies from the closure.
                if let Some(deps) = manifest.dependency_closure.get(hash) {
                    for dep_hash in deps {
                        if let Some(data) = bundle.blob_store.get(dep_hash) {
                            module_blobs.insert(*dep_hash, data.clone());
                        }
                    }
                }
            }
            for hash in manifest.type_schemas.values() {
                if let Some(data) = bundle.blob_store.get(hash) {
                    module_blobs.insert(*hash, data.clone());
                }
            }

            self.register_content_addressed_module(path, manifest, module_blobs);
        }

        // Also register legacy compiled modules.
        for module in &bundle.modules {
            let path = if let Some(prefix) = prefix {
                if module.module_path.is_empty() {
                    prefix.to_string()
                } else {
                    format!("{}::{}", prefix, module.module_path)
                }
            } else {
                module.module_path.clone()
            };

            self.bundle_resolver.register(
                path,
                ModuleCode::Compiled(Arc::from(module.bytecode_bytes.clone().into_boxed_slice())),
            );
        }
    }

    /// Register a content-addressed module from a manifest and its blob data.
    ///
    /// The manifest describes the module's exports and type schemas, each
    /// identified by a content hash. The `blobs` map provides pre-fetched
    /// blob data keyed by hash so the loader doesn't need to hit a remote
    /// store.
    pub fn register_content_addressed_module(
        &mut self,
        module_path: impl Into<String>,
        manifest: &crate::module_manifest::ModuleManifest,
        blobs: HashMap<[u8; 32], Vec<u8>>,
    ) {
        let manifest_bytes =
            rmp_serde::to_vec(manifest).expect("ModuleManifest serialization should not fail");
        self.bundle_resolver.register(
            module_path,
            ModuleCode::ContentAddressed {
                manifest_bytes: Arc::from(manifest_bytes.into_boxed_slice()),
                blob_cache: Arc::new(blobs),
            },
        );
    }

    /// Register bundle modules directly from path/code pairs.
    pub fn register_bundle_modules(&mut self, modules: Vec<(String, ModuleCode)>) {
        for (path, code) in modules {
            self.bundle_resolver.register(path, code);
        }
    }

    /// Set an external blob store for lazy-fetching content-addressed blobs
    /// on cache miss during module loading.
    pub fn set_blob_store(&mut self, store: Arc<dyn crate::blob_store::BlobStore>) {
        self.blob_store = Some(store);
    }

    /// Check whether an extension in-memory module is registered.
    pub fn has_extension_module(&self, module_path: &str) -> bool {
        self.extension_resolver.has(module_path)
    }

    /// List all registered extension in-memory module paths.
    pub fn extension_module_paths(&self) -> Vec<String> {
        self.extension_resolver.module_paths()
    }

    /// List all registered embedded stdlib module paths.
    pub fn embedded_stdlib_module_paths(&self) -> Vec<String> {
        self.embedded_stdlib_resolver.module_paths()
    }

    /// Get the resolved dependency paths.
    pub fn get_dependency_paths(&self) -> &HashMap<String, PathBuf> {
        &self.dependency_paths
    }

    /// Get all module search paths
    pub fn get_module_paths(&self) -> &[PathBuf] {
        &self.module_paths
    }

    /// Get the stdlib path
    pub fn get_stdlib_path(&self) -> &PathBuf {
        &self.stdlib_path
    }

    /// Set the stdlib path
    pub fn set_stdlib_path(&mut self, path: PathBuf) {
        self.stdlib_path = path;
    }

    /// Set the keychain used for module signature verification.
    ///
    /// When set, content-addressed modules are verified against the keychain
    /// before loading. If the keychain requires signatures, unsigned modules
    /// are rejected.
    pub fn set_keychain(&mut self, keychain: crate::crypto::Keychain) {
        self.keychain = Some(keychain);
    }

    /// Get a reference to the configured keychain, if any.
    pub fn keychain(&self) -> Option<&crate::crypto::Keychain> {
        self.keychain.as_ref()
    }

    /// Clear all module search paths (except stdlib)
    pub fn clear_module_paths(&mut self) {
        self.module_paths.clear();
    }

    /// Reset module paths to defaults
    pub fn reset_module_paths(&mut self) {
        self.module_paths = Self::default_module_paths();
    }

    /// Load a module by path
    pub fn load_module(&mut self, module_path: &str) -> Result<Arc<Module>> {
        self.load_module_with_context(module_path, None)
    }

    /// Resolve a module path to an absolute file path.
    pub fn resolve_module_path(&self, module_path: &str) -> Result<PathBuf> {
        self.resolve_module_path_with_context(module_path, None)
    }

    /// Resolve a module path with an optional importer context directory.
    pub fn resolve_module_path_with_context(
        &self,
        module_path: &str,
        context_path: Option<&PathBuf>,
    ) -> Result<PathBuf> {
        resolve_module_path_with_settings(
            module_path,
            context_path.map(|p| p.as_path()),
            self.stdlib_path.as_path(),
            &self.module_paths,
            &self.dependency_paths,
        )
    }

    fn load_module_from_resolved_path(
        &mut self,
        cache_key: String,
        compile_module_path: &str,
        file_path: PathBuf,
    ) -> Result<Arc<Module>> {
        let content = std::fs::read_to_string(&file_path).map_err(|e| ShapeError::ModuleError {
            message: format!("Failed to read module file: {}: {}", file_path.display(), e),
            module_path: Some(file_path.clone()),
        })?;

        // Parse the module
        let ast = parse_program(&content).map_err(|e| ShapeError::ModuleError {
            message: format!("Failed to parse module: {}: {}", compile_module_path, e),
            module_path: None,
        })?;
        let mut ast = ast;
        annotate_program_declaring_module_path(&mut ast, compile_module_path);
        annotate_program_native_abi_package_key(
            &mut ast,
            self.package_key_for_origin_path(Some(&file_path))
                .as_deref(),
        );

        // Process imports to track dependencies
        let dependencies = resolution::extract_dependencies(&ast);
        self.cache
            .store_dependencies(cache_key.clone(), dependencies.clone());

        // Load all dependencies first (with context of current module's directory)
        let module_dir = file_path.parent().map(|p| p.to_path_buf());
        for dep in &dependencies {
            self.load_module_with_context(dep, module_dir.as_ref())?;
        }

        // Compile the module
        let module = loading::compile_module(compile_module_path, ast)?;
        let module = Arc::new(module);

        // Cache it
        self.cache.insert(cache_key, module.clone());

        Ok(module)
    }

    fn load_module_from_source_artifact(
        &mut self,
        cache_key: String,
        compile_module_path: &str,
        source: &str,
        origin_path: Option<PathBuf>,
        context_path: Option<&PathBuf>,
    ) -> Result<Arc<Module>> {
        // Parse the module
        let ast = parse_program(source).map_err(|e| ShapeError::ModuleError {
            message: format!("Failed to parse module: {}: {}", compile_module_path, e),
            module_path: origin_path.clone(),
        })?;
        let mut ast = ast;
        annotate_program_declaring_module_path(&mut ast, compile_module_path);
        annotate_program_native_abi_package_key(
            &mut ast,
            self.package_key_for_origin_path(origin_path.as_deref())
                .as_deref(),
        );

        // Process imports to track dependencies
        let dependencies = resolution::extract_dependencies(&ast);
        self.cache
            .store_dependencies(cache_key.clone(), dependencies.clone());

        // Compile the module (collect AST exports) and cache it before loading
        // dependencies so that cross-module references can find it. Circular
        // dependency detection is handled in load_module_with_context which
        // checks the loading_stack before consulting the cache.
        let module = loading::compile_module(compile_module_path, ast)?;
        let module = Arc::new(module);
        self.cache.insert(cache_key, module.clone());

        // Load all dependencies (with best available context directory).
        let module_dir = origin_path
            .as_ref()
            .and_then(|path| path.parent().map(|p| p.to_path_buf()))
            .or_else(|| context_path.cloned());
        for dep in &dependencies {
            self.load_module_with_context(dep, module_dir.as_ref())?;
        }

        Ok(module)
    }

    fn resolve_module_artifact_with_context(
        &self,
        module_path: &str,
        context_path: Option<&PathBuf>,
    ) -> Result<ResolvedModuleArtifact> {
        let context = context_path.map(|p| p.as_path());

        if let Some(artifact) = self.extension_resolver.resolve(module_path, context)? {
            return Ok(artifact);
        }

        // Check bundle resolver (compiled bundle modules)
        if let Some(artifact) = self.bundle_resolver.resolve(module_path, context)? {
            return Ok(artifact);
        }

        if let Some(artifact) = self
            .embedded_stdlib_resolver
            .resolve(module_path, context)?
        {
            return Ok(artifact);
        }

        let filesystem = FilesystemResolver {
            stdlib_path: self.stdlib_path.as_path(),
            module_paths: &self.module_paths,
            dependency_paths: &self.dependency_paths,
        };

        filesystem
            .resolve(module_path, context)?
            .ok_or_else(|| {
                // Check if this is a bare-name import that should use a canonical path.
                let message = if let Some(hint) = bare_name_migration_hint(module_path) {
                    hint
                } else {
                    format!("Module not found: {}", module_path)
                };
                ShapeError::ModuleError {
                    message,
                    module_path: None,
                }
            })
    }

    /// Load a module with optional context path
    pub fn load_module_with_context(
        &mut self,
        module_path: &str,
        context_path: Option<&PathBuf>,
    ) -> Result<Arc<Module>> {
        // Check for circular dependencies BEFORE the cache check.
        // Modules are cached early (before their dependencies load) so that
        // cross-module references work. Without this order, a cached-but-
        // still-loading module would bypass cycle detection.
        self.cache.check_circular_dependency(module_path)?;

        // Check cache
        if let Some(module) = self.cache.get(module_path) {
            return Ok(module);
        }

        // Resolve module artifact from chained resolvers.
        let artifact = self.resolve_module_artifact_with_context(module_path, context_path)?;
        // Add to loading stack and ensure cleanup even on early error returns.
        self.cache.push_loading(module_path.to_string());
        let result = match artifact.code {
            ModuleCode::Source(source) => self.load_module_from_source_artifact(
                module_path.to_string(),
                module_path,
                source.as_ref(),
                artifact.origin_path,
                context_path,
            ),
            ModuleCode::Both { source, .. } => self.load_module_from_source_artifact(
                module_path.to_string(),
                module_path,
                source.as_ref(),
                artifact.origin_path,
                context_path,
            ),
            ModuleCode::Compiled(_compiled) => {
                // Create a minimal Module for compiled-only artifacts.
                // The bytecode will be loaded and executed by the VM directly.
                let module = Module {
                    name: module_path
                        .split("::")
                        .last()
                        .unwrap_or(module_path)
                        .to_string(),
                    path: module_path.to_string(),
                    exports: HashMap::new(), // VM resolves exports from bytecode at execution time
                    ast: shape_ast::ast::Program {
                        items: vec![],
                        docs: shape_ast::ast::ProgramDocs::default(),
                    },
                };
                let module = Arc::new(module);
                self.cache.insert(module_path.to_string(), module.clone());
                Ok(module)
            }
            ModuleCode::ContentAddressed {
                manifest_bytes,
                blob_cache,
            } => {
                // Deserialize the manifest to discover export names.
                let manifest: crate::module_manifest::ModuleManifest =
                    rmp_serde::from_slice(&manifest_bytes).map_err(|e| {
                        ShapeError::ModuleError {
                            message: format!(
                                "Failed to deserialize manifest for '{}': {}",
                                module_path, e
                            ),
                            module_path: None,
                        }
                    })?;

                // Verify manifest integrity (hash matches content).
                if !manifest.verify_integrity() {
                    return Err(ShapeError::ModuleError {
                        message: format!(
                            "Manifest integrity check failed for '{}': content hash mismatch",
                            module_path
                        ),
                        module_path: None,
                    });
                }

                // Verify signature against keychain when configured.
                if let Some(keychain) = &self.keychain {
                    let sig_data =
                        manifest
                            .signature
                            .as_ref()
                            .map(|sig| crate::crypto::ModuleSignatureData {
                                author_key: sig.author_key,
                                signature: sig.signature.clone(),
                                signed_at: sig.signed_at,
                            });
                    let result = keychain.verify_module(
                        &manifest.name,
                        &manifest.manifest_hash,
                        sig_data.as_ref(),
                    );
                    if let crate::crypto::VerifyResult::Rejected(reason) = result {
                        return Err(ShapeError::ModuleError {
                            message: format!(
                                "Signature verification failed for '{}': {}",
                                module_path, reason
                            ),
                            module_path: None,
                        });
                    }
                }

                // Build an exports map from the manifest. The actual blobs
                // are resolved lazily by the VM via the blob cache / blob store.
                // For the runtime's Module representation, we record export
                // names so import resolution can verify symbol existence.
                let mut exports = HashMap::new();
                for export_name in manifest.exports.keys() {
                    // Register a placeholder function export. The VM will
                    // resolve the real blob at execution time using the hash.
                    let placeholder_fn = shape_ast::ast::FunctionDef {
                        name: export_name.clone(),
                        name_span: shape_ast::ast::Span::default(),
                        declaring_module_path: None,
                        doc_comment: None,
                        params: vec![],
                        body: vec![],
                        return_type: None,
                        is_async: false,
                        is_comptime: false,
                        type_params: None,
                        where_clause: None,
                        annotations: vec![],
                    };
                    exports.insert(
                        export_name.clone(),
                        Export::Function(Arc::new(placeholder_fn)),
                    );
                }

                // Store the blob cache entries into the bundle resolver so
                // downstream loaders (VM) can fetch them by hash.
                for (hash, data) in blob_cache.iter() {
                    let hex_key = format!("__blob__{}", hex::encode(hash));
                    self.bundle_resolver.register(
                        hex_key,
                        ModuleCode::Compiled(Arc::from(data.clone().into_boxed_slice())),
                    );
                }

                // Fetch any missing blobs from the external BlobStore,
                // including transitive dependencies from the dependency closure.
                if let Some(ref store) = self.blob_store {
                    for (_name, hash) in manifest.exports.iter() {
                        let all_hashes: Vec<&[u8; 32]> = std::iter::once(hash)
                            .chain(
                                manifest
                                    .dependency_closure
                                    .get(hash)
                                    .into_iter()
                                    .flat_map(|v| v.iter()),
                            )
                            .collect();
                        for h in all_hashes {
                            let hex_key = format!("__blob__{}", hex::encode(h));
                            if !self.bundle_resolver.has(&hex_key) {
                                if let Some(data) = store.get(h) {
                                    self.bundle_resolver.register(
                                        hex_key,
                                        ModuleCode::Compiled(Arc::from(data.into_boxed_slice())),
                                    );
                                }
                            }
                        }
                    }
                }

                let module = Module {
                    name: manifest.name.clone(),
                    path: module_path.to_string(),
                    exports,
                    ast: shape_ast::ast::Program {
                        items: vec![],
                        docs: shape_ast::ast::ProgramDocs::default(),
                    },
                };
                let module = Arc::new(module);
                self.cache.insert(module_path.to_string(), module.clone());
                Ok(module)
            }
        };
        self.cache.pop_loading();
        result
    }

    /// Load and compile a module directly from an absolute/relative file path.
    ///
    /// Uses the same parsing/export/dependency logic as `load_module(...)`,
    /// but keys the cache by canonical file path.
    pub fn load_module_from_file(&mut self, file_path: &Path) -> Result<Arc<Module>> {
        let canonical = file_path
            .canonicalize()
            .unwrap_or_else(|_| file_path.to_path_buf());
        let cache_key = canonical.to_string_lossy().to_string();

        // Check cache first
        if let Some(module) = self.cache.get(&cache_key) {
            return Ok(module);
        }

        // Check for circular dependency
        self.cache.check_circular_dependency(&cache_key)?;

        self.cache.push_loading(cache_key.clone());
        let result = self.load_module_from_resolved_path(cache_key.clone(), &cache_key, canonical);
        self.cache.pop_loading();

        result
    }

    /// List all `std::core::...` import paths available in the configured stdlib.
    pub fn list_core_stdlib_module_imports(&self) -> Result<Vec<String>> {
        let mut embedded: Vec<String> = self
            .embedded_stdlib_resolver
            .module_paths()
            .into_iter()
            .filter(|name| name.starts_with("std::core::"))
            .collect();
        if !embedded.is_empty() {
            embedded.sort();
            embedded.dedup();
            return Ok(embedded);
        }

        if !self.stdlib_path.exists() || !self.stdlib_path.is_dir() {
            return Err(ShapeError::ModuleError {
                message: format!(
                    "Could not find stdlib directory at {}",
                    self.stdlib_path.display()
                ),
                module_path: Some(self.stdlib_path.clone()),
            });
        }

        resolution::list_core_stdlib_module_imports(self.stdlib_path.as_path())
    }

    /// List all `std::...` import paths available in the configured stdlib.
    pub fn list_stdlib_module_imports(&self) -> Result<Vec<String>> {
        let mut embedded: Vec<String> = self
            .embedded_stdlib_resolver
            .module_paths()
            .into_iter()
            .filter(|name| name.starts_with("std::"))
            .collect();
        if !embedded.is_empty() {
            embedded.sort();
            embedded.dedup();
            return Ok(embedded);
        }

        if !self.stdlib_path.exists() || !self.stdlib_path.is_dir() {
            return Err(ShapeError::ModuleError {
                message: format!(
                    "Could not find stdlib directory at {}",
                    self.stdlib_path.display()
                ),
                module_path: Some(self.stdlib_path.clone()),
            });
        }

        resolution::list_stdlib_module_imports(self.stdlib_path.as_path())
    }

    /// List all importable modules for a given workspace/file context.
    ///
    /// Includes:
    /// - `std::...` modules from stdlib
    /// - project root modules
    /// - `[modules].paths` entries from `shape.toml`
    /// - path dependencies from `shape.toml` (`[dependencies]`)
    /// - local fallback modules near `current_file` when outside a project
    pub fn list_importable_modules_with_context(
        &self,
        current_file: &Path,
        workspace_root: Option<&Path>,
    ) -> Vec<String> {
        let mut modules = self.list_stdlib_module_imports().unwrap_or_default();

        modules.extend(self.embedded_stdlib_resolver.module_paths());
        modules.extend(self.extension_resolver.module_paths());

        if let Some(project) = resolve_project_root(current_file, workspace_root) {
            modules.extend(
                resolution::list_modules_from_root(&project.root_path, None).unwrap_or_default(),
            );

            for module_path in project.resolved_module_paths() {
                modules.extend(
                    resolution::list_modules_from_root(&module_path, None).unwrap_or_default(),
                );
            }

            for (dep_name, dep_root) in resolve_path_dependencies(&project) {
                modules.extend(
                    resolution::list_modules_from_root(&dep_root, Some(dep_name.as_str()))
                        .unwrap_or_default(),
                );
            }
        } else if let Some(context_dir) = current_file.parent() {
            modules
                .extend(resolution::list_modules_from_root(context_dir, None).unwrap_or_default());
        }

        modules.sort();
        modules.dedup();
        modules.retain(|m| !m.is_empty());
        modules
    }

    /// Load `std::core::...` modules via the canonical module resolution pipeline.
    pub fn load_core_stdlib_modules(&mut self) -> Result<Vec<Arc<Module>>> {
        let mut modules = Vec::new();
        for import_path in self.list_core_stdlib_module_imports()? {
            modules.push(self.load_module(&import_path)?);
        }
        Ok(modules)
    }

    /// Load the standard library modules
    pub fn load_stdlib(&mut self) -> Result<()> {
        let _ = self.load_core_stdlib_modules()?;
        Ok(())
    }

    /// Get all loaded modules
    pub fn loaded_modules(&self) -> Vec<&str> {
        self.cache.loaded_modules()
    }

    /// Get a specific export from a module
    pub fn get_export(&self, module_path: &str, export_name: &str) -> Option<&Export> {
        self.cache.get_export(module_path, export_name)
    }

    /// Get a module by path
    pub fn get_module(&self, module_path: &str) -> Option<&Arc<Module>> {
        self.cache.get_module(module_path)
    }

    /// Resolve an import statement to actual exports
    pub fn resolve_import(&mut self, import_stmt: &ImportStmt) -> Result<HashMap<String, Export>> {
        let module = self.load_module(&import_stmt.from)?;
        cache::resolve_import(import_stmt, &module)
    }

    /// Clear the module cache
    pub fn clear_cache(&mut self) {
        self.cache.clear();
    }

    /// Get module dependencies
    pub fn get_dependencies(&self, module_path: &str) -> Option<&Vec<String>> {
        self.cache.get_dependencies(module_path)
    }

    /// Get all module dependencies recursively
    pub fn get_all_dependencies(&self, module_path: &str) -> Vec<String> {
        self.cache.get_all_dependencies(module_path)
    }

    fn package_key_for_origin_path(&self, origin_path: Option<&Path>) -> Option<String> {
        let origin_path = origin_path?;
        let origin = origin_path
            .canonicalize()
            .unwrap_or_else(|_| origin_path.to_path_buf());

        for dep_root in self.dependency_paths.values() {
            let dep_root = dep_root.canonicalize().unwrap_or_else(|_| dep_root.clone());
            if origin.starts_with(&dep_root)
                && let Some(project) = find_project_root(&dep_root)
            {
                return Some(normalize_package_identity(&project.root_path, &project.config).2);
            }
        }

        if let Some(project_root) = &self.current_project_root {
            let project_root = project_root
                .canonicalize()
                .unwrap_or_else(|_| project_root.clone());
            if origin.starts_with(&project_root)
                && let Some(project) = find_project_root(&project_root)
            {
                return Some(normalize_package_identity(&project.root_path, &project.config).2);
            }
        }

        None
    }
}

fn annotate_program_native_abi_package_key(program: &mut Program, package_key: Option<&str>) {
    let Some(package_key) = package_key else {
        return;
    };
    for item in &mut program.items {
        annotate_item_native_abi_package_key(item, package_key);
    }
}

fn annotate_program_declaring_module_path(program: &mut Program, module_path: &str) {
    for item in &mut program.items {
        annotate_item_declaring_module_path(item, module_path);
    }
}

fn annotate_item_native_abi_package_key(item: &mut shape_ast::ast::Item, package_key: &str) {
    use shape_ast::ast::{ExportItem, Item};

    match item {
        Item::ForeignFunction(def, _) => {
            if let Some(native) = def.native_abi.as_mut()
                && native.package_key.is_none()
            {
                native.package_key = Some(package_key.to_string());
            }
        }
        Item::Export(export, _) => {
            if let ExportItem::ForeignFunction(def) = &mut export.item
                && let Some(native) = def.native_abi.as_mut()
                && native.package_key.is_none()
            {
                native.package_key = Some(package_key.to_string());
            }
        }
        Item::Module(module, _) => {
            for nested in &mut module.items {
                annotate_item_native_abi_package_key(nested, package_key);
            }
        }
        _ => {}
    }
}

fn annotate_item_declaring_module_path(item: &mut shape_ast::ast::Item, module_path: &str) {
    use shape_ast::ast::{ExportItem, Item};

    match item {
        Item::Function(def, _) => {
            if def.declaring_module_path.is_none() {
                def.declaring_module_path = Some(module_path.to_string());
            }
        }
        Item::Export(export, _) => match &mut export.item {
            ExportItem::Function(def) => {
                if def.declaring_module_path.is_none() {
                    def.declaring_module_path = Some(module_path.to_string());
                }
            }
            ExportItem::ForeignFunction(_) => {}
            _ => {}
        },
        Item::Extend(extend, _) => {
            for method in &mut extend.methods {
                if method.declaring_module_path.is_none() {
                    method.declaring_module_path = Some(module_path.to_string());
                }
            }
        }
        Item::Impl(impl_block, _) => {
            for method in &mut impl_block.methods {
                if method.declaring_module_path.is_none() {
                    method.declaring_module_path = Some(module_path.to_string());
                }
            }
        }
        Item::Module(module, _) => {
            let nested_path = format!("{}::{}", module_path, module.name);
            for nested in &mut module.items {
                annotate_item_declaring_module_path(nested, &nested_path);
            }
        }
        _ => {}
    }
}

impl Default for ModuleLoader {
    fn default() -> Self {
        Self::new()
    }
}

/// Canonical module resolution entrypoint shared by runtime, VM, and tooling.
pub fn resolve_module_path_with_settings(
    module_path: &str,
    context_path: Option<&Path>,
    stdlib_path: &Path,
    module_paths: &[PathBuf],
    dependency_paths: &HashMap<String, PathBuf>,
) -> Result<PathBuf> {
    resolution::resolve_module_path_with_context(
        module_path,
        context_path,
        stdlib_path,
        module_paths,
        dependency_paths,
    )
}

fn resolve_project_root(current_file: &Path, workspace_root: Option<&Path>) -> Option<ProjectRoot> {
    workspace_root
        .and_then(find_project_root)
        .or_else(|| current_file.parent().and_then(find_project_root))
}

fn resolve_path_dependencies(project: &ProjectRoot) -> HashMap<String, PathBuf> {
    let mut resolved = HashMap::new();

    for (name, spec) in &project.config.dependencies {
        if let DependencySpec::Detailed(detailed) = spec {
            if let Some(path) = &detailed.path {
                let dep_path = project.root_path.join(path);
                let canonical = dep_path.canonicalize().unwrap_or(dep_path);
                resolved.insert(name.clone(), canonical);
            }
        }
    }

    resolved
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    #[test]
    fn test_compile_module_exports_function() {
        let source = r#"
pub fn greet(name) {
    return "Hello, " + name
}
"#;
        let ast = parse_program(source).unwrap();
        let module = loading::compile_module("test_module", ast).unwrap();

        assert!(
            module.exports.contains_key("greet"),
            "Expected 'greet' export, got: {:?}",
            module.exports.keys().collect::<Vec<_>>()
        );

        match module.exports.get("greet") {
            Some(Export::Function(func)) => {
                assert_eq!(func.name, "greet");
            }
            other => panic!("Expected Function export, got: {:?}", other),
        }
    }

    #[test]
    fn test_collect_exported_function_names_from_source() {
        let source = r#"
fn hidden() { 0 }
pub fn connect(uri) { uri }
pub fn ping() { 1 }
"#;
        let names = collect_exported_function_names_from_source("duckdb", source)
            .expect("should collect exported functions");
        assert_eq!(names, vec!["connect".to_string(), "ping".to_string()]);
    }

    #[test]
    fn test_stdlib_methods_are_annotated_with_declaring_module_path() {
        let mut loader = ModuleLoader::new();
        let module = loader
            .load_module("std::core::json_value")
            .expect("load stdlib module");

        let extend = module
            .ast
            .items
            .iter()
            .find_map(|item| match item {
                shape_ast::ast::Item::Extend(extend, _) => Some(extend),
                _ => None,
            })
            .expect("json_value module should contain an extend block");
        let method = extend
            .methods
            .iter()
            .find(|method| method.name == "get")
            .expect("json_value extend block should contain get()");

        assert_eq!(
            method.declaring_module_path.as_deref(),
            Some("std::core::json_value")
        );
    }

    #[test]
    fn test_load_module_from_temp_file() {
        use std::io::Write;

        // Create a temp file with a module
        let temp_dir = std::env::temp_dir();
        let module_path = temp_dir.join("test_load_module.shape");
        let mut file = std::fs::File::create(&module_path).unwrap();
        writeln!(
            file,
            r#"
pub fn add(a, b) {{
    return a + b
}}
"#
        )
        .unwrap();

        // Create loader and add temp dir to search paths
        let mut loader = ModuleLoader::new();
        loader.add_module_path(temp_dir.clone());

        // Load the module via search path (relative imports no longer supported)
        let result = loader.load_module_with_context("test_load_module", Some(&temp_dir));

        // Clean up
        std::fs::remove_file(&module_path).ok();

        // Verify
        let module = result.expect("Module should load");
        assert!(
            module.exports.contains_key("add"),
            "Expected 'add' export, got: {:?}",
            module.exports.keys().collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_load_module_from_file_path() {
        use std::io::Write;

        let temp_dir = tempfile::tempdir().expect("temp dir");
        let module_path = temp_dir.path().join("helpers.shape");
        let mut file = std::fs::File::create(&module_path).expect("create module");
        writeln!(
            file,
            r#"
pub fn helper(x) {{
    x
}}
"#
        )
        .expect("write module");

        let mut loader = ModuleLoader::new();
        let module = loader
            .load_module_from_file(&module_path)
            .expect("module should load from file path");
        assert!(
            module.exports.contains_key("helper"),
            "Expected 'helper' export, got: {:?}",
            module.exports.keys().collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_loaded_dependency_module_annotates_native_abi_with_package_key() {
        let root = tempfile::tempdir().expect("tempdir");
        let dep_root = root.path().join("dep_pkg");

        std::fs::create_dir_all(&dep_root).expect("create dep root");
        std::fs::write(
            root.path().join("shape.toml"),
            r#"
[project]
name = "app"
version = "0.1.0"

[dependencies]
dep_pkg = { path = "./dep_pkg" }
"#,
        )
        .expect("write root shape.toml");
        std::fs::write(
            dep_root.join("shape.toml"),
            r#"
[project]
name = "dep_pkg"
version = "1.2.3"
"#,
        )
        .expect("write dep shape.toml");
        std::fs::write(
            dep_root.join("index.shape"),
            r#"
extern C fn dep_call() -> i32 from "shared";
"#,
        )
        .expect("write dep source");

        let mut loader = ModuleLoader::new();
        loader.set_project_root(root.path(), &[]);
        loader.set_dependency_paths(HashMap::from([("dep_pkg".to_string(), dep_root.clone())]));

        let module = loader.load_module("dep_pkg").expect("load dep module");
        let foreign = module
            .ast
            .items
            .iter()
            .find_map(|item| match item {
                shape_ast::ast::Item::ForeignFunction(def, _) => Some(def),
                _ => None,
            })
            .expect("foreign function should exist");
        let native = foreign
            .native_abi
            .as_ref()
            .expect("native abi should exist");
        assert_eq!(native.package_key.as_deref(), Some("dep_pkg@1.2.3"));
    }

    #[test]
    fn test_collect_exported_symbols_detects_pub_function_and_enum() {
        let source = r#"
pub fn helper() { 1 }
pub enum Side { Buy, Sell }
"#;
        let ast = parse_program(source).unwrap();
        let exports = collect_exported_symbols(&ast).unwrap();

        let helper = exports
            .iter()
            .find(|e| e.name == "helper")
            .expect("expected helper export");
        assert_eq!(helper.name, "helper");
        assert!(helper.alias.is_none());
        assert_eq!(helper.kind, ModuleExportKind::Function);

        let side = exports
            .iter()
            .find(|e| e.name == "Side")
            .expect("expected Side export");
        assert_eq!(side.kind, ModuleExportKind::Enum);
    }

    #[test]
    fn test_collect_exported_symbols_detects_pub_annotation_and_builtin_exports() {
        let source = r#"
pub builtin fn execute(addr: string, code: string) -> string;
pub builtin type RemoteHandle;
pub annotation remote(addr) {
    metadata() { return { addr: addr }; }
}
"#;
        let ast = parse_program(source).unwrap();
        let exports = collect_exported_symbols(&ast).unwrap();

        let execute = exports
            .iter()
            .find(|e| e.name == "execute")
            .expect("expected execute export");
        assert_eq!(execute.kind, ModuleExportKind::BuiltinFunction);

        let handle = exports
            .iter()
            .find(|e| e.name == "RemoteHandle")
            .expect("expected RemoteHandle export");
        assert_eq!(handle.kind, ModuleExportKind::BuiltinType);

        let remote = exports
            .iter()
            .find(|e| e.name == "remote")
            .expect("expected remote annotation export");
        assert_eq!(remote.kind, ModuleExportKind::Annotation);
    }

    #[test]
    fn test_compile_module_exports_annotation() {
        let source = r#"
pub annotation remote(addr) {
    metadata() { return { addr: addr }; }
}
"#;
        let ast = parse_program(source).unwrap();
        let module = loading::compile_module("test_module", ast).unwrap();

        match module.exports.get("remote") {
            Some(Export::Annotation(annotation)) => {
                assert_eq!(annotation.name, "remote");
            }
            other => panic!("Expected Annotation export, got: {:?}", other),
        }
    }

    #[test]
    fn test_list_core_stdlib_module_imports_contains_core_modules() {
        let loader = ModuleLoader::new();
        let modules = loader
            .list_core_stdlib_module_imports()
            .expect("should list std.core modules");

        assert!(
            !modules.is_empty(),
            "expected non-empty std.core module list"
        );
        assert!(
            modules.iter().all(|m| m.starts_with("std::core::")),
            "expected std::core::* import paths, got: {:?}",
            modules
        );
        assert!(
            modules.iter().any(|m| m == "std::core::math"),
            "expected std::core::math in core module list"
        );
    }

    #[test]
    fn test_list_stdlib_module_imports_includes_non_core_namespaces() {
        let loader = ModuleLoader::new();
        let modules = loader
            .list_stdlib_module_imports()
            .expect("should list stdlib modules");

        assert!(
            modules.iter().any(|m| m.starts_with("std::finance::")),
            "expected finance stdlib modules in list, got: {:?}",
            modules
        );
    }

    #[test]
    fn test_embedded_stdlib_loads_without_filesystem_path() {
        let mut loader = ModuleLoader::new();
        loader.set_stdlib_path(std::env::temp_dir().join("shape_missing_stdlib_dir"));

        let module = loader
            .load_module("std::core::snapshot")
            .expect("embedded stdlib module should load without filesystem stdlib");
        assert!(
            module.exports.contains_key("snapshot"),
            "expected snapshot export from std::core::snapshot"
        );
    }

    #[test]
    fn test_list_importable_modules_with_context_includes_project_and_deps() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();

        std::fs::write(
            root.join("shape.toml"),
            r#"
[modules]
paths = ["lib"]

[dependencies]
mydep = { path = "deps/mydep" }
"#,
        )
        .unwrap();

        std::fs::create_dir_all(root.join("src")).unwrap();
        std::fs::create_dir_all(root.join("lib")).unwrap();
        std::fs::create_dir_all(root.join("deps/mydep")).unwrap();

        std::fs::write(root.join("src/main.shape"), "let x = 1").unwrap();
        std::fs::write(root.join("lib/tools.shape"), "pub fn tool() { 1 }").unwrap();
        std::fs::write(root.join("deps/mydep/index.shape"), "pub fn root() { 1 }").unwrap();
        std::fs::write(root.join("deps/mydep/util.shape"), "pub fn util() { 1 }").unwrap();

        let loader = ModuleLoader::new();
        let modules =
            loader.list_importable_modules_with_context(&root.join("src/main.shape"), None);

        assert!(
            modules.iter().any(|m| m == "tools"),
            "expected module path from [modules].paths, got: {:?}",
            modules
        );
        assert!(
            modules.iter().any(|m| m == "mydep"),
            "expected dependency index module path, got: {:?}",
            modules
        );
        assert!(
            modules.iter().any(|m| m == "mydep::util"),
            "expected dependency submodule path, got: {:?}",
            modules
        );
    }

    #[test]
    fn test_load_in_memory_extension_module() {
        let mut loader = ModuleLoader::new();
        loader.register_extension_module(
            "duckdb",
            ModuleCode::Source(Arc::from(
                r#"
pub fn connect(uri) { uri }
"#,
            )),
        );

        let module = loader
            .load_module("duckdb")
            .expect("in-memory extension module should load");
        assert!(
            module.exports.contains_key("connect"),
            "expected connect export, got {:?}",
            module.exports.keys().collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_load_in_memory_extension_module_with_dependency() {
        let mut loader = ModuleLoader::new();
        loader.register_extension_module(
            "b",
            ModuleCode::Source(Arc::from(
                r#"
pub fn answer() { 42 }
"#,
            )),
        );
        loader.register_extension_module(
            "a",
            ModuleCode::Source(Arc::from(
                r#"
from b use { answer }
pub fn use_answer() { answer() }
"#,
            )),
        );

        let module = loader
            .load_module("a")
            .expect("in-memory module with dependency should load");
        assert!(
            module.exports.contains_key("use_answer"),
            "expected use_answer export"
        );
        assert!(
            loader.get_module("b").is_some(),
            "dependency module b should load"
        );
    }

    #[test]
    fn test_load_bundle_modules() {
        use crate::package_bundle::{BundleMetadata, BundledModule, PackageBundle};

        let bundle = PackageBundle {
            metadata: BundleMetadata {
                name: "test".to_string(),
                version: "0.1.0".to_string(),
                compiler_version: "0.5.0".to_string(),
                source_hash: "abc".to_string(),
                bundle_kind: "portable-bytecode".to_string(),
                build_host: "x86_64-linux".to_string(),
                native_portable: true,
                entry_module: None,
                built_at: 0,
                readme: None,
            },
            modules: vec![BundledModule {
                module_path: "helpers".to_string(),
                bytecode_bytes: vec![1, 2, 3],
                export_names: vec!["helper".to_string()],
                source_hash: "def".to_string(),
            }],
            dependencies: std::collections::HashMap::new(),
            blob_store: std::collections::HashMap::new(),
            manifests: vec![],
            native_dependency_scopes: vec![],
            docs: std::collections::HashMap::new(),
        };

        let mut loader = ModuleLoader::new();
        loader.load_bundle(&bundle, Some("mylib"));

        // The bundle module should be resolvable
        let artifact = loader.resolve_module_artifact_with_context("mylib::helpers", None);
        assert!(artifact.is_ok(), "bundle module should be resolvable");
    }

    #[test]
    fn test_frontmatter_dependencies_resolve_without_shape_toml() {
        use std::io::Write;

        let temp_dir = tempfile::tempdir().expect("temp dir");
        let dep_dir = temp_dir.path().join("my_dep");
        std::fs::create_dir_all(&dep_dir).expect("create dep dir");

        // Write a dependency module
        let mut dep_file = std::fs::File::create(dep_dir.join("index.shape")).expect("create dep");
        writeln!(dep_file, "pub fn helper(x) {{ x + 1 }}").expect("write dep");

        // Write a main file with frontmatter dependencies (no shape.toml)
        let main_path = temp_dir.path().join("main.shape");
        let source = format!(
            "---\n[dependencies]\nmy_dep = {{ path = \"{}\" }}\n---\nimport my_dep\nmy_dep::helper(1)\n",
            dep_dir.display()
        );

        let mut loader = ModuleLoader::new();
        let cache = crate::extension_context::ExtensionModuleSchemaCache::new();
        loader.configure_for_context_with_source(&main_path, None, Some(&source), &cache);

        // Verify that dependency paths were set
        assert!(
            loader.dependency_paths.contains_key("my_dep"),
            "frontmatter dependency should be registered, got: {:?}",
            loader.dependency_paths.keys().collect::<Vec<_>>()
        );
    }
}