From 0979d5026219e548a9b69d125c8db628a830b0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Tue, 31 Mar 2026 08:32:57 -0700 Subject: [PATCH] Add CI workflow for validating C++ API snapshot (#56042) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56042 Adds CI workflow for validating whether the current C++ API snapshot is equivalent with the generated one. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D95963515 fbshipit-source-id: 4629999e2d09dbdcfb9fd3e6308a9254dd4f0237 --- .../workflows/validate-cxx-api-snapshots.yml | 82 +++++++++++++++++++ scripts/cxx-api/parser/__main__.py | 36 +++++--- 2 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/validate-cxx-api-snapshots.yml diff --git a/.github/workflows/validate-cxx-api-snapshots.yml b/.github/workflows/validate-cxx-api-snapshots.yml new file mode 100644 index 00000000000..4ba6770d2c7 --- /dev/null +++ b/.github/workflows/validate-cxx-api-snapshots.yml @@ -0,0 +1,82 @@ +name: Validate C++ API Snapshots + +on: + workflow_dispatch: + pull_request: + paths: + - "packages/react-native/ReactCommon/**" + - "packages/react-native/ReactAndroid/**" + - "packages/react-native/React/**" + - "packages/react-native/ReactApple/**" + - "packages/react-native/Libraries/**" + - "scripts/cxx-api/**" + push: + branches: + - main + - "*-stable" + paths: + - "packages/react-native/ReactCommon/**" + - "packages/react-native/ReactAndroid/**" + - "packages/react-native/React/**" + - "packages/react-native/ReactApple/**" + - "packages/react-native/Libraries/**" + - "scripts/cxx-api/**" + +env: + DOXYGEN_VERSION: "1.16.1" + +jobs: + validate_cxx_api_snapshots: + runs-on: ubuntu-latest + if: github.repository == 'facebook/react-native' + steps: + - name: Checkout + uses: actions/checkout@v6 + - name: Setup node.js + uses: ./.github/actions/setup-node + - name: Run yarn + uses: ./.github/actions/yarn-install + - name: Restore Doxygen cache + id: cache-doxygen + uses: actions/cache@v4 + with: + path: /tmp/doxygen-${{ env.DOXYGEN_VERSION }} + key: doxygen-${{ env.DOXYGEN_VERSION }} + - name: Install Doxygen + if: steps.cache-doxygen.outputs.cache-hit != 'true' + shell: bash + run: | + DOXYGEN_URL="https://github.com/doxygen/doxygen/releases/download/Release_${DOXYGEN_VERSION//./_}/doxygen-${DOXYGEN_VERSION}.linux.bin.tar.gz" + MAX_RETRIES=3 + for i in $(seq 1 $MAX_RETRIES); do + echo "Attempt $i of $MAX_RETRIES: Installing Doxygen ${DOXYGEN_VERSION}..." + curl -fsSL "$DOXYGEN_URL" -o /tmp/doxygen.tar.gz && \ + tar -xzf /tmp/doxygen.tar.gz -C /tmp && \ + echo "Doxygen installed successfully." && \ + break + echo "Attempt $i failed." + if [ $i -eq $MAX_RETRIES ]; then + echo "All $MAX_RETRIES attempts failed." + exit 1 + fi + sleep 5 + done + - name: Set DOXYGEN_BIN + shell: bash + run: echo "DOXYGEN_BIN=/tmp/doxygen-${DOXYGEN_VERSION}/bin/doxygen" >> "$GITHUB_ENV" + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + - name: Install Python dependencies + shell: bash + run: pip install doxmlparser natsort pyyaml + - name: Validate C++ API snapshots + shell: bash + continue-on-error: true + run: yarn cxx-api-validate --output-dir /tmp/cxx-api-snapshots + - name: Upload C++ API snapshots + uses: actions/upload-artifact@v6 + with: + name: cxx-api-snapshots + path: /tmp/cxx-api-snapshots/ diff --git a/scripts/cxx-api/parser/__main__.py b/scripts/cxx-api/parser/__main__.py index 00f5d219356..7d8635f754c 100644 --- a/scripts/cxx-api/parser/__main__.py +++ b/scripts/cxx-api/parser/__main__.py @@ -84,7 +84,7 @@ def build_snapshot_for_view( exclude_patterns: list[str], definitions: dict[str, str | int], output_dir: str, - codegen_platform: str | None = None, + codegen_dir: str | None = None, verbose: bool = True, input_filter: str = None, work_dir: str | None = None, @@ -97,14 +97,7 @@ def build_snapshot_for_view( if work_dir is None: work_dir = os.path.join(react_native_dir, "api") - if codegen_platform is not None: - codegen_output = os.path.join(work_dir, "codegen") - codegen_dir = build_codegen( - codegen_platform, - verbose=verbose, - output_path=codegen_output, - label=api_view, - ) + if codegen_dir is not None: include_directories.append(codegen_dir) elif verbose: print(f"[{api_view}] Skipping codegen") @@ -156,6 +149,23 @@ def build_snapshots( ] with tempfile.TemporaryDirectory(prefix="cxx-api-") as parent_tmp: + # Run codegen once per unique platform before parallel snapshot generation. + # Debug/release variants share the same codegen output, and running + # multiple codegen processes in parallel causes race conditions + # (e.g. concurrent yarn install in buildCodegenIfNeeded). + codegen_dirs: dict[str, str] = {} + for config in configs_to_build: + platform = config.codegen_platform + if platform is not None and platform not in codegen_dirs: + codegen_output = os.path.join(parent_tmp, f"codegen-{platform}") + os.makedirs(codegen_output, exist_ok=True) + codegen_dirs[platform] = build_codegen( + platform, + verbose=verbose, + output_path=codegen_output, + label=platform, + ) + with concurrent.futures.ThreadPoolExecutor() as executor: futures = {} for config in configs_to_build: @@ -169,7 +179,7 @@ def build_snapshots( exclude_patterns=config.exclude_patterns, definitions=config.definitions, output_dir=output_dir, - codegen_platform=config.codegen_platform, + codegen_dir=codegen_dirs.get(config.codegen_platform), verbose=verbose, input_filter=input_filter if config.input_filter else None, work_dir=work_dir, @@ -200,7 +210,7 @@ def build_snapshots( exclude_patterns=[], definitions={}, output_dir=output_dir, - codegen_platform=None, + codegen_dir=None, verbose=verbose, input_filter=input_filter, ) @@ -286,7 +296,9 @@ def main(): with tempfile.TemporaryDirectory() as tmpdir: snapshot_output_dir = ( - tmpdir if args.check else args.output_dir or get_default_snapshot_dir() + args.output_dir or tmpdir + if args.check + else args.output_dir or get_default_snapshot_dir() ) build_snapshots(