Skip to content

Commit 58ab7fa

Browse files
Apply PR #22753: core: move plugin intialisation to config layer override
2 parents 444adf2 + 9b85d2c commit 58ab7fa

5 files changed

Lines changed: 43 additions & 17 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Server } from "../../server/server"
22
import { cmd } from "./cmd"
33
import { withNetworkOptions, resolveNetworkOptions } from "../network"
4+
import { bootstrap } from "../bootstrap"
45
import { Flag } from "@opencode-ai/core/flag/flag"
56

67
export const ServeCommand = cmd({
@@ -11,7 +12,8 @@ export const ServeCommand = cmd({
1112
if (!Flag.OPENCODE_SERVER_PASSWORD) {
1213
console.log("Warning: OPENCODE_SERVER_PASSWORD is not set; server is unsecured.")
1314
}
14-
const opts = await resolveNetworkOptions(args)
15+
16+
const opts = await bootstrap(process.cwd(), () => resolveNetworkOptions(args))
1517
const server = await Server.listen(opts)
1618
console.log(`opencode server listening on http://${server.hostname}:${server.port}`)
1719

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { UI } from "@/cli/ui"
88
import * as Log from "@opencode-ai/core/util/log"
99
import { errorMessage } from "@/util/error"
1010
import { withTimeout } from "@/util/timeout"
11+
import { Instance } from "@/project/instance"
1112
import { withNetworkOptions, resolveNetworkOptionsNoConfig } from "@/cli/network"
1213
import { Filesystem } from "@/util/filesystem"
1314
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
@@ -190,7 +191,11 @@ export const TuiThreadCommand = cmd({
190191
const prompt = await input(args.prompt)
191192
const config = await TuiConfig.get()
192193

193-
const network = resolveNetworkOptionsNoConfig(args)
194+
const network = await Instance.provide({
195+
directory: cwd,
196+
fn: () => resolveNetworkOptionsNoConfig(args),
197+
})
198+
194199
const external =
195200
process.argv.includes("--port") ||
196201
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 "@opencode-ai/core/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
@@ -45,19 +45,37 @@ import { Workspace } from "@/control-plane/workspace"
4545
import { Worktree } from "@/worktree"
4646
import { Pty } from "@/pty"
4747
import { Installation } from "@/installation"
48+
import * as Effect from "effect/Effect"
4849
import { ShareNext } from "@/share/share-next"
4950
import { SessionShare } from "@/share/session"
5051
import { SyncEvent } from "@/sync"
5152
import { Npm } from "@opencode-ai/core/npm"
5253
import { memoMap } from "@opencode-ai/core/effect/memo-map"
5354

55+
// Adjusts the default Config layer to ensure that plugins are always initialised before
56+
// any other layers read the current config
57+
const ConfigWithPluginPriority = Layer.effect(
58+
Config.Service,
59+
Effect.gen(function* () {
60+
const config = yield* Config.Service
61+
const plugin = yield* Plugin.Service
62+
63+
return {
64+
...config,
65+
get: () => Effect.andThen(plugin.init(), config.get),
66+
getGlobal: () => Effect.andThen(plugin.init(), config.getGlobal),
67+
getConsoleState: () => Effect.andThen(plugin.init(), config.getConsoleState),
68+
}
69+
}),
70+
).pipe(Layer.provide(Layer.merge(Plugin.defaultLayer, Config.defaultLayer)))
71+
5472
export const AppLayer = Layer.mergeAll(
5573
Npm.defaultLayer,
5674
AppFileSystem.defaultLayer,
5775
Bus.defaultLayer,
5876
Auth.defaultLayer,
5977
Account.defaultLayer,
60-
Config.defaultLayer,
78+
ConfigWithPluginPriority,
6179
Git.defaultLayer,
6280
Ripgrep.defaultLayer,
6381
File.defaultLayer,

packages/opencode/src/project/bootstrap.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Plugin } from "../plugin"
21
import { Format } from "../format"
32
import { LSP } from "@/lsp/lsp"
43
import { File } from "../file"
@@ -7,6 +6,7 @@ import * as Project from "./project"
76
import * as Vcs from "./vcs"
87
import { Bus } from "../bus"
98
import { Command } from "../command"
9+
import { Plugin } from "../plugin"
1010
import { InstanceState } from "@/effect/instance-state"
1111
import * as Log from "@opencode-ai/core/util/log"
1212
import { FileWatcher } from "@/file/watcher"
@@ -17,20 +17,20 @@ import { Config } from "@/config/config"
1717
export const InstanceBootstrap = Effect.gen(function* () {
1818
const ctx = yield* InstanceState.context
1919
Log.Default.info("bootstrapping", { directory: ctx.directory })
20-
// everything depends on config so eager load it for nice traces
21-
yield* Config.Service.use((svc) => svc.get())
22-
// Plugin can mutate config so it has to be initialized before anything else.
23-
yield* Plugin.Service.use((svc) => svc.init())
2420
yield* Effect.all(
2521
[
26-
LSP.Service,
27-
ShareNext.Service,
28-
Format.Service,
29-
File.Service,
30-
FileWatcher.Service,
31-
Vcs.Service,
32-
Snapshot.Service,
33-
].map((s) => Effect.forkDetach(s.use((i) => i.init()))),
22+
Config.Service.use((i) => i.get()),
23+
...[
24+
Plugin.Service,
25+
LSP.Service,
26+
ShareNext.Service,
27+
Format.Service,
28+
File.Service,
29+
FileWatcher.Service,
30+
Vcs.Service,
31+
Snapshot.Service,
32+
].map((s) => s.use((i) => i.init())),
33+
].map((e) => Effect.forkDetach(e)),
3434
).pipe(Effect.withSpan("InstanceBootstrap.init"))
3535

3636
const projectID = ctx.project.id

0 commit comments

Comments
 (0)