Skip to content

Commit 216ba8f

Browse files
authored
fix(opencode): stop Azure model discovery from logging to stdout (anomalyco#46646)
1 parent df6aecd commit 216ba8f

2 files changed

Lines changed: 88 additions & 36 deletions

File tree

‎packages/opencode/src/plugin/azure.ts‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { InstallationVersion } from "@opencode-ai/core/installation/version"
55
import { which } from "@opencode-ai/core/util/which"
66
import type { Hooks } from "@opencode-ai/plugin"
77
import type { Provider } from "@opencode-ai/sdk/v2"
8-
import { Effect, Schema } from "effect"
8+
import { Schema } from "effect"
99
import { OAUTH_DUMMY_KEY } from "../auth"
1010
import { Process } from "../util/process"
1111

@@ -69,9 +69,9 @@ export async function AzureAuthPlugin(): Promise<Hooks> {
6969

7070
export function createAzureAuthHooks(
7171
run: AzureCommand,
72-
request: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response> = fetch,
73-
accounts: readonly AzureAccount[] = [],
74-
available = true,
72+
request: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>,
73+
accounts: readonly AzureAccount[],
74+
available: boolean,
7575
): Hooks {
7676
const tokens = new Map<string, { token: string; expires: number }>()
7777
async function token(scope: string) {
@@ -128,17 +128,13 @@ export function createAzureAuthHooks(
128128
id: "azure",
129129
async models(provider, context) {
130130
if (context.auth?.type !== "oauth") return provider.models
131+
// Discovery shells out to the Azure CLI, so skip it when the CLI is missing.
132+
if (!available) return provider.models
131133
const resource = context.auth.accountId
132134
if (!resource) return {}
133-
return discoverAzureModels(provider.models, resource, run).catch((error: unknown) => {
134-
Effect.runSync(
135-
Effect.logWarning("Azure model discovery failed", {
136-
resource,
137-
error: error instanceof Error ? error.message : String(error),
138-
}),
139-
)
140-
return provider.models
141-
})
135+
// This hook runs outside the app's Effect runtime, so logging here would go to the
136+
// console. Fall back to the configured models silently.
137+
return discoverAzureModels(provider.models, resource, run).catch(() => provider.models)
142138
},
143139
},
144140
auth: {

‎packages/opencode/test/plugin/azure.test.ts‎

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describe("plugin.azure", () => {
241241

242242
test("keeps the existing API-key method and adds Entra ID", () => {
243243
delete process.env.AZURE_RESOURCE_NAME
244-
const hooks = createAzureAuthHooks(azureShell([]))
244+
const hooks = createAzureAuthHooks(azureShell([]), fetch, [], true)
245245

246246
expect(hooks.auth?.provider).toBe("azure")
247247
expect(hooks.provider?.id).toBe("azure")
@@ -272,10 +272,15 @@ describe("plugin.azure", () => {
272272

273273
test("lists Azure CLI resources and allows entering another resource", () => {
274274
delete process.env.AZURE_RESOURCE_NAME
275-
const hooks = createAzureAuthHooks(azureShell([]), fetch, [
276-
{ name: "first-resource", resourceGroup: "first-group" },
277-
{ name: "second-resource", resourceGroup: "second-group" },
278-
])
275+
const hooks = createAzureAuthHooks(
276+
azureShell([]),
277+
fetch,
278+
[
279+
{ name: "first-resource", resourceGroup: "first-group" },
280+
{ name: "second-resource", resourceGroup: "second-group" },
281+
],
282+
true,
283+
)
279284

280285
expect(oauthMethod(hooks).prompts).toEqual([
281286
{
@@ -299,17 +304,25 @@ describe("plugin.azure", () => {
299304
})
300305

301306
test("uses the selected Azure CLI resource", async () => {
302-
const hooks = createAzureAuthHooks(azureShell([]), fetch, [
303-
{ name: "selected-resource", resourceGroup: "selected-group" },
304-
])
307+
const hooks = createAzureAuthHooks(
308+
azureShell([]),
309+
fetch,
310+
[{ name: "selected-resource", resourceGroup: "selected-group" }],
311+
true,
312+
)
305313
const authorization = await oauthMethod(hooks).authorize({ resourceSelection: "selected-resource" })
306314
if (authorization.method !== "auto") throw new Error("Unexpected Azure authorization method")
307315

308316
expect(await authorization.callback()).toMatchObject({ type: "success", accountId: "selected-resource" })
309317
})
310318

311319
test("uses a manually entered Azure resource that was not listed", async () => {
312-
const hooks = createAzureAuthHooks(azureShell([]), fetch, [{ name: "listed-resource", resourceGroup: "group" }])
320+
const hooks = createAzureAuthHooks(
321+
azureShell([]),
322+
fetch,
323+
[{ name: "listed-resource", resourceGroup: "group" }],
324+
true,
325+
)
313326
const authorization = await oauthMethod(hooks).authorize({
314327
resourceSelection: "__manual__",
315328
resourceName: "unlisted-resource",
@@ -321,7 +334,7 @@ describe("plugin.azure", () => {
321334

322335
test("checks Azure CLI and stores the resource name", async () => {
323336
const scopes: string[] = []
324-
const hooks = createAzureAuthHooks(azureShell(scopes))
337+
const hooks = createAzureAuthHooks(azureShell(scopes), fetch, [], true)
325338
const authorization = await oauthMethod(hooks).authorize({ resourceName: "test-resource" })
326339
if (authorization.method !== "auto") throw new Error("Unexpected Azure authorization method")
327340

@@ -335,18 +348,23 @@ describe("plugin.azure", () => {
335348
})
336349

337350
test("supports Azure CLI versions that only provide expiresOn", async () => {
338-
const hooks = createAzureAuthHooks(async () => ({
339-
accessToken: "legacy-token",
340-
expiresOn: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
341-
}))
351+
const hooks = createAzureAuthHooks(
352+
async () => ({
353+
accessToken: "legacy-token",
354+
expiresOn: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
355+
}),
356+
fetch,
357+
[],
358+
true,
359+
)
342360
const authorization = await oauthMethod(hooks).authorize({ resourceName: "test-resource" })
343361
if (authorization.method !== "auto") throw new Error("Unexpected Azure authorization method")
344362

345363
expect(await authorization.callback()).toMatchObject({ type: "success", accountId: "test-resource" })
346364
})
347365

348366
test("rejects Azure CLI tokens without a usable expiration", async () => {
349-
const hooks = createAzureAuthHooks(async () => ({ accessToken: "invalid-token" }))
367+
const hooks = createAzureAuthHooks(async () => ({ accessToken: "invalid-token" }), fetch, [], true)
350368
const authorization = await oauthMethod(hooks).authorize({ resourceName: "test-resource" })
351369
if (authorization.method !== "auto") throw new Error("Unexpected Azure authorization method")
352370

@@ -379,6 +397,9 @@ describe("plugin.azure", () => {
379397
],
380398
commands,
381399
),
400+
fetch,
401+
[],
402+
true,
382403
)
383404
const list = hooks.provider?.models
384405
if (!list) throw new Error("Azure provider model hook is missing")
@@ -410,6 +431,9 @@ describe("plugin.azure", () => {
410431
[{ name: "gpt-production", properties: { model: { name: "gpt-5-mini" }, provisioningState: "Succeeded" } }],
411432
commands,
412433
),
434+
fetch,
435+
[],
436+
true,
413437
)
414438
const list = hooks.provider?.models
415439
if (!list) throw new Error("Azure provider model hook is missing")
@@ -433,6 +457,9 @@ describe("plugin.azure", () => {
433457
],
434458
[],
435459
),
460+
fetch,
461+
[],
462+
true,
436463
)
437464
const list = hooks.provider?.models
438465
if (!list) throw new Error("Azure provider model hook is missing")
@@ -446,19 +473,43 @@ describe("plugin.azure", () => {
446473
})
447474

448475
test("keeps configured models available when Azure discovery fails", async () => {
449-
const hooks = createAzureAuthHooks(async () => {
450-
throw new Error("Azure CLI failed")
451-
})
476+
const hooks = createAzureAuthHooks(
477+
async () => {
478+
throw new Error("Azure CLI failed")
479+
},
480+
fetch,
481+
[],
482+
true,
483+
)
452484
const list = hooks.provider?.models
453485
if (!list) throw new Error("Azure provider model hook is missing")
454486

455487
const catalog = models("gpt-5-mini")
456488
expect(await list({ ...provider, models: catalog }, { auth: oauth })).toBe(catalog)
457489
})
458490

491+
test("skips model discovery when the Azure CLI is unavailable", async () => {
492+
const calls: string[][] = []
493+
const hooks = createAzureAuthHooks(
494+
async (args) => {
495+
calls.push(args)
496+
throw new Error("spawn az ENOENT")
497+
},
498+
fetch,
499+
[],
500+
false,
501+
)
502+
const list = hooks.provider?.models
503+
if (!list) throw new Error("Azure provider model hook is missing")
504+
505+
const catalog = models("gpt-5-mini")
506+
expect(await list({ ...provider, models: catalog }, { auth: oauth })).toBe(catalog)
507+
expect(calls).toEqual([])
508+
})
509+
459510
test("does not change API-key loading", async () => {
460511
const scopes: string[] = []
461-
const hooks = createAzureAuthHooks(azureShell(scopes))
512+
const hooks = createAzureAuthHooks(azureShell(scopes), fetch, [], true)
462513
const catalog = models("gpt-5-mini")
463514
const list = hooks.provider?.models
464515
if (!list) throw new Error("Azure provider model hook is missing")
@@ -471,10 +522,15 @@ describe("plugin.azure", () => {
471522
test("uses Azure CLI bearer tokens for Azure inference endpoints", async () => {
472523
const scopes: string[] = []
473524
const requests: Headers[] = []
474-
const hooks = createAzureAuthHooks(azureShell(scopes), async (_input, init) => {
475-
requests.push(new Headers(init?.headers))
476-
return new Response(null, { status: 200 })
477-
})
525+
const hooks = createAzureAuthHooks(
526+
azureShell(scopes),
527+
async (_input, init) => {
528+
requests.push(new Headers(init?.headers))
529+
return new Response(null, { status: 200 })
530+
},
531+
[],
532+
true,
533+
)
478534
const options = await loader(hooks)(async () => oauth, provider)
479535
const request = customFetch(options)
480536

0 commit comments

Comments
 (0)