Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/flags-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'flags': minor
'@flags-sdk/next': minor
'@flags-sdk/vite': minor
---

Flags files: declare the flags of an application in `app.flags` (`flag summerSale { key: "summer-sale", defaultValue: false }`: the name which the code imports, and the key in the flag management system), and import them with `import { summerSale } from './app.flags'`, or from their source with `import { summerSale } from 'flags:self'`, which has the flags of the source from all flags files. A flag can set its own `source`. The framework plugins compile each flags file into `defineSource` and `source.bindFlag`, without the network. `node --import flags/register` does the same without a bundler. For the types, the SDK writes `app.d.flags.ts` next to each file, for `allowArbitraryExtensions`, and TypeScript 7.1 can also read flags files with the content mapper of `flags` (`"contentMappers": [{ "package": "flags", "extensions": [".flags"] }]`). An application with flags files uses no catalogs. `flags format` formats flags files (`--check` for CI, `--stdin` for editors such as Zed), and `formatFlagsFile` of `flags/tooling` is the formatter.
2 changes: 1 addition & 1 deletion .github/workflows/tag-canary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- uses: actions/setup-node@v4
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version-file: ".node-version"
registry-url: https://registry.npmjs.org
Expand Down
17 changes: 16 additions & 1 deletion .zed/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,22 @@
"TSX": { "formatter": { "language_server": { "name": "biome" } } },
"JSON": { "formatter": { "language_server": { "name": "biome" } } },
"JSONC": { "formatter": { "language_server": { "name": "biome" } } },
"CSS": { "formatter": { "language_server": { "name": "biome" } } }
"CSS": { "formatter": { "language_server": { "name": "biome" } } },
// The formatter of the workspace package. Run `pnpm build -F flags` first
"Flags": {
"formatter": {
"external": {
"command": "node",
"arguments": [
"packages/flags/bin/flags.js",
"format",
"--stdin",
"--stdin-filepath",
"{buffer_path}"
]
}
}
}
},
"auto_install_extensions": {
"biome": true
Expand Down
38 changes: 38 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,41 @@

- Always use curly braces for `if`, `else`, loop, and other control-flow bodies in newly added code, including single-statement bodies. Do not refactor existing code solely to add braces.
- Avoid nested ternary expressions. Prefer explicit conditionals or small helper functions so each branch is easy to follow.

# Repository

pnpm (10.19) + Turborepo monorepo of the Flags SDK. Node `^22.11 || ^24 || >=26`.

The `standard` branch holds the Flags SDK v5 rewrite ("one standard for every framework"). Its spec is `packages/flags/SPEC.md`, the normative rules are `packages/flags/STANDARD.md`, and the guide for framework authors is `packages/flags/FRAMEWORK-AUTHORS.md`. Read the relevant section of `SPEC.md` before changing v5 behavior, and update it in the same change.

## Layout

| Path | Contents |
| --- | --- |
| `packages/flags` | The main SDK, published as `flags` (not `@vercel/flags`, which is the old name). Exports `flags`, `flags/runtime`, `flags/explorer`, `flags/manifest`, `flags/tooling`, `flags/config`, `flags/settings`, `flags/hooks`, `flags/framework` and the `flags` CLI. |
| `packages/framework-*` | One package per framework: `@flags-sdk/next`, `vite`, `sveltekit`, `nuxt`, `tanstack-start`, `hono`, `express`. They use only the public API of `flags`. |
| `packages/adapter-*` | Provider adapters, published as `@flags-sdk/<name>`. |
| `packages/vercel-flags-core` | `@vercel/flags-core` (keeps the `@vercel` scope): the Vercel Flags evaluation engine and `VercelProvider`. It has its own `CLAUDE.md`. |
| `packages/conformance`, `packages/prepare-flags-definitions` | Conformance tests for the standard; the build step that embeds flag definitions. |
| `apps/docs` | flags-sdk.dev (Geistdocs). Follow `apps/docs/AGENTS.md`. |
| `apps/playground`, `apps/shirt-shop-api` | Next.js apps. |
| `examples/` | `standard/*` (v5 examples), `providers/*` (one per provider), `playgrounds/*`, `shirt-shop`, `snippets`, `sveltekit-example`. |
| `tests/standard/*` | v5 integration apps per framework (next, next-cache-components, sveltekit, nuxt, tanstack-start, hono, express). `tests/` also has the older e2e apps. |
| `skills/` | Agent skills `flags-sdk` and `vercel-flags`. Keep them in line with the API when it changes (`pnpm validate-skills`). |

## Commands

- From the root: `pnpm build` (packages only), `pnpm test`, `pnpm type-check`, `pnpm check` (biome), `pnpm format`.
- One workspace: `pnpm <script> -F <name>`, for example `pnpm dev -F playground`.
- `packages/flags`: `pnpm test`, `pnpm type-check`, and `pnpm test:playground`. `src/playground.test.ts` walks through the v5 features by group. When you add or change a feature, update the walkthrough in the same change.
- Apps and tests use the `dist` output of the workspace packages. Run `pnpm build` (or `turbo build --filter=<pkg>`) after you change a package and before you run an app against it.

## Conventions

- Formatting and lint: Biome (`biome.json`). It uses single quotes in TS but double quotes in CSS. Run `pnpm exec biome check --write <files>` on the files you changed.
- Commits: the husky pre-commit hook runs `biome check` through lint-staged. Never run `lint-staged` by hand. If it fails outside a commit, it moves uncommitted changes into a "lint-staged automatic backup" stash. Make biome clean before you commit. After each commit, check `git show --stat HEAD`.
- Commit subjects use a scope prefix: `[flags] ...`, `[docs] ...`, `[flags-core] ...`, `[skills] ...`.
- New workspace apps go in `apps/*` or `examples/*`. The workspace already includes them, so `pnpm-workspace.yaml` needs no change. Use the versions the other apps pin: `next` `16.4.6`, `react`/`react-dom` `19.2.4`, `@types/react`/`@types/react-dom` `catalog:`, `tailwindcss` and `@tailwindcss/postcss` `^4.1.17`, `typescript` `^5.9.3`. Use `workspace:*` for SDK packages.
- `next dev` writes and restores a managed "This is NOT the Next.js you know" block in each Next.js app's `AGENTS.md`. Commit it; do not remove it. Before you write Next.js code, read the docs in that app's `node_modules/next/dist/docs/`.
- `minimumReleaseAge` is 2880 minutes (two days) in `pnpm-workspace.yaml`. A newly published third-party version cannot be installed until it is that old. `flags`, `next`, `@next/*`, `@flags-sdk/*` and `@vercel/*` are exempt.
- Docs wording: a flag that is evaluated at request time evaluates "dynamically", not "freshly". Precomputed values are "static".
249 changes: 249 additions & 0 deletions apps/docs/content/docs/flags/flags-files.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
---
title: Flags files
description: Declare the flags of your application in app.flags, and import them like a module.
---

A flags file declares the flags of your application. You add a flag to `app.flags`, and import it:

```js title="app.flags"
source self

flag summerSale {
key: "summer-sale"
description: "Shows the summer sale banner"
defaultValue: false
}

flag checkout {
description: "The checkout flow"
options: ["control", { value: "express", label: "Express checkout" }]
defaultValue: "control"
}
```

```ts title="app/page.tsx"
import { checkout, summerSale } from '../app.flags';

const sale = await summerSale();
const flow = await checkout(); // 'control' | 'express' | (string & {})
```

Each flag is a flag like each other: overrides, hooks, the evaluation context, one value per request and Flags Explorer work the same. Your flag management system still decides each value, with its rules and rollouts.

## Why flags files

Your code is the source of truth of which flags exist, and of what the code knows about each flag. Your flag management system is the source of truth of the values.

- **A branch shows its flags.** A new flag is a new block in `app.flags`, so the diff of a pull request shows each flag which the branch adds, and nothing else.
- **No network in the build.** The build and the type check read only your files. They work offline, in CI, and in a sandbox without credentials.
- **Agents see the flags.** An agent reads `app.flags` and knows each flag, its type and its options, and adds a flag without access to your flag management system.
- **A flag works before it exists.** Until you create the flag in your flag management system, it evaluates to its `defaultValue`.

## The format

A flags file has two statements:

- `source <name>` names the default [source](#the-source) of the flags of the file, e.g. `source self`. It is optional, and the default is `self`.
- `flag <name>` declares a flag. The name is a JavaScript name, e.g. `summerSale`, and it is what you import. See [The name and the key](#the-name-and-the-key).

The body of a flag, in `{ … }`, is optional. It is an object of [JSON5](https://json5.org): JSON, plus keys without quotes, strings in single quotes, and trailing commas. A new line separates two fields, so you need a comma only between two fields on one line. Its fields are `key`, `source` and the options of [`bindFlag`](/docs/flags/provider-flags):

| Field | Value |
| --- | --- |
| `key` | The key of the flag in your flag management system. Optional: the default is the name. |
| `source` | The source of this flag, if it is not the source of the file. |
| `description` | What the flag does. Your editor shows it on hover. |
| `type` | `"boolean"`, `"string"`, `"number"` or `"object"`. Optional, see below. |
| `defaultValue` | The value when the evaluation fails, e.g. before the flag exists in your flag management system. Without it, a failed evaluation throws. |
| `options` | The values which the code knows, as values or as `{ value: …, label: "…" }`. They do not limit the value. |

The type is `type`, or the type of `defaultValue`, or the type of the options. Without any of them, the flag is a boolean flag, so `flag newFeature` alone declares a boolean flag. An object flag takes an object, an array or `null`, and each of its options must be `{ value: … }`:

```js title="app.flags"
flag banner {
defaultValue: { text: "Welcome", color: "blue" }
options: [
{ value: { text: "Welcome", color: "blue" }, label: "Default" }
{ value: { text: "Sale!", color: "red" }, label: "Sale" }
]
}
```

Comments are `//` and `/* … */`, as in JavaScript.

### Only plain values

A flags file has only plain values: strings, numbers, `true`, `false`, `null`, arrays and objects. Nothing in a flags file runs. References, function calls, operators, template literals, `undefined`, `Infinity` and `NaN` are errors.

### The name and the key

The name after `flag` is the name in your code: you import it. The field `key` is the key of the flag in your flag management system. Without `key`, the key is the name:

```js title="app.flags"
flag checkout // the key is "checkout"

flag summerSale {
key: "summer-sale" // the key in your flag management system
}
```

```ts
import { checkout, summerSale } from './app.flags';
```

So a key can have any characters, e.g. `-` or `.`, and you can rename a flag in your code and keep its key. A name which is not a JavaScript name, e.g. `flag summer-sale`, is an error with the fix: `flag summerSale { key: "summer-sale" }`.

### A flag of another source

A flag can have its own source, for a single case. The source of the file stays the default for the other flags:

```js title="app.flags"
source self

flag summerSale

flag heroText {
key: "hero-text"
source: "marketing"
}
```

### Import from the source

You can also import a flag from its [source](#the-source). `flags:self` has the flags of the source `self` from all flags files:

```ts title="app/page.tsx"
import { checkout, summerSale } from 'flags:self';
import { heroText } from 'flags:marketing';
```

It is the same flag as the import from its file, so both work in one application. The plugin of your framework writes the modules of `flags:<source>` into `.flags`, which Git ignores, and their types into `flags/generated.d.ts`. Without a framework plugin, `flags sync` writes them. A key or a name in two files of one source is an error. Restart the dev server after you add a file with a new source.

### Errors

The build fails with the position of each error, and a hint:

```txt
app.flags:5:3 - error: Unknown field "defualtValue" in the flag "summerSale". Did you mean "defaultValue"?

defualtValue: false
~~~~~~~~~~~~
```

The SDK checks that each value has the type of its flag, that each option is in `options` only once, and that each flag is in the file only once. A `defaultValue` which is not one of the options is a warning.

## The source

The source of a flags file is the folder `flags/sources/<source>` of your application, in its root or in `src`:

```text
app.flags # source self (the default)
app/
marketing.flags # source marketing
flags/
sources/
self/
provider.ts # The OpenFeature provider
context.ts # Optional: getEvaluationContext(request, input)
hooks.ts # Optional: the OpenFeature hooks of the source
marketing/
provider.ts
```

With `@vercel/flags-core` in your dependencies, the source `self` needs no folder: it uses the flags of your Vercel project, with `new VercelProvider()`. Add `flags/sources/self/context.ts` to give it an evaluation context. See [Code generation](/docs/flags/code-generation#the-folder-of-a-source) for the files of a source.

You can have more than one flags file, also with the same source, e.g. one file next to each feature. Fewer flags in one file also means fewer conflicts when you merge branches.

## Set it up

### Next.js, SvelteKit, Nuxt and TanStack Start

The plugin of your framework compiles each flags file. You need nothing else: add [the plugin](/docs/flags/code-generation#2-add-the-plugin), and create `app.flags`.

### Hono, Express and Node.js

Without a bundler, start Node.js with the hooks of the SDK:

```sh title="Terminal"
node --import flags/register server.ts
```

```ts title="server.ts"
import { summerSale } from './app.flags';
```

This needs Node.js 22.15 or newer.

### TypeScript

The SDK writes a declaration file next to each flags file, e.g. `app.d.flags.ts`, when the dev server, the build or `node --import flags/register` compiles it. Every editor and every TypeScript version reads it. Turn on `allowArbitraryExtensions`, and let Git ignore the files:

```json title="tsconfig.json"
{
"compilerOptions": {
"allowArbitraryExtensions": true
}
}
```

```txt title=".gitignore"
*.d.flags.ts
```

Before a type check in CI, write them with `flags sync`:

```json title="package.json"
{
"scripts": {
"type-check": "flags sync && tsc --noEmit"
}
}
```

Without a dev server, for example with plain Node.js, run `flags watch` while you edit flags files. It writes the declaration files again after each change.

#### TypeScript 7.1

With **TypeScript 7.1** or newer, you can also add the content mapper of `flags` to `tsconfig.json`, and run `tsc --runExternalCode`. TypeScript then reads the flags files itself, and your editor goes from an import to the flag in `app.flags`. The SDK still writes the declaration files, for editors with an older TypeScript.

```json title="tsconfig.json"
{
"contentMappers": [{ "package": "flags", "extensions": [".flags"] }]
}
```

### Format

`flags format` formats the flags files of the current directory, or of the files and folders which you give. `flags format --check` changes nothing, and fails if a file is not formatted, e.g. in CI:

```sh
npx flags format
npx flags format --check
```

The formatter uses two spaces, one field on each line of a flag, and double quotes. A value which is on one line stays on one line while it fits into 80 columns, e.g. `options: ["control", "express"]`. Put a new line after `[` or `{` to keep it on many lines. The comments stay.

`flags format --stdin` formats the standard input, for editors. In [Zed](https://zed.dev), with the extension of flags files, add it to `.zed/settings.json` of the project, and Zed formats flags files when you save them:

```json title=".zed/settings.json"
{
"languages": {
"Flags": {
"formatter": {
"external": {
"command": "npx",
"arguments": ["--no", "flags", "format", "--stdin", "--stdin-filepath", "{buffer_path}"]
}
}
}
}
}
```

### GitHub

GitHub does not know flags files yet. JSON5 is the closest language which it knows, and highlights the strings, the keys and the comments:

```txt title=".gitattributes"
*.flags linguist-language=JSON5
```
1 change: 1 addition & 0 deletions apps/docs/content/docs/flags/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"---Defining flags---",
"inline-flags",
"provider-flags",
"flags-files",
"code-generation",
"types-and-options",
"---Using flags---",
Expand Down
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"!apps/docs/next.config.ts",
"!apps/docs/postcss.config.mjs",
"!apps/docs/source.config.ts",
"!apps/docs/proxy.ts"
"!apps/docs/proxy.ts",
"!packages/tree-sitter-flags/src"
]
},
"formatter": {
Expand Down
2 changes: 2 additions & 0 deletions examples/standard/next-flags-files/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Highlight the strings, keys and comments of flags files on GitHub
*.flags linguist-language=JSON5
6 changes: 6 additions & 0 deletions examples/standard/next-flags-files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tsconfig.tsbuildinfo
# The types of the flags files, written by the Flags SDK on dev, build
# and `flags sync`
*.d.flags.ts
# The types of flags:<source>, written by the Flags SDK in the same way
flags/generated.d.ts
9 changes: 9 additions & 0 deletions examples/standard/next-flags-files/AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- BEGIN:nextjs-agent-rules -->

## This is NOT the Next.js you know

This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in `node_modules/next/dist/docs/` (resolved from this file's directory; in monorepos the `next` package may not be visible from the repo root) before writing any code. Heed deprecation notices.

This block is written and re-added by `next dev` — verify at `node_modules/next/dist/server/lib/generate-agent-files.js`. Removing it from a diff only re-creates the uncommitted change; committing it with your work keeps the tree clean.

<!-- END:nextjs-agent-rules -->
1 change: 1 addition & 0 deletions examples/standard/next-flags-files/CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@AGENTS.md
Loading
Loading