@@ -295,6 +295,246 @@ http_archive(
295295- ** Path Handling** : Use Bazel's path utilities
296296- ** Platform Detection** : Use ` @platforms// ` constraints, not ` uname `
297297
298+ ## Dependency Management Patterns
299+
300+ ### 🎯 RULE #2 : STRATIFIED HYBRID APPROACH
301+
302+ ** Use the RIGHT download pattern for each dependency category**
303+
304+ This project uses a ** stratified hybrid approach** to dependency management, selecting the most appropriate mechanism based on the characteristics of each dependency type.
305+
306+ ### Decision Matrix
307+
308+ | Dependency Type | Pattern | Location | Why |
309+ | ----------------| ---------| ----------| -----|
310+ | ** Multi-platform GitHub binaries** | JSON Registry + secure_download | ` checksums/tools/*.json ` | Solves platform × version matrix, central security auditing |
311+ | ** Bazel Central Registry deps** | ` bazel_dep ` | ` MODULE.bazel ` | Ecosystem standard, automatic dependency resolution |
312+ | ** Source builds** | ` git_repository ` | ` wasm_tools_repositories.bzl ` | Bazel standard, maximum flexibility |
313+ | ** Universal WASM binaries** | JSON Registry (preferred) or ` http_file ` | ` checksums/tools/*.json ` or ` MODULE.bazel ` | Platform-independent, security auditable |
314+ | ** NPM packages** | Hermetic npm + package.json | ` toolchains/jco_toolchain.bzl ` | Ecosystem standard, package lock files |
315+
316+ ### Pattern 1: JSON Registry (Multi-Platform GitHub Binaries)
317+
318+ ** Use for** : Tools with different binaries per platform (wasm-tools, wit-bindgen, wac, wkg, wasmtime, wizer, wasi-sdk, nodejs, tinygo)
319+
320+ ** Why** : Elegantly handles the combinatorial explosion of (platforms × versions × URL patterns)
321+
322+ ** Structure** :
323+ ``` json
324+ {
325+ "tool_name" : " wasm-tools" ,
326+ "github_repo" : " bytecodealliance/wasm-tools" ,
327+ "latest_version" : " 1.240.0" ,
328+ "supported_platforms" : [" darwin_amd64" , " darwin_arm64" , " linux_amd64" , " linux_arm64" , " windows_amd64" ],
329+ "versions" : {
330+ "1.240.0" : {
331+ "release_date" : " 2025-10-08" ,
332+ "platforms" : {
333+ "darwin_arm64" : {
334+ "sha256" : " 8959eb9f494af13868af9e13e74e4fa0fa6c9306b492a9ce80f0e576eb10c0c6" ,
335+ "url_suffix" : " aarch64-macos.tar.gz"
336+ }
337+ // ... other platforms
338+ }
339+ }
340+ }
341+ }
342+ ```
343+
344+ ** Usage** :
345+ ``` python
346+ # In toolchain .bzl file
347+ from toolchains.secure_download import secure_download_tool
348+
349+ secure_download_tool(ctx, " wasm-tools" , " 1.240.0" , platform)
350+ ```
351+
352+ ** Benefits** :
353+ - ✅ Single source of truth for all versions and checksums
354+ - ✅ Central security auditing (` checksums/ ` directory)
355+ - ✅ Supports multiple versions side-by-side
356+ - ✅ Platform detection and URL construction automatic
357+ - ✅ Clean API via ` registry.bzl `
358+
359+ ### Pattern 2: Bazel Central Registry (` bazel_dep ` )
360+
361+ ** Use for** : Standard Bazel ecosystem dependencies (rules_rust, bazel_skylib, platforms, rules_cc, etc.)
362+
363+ ** Why** : Bazel's standard mechanism with automatic dependency resolution
364+
365+ ** Structure** :
366+ ``` starlark
367+ # MODULE.bazel
368+ bazel_dep(name = " rules_rust" , version = " 0.65.0" )
369+ bazel_dep(name = " bazel_skylib" , version = " 1.8.1" )
370+ bazel_dep(name = " platforms" , version = " 1.0.0" )
371+ ```
372+
373+ ** Benefits** :
374+ - ✅ Ecosystem standard - no learning curve
375+ - ✅ Automatic transitive dependency resolution
376+ - ✅ Maintained by Bazel team
377+ - ✅ Built-in security and version compatibility
378+
379+ ** Do NOT** :
380+ - ❌ Duplicate BCR deps in JSON registry
381+ - ❌ Use http_archive for tools available in BCR
382+
383+ ### Pattern 3: Git Repository (Source Builds)
384+
385+ ** Use for** : Custom forks, bleeding edge versions, or when source builds are required
386+
387+ ** Why** : Bazel-native source repository management
388+
389+ ** Structure** :
390+ ``` starlark
391+ # wasm_tools_repositories.bzl
392+ load(" @bazel_tools//tools/build_defs/repo:git.bzl" , " git_repository" )
393+
394+ git_repository(
395+ name = " wasm_tools_src" ,
396+ remote = " https://github.com/bytecodealliance/wasm-tools.git" ,
397+ tag = " v1.235.0" ,
398+ build_file = " //toolchains:BUILD.wasm_tools" ,
399+ )
400+ ```
401+
402+ ** When to use** :
403+ - Custom fork with patches
404+ - Need bleeding edge from main branch
405+ - Binary not available for your platform
406+ - Building from source is required for licensing
407+
408+ ** Prefer download over build** : When prebuilt binaries are available and work correctly, use Pattern 1 (JSON Registry) instead for faster, more hermetic builds.
409+
410+ ### Pattern 4: Universal WASM Binaries
411+
412+ ** Use for** : WebAssembly components (platform-independent .wasm files)
413+
414+ ** Preferred** : JSON Registry for consistency and security auditing
415+ ``` json
416+ // checksums/tools/file-ops-component.json
417+ {
418+ "tool_name" : " file-ops-component" ,
419+ "github_repo" : " pulseengine/bazel-file-ops-component" ,
420+ "latest_version" : " 0.1.0-rc.3" ,
421+ "supported_platforms" : [" wasm" ], // Universal
422+ "versions" : {
423+ "0.1.0-rc.3" : {
424+ "release_date" : " 2025-10-15" ,
425+ "platforms" : {
426+ "wasm" : {
427+ "sha256" : " 8a9b1aa8a2c9d3dc36f1724ccbf24a48c473808d9017b059c84afddc55743f1e" ,
428+ "url" : " https://github.com/.../file_ops_component.wasm"
429+ }
430+ }
431+ }
432+ }
433+ }
434+ ```
435+
436+ ** Alternative** : ` http_file ` for very simple cases (legacy)
437+ ``` starlark
438+ # MODULE.bazel (only for simple cases)
439+ http_file(
440+ name = " component_external" ,
441+ url = " https://github.com/.../component.wasm" ,
442+ sha256 = " abc123..." ,
443+ downloaded_file_path = " component.wasm" ,
444+ )
445+ ```
446+
447+ ** Recommendation** : Migrate all WASM components to JSON Registry for:
448+ - Consistent security auditing
449+ - Version management
450+ - Same tooling as other downloads
451+
452+ ### Pattern 5: NPM Packages
453+
454+ ** Use for** : Node.js ecosystem tools (jco, componentize-js)
455+
456+ ** Why** : npm is the standard package manager with lock file support
457+
458+ ** Structure** :
459+ ``` python
460+ # Download hermetic Node.js first (Pattern 1)
461+ secure_download_tool(ctx, " nodejs" , " 20.18.0" , platform)
462+
463+ # Use hermetic npm for package installation
464+ ctx.execute([npm_path,
" install" ,
" @bytecodealliance/[email protected] " ])
465+ ```
466+
467+ ** Benefits** :
468+ - ✅ Hermetic builds (no system Node.js dependency)
469+ - ✅ Package lock files for reproducibility
470+ - ✅ Ecosystem standard
471+
472+ ### Adding New Dependencies
473+
474+ ** Decision Tree** :
475+
476+ 1 . ** Is it in Bazel Central Registry?**
477+ - YES → Use ` bazel_dep ` (Pattern 2)
478+ - NO → Continue to step 2
479+
480+ 2 . ** Is it a GitHub release with platform-specific binaries?**
481+ - YES → Create JSON in ` checksums/tools/ ` (Pattern 1)
482+ - NO → Continue to step 3
483+
484+ 3 . ** Is it a universal WASM component?**
485+ - YES → Create JSON in ` checksums/tools/ ` with platform "wasm" (Pattern 4)
486+ - NO → Continue to step 4
487+
488+ 4 . ** Is it an NPM package?**
489+ - YES → Use hermetic npm installation (Pattern 5)
490+ - NO → Continue to step 5
491+
492+ 5 . ** Must it be built from source?**
493+ - YES → Use ` git_repository ` (Pattern 3)
494+ - NO → Reconsider if this dependency is needed
495+
496+ ### Security Best Practices
497+
498+ 1 . ** Always verify checksums** : All downloads MUST have SHA256 verification
499+ 2 . ** Central audit trail** : Prefer JSON registry for auditability
500+ 3 . ** Version pinning** : Always specify exact versions, never use "latest"
501+ 4 . ** Minimal versions** : Keep only latest stable + previous stable in JSON files
502+ 5 . ** Review changes** : All checksum changes require careful PR review
503+
504+ ### Maintenance Guidelines
505+
506+ ** Adding a new version to JSON registry** :
507+ ``` bash
508+ # 1. Download binaries for all platforms
509+ # 2. Calculate SHA256 checksums
510+ shasum -a 256 wasm-tools-1.241.0-* .tar.gz
511+
512+ # 3. Add version block to JSON file
513+ # 4. Update "latest_version" if appropriate
514+ # 5. Remove old versions if keeping only latest + previous
515+ ```
516+
517+ ** Updating a BCR dependency** :
518+ ``` starlark
519+ # Simply change version in MODULE.bazel
520+ bazel_dep(name = " rules_rust" , version = " 0.66.0" ) # Updated
521+ ```
522+
523+ ** Removing old versions** :
524+ - Keep latest stable version
525+ - Keep previous stable version (for rollback capability)
526+ - Remove all older versions
527+ - Update tests if they pin to old versions
528+
529+ ### Anti-Patterns to Avoid
530+
531+ ❌ ** DO NOT** create custom download mechanisms
532+ ❌ ** DO NOT** hardcode URLs in .bzl files
533+ ❌ ** DO NOT** duplicate BCR dependencies in JSON registry
534+ ❌ ** DO NOT** use http_archive for multi-platform binaries (use JSON registry)
535+ ❌ ** DO NOT** keep more than 2 versions per tool without strong justification
536+ ❌ ** DO NOT** use "strategy options" - pick ONE best approach per tool
537+
298538## Current State
299539
300540### Toolchains Implemented
0 commit comments