Skip to content

Commit

Permalink
fix npx convex run --watch --component-path and make schemas reacti…
Browse files Browse the repository at this point in the history
…ve (#29494)

fix the CLI so it passes through component path when you do `npx convex run --watch`.

and on schema failures we can rebuild when the data changes in the component whose schema failed.

also there was a bug: when the websocket starts up after creating a query, which happens occasionally in the dashboard and happens every time when the CLI calls `subscribe`, we weren't passing the component path the second time.

GitOrigin-RevId: ee29a46f68ca5726051686636faf1ddb434036c0
  • Loading branch information
ldanilek authored and Convex, Inc. committed Sep 3, 2024
1 parent 77e786b commit 7770d2e
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/browser/sync/local_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type LocalQuery = {
args: Record<string, Value>;
numSubscribers: number;
journal?: QueryJournal;
componentPath?: string;
};

export class LocalSyncState {
Expand Down Expand Up @@ -95,6 +96,7 @@ export class LocalSyncState {
args,
numSubscribers: 1,
journal,
componentPath,
};
this.querySet.set(queryToken, query);
this.queryIdToToken.set(queryId, queryToken);
Expand Down Expand Up @@ -279,6 +281,7 @@ export class LocalSyncState {
udfPath: localQuery.canonicalizedUdfPath,
args: [convexToJson(localQuery.args)],
journal: localQuery.journal,
componentPath: localQuery.componentPath,
};
modifications.push(add);

Expand Down
5 changes: 4 additions & 1 deletion src/bundler/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export type ErrorType =
// The `convex dev` command will wait for either file OR table data change
// to retry (if a table name is specified as the value in this Object).
| {
"invalid filesystem or db data": string | null;
"invalid filesystem or db data": {
tableName: string;
componentPath?: string;
} | null;
}
// The error was caused by either the local state (ie schema.ts content)
// or the state of the deployment environment variables.
Expand Down
1 change: 1 addition & 0 deletions src/cli/convexExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ async function waitForStableExportState(
adminKey,
"_system/cli/exports:getLatest",
{},
undefined,
donePromise,
{
onChange: (value: any) => {
Expand Down
1 change: 1 addition & 0 deletions src/cli/convexImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ export async function waitForStableImportState(
adminKey,
"_system/cli/queryImport",
{ importId },
undefined,
donePromise,
{
onChange: (value: any) => {
Expand Down
19 changes: 16 additions & 3 deletions src/cli/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,12 @@ export async function watchAndPush(
return;
}
const fileSystemWatch = getFileSystemWatch(ctx, watch, cmdOptions);
const tableWatch = getTableWatch(ctx, options, tableNameTriggeringRetry);
const tableWatch = getTableWatch(
ctx,
options,
tableNameTriggeringRetry?.tableName ?? null,
tableNameTriggeringRetry?.componentPath,
);
const envVarWatch = getDeplymentEnvVarWatch(
ctx,
options,
Expand All @@ -330,9 +335,14 @@ function getTableWatch(
adminKey: string;
},
tableName: string | null,
componentPath: string | undefined,
) {
return getFunctionWatch(ctx, credentials, "_system/cli/queryTable", () =>
tableName !== null ? { tableName } : null,
return getFunctionWatch(
ctx,
credentials,
"_system/cli/queryTable",
() => (tableName !== null ? { tableName } : null),
componentPath,
);
}

Expand All @@ -349,6 +359,7 @@ function getDeplymentEnvVarWatch(
credentials,
"_system/cli/queryEnvironmentVariables",
() => (shouldRetryOnDeploymentEnvVarChange ? {} : null),
undefined,
);
}

Expand All @@ -360,6 +371,7 @@ function getFunctionWatch(
},
functionName: string,
getArgs: () => Record<string, Value> | null,
componentPath: string | undefined,
) {
const [stopPromise, stop] = waitUntilCalled();
return {
Expand All @@ -375,6 +387,7 @@ function getFunctionWatch(
credentials.adminKey,
functionName,
args,
componentPath,
stopPromise,
{
onChange: () => {
Expand Down
7 changes: 6 additions & 1 deletion src/cli/lib/deploy2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ export async function waitForSchema(
return await ctx.crash({
exitCode: 1,
errorType: {
"invalid filesystem or db data": currentStatus.tableName ?? null,
"invalid filesystem or db data": currentStatus.tableName
? {
tableName: currentStatus.tableName,
componentPath: currentStatus.componentPath,
}
: null,
},
printedMessage: null, // TODO - move logging into here
});
Expand Down
6 changes: 5 additions & 1 deletion src/cli/lib/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ async function waitForReadySchema(
return await ctx.crash({
exitCode: 1,
errorType: {
"invalid filesystem or db data": data.schemaState.tableName ?? null,
"invalid filesystem or db data": data.schemaState.tableName
? {
tableName: data.schemaState.tableName,
}
: null,
},
printedMessage: null, // TODO - move logging into here
});
Expand Down
7 changes: 6 additions & 1 deletion src/cli/lib/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ export async function subscribeAndLog(
adminKey: string,
functionName: string,
args: Record<string, Value>,
componentPath: string | undefined,
) {
return subscribe(
ctx,
deploymentUrl,
adminKey,
functionName,
args,
componentPath,
waitForever(),
{
onStart() {
Expand All @@ -156,6 +158,7 @@ export async function subscribe(
adminKey: string,
functionName: string,
args: Record<string, Value>,
componentPath: string | undefined,
until: Promise<unknown>,
callbacks?: {
onStart?: () => void;
Expand All @@ -177,7 +180,9 @@ export async function subscribe(
},
);
client.setAdminAuth(adminKey);
const { unsubscribe } = client.subscribe(functionName, args);
const { unsubscribe } = client.subscribe(functionName, args, {
componentPath,
});

callbacks?.onStart?.();

Expand Down
1 change: 1 addition & 0 deletions src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const run = new Command("run")
adminKey,
functionName,
args,
options.componentPath,
);
}
return await runFunctionAndLog(
Expand Down

0 comments on commit 7770d2e

Please sign in to comment.