Skip to content

Conversation

@roger-zhangg
Copy link
Member

@roger-zhangg roger-zhangg commented Jul 30, 2025

Problem

The AWS SAM CLI file watcher was experiencing infinite loops during local development, particularly with LoopBack applications that create temporary .sandbox directories. This caused:

  • Extremely slow local invoke startup times (8+ minutes in some cases)
  • High CPU usage from continuous file system events
  • Poor developer experience during local development

Solution

1. Add --no-watch flag to local start-api and local start-lambda

This flag would disable the default behavior where a change in codebase will reset the docker container. User can use this flag to debug a changing local dir, and kill & restart the command to pick up new changes.

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

@roger-zhangg roger-zhangg requested a review from a team as a code owner July 30, 2025 01:58
vicheey
vicheey previously approved these changes Jul 30, 2025
"""

# Ignore patterns for file watching to avoid infinite loops and unnecessary events
DEFAULT_IGNORE_PATTERNS = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we provide a way for customer to provide customize ignore pattern? If none is provided, we can use the DEFAULT_IGNORE_PATTERNS.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes this should be the idea when we ship it. I will use this version to debug with customer first

reedham-aws
reedham-aws previously approved these changes Jul 30, 2025
@vicheey
Copy link
Contributor

vicheey commented Sep 9, 2025

DO NOT MERGE.

@roger-zhangg roger-zhangg deleted the branch aws:develop September 27, 2025 01:25
@roger-zhangg roger-zhangg reopened this Sep 27, 2025
@roger-zhangg roger-zhangg changed the base branch from hot-swap-fix to develop September 27, 2025 01:33
@roger-zhangg roger-zhangg dismissed stale reviews from reedham-aws and vicheey September 27, 2025 01:33

The base branch was changed.

@roger-zhangg roger-zhangg reopened this Sep 29, 2025
@roger-zhangg
Copy link
Member Author

This failing test seems unrelated, but will take a look

@roger-zhangg
Copy link
Member Author

also passed on my local

(.venv) ruojiazh@842f577b9ad2 aws-sam-cli % pytest tests/smoke/test_all_commands.py::TestAllCommands -n 4
======== 510 passed in 111.96s (0:01:51) ========

@reedham-aws
Copy link
Contributor

What is the reason that we no longer allow customers to provide a list of files/dirs to be ignored? It seems like that was the original intention.

@roger-zhangg
Copy link
Member Author

roger-zhangg commented Sep 30, 2025

What is the reason that we no longer allow customers to provide a list of files/dirs to be ignored? It seems like that was the original intention.

The root cause was actually not that. Originally the assumed root cause was few node lib is constantly changing when function is invoking and therefore triggering our file watcher, but then it turns out it's just the file watcher is somehow lagging and it picks up the filechanges way later than it should be. So the lib changes are done in build, which should be a legit change that should be picked up.

For what here is fixing, is in some sam project setup, it might have multiple lambda function sharing a same code dir (like a same file with different function as entrypoint). When we are doing sam local start-api, even only one function changes in code dir, our watcher will think every function has changed. Therefore killing the running container and putting everything into LAZY mode again. So the ignore pattern can't really fix this issue.

Instead, providing the --no-watch flag gives user more control on the scenario above. They can kill and restart the command when a reload is actually required.

reedham-aws
reedham-aws previously approved these changes Sep 30, 2025
"--no-watch",
is_flag=True,
default=False,
help="Disable file watching. Local code changes will not reset running docker container.",
Copy link
Contributor

Choose a reason for hiding this comment

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

From my understanding, this is only relevant when using warm_container_initialization_mode, right? That's probably something we should mention here, so it's easy for customers to understand when this matters. (same for the comment on start_lambda/cli.py

Comment on lines +247 to +250
if self._no_watch:
_function_providers_args[self._containers_mode].extend([False, True])
elif self._no_watch:
_function_providers_args[self._containers_mode].extend([False, False, True])
Copy link
Contributor

Choose a reason for hiding this comment

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

On the same topic from another comment, if a customer uses --no-watch without using --warm-container-initialization-mode it could be that this _function_providers_args[self._containers_mode] refers to SamFunctionProvider, which doesn't have the extra parameter for no_watch, and it would fail with an uncaught error. We should probably have a validation to not try to do that when it doesn't make sense.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants