Skip to content

Conversation

@vicheey
Copy link
Contributor

@vicheey vicheey commented Dec 3, 2025

Which issue(s) does this change fix?

#8477

Why is this change necessary?

sam sync --watch crashes with TypeError: can't subtract offset-naive and offset-aware datetimes when the .aws-sam/sync.toml file contains timestamps without timezone information (missing +00:00 suffix). This breaks the watch functionality for users who have previously run sync commands, preventing them from using the auto-sync feature.

How does it address the issue?

The fix ensures all timestamps loaded from sync.toml are timezone-aware UTC by checking if tzinfo is None after parsing with datetime.fromisoformat() and adding UTC timezone if missing. This maintains consistency with how SAM CLI writes timestamps (always UTC timezone-aware) and handles both old sync.toml files (without timezone info) and new ones (with timezone info) correctly. The fix is applied in two locations in samcli/commands/sync/sync_context.py:

  1. When loading latest_infra_sync_time from the sync state
  2. When loading sync_time for each resource sync state

What side effects does this change have?

None. The change is backward compatible and only affects the internal datetime handling. All timestamps are now consistently timezone-aware UTC, which is the expected behavior throughout the sync module.

Mandatory Checklist

PRs will only be reviewed after checklist is complete

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@vicheey vicheey requested a review from a team as a code owner December 3, 2025 22:39
@github-actions github-actions bot added area/sync sam sync command pr/internal labels Dec 3, 2025
if resource_sync_states_toml_table:
for resource_id in resource_sync_states_toml_table:
resource_sync_state_toml_table = resource_sync_states_toml_table.get(resource_id)
sync_time_str = resource_sync_state_toml_table.get(SYNC_TIME)
Copy link
Contributor

Choose a reason for hiding this comment

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

it could be that the datetime has a Z at the end right, like 2024-05-08T15:16:43Z, should we do something to handle this like 2024-05-08T15:16:43Z

latest_infra_sync_time = sync_state_toml_table.get(LATEST_INFRA_SYNC_TIME)
if latest_infra_sync_time:
latest_infra_sync_time = datetime.fromisoformat(str(latest_infra_sync_time))
latest_infra_sync_time_str = sync_state_toml_table.get(LATEST_INFRA_SYNC_TIME)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit but this is the same logic as above, it can probably be made into a method to use in both places

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/sync sam sync command pr/internal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants