fix: Add validation and error handling to ReplaceFileContents task - #163
fix: Add validation and error handling to ReplaceFileContents task#163gidadomukhtar wants to merge 1 commit into
Conversation
fix: Add validation and error handling to ReplaceFileContents task - Validate source file exists before processing - Validate replacement file exists if specified - Add comprehensive error logging for file I/O failures - Handle edge cases in replacement key parsing - Detect and warn on duplicate replacement keys - Wrap file operations in try-catch to prevent silent failures This improves robustness by ensuring the task fails gracefully with clear error messages instead of leaving corrupted output files. Fixes task failures when source or replacement files are missing.
There was a problem hiding this comment.
Pull request overview
This PR hardens the ReplaceFileContents MSBuild task by adding input validation and more explicit error handling/logging so missing files and I/O failures fail the build cleanly rather than producing corrupted/partial outputs.
Changes:
- Added source/destination/replacement-file validation and wrapped file operations in
try/catchwith explicit error logging. - Improved replacement parsing to handle empty inputs and malformed lines more defensively.
- Added duplicate replacement-key detection with warnings (last value wins).
Suppressed comments (1)
build-tools/prep/ReplaceFileContents.cs:6
using System.Text;appears unused in this file (noEncoding,StringBuilder, etc.). Since this PR already touches the using block, consider removing it to avoid unnecessary usings/warnings.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| try | ||
| { | ||
| // Validate source file exists | ||
| if (!File.Exists (SourceFile.ItemSpec)) | ||
| { | ||
| Log.LogError ($"Source file not found: {SourceFile.ItemSpec}"); |
|
|
||
| static readonly char[] Separator = new [] { '=' }; | ||
|
|
||
| static Dictionary<string, string> GetReplacementInfo (string[] replacements) |
|
@gidadomukhtar please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
fix: Add validation and error handling to ReplaceFileContents task
This improves robustness by ensuring the task fails gracefully with clear error messages instead of leaving corrupted output files.
Fixes task failures when source or replacement files are missing.