-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Hosts: add “No leading spaces” option and honor it when saving #41206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hosts: add “No leading spaces” option and honor it when saving #41206
Conversation
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
@davidegiacometti, I assume originally adding space is for helping Human visual alignment for those IPs. |
|
@yeelam-gordon Thanks! Yes, the original two-space indent looks intended for visual alignment. The motivation for this option is parser/tooling compatibility, not human readability. Some consumers expect the IP to start at column 0 and fail or behave unexpectedly when there are leading spaces. Example: squid and a few strict scripts that anchor on Trimming in our own parser wouldn’t help these external tools because they read the file as-is. This PR doesn’t change the default behavior (alignment remains the default); it only adds an opt‑in switch to write active entries without the extra leading spaces when a disabled entry exists. Comments and all other spacing remain unchanged. Before/after:
A unit test ( |
Hi, that's correct! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new "No leading spaces" option to the Hosts File Editor that prevents prepending spaces to active lines when saving the hosts file. The feature addresses formatting preferences where users want active entries to start at column 0 instead of being indented when disabled entries exist.
- Adds a new boolean setting
NoLeadingSpaceswith default value false to preserve current behavior - Modifies the hosts file writing logic to conditionally apply leading spaces based on the new setting
- Includes comprehensive UI integration and test coverage for the new functionality
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| HostsProperties.cs | Adds NoLeadingSpaces property with default false value |
| IUserSettings.cs | Extends interface to include NoLeadingSpaces property |
| UserSettings.cs | Implements NoLeadingSpaces property loading from settings |
| HostsViewModel.cs | Exposes NoLeadingSpaces property for UI binding |
| HostsPage.xaml | Adds toggle switch UI control for the new setting |
| Resources.resw | Adds localization strings for the new setting |
| HostsService.cs | Modifies write logic to honor NoLeadingSpaces setting |
| HostsServiceTest.cs | Adds unit test to verify NoLeadingSpaces functionality |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Summary of the Pull Request
Adds a new Hosts File Editor setting “No leading spaces” that prevents prepending spaces to active lines when saving the hosts file (when any entry is disabled). Default is Off to preserve current behavior.
PR Checklist
Closes: Hosts File Editor: Adds no whitespace option #36386
Communication: N/A (small, scoped option)
Tests: Added/updated and all pass
Localization: New en-US strings added; other locales handled by loc pipeline
Dev docs: N/A
New binaries: None
Documentation updated: N/A
Detailed Description of the Pull Request / Additional comments
-
src/settings-ui/Settings.UI.Library/HostsProperties.cs: addNoLeadingSpaces-
src/modules/Hosts/HostsUILib/Settings/IUserSettings.cs: addNoLeadingSpaces-
src/modules/Hosts/Hosts/Settings/UserSettings.cs: load/save value from settings.json-
src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs: exposeNoLeadingSpaces-
src/settings-ui/Settings.UI/SettingsXAML/Views/HostsPage.xaml: new SettingsCard toggle-
src/settings-ui/Settings.UI/Strings/en-us/Resources.resw: addHosts_NoLeadingSpaces.Header/Description-
src/modules/Hosts/HostsUILib/Helpers/HostsService.cs: gate indent withanyDisabled && !_userSettings.NoLeadingSpaces-
src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs:NoLeadingSpaces_Disabled_RemovesIndentBackward compatibility: default Off, current formatting unchanged unless the user enables the option.
Validation Steps Performed
Automated:
HostsEditor.UnitTestsincludingNoLeadingSpaces_Disabled_RemovesIndentpassing.Manual:
1. Run PowerToys (runner) as Admin.
2. Settings → Hosts File Editor → enable “No leading spaces”.
3. In editor, add active
127.0.0.10 example1and disabled127.0.0.11 example2; Save.4. Open
C:\Windows\System32\drivers\etc\hostsin Notepad.- ON: active line starts at column 0; disabled is
# 127....- OFF: active line begins with two spaces when a disabled entry exists.