Skip to content

Commit 9db67b4

Browse files
Apply PR #22753: core: move plugin intialisation to config layer override
2 parents 57442e2 + 4c72bfd commit 9db67b4

5 files changed

Lines changed: 33 additions & 13 deletions

File tree

packages/opencode/src/cli/cmd/serve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import os from "node:os"
44
import { Server } from "../../server/server"
55
import { cmd } from "./cmd"
66
import { withNetworkOptions, resolveNetworkOptions } from "../network"
7+
import { bootstrap } from "../bootstrap"
78
import { Flag } from "../../flag/flag"
89
import { Workspace } from "../../control-plane/workspace"
910
import { Project } from "../../project"
@@ -199,7 +200,7 @@ export const ServeCommand = cmd({
199200
}),
200201
describe: "starts a headless opencode server",
201202
handler: async (args) => {
202-
const opts = await resolveNetworkOptions(args)
203+
const opts = await bootstrap(process.cwd(), () => resolveNetworkOptions(args))
203204
const relayURL = (
204205
args["relay-url"] ??
205206
process.env.OPENCODE_EXPERIMENTAL_PUSH_RELAY_URL ??
@@ -254,7 +255,6 @@ export const ServeCommand = cmd({
254255
if (!Flag.OPENCODE_SERVER_PASSWORD) {
255256
console.log("Warning: OPENCODE_SERVER_PASSWORD is not set; server is unsecured.")
256257
}
257-
258258
const server = await Server.listen(opts)
259259
console.log(`opencode server listening on http://${server.hostname}:${server.port}`)
260260

packages/opencode/src/cli/cmd/tui/thread.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import { UI } from "@/cli/ui"
88
import { Log } from "@/util"
99
import { errorMessage } from "@/util/error"
1010
import { withTimeout } from "@/util/timeout"
11-
import { withNetworkOptions, resolveNetworkOptionsNoConfig } from "@/cli/network"
11+
import { withNetworkOptions, resolveNetworkOptions } from "@/cli/network"
1212
import { Filesystem } from "@/util"
1313
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
1414
import type { EventSource } from "./context/sdk"
1515
import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32"
1616
import { writeHeapSnapshot } from "v8"
1717
import { TuiConfig } from "./config/tui"
18+
import { Instance } from "@/project/instance"
1819

1920
declare global {
2021
const OPENCODE_WORKER_PATH: string
@@ -176,9 +177,13 @@ export const TuiThreadCommand = cmd({
176177
}
177178

178179
const prompt = await input(args.prompt)
179-
const config = await TuiConfig.get()
180-
181-
const network = resolveNetworkOptionsNoConfig(args)
180+
const { config, network } = await Instance.provide({
181+
directory: cwd,
182+
fn: async () => ({
183+
config: await TuiConfig.get(),
184+
network: await resolveNetworkOptions(args),
185+
}),
186+
})
182187
const external =
183188
process.argv.includes("--port") ||
184189
process.argv.includes("--hostname") ||

packages/opencode/src/cli/cmd/web.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { withNetworkOptions, resolveNetworkOptions } from "../network"
55
import { Flag } from "../../flag/flag"
66
import open from "open"
77
import { networkInterfaces } from "os"
8+
import { bootstrap } from "../bootstrap"
89

910
function getNetworkIPs() {
1011
const nets = networkInterfaces()
@@ -36,7 +37,7 @@ export const WebCommand = cmd({
3637
if (!Flag.OPENCODE_SERVER_PASSWORD) {
3738
UI.println(UI.Style.TEXT_WARNING_BOLD + "! OPENCODE_SERVER_PASSWORD is not set; server is unsecured.")
3839
}
39-
const opts = await resolveNetworkOptions(args)
40+
const opts = await bootstrap(process.cwd(), () => resolveNetworkOptions(args))
4041
const server = await Server.listen(opts)
4142
UI.empty()
4243
UI.println(UI.logo(" "))

packages/opencode/src/effect/app-runtime.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,32 @@ import { Installation } from "@/installation"
4848
import { ShareNext } from "@/share"
4949
import { SessionShare } from "@/share"
5050
import { Npm } from "@opencode-ai/shared/npm"
51+
import * as Effect from "effect/Effect"
52+
53+
// Adjusts the default Config layer to ensure that plugins are always initialised before
54+
// any other layers read the current config
55+
const ConfigWithPluginPriority = Layer.effect(
56+
Config.Service,
57+
Effect.gen(function* () {
58+
const config = yield* Config.Service
59+
const plugin = yield* Plugin.Service
60+
61+
return {
62+
...config,
63+
get: () => Effect.andThen(plugin.init(), config.get),
64+
getGlobal: () => Effect.andThen(plugin.init(), config.getGlobal),
65+
getConsoleState: () => Effect.andThen(plugin.init(), config.getConsoleState),
66+
}
67+
}),
68+
).pipe(Layer.provide(Layer.merge(Plugin.defaultLayer, Config.defaultLayer)))
5169

5270
export const AppLayer = Layer.mergeAll(
5371
Npm.defaultLayer,
5472
AppFileSystem.defaultLayer,
5573
Bus.defaultLayer,
5674
Auth.defaultLayer,
5775
Account.defaultLayer,
58-
Config.defaultLayer,
76+
ConfigWithPluginPriority,
5977
Git.defaultLayer,
6078
Ripgrep.defaultLayer,
6179
FileTime.defaultLayer,

packages/opencode/src/project/bootstrap.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@ import { Log } from "@/util"
1212
import { FileWatcher } from "@/file/watcher"
1313
import { ShareNext } from "@/share"
1414
import * as Effect from "effect/Effect"
15-
import { Config } from "@/config"
1615

1716
export const InstanceBootstrap = Effect.gen(function* () {
1817
Log.Default.info("bootstrapping", { directory: Instance.directory })
19-
// everything depends on config so eager load it for nice traces
20-
yield* Config.Service.use((svc) => svc.get())
21-
// Plugin can mutate config so it has to be initialized before anything else.
22-
yield* Plugin.Service.use((svc) => svc.init())
2318
yield* Effect.all(
2419
[
20+
Plugin.Service,
2521
LSP.Service,
2622
ShareNext.Service,
2723
Format.Service,

0 commit comments

Comments
 (0)