fix-trailing-newlines
Fix SA1518 StyleCop violations by removing trailing newlines from .cs, .xaml, .axaml, .csproj, .props, .targets, .editorconfig, .slnx, .sln, and .json files. Targets projects where .editorconfig has insert_final_newline = false. Use when SA1518 violations appear, trailing newline issues occur, or to enforce insert_final_newline = false across the repository.
From dotnetnpx claudepluginhub atc-net/atc-agentic-toolkit --plugin dotnetThis skill uses the workspace's default tool permissions.
scripts/fix_trailing_newlines.pyFix Trailing Newlines (SA1518)
What This Fixes
SA1518: File may not end with a newline character.
When .editorconfig has insert_final_newline = false, files must NOT end with trailing \r\n or \n characters. This skill scans all relevant files and strips any trailing newline bytes.
Target File Types
| Extension | Description |
|---|---|
.cs | C# source files |
.xaml | XAML markup files |
.axaml | Avalonia XAML files |
.csproj | C# project files |
.props | MSBuild property files |
.targets | MSBuild target files |
.editorconfig | Editor configuration files |
.slnx | Solution XML files |
.sln | Solution files |
.json | JSON configuration files |
Excluded Directories
The following directories are always skipped:
bin- Build outputobj- Intermediate build output.vs- Visual Studio metadata.git- Git repository datanode_modules- Node.js dependencies
Execution Steps
Step 1: Verify .editorconfig Setting
Before running, confirm the project uses insert_final_newline = false:
grep -r "insert_final_newline" .editorconfig
If insert_final_newline = true or not set, warn the user that removing trailing newlines may conflict with their editor configuration.
Step 2: Run the Fix Script
Execute the bundled Python script to scan and fix all files:
python scripts/fix_trailing_newlines.py [directory]
- If no directory is specified, defaults to the current working directory
- The script reports each fixed file and provides a summary
Step 3: Review Results
The script outputs:
- Each file that was fixed (relative path)
- Total files scanned and fixed
Step 4: Verify Build
After fixing, run:
dotnet build
Ensure the build succeeds and SA1518 warnings are resolved.
Manual Alternative
If the Python script is unavailable, perform the fix manually:
- Find all files with target extensions in the repository
- For each file, check if it ends with
\nor\r\n - If it does, remove all trailing newline characters from the end
- Save the file with binary write to avoid editors re-adding newlines
Guidelines
- Always check
.editorconfigbefore running to confirminsert_final_newline = false - The script preserves all file content except trailing newline bytes
- Empty files are skipped (no modification needed)
- The fix is idempotent - running it multiple times produces the same result