2025-07-05 23:27:00 -07:00
/ * *
* @license
* Copyright 2025 Google LLC
* SPDX - License - Identifier : Apache - 2.0
* /
2025-08-25 22:11:27 +02:00
import fs from 'node:fs/promises' ;
import path from 'node:path' ;
2026-01-06 20:09:39 -08:00
import process from 'node:process' ;
2026-02-20 13:22:45 -05:00
import {
homedir ,
getCompatibilityWarnings ,
WarningPriority ,
type StartupWarning ,
} from '@google/gemini-cli-core' ;
2026-01-09 11:56:22 -08:00
import type { Settings } from '../config/settingsSchema.js' ;
import {
isFolderTrustEnabled ,
isWorkspaceTrusted ,
} from '../config/trustedFolders.js' ;
2025-07-05 23:27:00 -07:00
type WarningCheck = {
id : string ;
2026-01-09 11:56:22 -08:00
check : ( workspaceRoot : string , settings : Settings ) = > Promise < string | null > ;
2026-02-20 13:22:45 -05:00
priority : WarningPriority ;
2025-07-05 23:27:00 -07:00
} ;
// Individual warning checks
const homeDirectoryCheck : WarningCheck = {
id : 'home-directory' ,
2026-02-20 13:22:45 -05:00
priority : WarningPriority.Low ,
2026-01-09 11:56:22 -08:00
check : async ( workspaceRoot : string , settings : Settings ) = > {
if ( settings . ui ? . showHomeDirectoryWarning === false ) {
return null ;
}
2025-07-05 23:27:00 -07:00
try {
const [ workspaceRealPath , homeRealPath ] = await Promise . all ( [
fs . realpath ( workspaceRoot ) ,
2026-01-06 20:09:39 -08:00
fs . realpath ( homedir ( ) ) ,
2025-07-05 23:27:00 -07:00
] ) ;
if ( workspaceRealPath === homeRealPath ) {
2026-01-09 11:56:22 -08:00
// If folder trust is enabled and the user trusts the home directory, don't show the warning.
if (
isFolderTrustEnabled ( settings ) &&
isWorkspaceTrusted ( settings ) . isTrusted
) {
return null ;
}
return 'Warning you are running Gemini CLI in your home directory.\nThis warning can be disabled in /settings' ;
2025-07-05 23:27:00 -07:00
}
return null ;
} catch ( _err : unknown ) {
return 'Could not verify the current directory due to a file system error.' ;
}
} ,
} ;
2025-07-21 06:57:09 +09:00
const rootDirectoryCheck : WarningCheck = {
id : 'root-directory' ,
2026-02-20 13:22:45 -05:00
priority : WarningPriority.High ,
2026-01-09 11:56:22 -08:00
check : async ( workspaceRoot : string , _settings : Settings ) = > {
2025-07-21 06:57:09 +09:00
try {
const workspaceRealPath = await fs . realpath ( workspaceRoot ) ;
const errorMessage =
'Warning: You are running Gemini CLI in the root directory. Your entire folder structure will be used for context. It is strongly recommended to run in a project-specific directory.' ;
// Check for Unix root directory
if ( path . dirname ( workspaceRealPath ) === workspaceRealPath ) {
return errorMessage ;
}
return null ;
} catch ( _err : unknown ) {
return 'Could not verify the current directory due to a file system error.' ;
}
} ,
} ;
2025-07-05 23:27:00 -07:00
// All warning checks
2025-07-21 06:57:09 +09:00
const WARNING_CHECKS : readonly WarningCheck [ ] = [
homeDirectoryCheck ,
rootDirectoryCheck ,
] ;
2025-07-05 23:27:00 -07:00
export async function getUserStartupWarnings (
2026-01-09 11:56:22 -08:00
settings : Settings ,
2025-09-03 10:41:53 -07:00
workspaceRoot : string = process . cwd ( ) ,
2026-02-20 13:06:35 -08:00
options ? : { isAlternateBuffer? : boolean } ,
2026-02-20 13:22:45 -05:00
) : Promise < StartupWarning [ ] > {
2025-07-05 23:27:00 -07:00
const results = await Promise . all (
2026-02-20 13:22:45 -05:00
WARNING_CHECKS . map ( async ( check ) = > {
const message = await check . check ( workspaceRoot , settings ) ;
if ( message ) {
return {
id : check.id ,
message ,
priority : check.priority ,
} ;
}
return null ;
} ) ,
2025-07-05 23:27:00 -07:00
) ;
2026-02-20 13:22:45 -05:00
const warnings = results . filter ( ( w ) : w is StartupWarning = > w !== null ) ;
2026-02-18 19:01:23 -05:00
if ( settings . ui ? . showCompatibilityWarnings !== false ) {
2026-02-20 13:06:35 -08:00
warnings . push (
. . . getCompatibilityWarnings ( {
isAlternateBuffer : options?.isAlternateBuffer ,
} ) ,
) ;
2026-02-18 19:01:23 -05:00
}
return warnings ;
2025-07-05 23:27:00 -07:00
}