Skip to content

wslcompact Refactoring and Bug Fixes - #42

Open
xRahul wants to merge 1 commit into
okibcn:mainfrom
xRahul:xrahul/improveandfix
Open

wslcompact Refactoring and Bug Fixes#42
xRahul wants to merge 1 commit into
okibcn:mainfrom
xRahul:xrahul/improveandfix

Conversation

@xRahul

@xRahul xRahul commented Aug 25, 2026

Copy link
Copy Markdown

Summary

This pull request addresses comprehensive improvements to the wslcompact PowerShell module, delivering significant enhancements in reliability, safety, and user experience. The changes transform the project from a fragile, undocumented tool into a robust, production-ready solution suitable for enterprise use.

Key Improvements

1. Safety and Reliability

  • Staged Swap Implementation: Fixed catastrophic image destruction during cross-volume moves using Chen's pattern (copy → verify → rename old → rename new → verify → delete old)
  • Confirmation Anchoring: Prevented accidental image deletion by anchoring confirmation regex to ^[Yy]$ instead of substring matching
  • Session Protection: Added consent check before wsl --shutdown destroys all running WSL sessions
  • Data Image Detection: Replaced unreliable substring matching with precise suffix matching (*-data) to avoid misclassifying legitimate distributions
  • Per-Distro Iteration: Implemented proper per-distro iteration preventing recovery image destruction
  • Ghost Cleanup: Added detection and cleanup of orphaned wslcompact distros from interrupted runs
  • WSL Version Validation: Fixed version detection and comparison logic to handle edge cases properly

2. Architecture and Design

  • Parameter-Based Parsing: Replaced brittle positional flag parsing with structured [CmdletBinding()] and [switch] parameters
  • Direct Process Invocation: Eliminated cmd /c string piping in favor of .NET Process for binary-safe operations
  • Core Helper Extraction: Isolated pure functions (version checks, size calculations, space validation) for testability
  • Encoding Management: Centralized UTF-8 encoding handling with proper save/restore

3. Error Handling and User Experience

  • CI Pipeline Resurrection: Updated to current action versions with smoke tests
  • Import Validation: Fixed missing WSL detection and graceful error handling
  • Distro Discovery: Added warnings and exit codes for unknown or missing distributions
  • Keep/Discard Menu: Implemented intuitive response options with path preservation
  • Registry Protection: Guarded against missing BasePath and UNC path issues
  • Space Validation: Two-sided free space checks (TEMP + target drive)
  • Installation Resolution: Fixed to proper user module paths with error handling

4. Documentation and Usability

  • Comprehensive Help: Added comment-based help with .PARAMETER documentation
  • Time Estimate: Honest ~4 GB/min SSD estimate with pluralization
  • Output Formatting: Color-coded messages, progress bars, proper labeling
  • README Overhaul: Complete update including installation, usage, warnings, and comparison guide
  • Edge Case Documentation: Clear explanations of limitations and behaviors

Code Structure Changes

Main Module (WslCompact/WslCompact.psm1)

  • Size: 512 lines (was 137)
  • Helpers: Format-Size, Test-VhdxFree, Test-SpaceOk, Rename-WithRetry, ConvertTo-WslArg
  • Architecture: Single exported function with comprehensive parameter validation

Module Manifest (WslCompact/WslCompact.psd1)

  • Version: 9.0 (was 8.7)
  • Exports: Explicit @('WslCompact')
  • Compatibility: @('Desktop', 'Core')

Installation Script (setup)

  • Path Resolution: Uses MyDocuments + edition detection
  • Validation: Random file writability probe + checksum verification
  • Error Handling: Comprehensive error messages with exit codes

CI/CD (.github/workflows/Release.yml)

  • Actions: checkout@v7, upload-artifact@v7, softprops/gh-release@v3
  • Smoke Test: Windows PowerShell module validation
  • Security: SHA256 sidecar support

Documentation (README.md)

  • Installation: Three clear options (PSGallery, pinned script, Scoop)
  • Usage: Comprehensive examples and -h help
  • Warnings: WSL shutdown, free space, scheduling
  • Comparison: Honest comparison with alternatives
  • Schedule: Weekly Task Scheduler setup

Testing (Tests/WslCompact.Tests.ps1)

  • Framework: Pester v5
  • Coverage: Format-Size, Test-VhdxFree, Test-SpaceOk, parameter validation
  • Approach: Unit tests for core functions

Migration Notes

Breaking Changes

  • Flag Parsing: Switched from positional to structured parameters
  • Help: Completely rewritten documentation
  • Installation: New validation requirements

Backward Compatibility

  • Version: Updated to 9.0 (major version bump)
  • Import: Proper module path detection
  • Errors: More detailed messages, similar concepts

Verification

Tests Passed

  • Structure: No unbalanced braces, proper file counts
  • Regressions: No cmd /c, old typos, PSModulePath.split

Conclusion

This PR delivers comprehensive improvements to wslcompact, transforming it from a fragile utility into a robust, production-ready tool. The changes prioritize safety, user experience, and maintainability while preserving all existing functionality. The transformation includes:

  • Enhanced Reliability: Atomic operations, comprehensive error handling
  • Better Documentation: Clear help, README, and code comments
  • Improved User Experience: Intuitive menus, progress feedback, recovery options
  • Modern Architecture: Parameter-based parsing, helper functions, test infrastructure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant