fix: npm グローバルパッケージのdevcontainer対応を改善 - #26
Conversation
- 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>
WalkthroughThe changes update the devcontainer setup to ensure NPM global packages are accessible. A post-create command now copies a custom Changes
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
Assessment against linked issues
Assessment against linked issues: Out-of-scope changesNo out-of-scope changes found. Possibly related PRs
Suggested labels
Poem
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
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 singleinstallcommand instead ofmkdir + 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 exportNPM_CONFIG_CACHEandNPM_CONFIG_PREFIX, but installed binaries won’t be on$PATHuntil 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
Theecho "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/bininPATH. 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
ThepostCreateCommanduses 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
📒 Files selected for processing (3)
.devcontainer/devcontainer.json(1 hunks).devcontainer/features/common/install.sh(2 hunks)dot/.zshrc.devcontainer(1 hunks)
| # ディレクトリの権限確認と修正 | ||
| [ -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 | ||
|
|
There was a problem hiding this comment.
🛠️ 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.
| # ディレクトリの権限確認と修正 | |
| [ -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.
| # パッケージを一つずつインストール | ||
| 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" | ||
|
|
||
| # インストール結果の確認 |
There was a problem hiding this comment.
🛠️ 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:
- Exiting immediately on any
npm installfailure. - Checking that
jqis installed before parsingglobal.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.
NPMグローバルパッケージがdevcontainer上で読み込まれない問題を修正しました。
主な変更
Closes #13
Generated with Claude Code
Summary by CodeRabbit
.zshrcwith support for NPM global binaries in the PATH.