Blog
Flag namespace in Opencode codebase.

Flag namespace in Opencode codebase.

In this article, we review Flag namespace in Opencode codebase. We will look at:

  1. Flag namespace

  2. Flag usage

I study patterns used in an open source project found on Github Trending. For this week, I reviewed some parts of opencode codebase and wrote this article.

Flag namespace

You will find the following definition in opencode/packages/opencode/src/flag/flag.ts.

export namespace Flag {
  export const OPENCODE_AUTO_SHARE = truthy("OPENCODE_AUTO_SHARE")
  export const OPENCODE_CONFIG = process.env["OPENCODE_CONFIG"]
  export const OPENCODE_CONFIG_CONTENT = process.env["OPENCODE_CONFIG_CONTENT"]
  export const OPENCODE_DISABLE_AUTOUPDATE = truthy("OPENCODE_DISABLE_AUTOUPDATE")
  export const OPENCODE_DISABLE_PRUNE = truthy("OPENCODE_DISABLE_PRUNE")
  export const OPENCODE_PERMISSION = process.env["OPENCODE_PERMISSION"]
  export const OPENCODE_DISABLE_DEFAULT_PLUGINS = truthy("OPENCODE_DISABLE_DEFAULT_PLUGINS")
  export const OPENCODE_DISABLE_LSP_DOWNLOAD = truthy("OPENCODE_DISABLE_LSP_DOWNLOAD")
  export const OPENCODE_ENABLE_EXPERIMENTAL_MODELS = truthy("OPENCODE_ENABLE_EXPERIMENTAL_MODELS")
  export const OPENCODE_DISABLE_AUTOCOMPACT = truthy("OPENCODE_DISABLE_AUTOCOMPACT")

  // Experimental
  export const OPENCODE_EXPERIMENTAL_WATCHER = truthy("OPENCODE_EXPERIMENTAL_WATCHER")

  function truthy(key: string) {
    const value = process.env[key]?.toLowerCase()
    return value === "true" || value === "1"
  }
}

Can you guess what is the purpose of truthy function here? it just converts a “true” || “1” to true, otherwise false.

Flag usage

In sst/opencode/packages/opencode/src/lsp/server.ts#L100, you will find the below code:

 if (!(await Bun.file(js).exists())) {
    if (Flag.OPENCODE_DISABLE_LSP_DOWNLOAD) return

There is also other references to the Flag in the same file as shown in the below image:

About me:

Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.

Email: ramu.narasinga@gmail.com

Want to learn from open-source? Solve challenges inspired by open-source projects.

References:

  1. https://github.com/sst/opencode/blob/dev/packages/opencode/src/flag/flag.ts

  2. https://github.com/sst/opencode/blob/dev/packages/opencode/src/lsp/server.ts#L11

  3. https://github.com/sst/opencode/blob/dev/packages/opencode/src/lsp/server.ts#L100