mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-10 22:21:22 -07:00
7.6 KiB
7.6 KiB
Gemini CLI Strict Development Rules
These rules apply strictly to all code modifications and additions within the Gemini CLI project.
Testing Guidelines
- Async/Await: Always use
waitForfrompackages/cli/src/test-utils/async.tsinstead ofvi.waitForfor allwaitForcalls withinpackages/cli. NEVER use fixed waits (e.g.,await delay(100)). Always usewaitForwith a predicate to ensure tests are stable and fast. Using the wrongwaitForcan result in flaky tests andactwarnings. - React Testing: Use
actto wrap all blocks in tests that change component state. UserenderorrenderWithProvidersfrompackages/cli/src/test-utils/render.tsxinstead ofrenderfromink-testing-librarydirectly. This prevents spuriousactwarnings. If test cases specify providers directly, consider whether the existingrenderWithProvidersshould be modified. - Snapshots: Use
toMatchSnapshotto verify that rendering works as expected rather than matching against the raw content of the output. When modifying snapshots, verify the changes are intentional and do not hide underlying bugs. - Parameterized Tests: Use parameterized tests where it reduces duplicated lines. Give the parameters explicit types to ensure the tests are type-safe.
- Mocks Management:
- Mock critical dependencies (
fs,os,child_process) ONLY at the top of the file. Ideally, avoid mocking these dependencies altogether. - Reuse existing mocks and fakes rather than creating new ones.
- Avoid mocking the file system whenever possible. If using the real file system is too difficult, consider writing an integration test instead.
- Always call
vi.restoreAllMocks()inafterEachto prevent test pollution. - Use
vi.useFakeTimers()for tests involving time-based logic to avoid flakiness.
- Mock critical dependencies (
- Typing in Tests: Avoid using
anyin tests; prefer proper types orunknownwith narrowing.
React Guidelines (packages/cli)
setStateand Side Effects: NEVER trigger side effects from within the body of asetStatecallback. Use a reducer oruseRefif necessary. These cases have historically introduced multiple bugs; typically, they should be resolved using a reducer.- Rendering: Do not introduce infinite rendering loops. Avoid synchronous
file I/O in React components as it will hang the UI. Do not implement new
logic for custom string measurement or string truncation. Use Ink layout
instead, leveraging
ResizeObserveras needed. - Keyboard Handling: Keyboard handling MUST go through
useKeyPress.tsfrom the Gemini CLI package rather than the standard ink library. This library supports reporting multiple keyboard events sequentially in the same React frame (critical for slow terminals). Handling this correctly often requires reducers to ensure multiple state updates are handled gracefully without overriding values. Refer totext-buffer.tsfor a canonical example. - Logging: Do not leave
console.log,console.warn, orconsole.errorin the code. - State & Effects: Ensure state initialization is explicit (e.g., use
undefinedrather thantrueas a default if the state is truly unknown). Carefully manageuseEffectdependencies. Prefer a reducer whenever practical. NEVER disablereact-hooks/exhaustive-deps; fix the code to correctly declare dependencies instead. - Context & Props: Avoid excessive property drilling. Leverage existing providers, extend them, or propose a new one if necessary. Only use providers for properties that are consistent across the entire application.
- Code Structure: Avoid complex
ifstatements whereswitchstatements could be used. KeepAppContainerminimal; refactor complex logic into React hooks. Evaluate whether business logic should be added tohookSystem.tsor integrated intopackages/corerather thanpackages/cli.
Core Guidelines (packages/core)
- Services: Implement services as classes with clear lifecycle management
(e.g.,
initialize()methods). Services should be stateless where possible, or use the centralizedStorageservice for persistence. - Cross-Service Communication: Prefer using the
coreEventsbus (frompackages/core/src/utils/events.ts) for asynchronous communication between services or to notify the UI of state changes. Avoid tight coupling between services. - Utilities: Use
debugLoggerfrompackages/core/src/utils/debugLogger.tsfor internal logging instead ofconsole. Ensure all shell operations usespawnAsyncfrompackages/core/src/utils/shell-utils.tsfor consistent error handling and promise management. Handle filesystem errors gracefully usingisNodeErrorfrompackages/core/src/utils/errors.ts. - Exports & Tooling: Add new tools to
packages/core/src/tools/and register them inpackages/core/src/tools/tool-registry.ts. Export all new public services, utilities, and types frompackages/core/src/index.ts.
Architectural Audit (Package Boundaries)
- Logic Placement: Non-UI logic (e.g., model orchestration, tool
implementation, git/filesystem operations) MUST reside in
packages/core.packages/clishould ONLY contain UI/Ink components, command-line argument parsing, and user interaction logic. - Environment Isolation: Core logic must not assume a TUI environment. Use
the
ConfirmationBusorOutputabstractions for communicating with the user from Core. - Decoupling: Actively look for opportunities to decouple services using
coreEvents. If a service imports another just to notify it of a change, use an event instead.
General Gemini CLI Design Principles
- Settings: Use settings for user-configurable options rather than adding
new command line arguments. Add new settings to
packages/cli/src/config/settingsSchema.ts. If a setting hasshowInDialog: true, it MUST be documented indocs/get-started/configuration.md. EnsurerequiresRestartis correctly set. - Logging: Use
debugLoggerfor rethrown errors to avoid duplicate logging. - Keyboard Shortcuts: Define all new keyboard shortcuts in
packages/cli/src/config/keyBindings.tsand document them indocs/cli/keyboard-shortcuts.md. Be careful of keybindings that require theMetakey, as only certain meta key shortcuts are supported on Mac. Avoid function keys and shortcuts commonly bound in VSCode.
TypeScript Best Practices
- Use
checkExhaustivein thedefaultclause ofswitchstatements to ensure all cases are handled. - Avoid using the non-null assertion operator (
!) unless absolutely necessary. - STRICT TYPING: Strictly forbid
anyandunknownin both CLI and Core packages.unknownis only allowed if it is immediately narrowed using type guards or Zod validation. - NEVER disable
@typescript-eslint/no-floating-promises. - Avoid making types nullable unless strictly necessary, as it hurts readability.
TUI Best Practices
- Terminal Compatibility: Consider how changes might behave differently
across terminals (e.g., VSCode terminal, SSH, Kitty, default Mac terminal,
iTerm2, Windows terminal). If modifying keyboard handling, integrate deeply
with existing files like
KeypressContext.tsxandterminalCapabilityManager.ts. - iTerm: Be aware that
ITERM_SESSION_IDmay be present when users run VSCode from within iTerm, even if the terminal is not iTerm.
Code Cleanup
- Refactoring: Actively clean up code duplication, technical debt, and boilerplate ("AI Slop") when working in the codebase.
- Prompts: Be aware that changes can impact the prompts sent to Gemini CLI and affect overall quality.