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
5 changes: 5 additions & 0 deletions .bumpy/env-graph-shape-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
varlock: minor
---

Add a shape mode for tools that only inspect a schema. `new EnvGraph({ mode: "shape" })` (or `loadEnvGraph({ mode: "shape" })`) runs only pure resolvers, skips root decorator execution and the cache, never downloads plugins, ignores the shell environment, keeps generated code inside the project, and skips files that are not regular files. `@setValuesBulk` is applied when its data is written in the schema, and a file that is only loaded because its condition could not be evaluated never loads a plugin. Plugin resolvers can opt in with `pure: true`. A `@required`/`@sensitive` or `forEnv()` that depends on a skipped value is reported as unknown (`isRequiredUnknownInShapeLoad`, `isSensitiveUnknownInShapeLoad`) and treated as optional and sensitive.
57 changes: 54 additions & 3 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"packages/integrations/*",
"packages/native-helpers/*",
"packages/plugins/*",
"packages/plugins/effect/test/effect3"
"packages/plugins/effect/test/effect3",
"packages/ts-content-mapper/example"
],
"scripts": {
"build": "turbo build --filter=\"!smoke-test-*\"",
Expand Down
67 changes: 67 additions & 0 deletions packages/ts-content-mapper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# @varlock/ts-content-mapper (prototype)

A [TypeScript 7.1 content mapper](https://github.com/microsoft/typescript-go) that types varlock's `ENV` straight from `.env.schema`. App code keeps `import { ENV } from 'varlock/env'`, and there is no generated `env.d.ts`: while `tsc` runs, the mapper turns the schema into virtual TypeScript that augments `varlock/env`.

This is a prototype. It is private, unpublished, and its API, options and package name can change.

## Try it

From a fresh clone of this branch:

```sh
bun install
bunx turbo run check --filter=@varlock/ts-content-mapper-example
```

The second command builds `varlock` and the mapper, then runs `tsc -p . --runExternalCode` in [`example/`](./example). It passes with no output. To see it catch a mistake, add a wrong-typed use to `example/src/index.ts` (for example `const port: string = ENV.PORT;`) and run it again from the example directory:

```sh
cd packages/ts-content-mapper/example
bun run check
# src/index.ts(..): error TS2322: Type 'number' is not assignable to type 'string'.
```

Schema mistakes show up the same way, located in `.env.schema` (for example `# @type=porrt` gives `TS2551 ... Did you mean 'port'?`).

## How it is wired

The example's [`tsconfig.json`](./example/tsconfig.json):

```jsonc
{
"contentMappers": [
{ "package": "@varlock/ts-content-mapper", "extensions": [".schema"], "options": { "exposeEnv": "global" } }
],
"include": ["src", ".env.schema"]
}
```

- `.env.schema` is in `include`, so TypeScript asks the mapper to transform it. TypeScript finds the mapper process through the `typescript.contentMapper` field of this package's `package.json` (`node dist/server.mjs`).
- The mapper's main output is varlock's own `@generateTsTypes` output for the schema (the same types `env.d.ts` codegen would write), with each key mapped back to its line in the schema, so hover, go to definition, find references and rename work across the schema and app code in an editor that runs TS 7.1.
- A second virtual file lowers the schema itself into checked TypeScript against an API generated from varlock's registries (data types, resolvers, decorators, including those added by plugins). Schema mistakes become ordinary TS errors at their location in `.env.schema`, and they do not hide errors in app code.
- The schema is loaded with varlock's shape mode (`new EnvGraph({ mode: 'shape' })`): `exec()` and other non-pure resolvers are not run, root decorators are not executed, plugins are never downloaded, and `process.env` is ignored. Installed plugins are still imported, so their module code runs.
- Values of `@sensitive` items are not copied into the virtual TypeScript.
- Debugging: `VARLOCK_MAPPER_LOG=<file>` appends one line per transform (`tsc` does not show mapper stderr), and `VARLOCK_MAPPER_DUMP=<dir>` writes the virtual files.

## Versions

- `typescript@7.1.0-dev.20261009.1` in the example. Content mappers only exist in TS 7.1 (7.0.2 has no `--runExternalCode`). `7.1.0-beta` was published on 2026-10-20, which is too new to pin here (we prefer versions at least two weeks old). This nightly is from 2026-10-09, and the example passes on both.
- `ts-content-mapper@0.1.0`, the content mapper SDK this package is built on.
- Node `^22.18.0 || >=24.11.0` (from `ts-content-mapper`).

## Limits

- **CLI flag only.** Content mappers need `tsc --runExternalCode`, and TypeScript only accepts that flag on the command line (TS6266), not in `tsconfig.json`. Every script or CI job that type-checks needs it. Without it, `tsc` reports `TS18068` and `ENV` has none of the schema keys.
- **Next.js** runs its own type check with fixed arguments, so it cannot pass the flag. You need `typescript.ignoreBuildErrors: true` in `next.config` plus a separate `tsc --noEmit --runExternalCode` step (after `next typegen`) before `next build`. A plain `next build` then skips type errors silently.
- **Types only.** The runtime is unchanged: values still come from varlock (`varlock run`, `varlock/auto-load` or a framework integration).
- **TS 7.1 adoption.** Editors need the TS 7.1 language server (`js/ts.experimental.useTsgo` in VS Code, pointed at the workspace's `typescript`). typescript-eslint does not support TS 7 yet.
- **Known gaps** (from the spike's real-world run): a few data type options the mapper does not know are reported as errors (`string(allowEmpty)` and similar), and errors inside `@import`ed schema files are not shown.

## Tests

```sh
bunx turbo run build --filter=@varlock/ts-content-mapper
cd packages/ts-content-mapper && bun run test:ci
```

Each test runs the real `tsc --runExternalCode` against the built mapper: the example type-checks and its type assertions hold (and fail when the schema changes), wrong-typed app code is a TS error, schema errors are TS errors located in `.env.schema`, `exec()` in a schema is never run, no network request is made (a pinned plugin that is not installed is refused, not downloaded), and sensitive values stay out of the virtual code.
33 changes: 33 additions & 0 deletions packages/ts-content-mapper/example/.env.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Example schema for @varlock/ts-content-mapper.
# There is no @generateTsTypes here: the content mapper types `ENV` from this file while tsc runs.
#
# @currentEnv=$APP_ENV
# @defaultRequired=false @defaultSensitive=false
# ---

# Which environment the app runs in
# @type=enum(development, staging, production) @required
APP_ENV=development

# Port the HTTP server listens on
# @type=port @required
PORT=3000

# Base URL of the upstream API
# @type=url @required
API_URL=https://api.example.com

# @type=enum(debug, info, warn, error) @required
LOG_LEVEL=info

# @type=boolean @required
ENABLE_SIGNUPS=false

# Error reporting, only set in deployed environments
# @type=url
SENTRY_DSN=

# Payment provider secret key, never committed
# @sensitive @required @type=string(startsWith=sk_)
# @example=sk_test_123
STRIPE_SECRET_KEY=
15 changes: 15 additions & 0 deletions packages/ts-content-mapper/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@varlock/ts-content-mapper-example",
"private": true,
"type": "module",
"scripts": {
"check": "tsc -p . --runExternalCode"
},
"dependencies": {
"varlock": "workspace:*"
},
"devDependencies": {
"@varlock/ts-content-mapper": "workspace:*",
"typescript": "7.1.0-dev.20261009.1"
}
}
29 changes: 29 additions & 0 deletions packages/ts-content-mapper/example/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ENV } from 'varlock/env';

// Every key below is typed from ../.env.schema by the content mapper. There is no env.d.ts.
const port: number = ENV.PORT;
const apiUrl: string = ENV.API_URL;
const signups: boolean = ENV.ENABLE_SIGNUPS;

function logLevelRank(level: typeof ENV.LOG_LEVEL) {
switch (level) {
case 'debug': return 0;
case 'info': return 1;
case 'warn': return 2;
case 'error': return 3;
default: return level satisfies never;
}
}

// SENTRY_DSN is optional, so it has to be narrowed before use
const sentryTarget = ENV.SENTRY_DSN ? ENV.SENTRY_DSN.replace(/^https?:\/\//, '') : 'disabled';

export const summary = {
port,
apiUrl,
signups,
logLevel: logLevelRank(ENV.LOG_LEVEL),
sentryTarget,
env: ENV.APP_ENV,
hasStripeKey: ENV.STRIPE_SECRET_KEY.startsWith('sk_'),
};
15 changes: 15 additions & 0 deletions packages/ts-content-mapper/example/src/type-assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Compile-time checks: this file fails `tsc` if the mapper types any of these keys differently.
import { ENV } from 'varlock/env';

type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
type Expect<T extends true> = T;

export type TypeAssertions = [
Expect<Equal<typeof ENV.PORT, number>>,
Expect<Equal<typeof ENV.API_URL, string>>,
Expect<Equal<typeof ENV.ENABLE_SIGNUPS, boolean>>,
Expect<Equal<typeof ENV.LOG_LEVEL, 'debug' | 'info' | 'warn' | 'error'>>,
Expect<Equal<typeof ENV.APP_ENV, 'development' | 'staging' | 'production'>>,
Expect<Equal<typeof ENV.SENTRY_DSN, string | undefined>>,
Expect<Equal<typeof ENV.STRIPE_SECRET_KEY, string>>,
];
17 changes: 17 additions & 0 deletions packages/ts-content-mapper/example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es2023",
"lib": ["ES2023"],
"module": "nodenext",
"moduleResolution": "nodenext",
"strict": true,
"noEmit": true,
"skipLibCheck": true
},
"contentMappers": [
{ "package": "@varlock/ts-content-mapper",
"extensions": [".schema"],
"options": { "exposeEnv": "global" } }
],
"include": ["src", ".env.schema"]
}
10 changes: 10 additions & 0 deletions packages/ts-content-mapper/example/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"check": {
"dependsOn": ["^build"],
"cache": false
}
}
}
Loading
Loading