fix(config): fallback when user info unavailable (#29332)
This commit is contained in:
@@ -762,7 +762,14 @@ export const layer = Layer.effect(
|
|||||||
result.permission = mergeDeep(perms, result.permission ?? {})
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (result.autoshare === true && !result.share) {
|
if (result.autoshare === true && !result.share) {
|
||||||
result.share = "auto"
|
result.share = "auto"
|
||||||
|
|||||||
@@ -46,7 +46,14 @@ export function parseManagedPlist(json: string): string {
|
|||||||
export async function readManagedPreferences() {
|
export async function readManagedPreferences() {
|
||||||
if (process.platform !== "darwin") return
|
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"
|
||||||
|
}
|
||||||
|
})()
|
||||||
const paths = [
|
const paths = [
|
||||||
path.join("/Library/Managed Preferences", user, `${MANAGED_PLIST_DOMAIN}.plist`),
|
path.join("/Library/Managed Preferences", user, `${MANAGED_PLIST_DOMAIN}.plist`),
|
||||||
path.join("/Library/Managed Preferences", `${MANAGED_PLIST_DOMAIN}.plist`),
|
path.join("/Library/Managed Preferences", `${MANAGED_PLIST_DOMAIN}.plist`),
|
||||||
|
|||||||
@@ -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 { Effect, Exit, Layer, Option } from "effect"
|
||||||
import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/unstable/http"
|
import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/unstable/http"
|
||||||
import { NodeFileSystem, NodePath } from "@effect/platform-node"
|
import { NodeFileSystem, NodePath } from "@effect/platform-node"
|
||||||
@@ -28,6 +28,7 @@ import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
|||||||
import { testEffect } from "../lib/effect"
|
import { testEffect } from "../lib/effect"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import fs from "fs/promises"
|
import fs from "fs/promises"
|
||||||
|
import os from "os"
|
||||||
import { pathToFileURL } from "url"
|
import { pathToFileURL } from "url"
|
||||||
import { Global } from "@opencode-ai/core/global"
|
import { Global } from "@opencode-ai/core/global"
|
||||||
import { ProjectID } from "../../src/project/schema"
|
import { ProjectID } from "../../src/project/schema"
|
||||||
@@ -291,6 +292,20 @@ it.instance("loads config with defaults when no files exist", () =>
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
it.instance("falls back to 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", () =>
|
it.effect("creates global jsonc config with schema when no global configs exist", () =>
|
||||||
withGlobalConfig({}, ({ dir }) =>
|
withGlobalConfig({}, ({ dir }) =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
|
|||||||
Reference in New Issue
Block a user