@@ -215,6 +215,22 @@ describe("loadDotEnv", () => {
215215 } ) ;
216216 } ) ;
217217
218+ it ( "loads global env when the working directory was deleted" , async ( ) => {
219+ await withIsolatedEnvAndCwd ( async ( ) => {
220+ await withDotEnvFixture ( async ( { stateDir } ) => {
221+ await writeEnvFile ( path . join ( stateDir , ".env" ) , "FOO=from-global\n" ) ;
222+ vi . spyOn ( process , "cwd" ) . mockImplementation ( ( ) => {
223+ throw new Error ( "ENOENT: uv_cwd" ) ;
224+ } ) ;
225+ delete process . env . FOO ;
226+
227+ loadDotEnv ( { quiet : true } ) ;
228+
229+ expect ( process . env . FOO ) . toBe ( "from-global" ) ;
230+ } ) ;
231+ } ) ;
232+ } ) ;
233+
218234 it ( "loads the Ubuntu gateway.env compatibility fallback after ~/.openclaw/.env" , async ( ) => {
219235 await withIsolatedEnvAndCwd ( async ( ) => {
220236 await withDotEnvFixture ( async ( { base, cwdDir } ) => {
@@ -719,6 +735,22 @@ describe("loadCliDotEnv", () => {
719735 } ) ;
720736 } ) ;
721737
738+ it ( "loads global CLI env when the working directory was deleted" , async ( ) => {
739+ await withIsolatedEnvAndCwd ( async ( ) => {
740+ await withDotEnvFixture ( async ( { stateDir } ) => {
741+ await writeEnvFile ( path . join ( stateDir , ".env" ) , "FOO=from-global\n" ) ;
742+ vi . spyOn ( process , "cwd" ) . mockImplementation ( ( ) => {
743+ throw new Error ( "ENOENT: uv_cwd" ) ;
744+ } ) ;
745+ delete process . env . FOO ;
746+
747+ loadCliDotEnv ( { quiet : true } ) ;
748+
749+ expect ( process . env . FOO ) . toBe ( "from-global" ) ;
750+ } ) ;
751+ } ) ;
752+ } ) ;
753+
722754 it ( "does not load gateway.env when OPENCLAW_STATE_DIR is explicitly set" , async ( ) => {
723755 await withIsolatedEnvAndCwd ( async ( ) => {
724756 await withDotEnvFixture ( async ( { base, cwdDir } ) => {
0 commit comments