Skip to content

fix: detect package manager for collection install suggestion#476

Open
mvtandas wants to merge 2 commits intonuxt:mainfrom
mvtandas:fix/detect-package-manager
Open

fix: detect package manager for collection install suggestion#476
mvtandas wants to merge 2 commits intonuxt:mainfrom
mvtandas:fix/detect-package-manager

Conversation

@mvtandas
Copy link
Copy Markdown

Summary

Fixes #473

The dev warning for missing icon collections always suggests npm i -D @iconify-json/<collection>, regardless of the project's actual package manager.

Fix

Detects the package manager at build time by checking lockfiles in the project root, then passes the install command to the server runtime via runtimeConfig.

Lockfile Suggested command
pnpm-lock.yaml pnpm add -D
yarn.lock yarn add -D
bun.lockb / bun.lock bun add -D
fallback npm i -D

Before

[Icon] Collection `mdi` is not found locally
We suggest to install it via `npm i -D @iconify-json/mdi`

After (in a pnpm project)

[Icon] Collection `mdi` is not found locally
We suggest to install it via `pnpm add -D @iconify-json/mdi`

The missing icon collection warning always suggested `npm i -D`,
regardless of the project's actual package manager. This detects
the package manager by checking lockfiles at build time and passes
the correct install command to the runtime via runtimeConfig.

Now suggests:
- `pnpm add -D` when pnpm-lock.yaml exists
- `yarn add -D` when yarn.lock exists
- `bun add -D` when bun.lockb or bun.lock exists
- `npm i -D` as fallback

Fixes nuxt#473
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 14, 2026

📝 Walkthrough

Walkthrough

The PR adds package-manager detection to the Nuxt Icon module. A new helper detectInstallCommand(rootDir) checks for common lockfiles (pnpm-lock.yaml, yarn.lock, bun.lockb, package-lock.json) and returns a matching dev-install command (pnpm add -D, yarn add -D, bun add -D, or npm i -D). The module assigns that command to nuxt.options.runtimeConfig.public.nuxtIconInstallCommand. The server API now reads useRuntimeConfig().public?.nuxtIconInstallCommand (falling back to npm i -D) when warning about missing icon collections.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: detecting the package manager to suggest the correct install command for collections.
Description check ✅ Passed The description is well-detailed and directly related to the changeset, explaining the problem, solution, and providing a clear before/after example.
Linked Issues check ✅ Passed The changes fully address issue #473 by detecting the package manager via lockfiles and passing the appropriate install command to the runtime, enabling context-aware suggestions.
Out of Scope Changes check ✅ Passed All changes are directly scoped to detecting and passing the package manager install command, with no extraneous modifications outside the stated objectives.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/module.ts`:
- Around line 234-245: The detectInstallCommand function only checks lockfiles
in the provided rootDir and misses parent workspace/monorepo layouts; update
detectInstallCommand to walk up parent directories from rootDir to filesystem
root (or stop at a workspace boundary) and check for known indicators in each
directory—pnpm-lock.yaml or pnpm-workspace.yaml => 'pnpm add -D', yarn.lock or
yarn workspaces indicators or package.json with "workspaces" => 'yarn add -D',
bun.lockb or bun.lock => 'bun add -D', and fallback to package.json's
"packageManager" field if present before finally returning 'npm i -D'; reference
detectInstallCommand and the helper pathResolve/existsSync utilities when adding
the upward directory traversal and checks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 55c2d7ef-22ae-498f-ade8-80b10be71616

📥 Commits

Reviewing files that changed from the base of the PR and between b45023d and 9170c28.

📒 Files selected for processing (2)
  • src/module.ts
  • src/runtime/server/api.ts

Comment thread src/module.ts
Comment on lines +234 to +245
function detectInstallCommand(rootDir: string): string {
if (existsSync(pathResolve(rootDir, 'pnpm-lock.yaml'))) {
return 'pnpm add -D'
}
if (existsSync(pathResolve(rootDir, 'yarn.lock'))) {
return 'yarn add -D'
}
if (existsSync(pathResolve(rootDir, 'bun.lockb')) || existsSync(pathResolve(rootDir, 'bun.lock'))) {
return 'bun add -D'
}
return 'npm i -D'
}
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 | 🟠 Major

Package manager detection misses common monorepo layouts.

This only checks one directory. If the lockfile lives in a parent workspace directory, the warning still falls back to npm i -D, so detection is incorrect for those projects.

Suggested fix
 function detectInstallCommand(rootDir: string): string {
-  if (existsSync(pathResolve(rootDir, 'pnpm-lock.yaml'))) {
-    return 'pnpm add -D'
-  }
-  if (existsSync(pathResolve(rootDir, 'yarn.lock'))) {
-    return 'yarn add -D'
-  }
-  if (existsSync(pathResolve(rootDir, 'bun.lockb')) || existsSync(pathResolve(rootDir, 'bun.lock'))) {
-    return 'bun add -D'
-  }
+  const lockfileCommandPairs: Array<[string, string]> = [
+    ['pnpm-lock.yaml', 'pnpm add -D'],
+    ['yarn.lock', 'yarn add -D'],
+    ['bun.lockb', 'bun add -D'],
+    ['bun.lock', 'bun add -D'],
+  ]
+
+  let currentDir = rootDir
+  while (true) {
+    for (const [lockfile, command] of lockfileCommandPairs) {
+      if (existsSync(pathResolve(currentDir, lockfile))) {
+        return command
+      }
+    }
+
+    const parentDir = pathResolve(currentDir, '..')
+    if (parentDir === currentDir) {
+      break
+    }
+    currentDir = parentDir
+  }
+
   return 'npm i -D'
 }
📝 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
function detectInstallCommand(rootDir: string): string {
if (existsSync(pathResolve(rootDir, 'pnpm-lock.yaml'))) {
return 'pnpm add -D'
}
if (existsSync(pathResolve(rootDir, 'yarn.lock'))) {
return 'yarn add -D'
}
if (existsSync(pathResolve(rootDir, 'bun.lockb')) || existsSync(pathResolve(rootDir, 'bun.lock'))) {
return 'bun add -D'
}
return 'npm i -D'
}
function detectInstallCommand(rootDir: string): string {
const lockfileCommandPairs: Array<[string, string]> = [
['pnpm-lock.yaml', 'pnpm add -D'],
['yarn.lock', 'yarn add -D'],
['bun.lockb', 'bun add -D'],
['bun.lock', 'bun add -D'],
]
let currentDir = rootDir
while (true) {
for (const [lockfile, command] of lockfileCommandPairs) {
if (existsSync(pathResolve(currentDir, lockfile))) {
return command
}
}
const parentDir = pathResolve(currentDir, '..')
if (parentDir === currentDir) {
break
}
currentDir = parentDir
}
return 'npm i -D'
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/module.ts` around lines 234 - 245, The detectInstallCommand function only
checks lockfiles in the provided rootDir and misses parent workspace/monorepo
layouts; update detectInstallCommand to walk up parent directories from rootDir
to filesystem root (or stop at a workspace boundary) and check for known
indicators in each directory—pnpm-lock.yaml or pnpm-workspace.yaml => 'pnpm add
-D', yarn.lock or yarn workspaces indicators or package.json with "workspaces"
=> 'yarn add -D', bun.lockb or bun.lock => 'bun add -D', and fallback to
package.json's "packageManager" field if present before finally returning 'npm i
-D'; reference detectInstallCommand and the helper pathResolve/existsSync
utilities when adding the upward directory traversal and checks.

Copy link
Copy Markdown

@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.

♻️ Duplicate comments (1)
src/module.ts (1)

241-252: ⚠️ Potential issue | 🟠 Major

Workspace lockfiles in parent dirs are still missed.

On Line 241, detection is limited to rootDir, so monorepo/workspace apps with lockfiles in parent directories will still get wrong suggestions (npm i -D fallback).

Suggested adjustment
 function detectInstallCommand(rootDir: string): string {
-  if (existsSync(pathResolve(rootDir, 'pnpm-lock.yaml'))) {
-    return 'pnpm add -D'
-  }
-  if (existsSync(pathResolve(rootDir, 'yarn.lock'))) {
-    return 'yarn add -D'
-  }
-  if (existsSync(pathResolve(rootDir, 'bun.lockb')) || existsSync(pathResolve(rootDir, 'bun.lock'))) {
-    return 'bun add -D'
-  }
-  return 'npm i -D'
+  const candidates: Array<[string, string]> = [
+    ['pnpm-lock.yaml', 'pnpm add -D'],
+    ['yarn.lock', 'yarn add -D'],
+    ['bun.lockb', 'bun add -D'],
+    ['bun.lock', 'bun add -D'],
+  ]
+
+  let currentDir = rootDir
+  while (true) {
+    for (const [lockfile, command] of candidates) {
+      if (existsSync(pathResolve(currentDir, lockfile))) {
+        return command
+      }
+    }
+
+    const parentDir = pathResolve(currentDir, '..')
+    if (parentDir === currentDir) {
+      break
+    }
+    currentDir = parentDir
+  }
+
+  return 'npm i -D'
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/module.ts` around lines 241 - 252, detectInstallCommand currently checks
only the provided rootDir; update it to walk up parent directories to the
filesystem root and check each directory for lockfiles (pnpm-lock.yaml,
yarn.lock, bun.lockb or bun.lock) using the existing existsSync and pathResolve
helpers. Return 'pnpm add -D', 'yarn add -D', or 'bun add -D' as soon as a
matching lockfile is found in any ancestor directory (use path.dirname to
iterate parents and stop when the directory no longer changes), otherwise fall
back to 'npm i -D'. Keep function name detectInstallCommand and the same
lockfile checks but perform them for each ancestor directory.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@src/module.ts`:
- Around line 241-252: detectInstallCommand currently checks only the provided
rootDir; update it to walk up parent directories to the filesystem root and
check each directory for lockfiles (pnpm-lock.yaml, yarn.lock, bun.lockb or
bun.lock) using the existing existsSync and pathResolve helpers. Return 'pnpm
add -D', 'yarn add -D', or 'bun add -D' as soon as a matching lockfile is found
in any ancestor directory (use path.dirname to iterate parents and stop when the
directory no longer changes), otherwise fall back to 'npm i -D'. Keep function
name detectInstallCommand and the same lockfile checks but perform them for each
ancestor directory.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7c648b2a-6e9e-4625-88e5-df2db891d47e

📥 Commits

Reviewing files that changed from the base of the PR and between 9170c28 and 66f70e2.

📒 Files selected for processing (1)
  • src/module.ts

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.

detect package manager for missing collection install suggestion

1 participant