Skip to content
Closed
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
4 changes: 2 additions & 2 deletions packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as Log from "@opencode-ai/core/util/log"
import { serviceUse } from "@opencode-ai/core/effect/service-use"
import path from "path"
import { pathToFileURL } from "url"
import os from "os"
import { mergeDeep } from "remeda"
import { Global } from "@opencode-ai/core/global"
import fsNode from "fs/promises"
Expand Down Expand Up @@ -41,6 +40,7 @@ import { ConfigReference } from "./reference"
import { ConfigServer } from "./server"
import { ConfigSkills } from "./skills"
import { ConfigVariable } from "./variable"
import { ConfigUsername } from "./username"
import { Npm } from "@opencode-ai/core/npm"
import { withTransientReadRetry } from "@/util/effect-http-client"

Expand Down Expand Up @@ -762,7 +762,7 @@ export const layer = Layer.effect(
result.permission = mergeDeep(perms, result.permission ?? {})
}

if (!result.username) result.username = os.userInfo().username
if (!result.username) result.username = ConfigUsername.get()

if (result.autoshare === true && !result.share) {
result.share = "auto"
Expand Down
4 changes: 2 additions & 2 deletions packages/opencode/src/config/managed.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export * as ConfigManaged from "./managed"

import { existsSync } from "fs"
import os from "os"
import path from "path"
import * as Log from "@opencode-ai/core/util/log"
import { Process } from "@/util/process"
import { ConfigUsername } from "./username"

const log = Log.create({ service: "config" })

Expand Down Expand Up @@ -46,7 +46,7 @@ export function parseManagedPlist(json: string): string {
export async function readManagedPreferences() {
if (process.platform !== "darwin") return

const user = os.userInfo().username
const user = ConfigUsername.get()
const paths = [
path.join("/Library/Managed Preferences", user, `${MANAGED_PLIST_DOMAIN}.plist`),
path.join("/Library/Managed Preferences", `${MANAGED_PLIST_DOMAIN}.plist`),
Expand Down
15 changes: 15 additions & 0 deletions packages/opencode/src/config/username.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export * as ConfigUsername from "./username"

import os from "os"
import * as Log from "@opencode-ai/core/util/log"

const log = Log.create({ service: "config" })

export function get() {
try {
return os.userInfo().username || "user"
} catch (err) {
log.warn("failed to read system username, using fallback", { err })
return "user"
}
}
17 changes: 16 additions & 1 deletion packages/opencode/test/config/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect, describe, afterEach, beforeEach } from "bun:test"
import { test, expect, describe, afterEach, beforeEach, spyOn } from "bun:test"
import { Effect, Exit, Layer, Option } from "effect"
import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/unstable/http"
import { NodeFileSystem, NodePath } from "@effect/platform-node"
Expand Down Expand Up @@ -28,6 +28,7 @@ import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { testEffect } from "../lib/effect"
import path from "path"
import fs from "fs/promises"
import os from "os"
import { pathToFileURL } from "url"
import { Global } from "@opencode-ai/core/global"
import { ProjectID } from "../../src/project/schema"
Expand Down Expand Up @@ -291,6 +292,20 @@ it.instance("loads config with defaults when no files exist", () =>
}),
)

it.instance("falls back to a generic username when system user info is unavailable", () =>
Effect.gen(function* () {
const userInfo = spyOn(os, "userInfo").mockImplementation(() => {
throw Object.assign(new Error("missing passwd entry"), { code: "ENOENT" })
})
try {
const config = yield* Config.use.get()
expect(config.username).toBe("user")
} finally {
userInfo.mockRestore()
}
}),
)

it.effect("creates global jsonc config with schema when no global configs exist", () =>
withGlobalConfig({}, ({ dir }) =>
Effect.gen(function* () {
Expand Down
Loading