Refactor utility to run buck2 commands as a method to run arbitrary commands (#48370)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48370

Changelog: [internal]

Small refactor in preparation for async commands with streaming.

Reviewed By: javache

Differential Revision: D67600611

fbshipit-source-id: 11fe6b6ccd8849f904338ccc9454361ad5923863
This commit is contained in:
Rubén Norte
2025-01-03 04:57:43 -08:00
committed by Facebook GitHub Bot
parent 726a72328e
commit 8f096ab4f8
3 changed files with 29 additions and 22 deletions
+4 -4
View File
@@ -24,7 +24,7 @@ import {
getDebugInfoFromCommandResult,
getShortHash,
printConsoleLogs,
runBuck2,
runBuck2Sync,
symbolicateStackTrace,
} from './utils';
import fs from 'fs';
@@ -43,7 +43,7 @@ const BUILD_OUTPUT_PATH = fs.mkdtempSync(
const PRINT_FANTOM_OUTPUT: false = false;
function parseRNTesterCommandResult(result: ReturnType<typeof runBuck2>): {
function parseRNTesterCommandResult(result: ReturnType<typeof runBuck2Sync>): {
logs: $ReadOnlyArray<ConsoleLogMessage>,
testResult: TestSuiteResult,
} {
@@ -99,7 +99,7 @@ function generateBytecodeBundle({
bytecodePath: string,
isOptimizedMode: boolean,
}): void {
const hermesCompilerCommandResult = runBuck2(
const hermesCompilerCommandResult = runBuck2Sync(
[
'run',
...getBuckModesForPlatform(isOptimizedMode),
@@ -202,7 +202,7 @@ module.exports = async function runTest(
});
}
const rnTesterCommandResult = runBuck2([
const rnTesterCommandResult = runBuck2Sync([
'run',
...getBuckModesForPlatform(
testConfig.mode === FantomTestConfigMode.Optimized,
+22 -15
View File
@@ -44,24 +44,17 @@ export function getBuckModesForPlatform(
return ['@//xplat/mode/react-force-cxx-platform', osPlatform];
}
type SpawnResultWithOriginalCommand = {
type SyncCommandResult = {
...ReturnType<typeof spawnSync>,
originalCommand: string,
...
};
export function runBuck2(args: Array<string>): SpawnResultWithOriginalCommand {
// If these tests are already running from withing a buck2 process, e.g. when
// they are scheduled by a `buck2 test` wrapper, calling `buck2` again would
// cause a daemon-level deadlock.
// To prevent this - explicitly pass custom `--isolation-dir`. Reuse the same
// dir across tests (even running in different jest processes) to properly
// employ caching.
if (process.env.BUCK2_WRAPPER != null) {
args.unshift('--isolation-dir', BUCK_ISOLATION_DIR);
}
const result = spawnSync('buck2', args, {
export function runCommandSync(
command: string,
args: Array<string>,
): SyncCommandResult {
const result = spawnSync(command, args, {
encoding: 'utf8',
env: {
...process.env,
@@ -71,12 +64,12 @@ export function runBuck2(args: Array<string>): SpawnResultWithOriginalCommand {
return {
...result,
originalCommand: `buck2 ${args.join(' ')}`,
originalCommand: `${command} ${args.join(' ')}`,
};
}
export function getDebugInfoFromCommandResult(
commandResult: SpawnResultWithOriginalCommand,
commandResult: SyncCommandResult,
): string {
const maybeSignal =
commandResult.signal != null ? `, signal: ${commandResult.signal}` : '';
@@ -102,6 +95,20 @@ export function getDebugInfoFromCommandResult(
return logLines.join('\n');
}
export function runBuck2Sync(args: Array<string>): SyncCommandResult {
// If these tests are already running from withing a buck2 process, e.g. when
// they are scheduled by a `buck2 test` wrapper, calling `buck2` again would
// cause a daemon-level deadlock.
// To prevent this - explicitly pass custom `--isolation-dir`. Reuse the same
// dir across tests (even running in different jest processes) to properly
// employ caching.
if (process.env.BUCK2_WRAPPER != null) {
args.unshift('--isolation-dir', BUCK_ISOLATION_DIR);
}
return runCommandSync('buck2', args);
}
export function getShortHash(contents: string): string {
return crypto.createHash('md5').update(contents).digest('hex').slice(0, 8);
}
+3 -3
View File
@@ -12,7 +12,7 @@
import {
getBuckModesForPlatform,
getDebugInfoFromCommandResult,
runBuck2,
runBuck2Sync,
} from '../utils';
// $FlowExpectedError[untyped-import]
import fs from 'fs';
@@ -94,7 +94,7 @@ async function warmUpMetro(isOptimizedMode: boolean): Promise<void> {
}
function warmUpHermesCompiler(isOptimizedMode: boolean): void {
const buildHermesCompilerCommandResult = runBuck2([
const buildHermesCompilerCommandResult = runBuck2Sync([
'build',
...getBuckModesForPlatform(isOptimizedMode),
'//xplat/hermes/tools/hermesc:hermesc',
@@ -108,7 +108,7 @@ function warmUpHermesCompiler(isOptimizedMode: boolean): void {
}
function warmUpRNTesterCLI(isOptimizedMode: boolean): void {
const buildRNTesterCommandResult = runBuck2([
const buildRNTesterCommandResult = runBuck2Sync([
'build',
...getBuckModesForPlatform(isOptimizedMode),
'//xplat/ReactNative/react-native-cxx/samples/tester:tester',