@@ -132,7 +132,7 @@ const androidSdk = Effect.gen(function* () {
132132 const platform = yield * HostProcess . Platform ;
133133 const home = environment . HOME ?? environment . USERPROFILE ?? "" ;
134134 const explicit = environment . ANDROID_HOME ?. trim ( ) || environment . ANDROID_SDK_ROOT ?. trim ( ) ;
135- const candidates = explicit
135+ const initialCandidates = explicit
136136 ? [ explicit ]
137137 : [
138138 path . join ( home , "Library" , "Android" , "sdk" ) ,
@@ -143,24 +143,18 @@ const androidSdk = Effect.gen(function* () {
143143 "Sdk" ,
144144 ) ,
145145 ] ;
146- if ( ! explicit ) {
147- for ( const directory of ( environment . PATH ?? "" ) . split ( platform === "win32" ? ";" : ":" ) ) {
148- if ( ! directory ) continue ;
149- const resolved = yield * fs
150- . realPath ( path . join ( directory , platform === "win32" ? "adb.exe" : "adb" ) )
151- . pipe ( Effect . option ) ;
152- if ( resolved . _tag === "Some" ) candidates . push ( path . dirname ( path . dirname ( resolved . value ) ) ) ;
153- }
154- }
155146 const exists = ( file : string ) => fs . exists ( file ) . pipe ( Effect . orElseSucceed ( ( ) => false ) ) ;
156- for ( const root of candidates ) {
157- const adb = yield * exists (
158- path . join ( root , "platform-tools" , platform === "win32" ? "adb.exe" : "adb" ) ,
159- ) ;
160- const emulator = yield * exists (
161- path . join ( root , "emulator" , platform === "win32" ? "emulator.exe" : "emulator" ) ,
162- ) ;
163- if ( explicit || adb || emulator ) {
147+ const inspectCandidate = ( root : string ) =>
148+ Effect . gen ( function * ( ) {
149+ const adb = yield * exists (
150+ path . join ( root , "platform-tools" , platform === "win32" ? "adb.exe" : "adb" ) ,
151+ ) ;
152+ const emulator = yield * exists (
153+ path . join ( root , "emulator" , platform === "win32" ? "emulator.exe" : "emulator" ) ,
154+ ) ;
155+ if ( ! explicit && ! adb && ! emulator ) {
156+ return null ;
157+ }
164158 const avdmanager = yield * exists (
165159 path . join (
166160 root ,
@@ -176,6 +170,29 @@ const androidSdk = Effect.gen(function* () {
176170 path . join ( root , "tools" , "bin" , platform === "win32" ? "avdmanager.bat" : "avdmanager" ) ,
177171 ) ) ;
178172 return { root, adb, emulator, avdmanager, legacyAvdmanager } ;
173+ } ) ;
174+
175+ for ( const root of initialCandidates ) {
176+ const found = yield * inspectCandidate ( root ) ;
177+ if ( found ) return found ;
178+ }
179+
180+ if ( ! explicit ) {
181+ const pathValue = environment . PATH ?? environment . Path ?? "" ;
182+ const seenDirectories = new Set < string > ( ) ;
183+ const adbBinary = platform === "win32" ? "adb.exe" : "adb" ;
184+ for ( const rawDirectory of pathValue . split ( platform === "win32" ? ";" : ":" ) ) {
185+ const directory = rawDirectory . trim ( ) ;
186+ if ( ! directory ) continue ;
187+ const dirKey = platform === "win32" ? directory . toLowerCase ( ) : directory ;
188+ if ( seenDirectories . has ( dirKey ) ) continue ;
189+ seenDirectories . add ( dirKey ) ;
190+ const adbCandidate = path . join ( directory , adbBinary ) ;
191+ if ( ! ( yield * exists ( adbCandidate ) ) ) continue ;
192+ const resolved = yield * fs . realPath ( adbCandidate ) . pipe ( Effect . option ) ;
193+ const root = path . dirname ( path . dirname ( Option . getOrElse ( resolved , ( ) => adbCandidate ) ) ) ;
194+ const found = yield * inspectCandidate ( root ) ;
195+ if ( found ) return found ;
179196 }
180197 }
181198 return { root : null , adb : false , emulator : false , avdmanager : false , legacyAvdmanager : false } ;
0 commit comments