@@ -7,6 +7,7 @@ import { FSUtil } from "@opencode-ai/core/fs-util"
77import { Global } from "../../global"
88import { ConfigPaths } from "@/config/paths"
99import { ConfigManaged } from "@/config/managed"
10+ import { validateLoaded } from "@/config/config"
1011
1112/**
1213 * Whether the user config turns telemetry off, read without an Instance.
@@ -23,8 +24,9 @@ import { ConfigManaged } from "@/config/managed"
2324 *
2425 * - `disabled`: the last source that sets `telemetry.disabled` sets it to `true`.
2526 * - `enabled`: no source turns it off.
26- * - `unreadable`: a source that could not be read or parsed comes after the last one that set
27- * the value, so the answer is unknown. Callers fail closed for anything an opt-out forbids.
27+ * - `unreadable`: some source could not be read, parsed or validated as `Config` does (a missing
28+ * `{file:}` reference, a value its schema rejects). `Config` would fail to load at all, so the
29+ * answer is unknown. Callers fail closed for anything an opt-out forbids.
2830 */
2931export type State = "disabled" | "enabled" | "unreadable"
3032
@@ -45,17 +47,25 @@ function worktreeOf(directory: string): string | undefined {
4547export async function disabledState ( directory : string = process . cwd ( ) ) : Promise < State > {
4648 try {
4749 let value : boolean | undefined
50+ // Any source that fails to read, parse or validate fails Config's whole load, so the answer
51+ // is unknown however the other sources set it.
4852 let unknown = false
49- const apply = ( data : unknown ) => {
53+ const take = ( data : unknown ) => {
5054 const disabled = ( data as { telemetry ?: { disabled ?: unknown } } | undefined ) ?. telemetry ?. disabled
5155 if ( disabled === undefined ) return
52- // Config's schema accepts only a boolean; anything else makes the whole config invalid there.
53- if ( typeof disabled !== "boolean" ) {
56+ if ( typeof disabled !== "boolean" ) unknown = true
57+ else value = disabled
58+ }
59+ // Parse as the loader does (a missing `{file:}` reference is an error) and validate with the
60+ // loader's own schema step, so a source Config would reject cannot set the answer.
61+ const parse = async ( text : string , input : string | { source : string ; dir : string } ) => {
62+ if ( ! text . trim ( ) ) return // the loader treats an empty file as `{}`
63+ const source = typeof input === "string" ? input : input . source
64+ try {
65+ take ( validateLoaded ( await ConfigPaths . parseText ( text , input , "error" , { track : false } ) , source ) )
66+ } catch {
5467 unknown = true
55- return
5668 }
57- value = disabled
58- unknown = false
5969 }
6070 const readFile = async ( file : string ) => {
6171 let text : string
@@ -65,25 +75,15 @@ export async function disabledState(directory: string = process.cwd()): Promise<
6575 if ( ( err as NodeJS . ErrnoException ) ?. code !== "ENOENT" ) unknown = true
6676 return
6777 }
68- try {
69- apply ( await ConfigPaths . parseText ( text , file , "empty" , { track : false } ) )
70- } catch {
71- unknown = true
72- }
73- }
74- const readText = async ( text : string , source : string , dir : string ) => {
75- try {
76- apply ( await ConfigPaths . parseText ( text , { source, dir } , "empty" , { track : false } ) )
77- } catch {
78- unknown = true
79- }
78+ await parse ( text , file )
8079 }
80+ const readText = ( text : string , source : string , dir : string ) => parse ( text , { source, dir } )
8181
8282 for ( const file of GLOBAL_FILES ) await readFile ( path . join ( Global . Path . config , file ) )
8383 // The legacy TOML global config, which Config merges after the JSON files (and migrates).
8484 const legacy = path . join ( Global . Path . config , "config" )
8585 try {
86- apply ( Bun . TOML . parse ( await fs . readFile ( legacy , "utf-8" ) ) )
86+ take ( Bun . TOML . parse ( await fs . readFile ( legacy , "utf-8" ) ) )
8787 } catch ( err ) {
8888 if ( ( err as NodeJS . ErrnoException ) ?. code !== "ENOENT" ) unknown = true
8989 }
0 commit comments