Refuse the shared home before reporting a missing database
Cursor caught a consequence of moving the shared-home guard after path resolution: the missing-target check ran first, so a shared home with no database yet got "start the dev server (`bun run dev`), then seed" — advice that creates the database in the shared home and walks the user into the exact write the guard refuses. The refusals run first again. That surfaced why the previous ordering existed: realPath fails on a missing final component, and falling back to the literal path would lose symlink resolution precisely when the bad advice would be given. Canonicalize the directory and re-join the filename instead, so a symlinked state dir is resolved whether or not the database behind it exists yet. Verified all three ways: a shared home without a database is refused as shared-home, a symlinked base without a database behind it is refused as shared-home, and a legitimate empty target still gets the missing-target hint. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
14a9306ea6
commit
aad1bdd49a
1 file changed
+19
-6
+19
-6
@@ -237,9 +237,6 @@ const devSeedCli = Command.make("dev-seed", {
|
||||
]);
|
||||
const sourceDbPath = stateDbPath(path, sourceBaseDir, sourceStateDir);
|
||||
const targetDbPath = stateDbPath(path, targetBaseDir, targetStateDir);
|
||||
if (!(yield* fileSystem.exists(targetDbPath))) {
|
||||
return yield* new DevSeedTargetError({ reason: "missing-target", detail: targetDbPath });
|
||||
}
|
||||
|
||||
// The whole point is to keep dev data off the real home; overwriting it
|
||||
// here would be the exact accident this command exists to avoid.
|
||||
@@ -248,10 +245,19 @@ const devSeedCli = Command.make("dev-seed", {
|
||||
// base can canonicalize to itself while its `userdata` or `dev`
|
||||
// subdirectory is a symlink into the shared home, and comparing bases
|
||||
// then waves through a seed whose actual write target is the real
|
||||
// database. The db path is what gets opened, so it is what gets checked.
|
||||
// database. The directory is canonicalized and the filename re-joined
|
||||
// because the database may not exist yet — realPath fails on a missing
|
||||
// final component, and falling back to the literal path would lose the
|
||||
// symlink resolution in exactly the case where the next error's advice
|
||||
// ("start the dev server") would create the database in the shared home.
|
||||
const canonicalDbPath = (databasePath: string) =>
|
||||
fileSystem.realPath(path.dirname(databasePath)).pipe(
|
||||
Effect.map((directory) => path.join(directory, path.basename(databasePath))),
|
||||
Effect.orElseSucceed(() => databasePath),
|
||||
);
|
||||
const [canonicalTargetDb, canonicalSourceDb, canonicalShared] = yield* Effect.all([
|
||||
fileSystem.realPath(targetDbPath).pipe(Effect.orElseSucceed(() => targetDbPath)),
|
||||
fileSystem.realPath(sourceDbPath).pipe(Effect.orElseSucceed(() => sourceDbPath)),
|
||||
canonicalDbPath(targetDbPath),
|
||||
canonicalDbPath(sourceDbPath),
|
||||
fileSystem.realPath(sharedHome).pipe(Effect.orElseSucceed(() => sharedHome)),
|
||||
]);
|
||||
if (canonicalTargetDb.startsWith(`${canonicalShared}${path.sep}`)) {
|
||||
@@ -265,6 +271,13 @@ const devSeedCli = Command.make("dev-seed", {
|
||||
return yield* new DevSeedTargetError({ reason: "same-source", detail: canonicalTargetDb });
|
||||
}
|
||||
|
||||
// After the refusals: this error's advice is to start a dev server, which
|
||||
// creates the database — the right next step everywhere the guards above
|
||||
// did not already rule the target out.
|
||||
if (!(yield* fileSystem.exists(targetDbPath))) {
|
||||
return yield* new DevSeedTargetError({ reason: "missing-target", detail: targetDbPath });
|
||||
}
|
||||
|
||||
// SQLite locking is not enough on its own: the seed's transaction can take
|
||||
// the write lock between a running server's own writes and commit cleanly.
|
||||
// What it cannot do is tell that server the projections it is holding in
|
||||
|
||||
Reference in new issue
Block a user