Skip to content

feat: optimize devcontainer build performance - #24

Merged
keito4 merged 2 commits into
mainfrom
claude/issue-23-20250529_081248
May 29, 2025
Merged

feat: optimize devcontainer build performance#24
keito4 merged 2 commits into
mainfrom
claude/issue-23-20250529_081248

Conversation

@keito4

@keito4 keito4 commented May 29, 2025

Copy link
Copy Markdown
Owner

devcontainerのビルド時間を最適化しました。

変更内容

  • Docker層キャッシュの有効化
  • npmインストールの最適化
  • エラーハンドリングの改善

Fixes #23

Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Improved development container build performance by enabling Docker layer caching and configuring build cache settings.
    • Updated environment setup commands to run during container creation for more reliable initialization.
    • Enhanced npm installation process with better handling of missing commands and configuration files, clearer messaging, and improved caching for faster installs.

- Add build cacheFrom configuration for better layer caching
- Enable postCreateCommand instead of postStartCommand for setup
- Optimize npm install in common feature with caching and offline preference
- Improve error handling and path resolution in install script
- Add cache configuration file for future BuildKit optimizations

Co-authored-by: keito4 <keito4@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented May 29, 2025

Copy link
Copy Markdown

"""

Walkthrough

The pull request adds a Docker BuildKit cache configuration file, updates the devcontainer to activate BuildKit and shift Homebrew setup to container creation, and enhances the npm installation script with improved npm detection, cache handling, global.json file search, and safer dependency installation.

Changes

File(s) Change Summary
.devcontainer/.devcontainer-cache Added Docker BuildKit enablement and cache mount points for npm and Linuxbrew to speed up container builds.
.devcontainer/devcontainer.json Replaced commented postStartCommand with active postCreateCommand running Homebrew environment setup script.
.devcontainer/features/common/install.sh Added npm presence check, configured npm cache dir, improved global.json path search with find, and safer npm install.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant DevContainer
    participant Docker
    participant ShellScript

    Developer->>DevContainer: Build container
    DevContainer->>Docker: Enable BuildKit and use cache mounts
    Docker-->>DevContainer: Build with cached layers
    DevContainer->>ShellScript: Run install.sh (postCreateCommand)
    ShellScript->>ShellScript: Check npm, set cache, find global.json
    ShellScript->>ShellScript: Extract dependencies and install npm packages
    ShellScript-->>DevContainer: Output status and completion message
Loading

Assessment against linked issues

Objective Addressed Explanation
Speed up devcontainer build by caching (#23)

Possibly related PRs

Suggested labels

codex

Poem

In the garden of code, where containers grow fast,
A cache was unearthed to make slow builds the past.
With npm now smarter, and zsh set to run,
This rabbit hops quicker—dev work’s more fun!
🐇✨
"""


📜 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 4a05940 and 3f84d5c.

📒 Files selected for processing (3)
  • .devcontainer/.devcontainer-cache (1 hunks)
  • .devcontainer/devcontainer.json (1 hunks)
  • .devcontainer/features/common/install.sh (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • .devcontainer/devcontainer.json
  • .devcontainer/.devcontainer-cache
  • .devcontainer/features/common/install.sh
✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (1)
.devcontainer/.devcontainer-cache (1)

8-8: Consider making cache paths dynamic or configurable
Hardcoding /tmp/.npm-cache:/home/linuxbrew/.linuxbrew/var/cache may break if Brew’s cache directory moves. You could compute Homebrew’s cache at runtime (e.g., brew --cache) or allow overriding via an environment variable.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3fe7fe5 and 4a05940.

📒 Files selected for processing (3)
  • .devcontainer/.devcontainer-cache (1 hunks)
  • .devcontainer/devcontainer.json (2 hunks)
  • .devcontainer/features/common/install.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck (0.10.0)
.devcontainer/features/common/install.sh

[warning] 36-36: Quote this to prevent word splitting.

(SC2046)

🔇 Additional comments (5)
.devcontainer/devcontainer.json (1)

31-31: Approved: Switching to postCreateCommand for Homebrew initialization
Moving Homebrew setup to postCreateCommand ensures it only runs once at creation time, reducing overhead on every container start. The quoting looks correct and will source your updated ~/.zshrc.

.devcontainer/features/common/install.sh (4)

13-17: Correctly skip npm steps if npm is missing
The check and early exit for missing npm is clear and prevents errors.


19-23: Configure npm to use a dedicated cache directory
Creating and globally setting NPM_CACHE_DIR is an effective way to leverage layer caching for npm.


57-67: Robust handling of JSON dependencies
The jq checks and conditional installation logic are solid, ensuring you only run npm install -g when packages exist.


25-30:

Details

✅ Verification successful

Verify all expected global.json paths are accurate
You’ve added /workspace/npm/global.json and npm/global.json; please ensure these align with your workspace layout (e.g., correct top-level dir names).

You can run:

to confirm where your configuration files actually live.


🏁 Script executed:

rg --ignore-case "global.json" -n .

Length of output: 280


🏁 Script executed:

#!/bin/bash
# Locate where REPO_PATH is defined in install.sh for context on workspace root
rg -n "REPO_PATH" .devcontainer/features/common/install.sh

Length of output: 58


🏁 Script executed:

#!/bin/bash
# Find where REPO_PATH is set or exported in the repository to verify workspace root
rg -n "REPO_PATH=" .

Length of output: 102


✅ GLOBAL_JSON paths are correct
I’ve confirmed via rg -n global.json that the only global.json file lives at npm/global.json in the repo root. The added patterns

  • /workspaces/*/npm/global.json (Codespaces default mount)
  • /workspace/npm/global.json (Remote-Containers default mount)
  • $HOME/npm/global.json (home-directory fallback)
  • npm/global.json (relative-path fallback)

cover all expected layouts. No further changes needed.

# This file is used to improve build performance by caching layers

# Enable Docker BuildKit for better caching
DOCKER_BUILDKIT=1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Verification agent

❓ Verification inconclusive

Ensure BuildKit is actually enabled during devcontainer builds
Setting DOCKER_BUILDKIT=1 is correct, but verify that this file is consumed by your build invocation (e.g., via --env-file or equivalent in devcontainer.json). Without loading it, BuildKit won’t be enabled and layer caching won’t apply.

Run this script to confirm references to .devcontainer-cache exist in your config:


🏁 Script executed:

#!/bin/bash
# Verify that .devcontainer/.devcontainer-cache is referenced in devcontainer.json
rg --fixed-strings --line-number ".devcontainer-cache" -n

Length of output: 57


Add the cache env-file to your devcontainer configuration
It appears .devcontainer/.devcontainer-cache is never referenced in your devcontainer.json, so BuildKit won’t actually be enabled. You need to load that file via the build.envFile (or runArgs: ["--env-file", ...]) property.

Locations to update:

  • devcontainer.json (at project root)

Suggested change:

--- devcontainer.json
+++ devcontainer.json
@@
   "build": {
     "dockerfile": "Dockerfile",
-    // …other build options
+    "envFile": [
+      ".devcontainer/.devcontainer-cache"
+    ]
   },
   // …other config

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In .devcontainer/.devcontainer-cache at line 5, the environment variable
DOCKER_BUILDKIT=1 is set but this file is not referenced in devcontainer.json,
so BuildKit is not enabled during builds. To fix this, update devcontainer.json
at the project root to include the .devcontainer/.devcontainer-cache file in the
build.envFile property or add it to runArgs with --env-file. This ensures the
environment variable is loaded and BuildKit is activated for caching.

Comment thread .devcontainer/devcontainer.json Outdated
Comment on lines +18 to +20
"build": {
"cacheFrom": "mcr.microsoft.com/devcontainers/base:ubuntu"
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Incomplete build configuration prevents Docker layer caching
The cacheFrom field alone isn’t enough—Dev Containers require a dockerfile (and optionally context) to build your image. Without them, your container will be pulled instead of built, and caching won’t apply.

Please update to something like:

  "build": {
-   "cacheFrom": "mcr.microsoft.com/devcontainers/base:ubuntu"
+   "dockerfile": "Dockerfile",
+   "context": ".",
+   "cacheFrom": "mcr.microsoft.com/devcontainers/base:ubuntu"
  },
📝 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
"build": {
"cacheFrom": "mcr.microsoft.com/devcontainers/base:ubuntu"
},
"build": {
"dockerfile": "Dockerfile",
"context": ".",
"cacheFrom": "mcr.microsoft.com/devcontainers/base:ubuntu"
},
🤖 Prompt for AI Agents
In .devcontainer/devcontainer.json around lines 18 to 20, the build
configuration is incomplete because it only includes the cacheFrom field. To
enable Docker layer caching properly, add the dockerfile field specifying the
path to your Dockerfile, and optionally include the context field if your build
context differs from the default. This ensures the container image is built
locally with caching instead of being pulled.

for path in "${GLOBAL_JSON_PATHS[@]}"; do
# Use find to avoid shell expansion issues with wildcards
if [[ "$path" == *"*"* ]]; then
found_file=$(find $(dirname "$path") -name "global.json" -type f 2>/dev/null | head -1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Prevent word-splitting in find invocation
Per ShellCheck SC2046, wrap the directory in quotes to avoid splitting on spaces:

-found_file=$(find $(dirname "$path") -name "global.json" -type f 2>/dev/null | head -1)
+dir="$(dirname "$path")"
+found_file=$(find "$dir" -maxdepth 1 -type f -name "global.json" 2>/dev/null | head -1)
📝 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
found_file=$(find $(dirname "$path") -name "global.json" -type f 2>/dev/null | head -1)
dir="$(dirname "$path")"
found_file=$(find "$dir" -maxdepth 1 -type f -name "global.json" 2>/dev/null | head -1)
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 36-36: Quote this to prevent word splitting.

(SC2046)

🤖 Prompt for AI Agents
In .devcontainer/features/common/install.sh at line 36, the directory argument
to the find command is not quoted, which can cause word splitting if the path
contains spaces. To fix this, wrap $(dirname "$path") in double quotes so the
find command receives the directory as a single argument and avoids word
splitting issues.

@keito4
keito4 merged commit e92d228 into main May 29, 2025
@keito4
keito4 deleted the claude/issue-23-20250529_081248 branch January 4, 2026 08:08
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.

devcontainerのビルドが都度時間がかかってしまう

1 participant