Repository navigation
Description
What version of Effect is running?
effect 4.1.2, @effect/sql-pg 4.1.2 (TypeScript 7.0.2, Node.js 24.22.1)
What steps can reproduce the bug?
Providing a generic SqlClient removes a PgClient requirement from the types, though no PgClient was provided:
import { PgClient } from '@effect/sql-pg'
import { Effect, Layer } from 'effect'
import { SqlClient } from 'effect/sql'
const program = Effect.gen(function* () {
const pg = yield* PgClient.PgClient
return pg.config
})
// Any SqlClient that isn't a PgClient, e.g. SQLite in tests
const sqlClient = Layer.succeed(SqlClient.SqlClient, {} as SqlClient.SqlClient)
// Type-checks: the PgClient requirement has been removed
const runnable: Effect.Effect<unknown, never, never> = Effect.provide(program, sqlClient)
Effect.runPromise(runnable).then(console.log, error => console.error(String(error)))Layer.provide behaves the same way.
What is the expected behavior?
Effect.provide(program, sqlClient) should leave PgClient in the requirements, so the assignment to Effect.Effect<unknown, never, never> is a type error.
What do you see instead?
It type-checks, then fails at runtime:
Error: Service not found: @effect/sql-pg/PgClient
We hit this in an app whose integration tests run on LibsqlClient and only some specs use Postgres. A layer that needed PgClient moved into the shared app layer, tsc passed, and the non-Postgres specs failed at startup.
Additional information
PgClient is declared as Context.Service<PgClient, PgClient>, so its identifier type is the shape interface, and interface PgClient extends Client.SqlClient. Remaining requirements are computed with Exclude<R, ROut>, which removes every member assignable to what's provided. A PgClient is assignable to a SqlClient, so providing SqlClient removes it. The [TypeId] brand on PgClient doesn't help, because it only stops the reverse: a SqlClient isn't assignable to a PgClient, so providing only PgClient correctly leaves a SqlClient requirement.
The runtime looks services up by key, so this is unsound only at the type level. LibsqlClient is declared the same way (Context.Service<LibsqlClient, LibsqlClient>, extends Client.SqlClient), and I'd expect the other drivers to be too.
A possible fix is to give driver tags an identifier type that isn't a subtype of SqlClient's, e.g. the class form (class PgClient extends Context.Service<PgClient, Shape>()('@effect/sql-pg/PgClient') {}), whose literal key keeps identifiers distinct, or a separately branded identifier. This would stop PgClient.PgClient from naming the shape type, so it's a breaking type change, though it would only flag code that currently fails at runtime.
#8149 and #8153 changed how services are declared across packages, including the SQL drivers, but neither was merged and neither mentions this.
Activity