Skip to content

Commit

Permalink
Refactor utility to run buck2 commands as a method to run arbitrary c…
Browse files Browse the repository at this point in the history
…ommands (#48370)

Summary:
Pull Request resolved: #48370

Changelog: [internal]

Small refactor in preparation for async commands with streaming.

Differential Revision: D67600611
  • Loading branch information
rubennorte authored and facebook-github-bot committed Dec 23, 2024
1 parent d0d7208 commit 4f47991
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
8 changes: 4 additions & 4 deletions packages/react-native-fantom/runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getDebugInfoFromCommandResult,
getShortHash,
printConsoleLogs,
runBuck2,
runBuck2Sync,
symbolicateStackTrace,
} from './utils';
import fs from 'fs';
Expand All @@ -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,
} {
Expand Down Expand Up @@ -99,7 +99,7 @@ function generateBytecodeBundle({
bytecodePath: string,
isOptimizedMode: boolean,
}): void {
const hermesCompilerCommandResult = runBuck2(
const hermesCompilerCommandResult = runBuck2Sync(
[
'run',
...getBuckModesForPlatform(isOptimizedMode),
Expand Down Expand Up @@ -202,7 +202,7 @@ module.exports = async function runTest(
});
}

const rnTesterCommandResult = runBuck2([
const rnTesterCommandResult = runBuck2Sync([
'run',
...getBuckModesForPlatform(
testConfig.mode === FantomTestConfigMode.Optimized,
Expand Down
37 changes: 22 additions & 15 deletions packages/react-native-fantom/runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}` : '';
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-fantom/runner/warmup/warmup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import {
getBuckModesForPlatform,
getDebugInfoFromCommandResult,
runBuck2,
runBuck2Sync,
} from '../utils';
// $FlowExpectedError[untyped-import]
import fs from 'fs';
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 4f47991

Please sign in to comment.