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
9 changes: 8 additions & 1 deletion packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,14 @@ const rawLayer = Layer.effect(
result.permission = mergeDeep(perms, result.permission ?? {})
}

if (!result.username) result.username = os.userInfo().username
if (!result.username) {
try {
result.username = os.userInfo().username || "user"
} catch (err) {
log.warn("failed to read system username, using fallback", { err })
result.username = "user"
}
}
Comment thread
Astro-Han marked this conversation as resolved.

if (result.autoshare === true && !result.share) {
result.share = "auto"
Expand Down
9 changes: 8 additions & 1 deletion packages/opencode/src/config/managed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ export function parseManagedPlist(json: string): string {
export async function readManagedPreferences() {
if (process.platform !== "darwin") return

const user = os.userInfo().username
const user = (() => {
try {
return os.userInfo().username || "user"
} catch (err) {
log.warn("failed to read system username, using fallback", { err })
return "user"
}
})()
Comment thread
Astro-Han marked this conversation as resolved.
const domain = managedPlistDomain()
const paths = [
path.join("/Library/Managed Preferences", user, `${domain}.plist`),
Expand Down
41 changes: 41 additions & 0 deletions packages/opencode/test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const infra = CrossSpawnSpawner.defaultLayer.pipe(
)
import path from "path"
import fs from "fs/promises"
import os from "os"
import { pathToFileURL } from "url"
import { Global } from "../../src/global"
import { ProjectID } from "../../src/project/schema"
Expand Down Expand Up @@ -3199,3 +3200,43 @@ describe("OPENCODE_PERMISSION env var", () => {
}
})
})

// Regression for #29332: os.userInfo() can throw on minimal hosts with no
// passwd entry (e.g. some containers), which used to crash config load.
// The loader should fall back to a generic "user" instead of throwing.
describe("system username fallback", () => {
test("falls back to 'user' when os.userInfo() throws", async () => {
const userInfo = spyOn(os, "userInfo").mockImplementation(() => {
throw Object.assign(new Error("missing passwd entry"), { code: "ENOENT" })
})
try {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const config = await load()
expect(config.username).toBe("user")
},
})
} finally {
userInfo.mockRestore()
}
})

test("readManagedPreferences does not throw when os.userInfo() throws on darwin", async () => {
const originalPlatform = process.platform
const userInfo = spyOn(os, "userInfo").mockImplementation(() => {
throw Object.assign(new Error("missing passwd entry"), { code: "ENOENT" })
})
Object.defineProperty(process, "platform", { value: "darwin" })
try {
// The macOS managed-preferences paths do not exist on CI hosts, so the
// lookup returns undefined; the regression is that the username fallback
// must not let os.userInfo() throw out of readManagedPreferences().
await expect(ConfigManaged.readManagedPreferences()).resolves.toBeUndefined()
} finally {
Object.defineProperty(process, "platform", { value: originalPlatform })
userInfo.mockRestore()
}
})
})
Loading