Skip to content

Conversation

@j-d-ha
Copy link
Collaborator

@j-d-ha j-d-ha commented Dec 21, 2025

🚀 Pull Request

📋 Summary

This PR refactors the UsePublicModifier option handling in the source generator to improve type safety and correctly handle empty/null boolean MSBuild properties. The change addresses an issue where bool.Parse() would throw an exception when the MSBuild property was not explicitly set (null/empty string).

Key Changes:

  • Changed UsePublicModifier from bool to string? in CompilationOptions (src/LayeredCraft.SourceGeneratorTools.Generator/CompilationOptions.cs:6)
  • Replaced bool.Parse() with bool.TryParse() to safely handle null/empty values (src/LayeredCraft.SourceGeneratorTools.Generator/SourceGeneratorToolsGenerator.cs:45-46)
  • Added test case to verify correct behavior with empty options (test/LayeredCraft.SourceGeneratorTools.Generator.UnitTests/SourceGeneratorToolsGeneratorTests.cs:57-66)

✅ Checklist

  • My changes build cleanly
  • I've added/updated relevant tests
  • I've added/updated documentation or README
  • I've followed the coding style for this project
  • I've tested the changes locally (if applicable)

🧪 Related Issues or PRs

This addresses an edge case where MSBuild properties may not be set, which would cause the generator to fail with a parsing exception.


💬 Notes for Reviewers

The main improvement here is defensive programming - using bool.TryParse() instead of bool.Parse() ensures the generator handles missing or empty MSBuild properties gracefully. The default behavior (when the property is not set or invalid) is now false, which matches the original intent of bool.Parse(usePublic ?? "false").

The new test case Initialize_EmptyOptions_GeneratesOutput verifies that the generator can handle empty configuration values without throwing exceptions.

…ype safety

- Updated `UsePublicModifier` from `bool` to `string?` for better flexibility in parsing.
- Refactored `SourceGeneratorToolsGenerator` to handle validation using `bool.TryParse`.
- Adjusted logic for file stream content selection based on parsed public modifier.
- Added snapshot tests for `Initialize_EmptyOptions_GeneratesOutput` to validate the changes.
@j-d-ha j-d-ha merged commit 0bd5f98 into main Dec 21, 2025
1 check passed
@j-d-ha j-d-ha deleted the fix/options-parsing-for-bool branch December 21, 2025 15:44
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.

2 participants