This PR contains a set of bug fixes and general improvements to [Awake](https://awake.den.dev/) and developer experience tooling for building the module. ### Awake Fixes - **#32544** - Fixed an issue where Awake settings became non-functional after the PC wakes from sleep. Added `WM_POWERBROADCAST` handling to detect system resume events (`PBT_APMRESUMEAUTOMATIC`, `PBT_APMRESUMESUSPEND`) and re-apply `SetThreadExecutionState` to restore the awake state. - **#36150** - Fixed an issue where Awake would not prevent sleep when AC power is connected. Added `PBT_APMPOWERSTATUSCHANGE` handling to re-apply `SetThreadExecutionState` when the power source changes (AC/battery transitions). - **#41674** - Fixed silent failure when `SetThreadExecutionState` fails. The monitor thread now handles the return value, logs an error, and reverts to passive mode with updated tray icon. - **#41738** - Fixed `--display-on` CLI flag default from `true` to `false` to align with documentation and PowerToys settings behavior. This is a breaking change for scripts relying on the undocumented default. - **#41918** - Fixed `WM_COMMAND` message processing flaw in `TrayHelper.WndProc` that incorrectly compared enum values against enum count. Added proper bounds checking for custom tray time entries. - **#44134** - Documented that `ES_DISPLAY_REQUIRED` (used when "Keep display on" is enabled) blocks Task Scheduler idle detection, preventing scheduled maintenance tasks like SSD TRIM. Workaround: disable "Keep display on" or manually run `Optimize-Volume -DriveLetter C -ReTrim`. - **#38770** - Fixed tray icon failing to appear after Windows updates. Increased retry attempts and delays for icon Add operations (10 attempts, up to ~15.5 seconds total) while keeping existing fast retry behavior for Update/Delete operations. - **#40501** - Fixed tray icon not disappearing when Awake is disabled. The `SetShellIcon` function was incorrectly requiring an icon for Delete operations, causing the `NIM_DELETE` message to never be sent. - Fixed an issue where toggling "Keep screen on" during an active timed session would disrupt the countdown timer. The display setting now updates directly without restarting the timer, preserving the exact remaining time. ### Performance Optimizations - Fixed O(n²) loop in `TrayHelper.CreateAwakeTimeSubMenu` by replacing `ElementAt(i)` with `foreach` iteration. - Fixed Observable subscription leak in `Manager.cs` by storing `IDisposable` and disposing in `CancelExistingThread()`. Also removed dead `_tokenSource` code that was no longer used. - Reduced allocations in `SingleThreadSynchronizationContext` by changing `Tuple<>` to `ValueTuple`. - Replaced dedicated exit event thread with `ThreadPool.RegisterWaitForSingleObject()` to reduce resource usage. ### Code Quality - Replaced `Console.WriteLine` with `Logger.LogError` in `TrayHelper.cs` for consistent logging. - Added proper error logging to silent exception catches in `AwakeService.cs`. - Removed dead `Math.Min(minutes, int.MaxValue)` code where `minutes` is already an `int`. - Extracted hardcoded tray icon ID to named constant `TrayIconId`. - Standardized null coalescing for `GetSettings<AwakeSettings>()` calls across all files. ### Debugging Experience Fixes - Fixed first-chance exceptions in `settings_window.cpp` during debugging. Added `HasKey()` check before accessing `hotkey_changed` property to prevent `hresult_error` exceptions when the property doesn't exist in module settings. - Fixed first-chance exceptions in FindMyMouse `parse_settings` during debugging. Refactored to extract the properties object once and added `HasKey()` checks before all `GetNamedObject()` calls. This prevents `winrt::hresult_error` exceptions when optional settings keys (like legacy `overlay_opacity`) don't exist, improving the debugging experience by eliminating spurious exception breaks. - Fixed LightSwitch.UITests build failures when building from a clean state. Added missing project references (`ManagedCommon`, `LightSwitchModuleInterface`) with `ReferenceOutputAssembly=false` to ensure proper build ordering, and added existence check for the native DLL copy operation. ### Developer Experience - Added `setup-dev-environment.ps1` script to automate development environment setup. - Added `clean-artifacts.ps1` script to resolve build errors from corrupted build state or missing image files. - Added build script that allows standalone command line build of the Awake module. - Added troubleshooting section to `doc/devdocs/development/debugging.md` with guidance on resolving common build errors.
5.9 KiB
Debugging PowerToys
This document covers techniques and tools for debugging PowerToys.
Pre-Debugging Setup
Before you can start debugging PowerToys, you need to set up your development environment:
- Fork the repository and clone it to your machine
- Navigate to the repository root directory
- Run
git submodule update --init --recursiveto initialize all submodules - Change directory to
.configand runwinget configure .\configuration.vsEnterprise.winget(pick the configuration file that matches your Visual Studio distribution)
Optional: Building Outside Visual Studio
You can build the entire solution from the command line, which is sometimes faster than building within Visual Studio:
- Open Developer Command Prompt for VS 2022
- Navigate to the repository root directory
- Run the following command(don't forget to set the correct platform):
msbuild -restore -p:RestorePackagesConfig=true -p:Platform=ARM64 -m PowerToys.slnx /tl /p:NuGetInteractive="true" - This process should complete in approximately 13-14 minutes for a full build
Debugging Techniques
Visual Studio Debugging
To debug the PowerToys application in Visual Studio, set the runner project as your start-up project, then start the debugger.
Some PowerToys modules must be run with the highest permission level if the current user is a member of the Administrators group. The highest permission level is required to be able to perform some actions when an elevated application (e.g. Task Manager) is in the foreground or is the target of an action. Without elevated privileges some PowerToys modules will still work but with some limitations:
- The
FancyZonesmodule will not be able to move an elevated window to a zone. - The
Shortcut Guidemodule will not appear if the foreground window belongs to an elevated application.
Therefore, it is recommended to run Visual Studio with elevated privileges when debugging these scenarios. If you want to avoid running Visual Studio with elevated privileges and don't mind the limitations described above, you can do the following: open the runner project properties and navigate to the Linker -> Manifest File settings, edit the UAC Execution Level property and change it from highestAvailable (level='highestAvailable') to `asInvoker (/level='asInvoker').
Shell Process Debugging Tool
The Shell Process Debugging Tool is a Visual Studio extension that helps debug multiple processes, which is especially useful for PowerToys modules started by the runner.
Debugging Setup Process
- Install "Debug Child Processes" Visual Studio extension
- Configure which processes to debug and what debugger to use for each
- Start PowerToys from Visual Studio
- The extension will automatically attach to specified child processes when launched
Debugging Color Picker Example
- Set breakpoints in both ColorPicker and its module interface
- Use Shell Process Debugging to attach to ColorPickerUI.exe
- Debug .NET and native code together
- Runner needs to be running to properly test activation
Debugging DLL Main/Module Interface
- Breakpoints in DLL code will be hit when loaded by runner
- No special setup needed as DLL is loaded into runner process
Debugging Short-Lived Processes
- For processes with short lifetimes (like in Workspaces)
- List all processes explicitly in debugging configuration
- Set correct debugger type (.NET debugger for C# code)
Finding Registered Events
- Run WinObj tool from SysInternals as administrator
- Search for event name
- Shows handles to the event (typically runner and module)
Common Debugging Usage Patterns
Debugging with Bug Report
- Check module-specific logs for exceptions/crashes
- Copy user's settings to your AppData to reproduce their configuration
- Check Event Viewer XML files if logs don't show crashes
- Compare installation_folder_structure.txt to detect corrupted installations
- Check installer logs for installation-related issues
- Look at Windows version and language settings for patterns across users
Installer Debugging
- Can only build installer in Release mode
- Typically debug using logs and message boxes
- Logs located in:
%LOCALAPPDATA%\Temp\PowerToys_bootstrapper_*.log- MSI tool logs%LOCALAPPDATA%\Temp\PowerToys_*.log- Custom installer logs
- Logs in Bug Reports are useful for troubleshooting installation issues
Settings UI Debugging
- Use shell process debugging to connect to newly created processes
- Debug the
PowerToys.Settings.exeprocess - Add breakpoints as needed for troubleshooting
- Logs are stored in the local app directory:
%LOCALAPPDATA%\Microsoft\PowerToys - Check Event Viewer for application crashes related to
PowerToys.Settings.exe - Crash dumps can be obtained from Event Viewer
Troubleshooting Build Errors
Missing Image Files or Corrupted Build State
If you encounter build errors about missing image files (e.g., .png, .ico, or other assets), this typically indicates a corrupted build state. To resolve:
-
Clean the solution in Visual Studio: Build > Clean Solution
Or from the command line (Developer Command Prompt for VS 2022):
msbuild PowerToys.slnx /t:Clean /p:Platform=x64 /p:Configuration=Debug -
Delete build output and package folders from the repository root:
x64/ARM64/Debug/Release/packages/
-
Rebuild the solution
Helper Script
A PowerShell script is available to automate this cleanup:
.\tools\build\clean-artifacts.ps1
This script will run MSBuild Clean and remove the build folders listed above. Use -SkipMSBuildClean if you only want to delete the folders without running MSBuild Clean.
After cleaning, rebuild with:
msbuild -restore -p:RestorePackagesConfig=true -p:Platform=x64 -m PowerToys.slnx