Skip to content

fix: npm グローバルパッケージのdevcontainer対応を改善 - #26

Closed
keito4 wants to merge 1 commit into
mainfrom
claude/issue-13-20250530_055229
Closed

fix: npm グローバルパッケージのdevcontainer対応を改善#26
keito4 wants to merge 1 commit into
mainfrom
claude/issue-13-20250530_055229

Conversation

@keito4

@keito4 keito4 commented May 30, 2025

Copy link
Copy Markdown
Owner

NPMグローバルパッケージがdevcontainer上で読み込まれない問題を修正しました。

主な変更

  • install.shでのnpmパッケージインストール処理を改善
  • devcontainer専用の.zshrc設定を追加
  • npmグローバルパッケージのPATH設定を追加

Closes #13

Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Updated development container setup to automatically configure .zshrc with support for NPM global binaries in the PATH.
    • Improved npm directory setup and permissions for a smoother development environment.
    • Enhanced logging for npm package installations and environment setup within the dev container.

- install.shでのnpmパッケージインストール処理を修正
  - --location=userオプションを削除してグローバルインストールを確実に実行
  - パッケージを個別にインストールしてエラーハンドリングを強化
  - NPM_CONFIG_PREFIXを設定して専用グローバルディレクトリを指定
  - インストール結果の検証を追加

- devcontainer.jsonにpostCreateCommandを追加
  - devcontainer専用の.zshrcを自動設定

- .zshrc.devcontainerにnpmグローバルパッケージのPATH設定を追加
  - ~/.npm-global/binをPATHに追加してグローバルパッケージを利用可能に

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

coderabbitai Bot commented May 30, 2025

Copy link
Copy Markdown

Walkthrough

The changes update the devcontainer setup to ensure NPM global packages are accessible. A post-create command now copies a custom .zshrc file into the home directory, which updates the PATH to include the NPM global bin directory. The install script is improved to configure NPM directories and install packages individually.

Changes

File(s) Change Summary
.devcontainer/devcontainer.json Added postCreateCommand to copy .zshrc.devcontainer to home as .zshrc with success/failure messaging.
.devcontainer/features/common/install.sh Sets up NPM cache/global dirs, exports related env vars, installs each NPM package individually with logging.
dot/.zshrc.devcontainer Adds $HOME/.npm-global/bin to PATH if present, ensuring NPM global binaries are accessible in shell.

Sequence Diagram(s)

sequenceDiagram
    participant DevContainer as Devcontainer
    participant UserShell as User Shell
    participant InstallScript as install.sh

    DevContainer->>InstallScript: Run install.sh on build
    InstallScript->>InstallScript: Set up npm cache & global dirs
    InstallScript->>InstallScript: Export npm env vars
    InstallScript->>InstallScript: Install npm packages one by one
    InstallScript->>InstallScript: Output global npm bin path

    DevContainer->>UserShell: postCreateCommand (copy .zshrc.devcontainer to ~/.zshrc)
    UserShell->>UserShell: Source ~/.zshrc (PATH includes .npm-global/bin)
    UserShell->>UserShell: NPM global binaries available in PATH
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure NPM global packages are installed and accessible in devcontainer (#13)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Possibly related PRs

Suggested labels

codex

Poem

A rabbit hops in a devcontainer shell,
Where npm globals now work so well.
With .zshrc set and PATH made right,
Each package shines, a coder’s delight!
“Success!” it cheers, hopping with glee—
Now node tools run, as easy as can be!
🐇✨

✨ 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: 2

🧹 Nitpick comments (5)
.devcontainer/features/common/install.sh (3)

16-19: Explicitly create npm cache and global dirs with correct permissions
Good setup of custom npm directories. To ensure directories have secure default permissions and are created atomically, consider using a single install command instead of mkdir + chown. For example:

- mkdir -p "$NPM_CACHE_DIR" "$NPM_GLOBAL_DIR"
+ install -d -m 0755 "$NPM_CACHE_DIR" "$NPM_GLOBAL_DIR"

This avoids race conditions and enforces proper permissions on creation.


26-28: Make npm settings available immediately
You export NPM_CONFIG_CACHE and NPM_CONFIG_PREFIX, but installed binaries won’t be on $PATH until shell startup. It may be helpful to export the npm bin directory now so tools in this script can run globally installed packages:

 export NPM_CONFIG_PREFIX="$NPM_GLOBAL_DIR"
+export PATH="$NPM_GLOBAL_DIR/bin:$PATH"

30-30: Clarify PATH instructions for users
The echo "NPM global bin directory: $NPM_GLOBAL_DIR/bin" line informs users, but consider adding a brief note suggesting they add this to their PATH if they run scripts manually outside devcontainer context.

dot/.zshrc.devcontainer (1)

70-74: Ensure idempotent PATH modification for npm globals
Great inclusion of $HOME/.npm-global/bin in PATH. For clarity and idempotency, prepend only if not already present:

-if [[ -d "$HOME/.npm-global/bin" ]]; then
-  export PATH="$HOME/.npm-global/bin:$PATH"
-fi
+if [[ -d "$HOME/.npm-global/bin" ]] && [[ ":$PATH:" != *":$HOME/.npm-global/bin:"* ]]; then
+  export PATH="$HOME/.npm-global/bin:$PATH"
+fi
.devcontainer/devcontainer.json (1)

23-23: Use workspace variables instead of hard-coded paths
The postCreateCommand uses an absolute /workspaces/config/... path, which may break if the repository folder name changes. Leverage ${containerWorkspaceFolder} to make it robust:

-"postCreateCommand": "cp /workspaces/config/dot/.zshrc.devcontainer ~/.zshrc && echo 'Devcontainer .zshrc configured' || echo 'Failed to copy .zshrc'",
+"postCreateCommand": "cp \"${containerWorkspaceFolder}/dot/.zshrc.devcontainer\" \"$HOME/.zshrc\" && echo 'Devcontainer .zshrc configured' || echo 'Failed to copy .zshrc'",
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8a2f9e1 and 652a2b8.

📒 Files selected for processing (3)
  • .devcontainer/devcontainer.json (1 hunks)
  • .devcontainer/features/common/install.sh (2 hunks)
  • dot/.zshrc.devcontainer (1 hunks)

Comment on lines +21 to +24
# ディレクトリの権限確認と修正
[ -w "$NPM_CACHE_DIR" ] || chown -R "$(id -u):$(id -g)" "$NPM_CACHE_DIR" || true
[ -w "$NPM_GLOBAL_DIR" ] || chown -R "$(id -u):$(id -g)" "$NPM_GLOBAL_DIR" || true

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

Avoid silent failures when fixing ownership
The chown -R … || true pattern suppresses errors that may indicate deeper permission issues. If ownership cannot be corrected, it’s better to log and exit with failure to alert maintainers. For example:

-[ -w "$NPM_CACHE_DIR" ] || chown -R "$(id -u):$(id -g)" "$NPM_CACHE_DIR" || true
+[ -w "$NPM_CACHE_DIR" ] || { echo "ERROR: Cannot set write permissions on $NPM_CACHE_DIR"; exit 1; }

Repeat similarly for $NPM_GLOBAL_DIR.

📝 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
# ディレクトリの権限確認と修正
[ -w "$NPM_CACHE_DIR" ] || chown -R "$(id -u):$(id -g)" "$NPM_CACHE_DIR" || true
[ -w "$NPM_GLOBAL_DIR" ] || chown -R "$(id -u):$(id -g)" "$NPM_GLOBAL_DIR" || true
# ディレクトリの権限確認と修正
[ -w "$NPM_CACHE_DIR" ] || { echo "ERROR: Cannot set write permissions on $NPM_CACHE_DIR"; exit 1; }
[ -w "$NPM_GLOBAL_DIR" ] || { echo "ERROR: Cannot set write permissions on $NPM_GLOBAL_DIR"; exit 1; }
🤖 Prompt for AI Agents
In .devcontainer/features/common/install.sh around lines 21 to 24, the current
use of 'chown -R ... || true' suppresses errors when changing ownership of
$NPM_CACHE_DIR and $NPM_GLOBAL_DIR, potentially hiding permission issues. Modify
the script to check the result of each chown command, and if it fails, log an
error message indicating the failure and exit the script with a non-zero status
to alert maintainers instead of silently continuing.

Comment on lines +71 to +84
# パッケージを一つずつインストール
while IFS= read -r package; do
if [ -n "$package" ]; then
echo "Installing package: $package"
npm install -g "$package" --prefer-offline --no-audit --no-fund
if [ $? -eq 0 ]; then
echo "✓ Successfully installed: $package"
else
echo "✗ Failed to install: $package"
fi
fi
done <<< "$packages"

# インストール結果の確認

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

Fail-fast on package installation errors and verify prerequisites
While iterating installs provides per-package logs, failures do not abort the script, which may hide critical issues. Consider:

  1. Exiting immediately on any npm install failure.
  2. Checking that jq is installed before parsing global.json.

Example diff:

+command -v jq >/dev/null 2>&1 || { echo "ERROR: jq is required for parsing global.json"; exit 1; }
 while IFS= read -r package; do
-  npm install -g "$package" --prefer-offline --no-audit --no-fund
+  npm install -g "$package" --prefer-offline --no-audit --no-fund || exit 1
🤖 Prompt for AI Agents
In .devcontainer/features/common/install.sh around lines 71 to 84, the script
currently continues installing packages even if an npm install fails, which can
hide critical errors. Modify the script to exit immediately if any npm install
command fails by checking the exit status and calling exit on failure.
Additionally, before parsing global.json with jq, add a check to verify that jq
is installed and exit with an error message if it is missing.

@keito4 keito4 closed this May 30, 2025
@keito4
keito4 deleted the claude/issue-13-20250530_055229 branch March 1, 2026 09:48
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.

NPMグローバルパッケージがdevcontainer上では読み込まれない

1 participant