Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/language-server/src/workspace.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { cwd } from 'node:process'
import { describe, expect, it } from 'vitest'
import { URI } from 'vscode-uri'
import { detectPackageManagerFromProject } from './workspace'

describe('detectPackageManagerFromProject', () => {
it('detects the real repository as pnpm', async () => {
await expect(detectPackageManagerFromProject(URI.file(cwd()).path)).resolves.toBe('pnpm')
})
})
13 changes: 1 addition & 12 deletions packages/language-server/src/workspace.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import { detect } from 'package-manager-detector/detect'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { detectPackageManagerFromProject } from './workspace'

vi.mock('package-manager-detector/detect', () => ({
detect: vi.fn(),
}))

const { detect } = await import('package-manager-detector/detect')

describe('detectPackageManagerFromProject', () => {
afterEach(() => {
vi.mocked(detect).mockReset()
})

Comment on lines +1 to 13

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- workspace.ts ---'
sed -n '1,120p' packages/language-server/src/workspace.ts

printf '%s\n' '--- workspace.test.ts ---'
sed -n '1,220p' packages/language-server/src/workspace.test.ts

printf '%s\n' '--- relevant diff ---'
git diff -- packages/language-server/src/workspace.ts packages/language-server/src/workspace.test.ts

Repository: npmx-dev/vscode-npmx

Length of output: 4942


🏁 Script executed:

#!/bin/bash
set -eu

python3 - <<'PY'
from pathlib import Path
import re

source = Path("packages/language-server/src/workspace.ts").read_text()
test = Path("packages/language-server/src/workspace.test.ts").read_text()

function = re.search(
    r"export async function detectPackageManagerFromProject\(rootPath: string\).*?"
    r"\n\}\n",
    source,
    re.S,
)
if not function:
    raise SystemExit("detectPackageManagerFromProject was not found")

body = function.group(0)
required = [
    "const fsPath = URI.file(rootPath).fsPath",
    "cwd: fsPath",
    "stopDir: fsPath",
    "case 'bun':",
    "case 'npm':",
    "case 'pnpm':",
    "case 'yarn':",
    "return 'npm'",
]
missing = [item for item in required if item not in body]
if missing:
    raise SystemExit(f"Missing expected function behaviour: {missing}")

supported_cases = re.findall(r"mockResolvedValue\(\{\s*name:\s*'([^']+)'", test)
print("supported detector names handled by source:", ["bun", "npm", "pnpm", "yarn"])
print("detector names mocked by current tests:", supported_cases)
print("native-path call contract in source: cwd and stopDir both receive fsPath")
print("supported-manager test present:", any(name in {"bun", "npm", "pnpm", "yarn"} for name in supported_cases))
PY

printf '%s\n' '--- package declarations for URI and detector ---'
rg -n '"(vscode-uri|package-manager-detector)"' package.json packages pnpm-lock.yaml yarn.lock package-lock.json 2>/dev/null || true

Repository: npmx-dev/vscode-npmx

Length of output: 830


Add a supported-manager test. Mock detect() to return pnpm, assert that the function resolves to pnpm, and assert that detect() receives URI.file('/repo').fsPath as both cwd and stopDir.

it('returns supported package managers directly', async () => {
vi.mocked(detect).mockResolvedValue({ name: 'pnpm', agent: 'pnpm' })

await expect(detectPackageManagerFromProject('/repo')).resolves.toBe('pnpm')
expect(detect).toHaveBeenCalledWith({
cwd: '/repo',
stopDir: '/repo',
})
})

it('falls back to npm for unsupported detectors', async () => {
vi.mocked(detect).mockResolvedValue({ name: 'deno', agent: 'deno' })

Expand Down
7 changes: 5 additions & 2 deletions packages/language-server/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ import { URI } from 'vscode-uri'
* @internal
*/
export async function detectPackageManagerFromProject(rootPath: string): Promise<PackageManager> {
// `rootPath` is a URI path (posix-style) coming from `WorkspaceContext`,
// but `detect()` reads the filesystem and requires a platform-native path.
const fsPath = URI.file(rootPath).fsPath
const result = await detect({
cwd: rootPath,
stopDir: rootPath,
cwd: fsPath,
stopDir: fsPath,
})

switch (result?.name) {
Expand Down
Loading