From 9d2f11f505b247d430a614cedee21fedf1f46f61 Mon Sep 17 00:00:00 2001 From: Peter Velkov Date: Wed, 16 Feb 2022 02:58:18 -0800 Subject: [PATCH] Fix adb path check on Windows (#3434) Summary: The adb path check seem to be platform dependent Instead of pre-validating the path we can execute the intended command and retry with an alternative adb path only if it fails with file not found error Related to https://github.com/facebook/flipper/issues/3430 (adb is not detected) ## Changelog Fix ADB detection and startup on Windows Pull Request resolved: https://github.com/facebook/flipper/pull/3434 Test Plan: #### adb is detected successfully when `adb.exe` is in `platform-tools` ![image](https://user-images.githubusercontent.com/12156624/153605414-b85c1aef-1e60-457d-be90-3673900d7cc5.png) #### Falling back to alternative path when `adb.exe` is the SDK folder ![image](https://user-images.githubusercontent.com/12156624/153605693-0adb9c1b-e77f-44a1-9d2c-00cf40f59645.png) #### Otherwise failing if `adb.exe` is not found ![image](https://user-images.githubusercontent.com/12156624/153605797-307304d3-e513-459a-9e28-53d95ba94642.png) Reviewed By: passy Differential Revision: D34240518 Pulled By: aigoncharov fbshipit-source-id: db834bbdc9815e5ad41f7a1329ec8d5869f6f24b --- .../src/devices/android/adbClient.tsx | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/desktop/flipper-server-core/src/devices/android/adbClient.tsx b/desktop/flipper-server-core/src/devices/android/adbClient.tsx index 7b97d7e95..5b2c9c8f7 100644 --- a/desktop/flipper-server-core/src/devices/android/adbClient.tsx +++ b/desktop/flipper-server-core/src/devices/android/adbClient.tsx @@ -12,7 +12,6 @@ import {execFile} from 'promisify-child-process'; import adbConfig from './adbConfig'; import adbkit, {Client} from 'adbkit'; import path from 'path'; -import {pathExists} from 'fs-extra'; type Config = { androidHome: string; @@ -37,16 +36,24 @@ export async function initializeAdbClient( however, it sometimes fails with ENOENT errors. So instead, we start it manually before requesting a client. */ async function createClient(config: Config): Promise { - const androidHome = config.androidHome; - let adbPath = path.resolve(androidHome, 'platform-tools', 'adb'); - if (!(await pathExists(adbPath))) { - console.info('falling back to the alternative adb path'); - adbPath = path.resolve(androidHome, 'adb'); - } return reportPlatformFailures( - execFile(adbPath, ['start-server']).then(() => + startAdbServer(config.androidHome).then(() => adbkit.createClient(adbConfig()), ), 'createADBClient.shell', ); } + +async function startAdbServer(androidHome: string) { + const adbPath = path.resolve(androidHome, 'platform-tools', 'adb'); + const args = ['start-server']; + + return execFile(adbPath, args).catch((error) => { + if (error.code == 'ENOENT') { + console.info('falling back to the alternative adb path'); + return execFile(path.resolve(androidHome, 'adb'), args); + } + + throw error; + }); +}