fix: improve cursor auto-import reliability on macOS (#161)
The macOS auto-import was failing with "Cursor database not found" even when Cursor was installed and logged in. This was caused by a single hardcoded path and no fallback when the DB file existed but couldn't be opened (e.g. WAL lock, Insiders variant). Changes (macOS only — linux/win32 paths are unchanged): - Probe both standard and Insiders DB locations on macOS - Return a descriptive error when the DB file exists but can't be opened - Try multiple known key names for token and machine ID - Add fuzzy key fallback for future Cursor schema changes - Normalize JSON-encoded string values from the DB Adds unit tests covering all new and existing behavior. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
73388a02a1
commit
d7e06c3085
2 changed files with 263 additions and 9 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { access, constants } from "fs/promises";
|
||||
import { homedir } from "os";
|
||||
import { join } from "path";
|
||||
import Database from "better-sqlite3";
|
||||
|
|
@ -12,9 +13,31 @@ export async function GET() {
|
|||
const platform = process.platform;
|
||||
let dbPath;
|
||||
|
||||
// Determine database path based on platform
|
||||
if (platform === "darwin") {
|
||||
dbPath = join(homedir(), "Library/Application Support/Cursor/User/globalStorage/state.vscdb");
|
||||
// macOS: probe multiple locations (standard + Insiders)
|
||||
const userHome = homedir();
|
||||
const candidateDbPaths = [
|
||||
join(userHome, "Library/Application Support/Cursor/User/globalStorage/state.vscdb"),
|
||||
join(userHome, "Library/Application Support/Cursor - Insiders/User/globalStorage/state.vscdb"),
|
||||
];
|
||||
|
||||
for (const path of candidateDbPaths) {
|
||||
try {
|
||||
await access(path, constants.R_OK);
|
||||
dbPath = path;
|
||||
break;
|
||||
} catch {
|
||||
// Continue probing next candidate.
|
||||
}
|
||||
}
|
||||
|
||||
if (!dbPath) {
|
||||
return NextResponse.json({
|
||||
found: false,
|
||||
error:
|
||||
"Cursor database not found in known macOS locations. Make sure Cursor IDE is installed and opened at least once.",
|
||||
});
|
||||
}
|
||||
} else if (platform === "linux") {
|
||||
dbPath = join(homedir(), ".config/Cursor/User/globalStorage/state.vscdb");
|
||||
} else if (platform === "win32") {
|
||||
|
|
@ -31,6 +54,12 @@ export async function GET() {
|
|||
try {
|
||||
db = new Database(dbPath, { readonly: true, fileMustExist: true });
|
||||
} catch (error) {
|
||||
if (platform === "darwin") {
|
||||
return NextResponse.json({
|
||||
found: false,
|
||||
error: `Found Cursor database at ${dbPath} but could not open it: ${error.message}`,
|
||||
});
|
||||
}
|
||||
return NextResponse.json({
|
||||
found: false,
|
||||
error: "Cursor database not found. Make sure Cursor IDE is installed and you are logged in.",
|
||||
|
|
@ -38,17 +67,58 @@ export async function GET() {
|
|||
}
|
||||
|
||||
try {
|
||||
// Extract tokens from database
|
||||
const accessTokenKeys = [
|
||||
"cursorAuth/accessToken",
|
||||
"cursorAuth/token",
|
||||
];
|
||||
const machineIdKeys = [
|
||||
"storage.serviceMachineId",
|
||||
"storage.machineId",
|
||||
"telemetry.machineId",
|
||||
];
|
||||
const desiredKeys = [...accessTokenKeys, ...machineIdKeys];
|
||||
|
||||
const rows = db.prepare(
|
||||
"SELECT key, value FROM itemTable WHERE key IN (?, ?)"
|
||||
).all("cursorAuth/accessToken", "storage.serviceMachineId");
|
||||
`SELECT key, value FROM itemTable WHERE key IN (${desiredKeys.map(() => "?").join(",")})`
|
||||
).all(...desiredKeys);
|
||||
|
||||
const normalizeValue = (value) => {
|
||||
if (typeof value !== "string") return value;
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
return typeof parsed === "string" ? parsed : value;
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
const tokens = {};
|
||||
for (const row of rows) {
|
||||
if (row.key === "cursorAuth/accessToken") {
|
||||
tokens.accessToken = row.value;
|
||||
} else if (row.key === "storage.serviceMachineId") {
|
||||
tokens.machineId = row.value;
|
||||
if (accessTokenKeys.includes(row.key) && !tokens.accessToken) {
|
||||
tokens.accessToken = normalizeValue(row.value);
|
||||
} else if (machineIdKeys.includes(row.key) && !tokens.machineId) {
|
||||
tokens.machineId = normalizeValue(row.value);
|
||||
}
|
||||
}
|
||||
|
||||
// Fuzzy fallback for newer/changed key names (macOS only, where the
|
||||
// issue was originally reported; other platforms use exact keys).
|
||||
if (platform === "darwin" && (!tokens.accessToken || !tokens.machineId)) {
|
||||
const fallbackRows = db.prepare(
|
||||
"SELECT key, value FROM itemTable WHERE key LIKE '%cursorAuth/%' OR key LIKE '%machineId%' OR key LIKE '%serviceMachineId%'"
|
||||
).all();
|
||||
|
||||
for (const row of fallbackRows) {
|
||||
const key = row.key || "";
|
||||
const value = normalizeValue(row.value);
|
||||
|
||||
if (!tokens.accessToken && key.toLowerCase().includes("accesstoken")) {
|
||||
tokens.accessToken = value;
|
||||
}
|
||||
|
||||
if (!tokens.machineId && key.toLowerCase().includes("machineid")) {
|
||||
tokens.machineId = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue