Skip to content

Conversation

@jaipaljadeja
Copy link
Member

fixes prod indexers missing runtime values when running using node .apibara/build/start.mjs --indexers starknet.

added an internal flag (--standalone default: true) to the runtime start.mjs to switch between how and from where runtime values are resolved.

in apibara build -> apibara start, we run the start.mjs as child process with flag --no-standalone, thus runtime values are resolved from internal env variables which are set by the apibara cli itself, basically same as how runtime values are resolved in dev mode.

when running indexers using node, it uses a virtual module created in rolldown during build, which exports static values for runtimeConfig, preset, presets and also exports userEnvRuntimeConfig which is JSON parsed value of user's runtime override env variable - APIBARA_RUNTIME_CONFIG.

its important to note that in contrast to above, dev.mjs is only supposed to be run with apibara cli only and NOT with node.

@jaipaljadeja jaipaljadeja requested a review from fracek April 18, 2025 21:20
@coderabbitai
Copy link

coderabbitai bot commented Apr 18, 2025

📝 Walkthrough

Walkthrough

This change refactors the way runtime configuration and preset data are provided to the CLI and indexer processes. It introduces a new static configuration plugin for the Rolldown bundler, which embeds configuration values into a virtual module at build time. The CLI's start command and related logic are updated to accept new arguments and to use these statically embedded configurations when running in standalone mode, while maintaining compatibility with dynamic runtime data when not in standalone mode. Import paths for shared CLI utilities are updated to use the package-level export.

Changes

File(s) Change Summary
packages/cli/src/cli/commands/add.ts,
.../build.ts,
.../dev.ts,
.../init.ts,
.../write-project-info.ts
Updated import paths for checkForUnknownArgs (and commonArgs where relevant) from a relative local path to the package-level export "apibara/common". No logic changes.
packages/cli/src/cli/commands/prepare.ts Changed import of checkForUnknownArgs and commonArgs to "apibara/common" and removed an unused import from "apibara/types". No logic changes.
packages/cli/src/cli/commands/start.ts Updated imports as above. Modified the construction of the childArgs array to always include --no-standalone and conditionally add --preset if provided, affecting how the indexer is started and ensuring correct runtime value loading.
packages/cli/src/common/index.ts Added export * from "./cli" to re-export all exports from the cli module, expanding the public API of the common package.
packages/cli/src/rolldown/config.ts Added the new staticConfig plugin to the Rolldown configuration, ensuring it is the first plugin applied. No other configuration logic changed.
packages/cli/src/rolldown/plugins/static-config.ts Introduced the staticConfig plugin, which serializes and embeds configuration and preset data into a virtual module for use at runtime.
packages/cli/src/runtime/start.ts The startCommand now accepts new preset and standalone arguments, and conditionally loads configuration from static build-time values or from environment variables, depending on the standalone flag. The command handler and argument parsing were updated accordingly.
packages/cli/src/types/virtual/static-config.d.ts Added TypeScript declarations for the virtual static configuration module, exporting typed constants for preset, presets, runtimeConfig, and userEnvRuntimeConfig.
change/apibara-ada1fb63-906b-4e5d-adf0-79c86ee61a13.json Added a prerelease metadata file documenting a patch related to production mode handling for runtime values in the CLI. No code changes.

Sequence Diagram(s)

sequenceDiagram
    participant CLI as CLI Process
    participant Rolldown as Rolldown Bundler
    participant StaticConfig as Static Config Plugin
    participant Indexer as Indexer Process

    CLI->>Rolldown: Build project
    Rolldown->>StaticConfig: Apply staticConfig plugin
    StaticConfig-->>Rolldown: Embed config into virtual module
    Rolldown-->>CLI: Output bundle with embedded config

    CLI->>Indexer: Start indexer with args (preset, --no-standalone, etc.)
    Indexer->>StaticConfig: Import config from virtual module
    StaticConfig-->>Indexer: Provide preset, presets, runtimeConfig
    Indexer->>Indexer: Run using provided configuration
Loading

Possibly related PRs

Suggested reviewers

  • fracek

Poem

Hopping through configs, a bunny so sly,
Embeds the settings—no need to ask why!
With static and runtime, the CLI’s spry,
Plugins and presets now bundled up high.
The indexer starts, no need for alarm—
For rabbits and coders, config is now charm!
🐇✨

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 87b7608 and 6e2b994.

📒 Files selected for processing (13)
  • change/apibara-ada1fb63-906b-4e5d-adf0-79c86ee61a13.json (1 hunks)
  • packages/cli/src/cli/commands/add.ts (1 hunks)
  • packages/cli/src/cli/commands/build.ts (1 hunks)
  • packages/cli/src/cli/commands/dev.ts (1 hunks)
  • packages/cli/src/cli/commands/init.ts (1 hunks)
  • packages/cli/src/cli/commands/prepare.ts (1 hunks)
  • packages/cli/src/cli/commands/start.ts (2 hunks)
  • packages/cli/src/cli/commands/write-project-info.ts (1 hunks)
  • packages/cli/src/common/index.ts (1 hunks)
  • packages/cli/src/rolldown/config.ts (2 hunks)
  • packages/cli/src/rolldown/plugins/static-config.ts (1 hunks)
  • packages/cli/src/runtime/start.ts (2 hunks)
  • packages/cli/src/types/virtual/static-config.d.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
packages/cli/src/rolldown/config.ts (1)
packages/cli/src/rolldown/plugins/static-config.ts (1)
  • staticConfig (6-21)
packages/cli/src/rolldown/plugins/static-config.ts (2)
packages/cli/src/types/apibara.ts (1)
  • Apibara (13-20)
packages/cli/src/common/constants.ts (1)
  • USER_ENV_APIBARA_RUNTIME_CONFIG (6-6)
packages/cli/src/runtime/start.ts (6)
packages/cli/src/common/cli.ts (1)
  • checkForUnknownArgs (16-40)
packages/cli/src/types/virtual/static-config.d.ts (1)
  • preset (1-1)
packages/cli/src/common/helper.ts (2)
  • getProcessedRuntimeConfig (41-86)
  • getRuntimeDataFromEnv (10-32)
packages/cli/src/runtime/internal/app.ts (1)
  • createIndexer (24-97)
packages/indexer/src/indexer.ts (1)
  • createIndexer (129-150)
packages/cli/src/runtime/index.ts (1)
  • createIndexer (1-1)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (17)
packages/cli/src/cli/commands/add.ts (1)

1-1: Consolidated import from shared utilities package
Switching the import of checkForUnknownArgs to "apibara/common" aligns with our goal of centralizing CLI helpers. This change is consistent with other command modules—great for maintainability.

packages/cli/src/cli/commands/init.ts (1)

1-1: Unified import of argument validation helper
Importing checkForUnknownArgs from the new "apibara/common" package keeps argument‐validation logic DRY and consistent across commands. Ensure the package re-exports this helper correctly.

packages/cli/src/cli/commands/write-project-info.ts (1)

3-3: Standardized import for unknown-args check and common arguments
Updating both checkForUnknownArgs and commonArgs to come from "apibara/common" centralizes shared CLI configuration. This helps prevent divergence in argument handling.

packages/cli/src/cli/commands/build.ts (1)

1-1: Centralized import of CLI helpers
Changing to import { checkForUnknownArgs, commonArgs } from "apibara/common" maintains consistency with other commands and ensures reuse of shared argument definitions.

packages/cli/src/cli/commands/dev.ts (1)

2-2: Consistent sourcing of validation and common-args utilities
The shift to importing checkForUnknownArgs and commonArgs from "apibara/common" follows the new modular structure. This helps keep the dev command in sync with the rest of the CLI suite.

packages/cli/src/cli/commands/prepare.ts (1)

1-1: Import path refactoring looks good

This change centralizes common CLI utilities by importing from the package-level "apibara/common" rather than using relative paths. This is a good practice that improves maintainability.

change/apibara-ada1fb63-906b-4e5d-adf0-79c86ee61a13.json (1)

1-7: Change metadata correctly documents the PR purpose

The metadata accurately reflects that this is a patch-level bug fix addressing runtime values in production mode, which aligns with the PR objectives.

packages/cli/src/common/index.ts (1)

3-3: Appropriate barrel file update to support refactored imports

This change exports the CLI module from the common package, enabling the package-level imports used in CLI command files like prepare.ts. This is a good architectural practice for centralizing package exports.

packages/cli/src/rolldown/config.ts (2)

13-13: Import for new static configuration plugin

Adding the staticConfig plugin import supports the PR objective of fixing production mode for runtime values by enabling build-time embedding of configuration constants.


81-81: Correctly registers the staticConfig plugin

The staticConfig plugin is appropriately registered first in the plugins array, ensuring that the virtual module with embedded configuration values is available to subsequent plugins during the build process.

packages/cli/src/cli/commands/start.ts (2)

2-2: Approved: Import from package instead of relative path.

This change improves maintainability by importing common utilities from the package-level export rather than using relative imports.


53-61: Properly configured child process arguments to ensure runtime values load correctly.

The updated implementation includes the critical --no-standalone flag and conditionally passes the preset parameter. These changes ensure that runtime values are resolved from internal environment variables set by the CLI when using apibara start, mirroring the resolution method used in development mode.

packages/cli/src/rolldown/plugins/static-config.ts (1)

1-21: Good implementation of static configuration plugin.

This new plugin effectively embeds runtime configuration values as static constants during build time. It correctly:

  1. Extracts configuration options from the Apibara instance
  2. Creates a virtual module with serialized configuration values
  3. Handles user environment variable runtime config parsing at runtime

The implementation properly supports the standalone mode execution pathway by making configuration values available without requiring the CLI to set environment variables.

packages/cli/src/types/virtual/static-config.d.ts (1)

1-4: Appropriate type declarations for virtual module exports.

The type declarations correctly define the structure of the static configuration exports, providing proper TypeScript typing for the virtual module created by the staticConfig plugin.

packages/cli/src/runtime/start.ts (3)

2-17: Well-organized imports for runtime configuration.

The updated imports properly organize the common utilities and add the static configuration values from the virtual module. The comment on line 11-12 clearly explains when these static values are used.


31-40: Good addition of preset and standalone arguments.

The new arguments are clearly defined with appropriate types and descriptions. The default value of true for standalone ensures backward compatibility for direct node execution.


42-64: Effective implementation of dual configuration resolution paths.

The bifurcated logic correctly handles both standalone and non-standalone execution:

  1. For standalone mode (direct node execution), it uses statically embedded configuration
  2. For non-standalone mode (via CLI), it retrieves runtime data from environment variables

The clear comments and well-structured code make the intention and behavior easy to understand.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@fracek fracek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and now it works great!

@fracek fracek merged commit a300007 into apibara:main Apr 19, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants