From 742c9265a260a9c598a1f65e0996d926b4b9c022 Mon Sep 17 00:00:00 2001 From: George Weale Date: Tue, 6 Jan 2026 17:37:00 -0800 Subject: [PATCH] fix: Validate app name in `adk create` command The `adk create` command now checks if the provided agent name is a valid Python identifier. An invalid name, such as one containing hyphens, will raise a `click.BadParameter` error before any files are created. Close #3977 Co-authored-by: George Weale PiperOrigin-RevId: 853001295 --- src/google/adk/cli/cli_create.py | 8 ++++++++ tests/unittests/cli/utils/test_cli_create.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/google/adk/cli/cli_create.py b/src/google/adk/cli/cli_create.py index 9085586e..12fb8841 100644 --- a/src/google/adk/cli/cli_create.py +++ b/src/google/adk/cli/cli_create.py @@ -21,6 +21,8 @@ from typing import Tuple import click +from ..apps.app import validate_app_name + _INIT_PY_TEMPLATE = """\ from . import agent """ @@ -294,6 +296,12 @@ def run_cmd( VertexAI as backend. type: Optional[str], Whether to define agent with config file or code. """ + app_name = os.path.basename(os.path.normpath(agent_name)) + try: + validate_app_name(app_name) + except ValueError as exc: + raise click.BadParameter(str(exc)) from exc + agent_folder = os.path.join(os.getcwd(), agent_name) # check folder doesn't exist or it's empty. Otherwise, throw if os.path.exists(agent_folder) and os.listdir(agent_folder): diff --git a/tests/unittests/cli/utils/test_cli_create.py b/tests/unittests/cli/utils/test_cli_create.py index 33b3f877..f6e82745 100644 --- a/tests/unittests/cli/utils/test_cli_create.py +++ b/tests/unittests/cli/utils/test_cli_create.py @@ -156,6 +156,23 @@ def test_run_cmd_overwrite_reject( ) +def test_run_cmd_invalid_app_name( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """Invalid app names should be rejected before creating any files.""" + monkeypatch.setattr(os, "getcwd", lambda: str(tmp_path)) + + with pytest.raises(click.BadParameter, match="Invalid app name"): + cli_create.run_cmd( + "my-agent", + model="gemini-2.0-flash-001", + google_api_key=None, + google_cloud_project=None, + google_cloud_region=None, + type="code", + ) + + def test_run_cmd_with_type_config( monkeypatch: pytest.MonkeyPatch, tmp_path: Path ) -> None: