Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: dev dependencies removed in docker build #1742

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

claytonneal
Copy link
Member

@claytonneal claytonneal commented Jan 23, 2025

Description

Remove dev dependencies in RPC proxy docker build

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Local unit tests

Test Configuration:

  • Node.js Version: 18.18.0

Checklist:

  • My code follows the coding standards of this project
  • I have performed a self-review of my code
  • I have commented on my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • New and existing integration tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have not added any vulnerable dependencies to my code

Summary by CodeRabbit

  • Chores
    • Enhanced Docker build process for production environment
    • Added script to clean and optimize package.json files
    • Removed development dependencies and simplified dependency versioning during build

@claytonneal claytonneal requested a review from a team as a code owner January 23, 2025 17:55
Copy link

coderabbitai bot commented Jan 23, 2025

Walkthrough

The pull request introduces a new shell script adjust-packages.sh in the docker/rpc-proxy directory to optimize the Docker build process for a production environment. The script modifies package.json files by removing development dependencies and simplifying version selectors. The Dockerfile is updated to incorporate this script, installing the jq utility and executing the script during the build stage to clean package configurations.

Changes

File Change Summary
docker/rpc-proxy/Dockerfile Added steps to copy and execute adjust-packages.sh script, installing jq utility
docker/rpc-proxy/adjust-packages.sh New shell script to process and clean package.json files by removing dev dependencies and simplifying version selectors

Sequence Diagram

sequenceDiagram
    participant Dockerfile
    participant Script as adjust-packages.sh
    participant PackageJSON as package.json files

    Dockerfile->>Script: Copy and execute script
    Script->>PackageJSON: Find package.json files
    Script->>PackageJSON: Remove devDependencies
    Script->>PackageJSON: Simplify dependency versions
Loading

Possibly related PRs

Suggested reviewers

  • fabiorigam
  • leszek-vechain

Poem

🐰 In Docker's realm, a script so neat,
Cleaning packages, making builds complete
With jq in hand and magic touch,
Removing dev deps, we don't need much!
A leaner build, a rabbit's delight 🚀


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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. (Beta)
  • @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

github-actions bot commented Jan 23, 2025

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 98%
98.93% (4378/4425) 96.95% (1400/1444) 98.9% (906/916)
Title Tests Skipped Failures Errors Time
core 836 0 💤 0 ❌ 0 🔥 2m 29s ⏱️
network 731 0 💤 0 ❌ 0 🔥 5m 7s ⏱️
errors 40 0 💤 0 ❌ 0 🔥 17.123s ⏱️
logging 3 0 💤 0 ❌ 0 🔥 19.009s ⏱️
hardhat-plugin 19 0 💤 0 ❌ 0 🔥 1m 4s ⏱️
aws-kms-adapter 23 0 💤 0 ❌ 0 🔥 1m 25s ⏱️
ethers-adapter 5 0 💤 0 ❌ 0 🔥 1m 19s ⏱️
rpc-proxy 37 0 💤 0 ❌ 0 🔥 1m 9s ⏱️

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (3)
docker/rpc-proxy/adjust-packages.sh (2)

1-10: Enhance script documentation.

Consider adding the following details to the documentation:

  • Prerequisites (e.g., jq dependency)
  • Exit codes and their meanings
  • Error handling behavior
 #!/bin/bash

 # -----------------------------------------------------------------------------
 # Info:
+# Prerequisites:
+#   - jq: JSON processor (required)
+#
 # - input: directory to search for package.json files
 # - output: 
 #     package.json without devDependencies section
 #     package.json without version selectors in dependencies
+#
+# Exit codes:
+#   0: Success
+#   1: Invalid/missing input directory
+#   2: jq command not found
 # -----------------------------------------------------------------------------

42-42: Improve version selector handling.

The current version selector removal logic might miss some cases:

  1. Only handles caret (^) and tilde (~)
  2. Multiple ltrimstr might not handle all cases correctly

Consider using a more robust approach:

-    jq '(.dependencies |= with_entries(.value |= ltrimstr("^") | ltrimstr("~")))' "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON"
+    jq '(.dependencies |= with_entries(.value |= sub("^[~^]"; "") | sub("^>=?"; "") | sub("^<=?"; "") | sub("^="; "")))' "$PACKAGE_JSON" > "$TEMP_FILE" && mv -f "$TEMP_FILE" "$PACKAGE_JSON"
docker/rpc-proxy/Dockerfile (1)

22-25: Optimize the package cleaning steps.

While the implementation is functionally correct, we can optimize these steps to reduce the number of layers and improve maintainability.

Consider combining the steps into a single RUN instruction:

-# Clean the package.json files ready for production
-RUN apk add --no-cache jq
-RUN chmod +x ./adjust-packages.sh
-RUN /bin/sh ./adjust-packages.sh ./
+# Clean the package.json files ready for production
+RUN apk add --no-cache jq && \
+    chmod +x ./adjust-packages.sh && \
+    ./adjust-packages.sh ./

This change:

  1. Reduces the number of layers in the final image
  2. Maintains the same functionality
  3. Removes the redundant /bin/sh invocation
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8537acc and e758c1e.

📒 Files selected for processing (2)
  • docker/rpc-proxy/Dockerfile (1 hunks)
  • docker/rpc-proxy/adjust-packages.sh (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: rpc-proxy / docker / docker
  • GitHub Check: rpc-proxy / test / test
  • GitHub Check: unit-integration-test-browser / Build & Lint (latest)
  • GitHub Check: unit-integration-test / Build & Lint (latest)
  • GitHub Check: unit-integration-test-browser / Build & Lint (lts/*)
  • GitHub Check: unit-integration-test / Build & Lint (lts/*)
  • GitHub Check: unit-integration-test-browser / Build & Lint (18)
  • GitHub Check: unit-integration-test / Build & Lint (18)
  • GitHub Check: test-apps / Install and test example apps
  • GitHub Check: install-build / Build & Lint
  • GitHub Check: Execute doc examples
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (3)
docker/rpc-proxy/adjust-packages.sh (1)

1-48: Verify Docker build integration.

Let's ensure the script is properly integrated into the Docker build process and that it effectively removes development dependencies.

✅ Verification successful

Docker integration verified successfully ✓

The script is properly integrated into the Docker build process with all necessary components in place:

  • Installed dependencies (jq)
  • Correct permissions
  • Executed at the right stage (after build, before production copy)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify script integration in Dockerfile and its effectiveness

# Test 1: Check if the script is referenced in Dockerfile
echo "Checking Dockerfile integration..."
rg -l "adjust-packages.sh" --type dockerfile

# Test 2: Verify script permissions in Dockerfile
echo "Checking script permissions..."
rg "chmod.*adjust-packages.sh" --type dockerfile

# Test 3: Check if jq is installed in Dockerfile
echo "Checking jq installation..."
rg "jq" --type dockerfile

Length of output: 441


Script:

#!/bin/bash
# Find and examine Dockerfile contents
echo "=== Locating Dockerfiles ==="
fd -t f "^[Dd]ockerfile"

echo -e "\n=== Examining Dockerfile contents ==="
fd -t f "^[Dd]ockerfile" -x cat {}

Length of output: 3175

docker/rpc-proxy/Dockerfile (2)

17-17: Verify the contents of adjust-packages.sh script.

The placement of the COPY instruction is correct. However, we should verify the script's implementation to ensure it correctly handles package.json modifications.

Let's examine the script's contents:

✅ Verification successful

Script implementation is correct and safe for package.json processing

The script correctly:

  • Removes development dependencies from package.json files
  • Strips version selectors for stable builds
  • Includes proper error handling and atomic updates
  • Has appropriate executable permissions
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the contents and permissions of adjust-packages.sh

# Check if the script exists and show its contents
cat ./docker/rpc-proxy/adjust-packages.sh

# Verify script permissions
ls -l ./docker/rpc-proxy/adjust-packages.sh

Length of output: 1950


Line range hint 1-56: Verify build process after dependency cleanup.

The implementation correctly addresses the PR objective of removing development dependencies. However, we should verify that:

  1. The build process still works after removing devDependencies
  2. Workspace dependencies are correctly handled

Let's verify the build process:

✅ Verification successful

Dependencies and build process are properly configured

The implementation correctly:

  • Maintains all required runtime dependencies
  • Uses proper workspace package structure
  • Implements clean separation between build and runtime stages
  • Handles production dependency installation correctly
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the build process and workspace dependencies

# Check workspace dependencies in package.json files
echo "Checking workspace dependencies..."
find . -name "package.json" -exec jq '.workspaces, .dependencies' {} \;

# List all dependencies that will be installed in production
echo "Listing production dependencies..."
yarn workspace @vechain/sdk-rpc-proxy list --production --depth=0

Length of output: 3456


Script:

#!/bin/bash
# Description: Verify production dependencies using npm instead of yarn

# Check the rpc-proxy package.json directly
echo "Checking RPC proxy package.json..."
cat packages/rpc-proxy/package.json | jq '{name: .name, dependencies: .dependencies, peerDependencies: .peerDependencies}'

# List files that will be included in the production build
echo -e "\nChecking production build files..."
find packages/rpc-proxy/dist -type f 2>/dev/null || echo "dist directory not found (build not executed)"

Length of output: 767

Comment on lines +11 to +22
# Check if the search directory is provided
if [ -z "$1" ]; then
echo "Usage: $0 <path-to-directory>"
exit 1
fi
SEARCH_DIR="$1"

# Check if the search directory exists
if [ ! -d "$SEARCH_DIR" ]; then
echo "Error: Directory '$SEARCH_DIR' not found."
exit 1
fi
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add essential error handling and cleanup.

The script needs additional safeguards:

  1. Validate jq dependency
  2. Clean up temporary files on exit
  3. Handle script interruption
+# Check if jq is available
+if ! command -v jq >/dev/null 2>&1; then
+  echo "Error: jq is required but not installed."
+  exit 2
+fi
+
+# Cleanup function
+cleanup() {
+  rm -f temp.json
+  exit $?
+}
+
+# Set up trap for cleanup
+trap cleanup EXIT
+trap 'exit 2' INT TERM
+
 # Check if the search directory is provided
 if [ -z "$1" ]; then
   echo "Usage: $0 <path-to-directory>"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Check if the search directory is provided
if [ -z "$1" ]; then
echo "Usage: $0 <path-to-directory>"
exit 1
fi
SEARCH_DIR="$1"
# Check if the search directory exists
if [ ! -d "$SEARCH_DIR" ]; then
echo "Error: Directory '$SEARCH_DIR' not found."
exit 1
fi
# Check if jq is available
if ! command -v jq >/dev/null 2>&1; then
echo "Error: jq is required but not installed."
exit 2
fi
# Cleanup function
cleanup() {
rm -f temp.json
exit $?
}
# Set up trap for cleanup
trap cleanup EXIT
trap 'exit 2' INT TERM
# Check if the search directory is provided
if [ -z "$1" ]; then
echo "Usage: $0 <path-to-directory>"
exit 1
fi
SEARCH_DIR="$1"
# Check if the search directory exists
if [ ! -d "$SEARCH_DIR" ]; then
echo "Error: Directory '$SEARCH_DIR' not found."
exit 1
fi

Comment on lines +25 to +48
# Find all package.json files in the specified directory (excluding node_modules)
find "$SEARCH_DIR" -type f -name "package.json" ! -path "*/node_modules/*" | while read PACKAGE_JSON; do
echo "Processing: $PACKAGE_JSON"


# Remove the "devDependencies" section and overwrite the file
if jq -e '.devDependencies' "$PACKAGE_JSON" > /dev/null; then
jq 'del(.devDependencies)' "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON"
echo "devDependencies removed from $PACKAGE_JSON"
else
echo "No devDependencies section found in $PACKAGE_JSON. No changes made."
fi

# Remove version selectors from dependencies
# Check if the dependencies section exists in the package.json file
if jq -e '.dependencies' "$PACKAGE_JSON" > /dev/null; then
# Remove version selectors from dependencies
jq '(.dependencies |= with_entries(.value |= ltrimstr("^") | ltrimstr("~")))' "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON"
echo "Version selectors (caret/tilde) removed from dependencies in $PACKAGE_JSON"
else
echo "No dependencies section found in $PACKAGE_JSON. No changes made."
fi

done
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix potential race conditions and improve error handling.

The current implementation has several potential issues:

  1. Race condition with shared temp.json
  2. Missing error handling for jq operations
  3. Potential issues with filenames containing spaces
  4. Non-atomic file operations

Apply these improvements:

+# Create a unique temporary file
+TEMP_FILE=$(mktemp)
+
 # Find all package.json files in the specified directory (excluding node_modules)
-find "$SEARCH_DIR" -type f -name "package.json" ! -path "*/node_modules/*" | while read PACKAGE_JSON; do
+find "$SEARCH_DIR" -type f -name "package.json" ! -path "*/node_modules/*" -print0 | while IFS= read -r -d '' PACKAGE_JSON; do
   echo "Processing: $PACKAGE_JSON"
 
   # Remove the "devDependencies" section and overwrite the file
   if jq -e '.devDependencies' "$PACKAGE_JSON" > /dev/null; then
-      jq 'del(.devDependencies)' "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON"
+      if ! jq 'del(.devDependencies)' "$PACKAGE_JSON" > "$TEMP_FILE"; then
+          echo "Error: Failed to process devDependencies in $PACKAGE_JSON"
+          continue
+      fi
+      if ! mv -f "$TEMP_FILE" "$PACKAGE_JSON"; then
+          echo "Error: Failed to update $PACKAGE_JSON"
+          continue
+      fi
       echo "devDependencies removed from $PACKAGE_JSON"
   else
       echo "No devDependencies section found in $PACKAGE_JSON. No changes made."
   fi
 
   # Remove version selectors from dependencies
   # Check if the dependencies section exists in the package.json file
   if jq -e '.dependencies' "$PACKAGE_JSON" > /dev/null; then
     # Remove version selectors from dependencies
-    jq '(.dependencies |= with_entries(.value |= ltrimstr("^") | ltrimstr("~")))' "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON"
+    if ! jq '(.dependencies |= with_entries(.value |= ltrimstr("^") | ltrimstr("~")))' "$PACKAGE_JSON" > "$TEMP_FILE"; then
+        echo "Error: Failed to process dependencies in $PACKAGE_JSON"
+        continue
+    fi
+    if ! mv -f "$TEMP_FILE" "$PACKAGE_JSON"; then
+        echo "Error: Failed to update $PACKAGE_JSON"
+        continue
+    fi
     echo "Version selectors (caret/tilde) removed from dependencies in $PACKAGE_JSON"
   else
     echo "No dependencies section found in $PACKAGE_JSON. No changes made."
   fi
 done
+
+# Check if the loop completed successfully
+EXIT_CODE=$?
+if [ $EXIT_CODE -ne 0 ]; then
+    echo "Error: Script failed with exit code $EXIT_CODE"
+    exit $EXIT_CODE
+fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Find all package.json files in the specified directory (excluding node_modules)
find "$SEARCH_DIR" -type f -name "package.json" ! -path "*/node_modules/*" | while read PACKAGE_JSON; do
echo "Processing: $PACKAGE_JSON"
# Remove the "devDependencies" section and overwrite the file
if jq -e '.devDependencies' "$PACKAGE_JSON" > /dev/null; then
jq 'del(.devDependencies)' "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON"
echo "devDependencies removed from $PACKAGE_JSON"
else
echo "No devDependencies section found in $PACKAGE_JSON. No changes made."
fi
# Remove version selectors from dependencies
# Check if the dependencies section exists in the package.json file
if jq -e '.dependencies' "$PACKAGE_JSON" > /dev/null; then
# Remove version selectors from dependencies
jq '(.dependencies |= with_entries(.value |= ltrimstr("^") | ltrimstr("~")))' "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON"
echo "Version selectors (caret/tilde) removed from dependencies in $PACKAGE_JSON"
else
echo "No dependencies section found in $PACKAGE_JSON. No changes made."
fi
done
# Create a unique temporary file
TEMP_FILE=$(mktemp)
# Find all package.json files in the specified directory (excluding node_modules)
find "$SEARCH_DIR" -type f -name "package.json" ! -path "*/node_modules/*" -print0 | while IFS= read -r -d '' PACKAGE_JSON; do
echo "Processing: $PACKAGE_JSON"
# Remove the "devDependencies" section and overwrite the file
if jq -e '.devDependencies' "$PACKAGE_JSON" > /dev/null; then
if ! jq 'del(.devDependencies)' "$PACKAGE_JSON" > "$TEMP_FILE"; then
echo "Error: Failed to process devDependencies in $PACKAGE_JSON"
continue
fi
if ! mv -f "$TEMP_FILE" "$PACKAGE_JSON"; then
echo "Error: Failed to update $PACKAGE_JSON"
continue
fi
echo "devDependencies removed from $PACKAGE_JSON"
else
echo "No devDependencies section found in $PACKAGE_JSON. No changes made."
fi
# Remove version selectors from dependencies
# Check if the dependencies section exists in the package.json file
if jq -e '.dependencies' "$PACKAGE_JSON" > /dev/null; then
# Remove version selectors from dependencies
if ! jq '(.dependencies |= with_entries(.value |= ltrimstr("^") | ltrimstr("~")))' "$PACKAGE_JSON" > "$TEMP_FILE"; then
echo "Error: Failed to process dependencies in $PACKAGE_JSON"
continue
fi
if ! mv -f "$TEMP_FILE" "$PACKAGE_JSON"; then
echo "Error: Failed to update $PACKAGE_JSON"
continue
fi
echo "Version selectors (caret/tilde) removed from dependencies in $PACKAGE_JSON"
else
echo "No dependencies section found in $PACKAGE_JSON. No changes made."
fi
done
# Check if the loop completed successfully
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "Error: Script failed with exit code $EXIT_CODE"
exit $EXIT_CODE
fi

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.

💡 [REQUEST] - Create PROD yarn.lock for the rpc-proxy Docker image
2 participants