fix: handle PowerShell ENOENT error in os-name on Windows (#9897)

Co-authored-by: Roo Code <roomote@roocode.com>
This commit is contained in:
roomote[bot] 2026-01-07 13:07:25 -05:00 • committed by GitHub
parent 43f7ce025f
commit d7d3f4099b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 78 additions and 1 deletions

View file

@ -0,0 +1,66 @@
import os from "os"
// Mock the modules - must be hoisted before imports
vi.mock("os-name", () => ({
default: vi.fn(),
}))
vi.mock("../../../../utils/shell", () => ({
getShell: vi.fn(() => "/bin/bash"),
}))
import { getSystemInfoSection } from "../system-info"
import osName from "os-name"
const mockOsName = osName as unknown as ReturnType<typeof vi.fn>
describe("getSystemInfoSection", () => {
const mockCwd = "/test/workspace"
const mockHomeDir = "/home/user"
beforeEach(() => {
vi.spyOn(os, "homedir").mockReturnValue(mockHomeDir)
vi.spyOn(os, "platform").mockReturnValue("linux" as any)
vi.spyOn(os, "release").mockReturnValue("5.15.0")
})
afterEach(() => {
vi.clearAllMocks()
})
it("should return system info with os-name when available", () => {
mockOsName.mockReturnValue("Ubuntu 22.04")
const result = getSystemInfoSection(mockCwd)
expect(result).toContain("Operating System: Ubuntu 22.04")
expect(result).toContain("Default Shell: /bin/bash")
expect(result).toContain(`Home Directory: ${mockHomeDir}`)
expect(result).toContain(`Current Workspace Directory: ${mockCwd}`)
})
it("should fallback to platform and release when os-name throws error", () => {
mockOsName.mockImplementation(() => {
throw new Error("Command failed with ENOENT: powershell")
})
const result = getSystemInfoSection(mockCwd)
expect(result).toContain("Operating System: linux 5.15.0")
expect(result).toContain("Default Shell: /bin/bash")
expect(result).toContain(`Home Directory: ${mockHomeDir}`)
expect(result).toContain(`Current Workspace Directory: ${mockCwd}`)
})
it("should handle Windows platform in fallback", () => {
mockOsName.mockImplementation(() => {
throw new Error("Command failed with ENOENT: powershell")
})
vi.spyOn(os, "platform").mockReturnValue("win32" as any)
vi.spyOn(os, "release").mockReturnValue("10.0.19043")
const result = getSystemInfoSection(mockCwd)
expect(result).toContain("Operating System: win32 10.0.19043")
})
})

View file

@ -4,11 +4,22 @@ import osName from "os-name"
import { getShell } from "../../../utils/shell"
export function getSystemInfoSection(cwd: string): string {
// Try to get detailed OS name, fall back to basic info if it fails
let osInfo: string
try {
osInfo = osName()
} catch (error) {
// Fallback when os-name fails (e.g., PowerShell not available on Windows)
const platform = os.platform()
const release = os.release()
osInfo = `${platform} ${release}`
}
let details = `====
SYSTEM INFORMATION
Operating System: ${osName()}
Operating System: ${osInfo}
Default Shell: ${getShell()}
Home Directory: ${os.homedir().toPosix()}
Current Workspace Directory: ${cwd.toPosix()}