Skip to content

Conversation

@linyows
Copy link
Owner

@linyows linyows commented Feb 6, 2026

Summary

  • Fixed a bug where getNextId() always extracted the first 3 characters as the ID, causing incorrect ID generation for formats like {{@id{4}}}
  • Changed to dynamically extract all leading digits from filenames, supporting any ID width
  • Added unit tests for getNextId covering 3-digit, 4-digit, empty directory, and non-numeric filename cases

Test plan

  • All tests pass with zig build test
  • With {{@id{4}}} format and existing 0001-foo.md, 0002-bar.md, running draft adr "baz" generates 0003-baz.md
  • Default {{@id}} (3-digit) format continues to work as expected

🤖 Generated with Claude Code

getNextId was hardcoded to extract only the first 3 characters as the ID,
causing incorrect ID generation when filename_format uses wider IDs like
{{@id{4}}}. For example, with files 0001-foo.md and 0002-bar.md, the next
file would incorrectly be 0001-baz.md instead of 0003-baz.md.

Changed to extract all leading digits from filenames dynamically, making
it work correctly regardless of ID width.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@linyows linyows requested a review from Copilot February 6, 2026 14:27
Copy link

Copilot AI left a 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 fixes a bug in the getNextId function that prevented it from correctly handling variable-width numeric IDs. Previously, the function hardcoded extraction of the first 3 characters, breaking support for formats like {{@id{4}}}.

Changes:

  • Modified getNextId to dynamically extract all leading digits from filenames instead of hardcoding 3 characters
  • Removed zero-padding from returned ID values (now returns "1" instead of "001")
  • Added comprehensive unit tests covering 3-digit, 4-digit, empty directory, and non-numeric filename scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

var dir = cwd.openDir(output_dir, .{ .iterate = true }) catch |err| {
if (err == error.FileNotFound) {
return try std.fmt.allocPrint(allocator, "{d:0>3}", .{1});
return try std.fmt.allocPrint(allocator, "{d}", .{1});
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function now returns unpadded ID strings (e.g., '1' instead of '001'), which may break existing code that expects zero-padded IDs. Consider whether the calling code handles formatting separately, or if padding width should be determined from existing files in the directory.

Copilot uses AI. Check for mistakes.
}

return try std.fmt.allocPrint(allocator, "{d:0>3}", .{max_id + 1});
return try std.fmt.allocPrint(allocator, "{d}", .{max_id + 1});
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function now returns unpadded ID strings, which differs from the previous zero-padded format. This may break existing code that expects zero-padded IDs. Consider whether the calling code handles formatting separately, or if padding width should be determined from existing files in the directory.

Copilot uses AI. Check for mistakes.
@linyows linyows merged commit 4cdac77 into main Feb 6, 2026
3 checks passed
@linyows linyows deleted the fix/getNextId-variable-width-id branch February 6, 2026 14:31
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.

1 participant