fix(cli): load .env file before agent import validation in deploy#4713
fix(cli): load .env file before agent import validation in deploy#4713OiPunk wants to merge 1 commit intogoogle:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical issue in the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses a bug where environment variables from .env files were not available during agent import validation, by correctly loading the .env file before the agent module is imported. No security vulnerabilities were found. I've included one suggestion to improve the robustness of the new test by ensuring resource cleanup even in case of test failure.
| # Should not raise because .env is loaded before import | ||
| cli_deploy._validate_agent_import( | ||
| str(tmp_path), "root_agent", is_config_agent=False | ||
| ) | ||
|
|
||
| # Clean up the env var | ||
| os.environ.pop("_ADK_TEST_DEPLOY_VAR", None) |
There was a problem hiding this comment.
To ensure the environment variable is cleaned up even if the test fails, it's a good practice to wrap the test assertion logic in a try...finally block. This prevents state from one test leaking into another, which can cause flaky tests.
| # Should not raise because .env is loaded before import | |
| cli_deploy._validate_agent_import( | |
| str(tmp_path), "root_agent", is_config_agent=False | |
| ) | |
| # Clean up the env var | |
| os.environ.pop("_ADK_TEST_DEPLOY_VAR", None) | |
| try: | |
| # Should not raise because .env is loaded before import | |
| cli_deploy._validate_agent_import( | |
| str(tmp_path), "root_agent", is_config_agent=False | |
| ) | |
| finally: | |
| # Clean up the env var | |
| os.environ.pop("_ADK_TEST_DEPLOY_VAR", None) |
When using `adk deploy agent_engine --validate-agent-import`, the `_validate_agent_import` function imports the agent module without first loading `.env` files. This causes agents that depend on environment variables at import time (e.g. pydantic BaseSettings) to fail validation with errors like `ValidationError`. This fix calls `envs.load_dotenv_for_agent()` before the `importlib.import_module()` call, consistent with how `AgentLoader` already handles `.env` loading during normal agent loading. Fixes google#4688
ca1547c to
9231b68
Compare
Summary
_validate_agent_import()inadk deploy agent_engine --validate-agent-importto load.envfiles before importing the agent moduleBaseSettings) would fail validation withValidationErrorbecause the.envfile was not loadedenvs.load_dotenv_for_agent()beforeimportlib.import_module(), consistent with howAgentLoader._perform_load()handles.envloadingFixes #4688
Test plan
test_loads_dotenv_before_importthat creates an agent.py reading an env var at import time, with a.envfile providing the value, and verifies validation succeedstest_cli_deploy.pycontinue to passtest_agent_loader.pycontinue to passisortandpyinkPassed pytest results