Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

crbn: local dev behaviour

Sidwebworksed04c4e55fcb
diff --git a/package.json b/package.json
index d4f609f4d6..be454f8269 100755
--- a/package.json
+++ b/package.json
@@ -44,7 +44,7 @@
"db:seed:dev": "pnpm --filter @carbon/database db:seed:dev --",
"db:types": "tsx scripts/generate-db-types.ts",
"deploy": "turbo run deploy",
- "dev": "crbn up",
+ "dev": "crbn up --no-portless",
"dev:academy": "turbo run dev --filter=./apps/academy --filter=./packages/",
"dev:packages": "turbo run dev --filter=./packages/*",
"dev:starter": "turbo run dev --filter=./apps/starter --filter=./packages/",
diff --git a/packages/dev/bin/crbn b/packages/dev/bin/crbn
index a8c9dc0f1e..621b68b773 100755
--- a/packages/dev/bin/crbn
+++ b/packages/dev/bin/crbn
@@ -238,6 +238,18 @@ cmd_checkout() {
return 0
fi
+ # Never create a separate worktree for the main checkout's branch — just cd
+ # into the repo root. `git worktree add main` would detach and confuse.
+ local main_branch
+ main_branch="$(git -C "$root" symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null || echo "origin/main")"
+ main_branch="${main_branch#origin/}"
+ if [[ "$name" == "$main_branch" ]]; then
+ info "main checkout at ${C_DIM}${root}${C_RESET}"
+ printf "cd %q\n" "$root"
+ maybe_up "$root" "$do_up"
+ return 0
+ fi
+
# Need to materialize a worktree. Resolve the ref locally or via origin.
local resolved=""
if (( local_exists )); then
@@ -330,13 +342,13 @@ _checkout_pr() {
maybe_up "$target" "$do_up"
}
-# crbn copy inside the new worktree (so it reads package.json#crbn.copy from
-# the main checkout via --git-common-dir). All output goes to stderr to keep
-# stdout reserved for the eval'd `cd` line.
+# crbn env sync inside the new worktree (so it reads package.json#crbn.copy
+# from the main checkout via --git-common-dir). All output goes to stderr to
+# keep stdout reserved for the eval'd `cd` line.
do_post_create() {
local target="$1"
- if ! ( cd "$target" && delegate_ts copy >&2 ); then
- warn "copy step failed (worktree was still created)"
+ if ! ( cd "$target" && delegate_ts env sync >&2 ); then
+ warn "env sync step failed (worktree was still created)"
fi
}
@@ -368,11 +380,13 @@ crbn — Carbon dev CLI
crbn checkout <pr-number> Fetch the PR head from GitHub into a
new `pr-<num>` branch + worktree.
crbn new Interactive: pick branch + base ref,
- create worktree, optionally copy .env.
+ create worktree, sync env from main.
crbn list Show all worktrees with stack status.
crbn remove Pick a worktree to delete (with stack
teardown).
- crbn copy Sync files (package.json#crbn.copy)
+ crbn copy <file...> Copy file(s) from main checkout into
+ current worktree.
+ crbn env sync Sync files listed in package.json#crbn.copy
from main checkout into cwd.
Stack:
@@ -400,7 +414,7 @@ case "$cmd" in
checkout)
cmd_checkout "$@"
;;
- copy|up|down|reset|status|migrate|new|list|remove|complete)
+ copy|env|up|down|reset|status|migrate|new|list|remove|complete)
delegate_ts "$cmd" "$@"
;;
*)
diff --git a/packages/dev/src/commands/copy.ts b/packages/dev/src/commands/copy.ts
index b572fa5401..313234e7fa 100644
--- a/packages/dev/src/commands/copy.ts
+++ b/packages/dev/src/commands/copy.ts
@@ -1,6 +1,6 @@
-import { copyFileSync, existsSync, readFileSync } from "node:fs";
+import { copyFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
import { intro, log, outro } from "@clack/prompts";
-import { join } from "pathe";
+import { dirname, join, relative } from "pathe";
import pc from "picocolors";
import { mainCheckoutRoot } from "../git.js";
import { isAtLeastAsNew } from "../helpers.js";
@@ -10,7 +10,7 @@ const DEFAULT_INCLUDES = [".env"];
// Auto-heal stale copy files: when main checkout's `.env` (or any file listed
// in package.json#crbn.copy) is newer than the worktree's, refresh it.
// `crbn checkout <existing-branch>` fast-paths past `do_post_create`, so
-// existing worktrees never re-run `crbn copy` and drift from main when new
+// existing worktrees never re-run `crbn env sync` and drift from main when new
// env vars/secrets land there. Returns the list of files actually copied.
export async function syncStaleCopyFiles(cwd: string): Promise<string[]> {
const mainRoot = await mainCheckoutRoot(cwd);
@@ -29,14 +29,14 @@ export async function syncStaleCopyFiles(cwd: string): Promise<string[]> {
return copied;
}
-// Copy files listed in package.json#crbn.copy from main checkout into cwd.
-export async function copy() {
- intro("Carbon · copy");
+// `crbn env sync` — copy files listed in package.json#crbn.copy from main.
+export async function envSync() {
+ intro("Carbon · env sync");
const cwd = process.cwd();
const mainRoot = await mainCheckoutRoot();
if (mainRoot === cwd) {
- log.warn("already in main checkout — nothing to copy");
+ log.warn("already in main checkout — nothing to sync");
outro("");
return;
}
@@ -55,6 +55,45 @@ export async function copy() {
copied++;
}
+ outro(
+ `${copied} file${copied === 1 ? "" : "s"} synced from ${pc.dim(mainRoot)}`
+ );
+}
+
+// `crbn copy <file> [file...]` — copy arbitrary files from main checkout.
+export async function copy(files: string[]) {
+ intro("Carbon · copy");
+
+ const cwd = process.cwd();
+ const mainRoot = await mainCheckoutRoot();
+ if (mainRoot === cwd) {
+ log.warn("already in main checkout — nothing to copy");
+ outro("");
+ return;
+ }
+
+ if (files.length === 0) {
+ log.error("specify at least one file to copy from main checkout");
+ outro("");
+ process.exit(1);
+ }
+
+ let copied = 0;
+ for (const file of files) {
+ // Resolve relative to worktree root so paths are consistent.
+ const rel = relative(cwd, join(cwd, file));
+ const src = join(mainRoot, rel);
+ const dest = join(cwd, rel);
+ if (!existsSync(src)) {
+ log.warn(`${pc.dim(rel)} missing in main checkout — skipped`);
+ continue;
+ }
+ mkdirSync(dirname(dest), { recursive: true });
+ copyFileSync(src, dest);
+ log.info(`${pc.green("✓")} ${rel}`);
+ copied++;
+ }
+
outro(
`${copied} file${copied === 1 ? "" : "s"} copied from ${pc.dim(mainRoot)}`
);
diff --git a/packages/dev/src/commands/list.ts b/packages/dev/src/commands/list.ts
index 5f7cfa006a..4112f82814 100644
--- a/packages/dev/src/commands/list.ts
+++ b/packages/dev/src/commands/list.ts
@@ -3,7 +3,7 @@ import pc from "picocolors";
import { listWorktrees as gitListWorktrees } from "../git.js";
import { dockerProjectStates } from "../services/compose.js";
import { worktreesTable } from "../ui.js";
-import { listSlugs } from "../worktree.js";
+import { listSlugs, projectName } from "../worktree.js";
export async function listWorktrees() {
intro("Carbon · worktrees");
@@ -18,7 +18,7 @@ export async function listWorktrees() {
const rows = wts.map((w) => {
const slug = slugForPath(w.path, registry);
const dockerState = slug
- ? (dockerStates.get(`carbon-${slug}`) ?? null)
+ ? (dockerStates.get(projectName(slug)) ?? null)
: null;
return {
path: w.path,
diff --git a/packages/dev/src/commands/remove.ts b/packages/dev/src/commands/remove.ts
index 0bd81d57e5..265dc50f2e 100644
--- a/packages/dev/src/commands/remove.ts
+++ b/packages/dev/src/commands/remove.ts
@@ -9,6 +9,7 @@ import {
} from "@clack/prompts";
import pc from "picocolors";
import {
+ deleteBranch,
listWorktrees as gitListWorktrees,
isDirty,
mainCheckoutRoot,
@@ -21,9 +22,10 @@ import {
pruneStaleRoutes,
unregisterAliases
} from "../services/portless.js";
-import { getSlot, listSlugs, removeSlot } from "../worktree.js";
+import { getSlot, listSlugs, projectName, removeSlot } from "../worktree.js";
-export async function removeWorktreeCmd() {
+export async function removeWorktreeCmd(opts?: { prune?: boolean }) {
+ const pruneBranches = opts?.prune === true;
intro("Carbon · remove worktree");
const wtsAll = await gitListWorktrees();
@@ -62,7 +64,7 @@ export async function removeWorktreeCmd() {
target,
dirty: await isDirty(target.path),
slug,
- projectLabel: slug ? `carbon-${slug}` : null,
+ projectLabel: slug ? projectName(slug) : null,
slotInfo: slug ? getSlot(slug) : null,
branchPrefix: slug ? branchToPrefix(target.branch, slug) : null
};
@@ -126,6 +128,11 @@ export async function removeWorktreeCmd() {
progress(`${label}: removing worktree`);
await removeWorktree(target.path, dirty);
+ if (pruneBranches && target.branch) {
+ progress(`${label}: deleting branch`);
+ await deleteBranch(target.branch);
+ }
+
if (slug) removeSlot(slug);
done++;
diff --git a/packages/dev/src/commands/up.ts b/packages/dev/src/commands/up.ts
index fb80f3216d..f88682d46d 100644
--- a/packages/dev/src/commands/up.ts
+++ b/packages/dev/src/commands/up.ts
@@ -217,6 +217,13 @@ async function provisionSlot(
// Always resolve own slot so PORT_ERP/PORT_MES are claimed for this
// worktree and won't collide with the borrowed stack's running dev servers.
const ownSlot = await resolveSlot(slug, root);
+ // Pin well-known ports in localhost mode so URLs are predictable and
+ // OAuth redirect URIs can be registered once in Google/Azure console.
+ if (!portless && !borrowedEntry) {
+ ownSlot.ports.PORT_API = 54321;
+ ownSlot.ports.PORT_ERP = 3000;
+ ownSlot.ports.PORT_MES = 3001;
+ }
const slot = borrowedEntry
? {
// Backend ports (DB, API, Studio, Inbucket, Inngest) come from the
diff --git a/packages/dev/src/env.ts b/packages/dev/src/env.ts
index cfcd5d1368..faff0ccd3c 100644
--- a/packages/dev/src/env.ts
+++ b/packages/dev/src/env.ts
@@ -59,13 +59,17 @@ export function renderEnv(opts: {
lines.push(`SUPABASE_JWT_SECRET=${jwt.secret}`);
lines.push(`SUPABASE_ANON_KEY=${jwt.anonKey}`);
lines.push(`SUPABASE_SERVICE_ROLE_KEY=${jwt.serviceKey}`);
- const apiBase = portless ? `https://${host("api")}` : local(ports.PORT_API);
- // OAuth callback must match the URL scheme apps actually use.
+ // OAuth callback: portless uses the shared api.carbon.dev alias; localhost
+ // mode uses the well-known Supabase default port so the redirect URI is
+ // predictable and can be registered in Google/Azure console once.
+ const oauthBase = portless
+ ? "https://api.carbon.dev"
+ : "http://localhost:54321";
lines.push(
- `SUPABASE_AUTH_EXTERNAL_GOOGLE_REDIRECT_URI=${portless ? "https://api.carbon.dev" : apiBase}/auth/v1/callback`
+ `SUPABASE_AUTH_EXTERNAL_GOOGLE_REDIRECT_URI=${oauthBase}/auth/v1/callback`
);
lines.push(
- `SUPABASE_AUTH_EXTERNAL_AZURE_REDIRECT_URI=${portless ? "https://api.carbon.dev" : apiBase}/auth/v1/callback`
+ `SUPABASE_AUTH_EXTERNAL_AZURE_REDIRECT_URI=${oauthBase}/auth/v1/callback`
);
lines.push("");
lines.push("# Aux services");
diff --git a/packages/dev/src/main.ts b/packages/dev/src/main.ts
index 3fe324a830..3f864d3b15 100644
--- a/packages/dev/src/main.ts
+++ b/packages/dev/src/main.ts
@@ -1,5 +1,5 @@
import { defineCommand, runMain } from "citty";
-import { copy } from "./commands/copy.js";
+import { copy, envSync } from "./commands/copy.js";
import { down } from "./commands/down.js";
import { listWorktrees } from "./commands/list.js";
import { migrate } from "./commands/migrate.js";
@@ -100,7 +100,10 @@ const main = defineCommand({
description: "Branch name (pre-fills the prompt)"
}
},
- run: ({ args }) => newWorktree({ branch: args.branch })
+ run: ({ args }) =>
+ newWorktree({
+ branch: typeof args.branch === "string" ? args.branch : undefined
+ })
}),
list: defineCommand({
meta: { description: "List worktrees with stack status" },
@@ -108,14 +111,46 @@ const main = defineCommand({
}),
remove: defineCommand({
meta: { description: "Pick a worktree to delete (with stack teardown)" },
- run: () => removeWorktreeCmd()
+ args: {
+ prune: {
+ type: "boolean",
+ default: false,
+ description: "Also delete the git branch after removing the worktree"
+ }
+ },
+ run: ({ args }) => removeWorktreeCmd({ prune: args.prune === true })
}),
copy: defineCommand({
meta: {
- description:
- "Copy files listed in package.json#crbn.copy from main checkout into cwd"
+ description: "Copy file(s) from main checkout into current worktree"
},
- run: () => copy()
+ args: {
+ files: {
+ type: "positional",
+ required: true,
+ description: "File path(s) to copy from main checkout"
+ }
+ },
+ run: ({ args }) => {
+ const files = Array.isArray(args.files)
+ ? args.files.filter((f): f is string => typeof f === "string")
+ : typeof args.files === "string"
+ ? [args.files]
+ : [];
+ return copy(files);
+ }
+ }),
+ env: defineCommand({
+ meta: { description: "Environment file management" },
+ subCommands: {
+ sync: defineCommand({
+ meta: {
+ description:
+ "Sync files listed in package.json#crbn.copy from main checkout"
+ },
+ run: () => envSync()
+ })
+ }
}),
// Stubs so shell completion lists these — the bash router (`bin/crbn`)
// intercepts them before tsx is invoked. Direct invocation lands here.
diff --git a/packages/dev/src/ui.ts b/packages/dev/src/ui.ts
index f3264b3820..2d70247c1c 100644
--- a/packages/dev/src/ui.ts
+++ b/packages/dev/src/ui.ts
@@ -2,7 +2,12 @@ import Table from "cli-table3";
import pc from "picocolors";
import { type AppId, TLD } from "./constants.js";
import type { Container } from "./services/compose.js";
-import { PORT_NAMES, type PortMap, SHARED_REDIS_PORT } from "./worktree.js";
+import {
+ PORT_NAMES,
+ type PortMap,
+ projectName,
+ SHARED_REDIS_PORT
+} from "./worktree.js";
// ---------------------------------------------------------------------------
// Tables (status / list)
@@ -72,7 +77,7 @@ export function worktreesTable(
...BASE_STYLE
});
for (const r of rows) {
- const project = r.slug ? `carbon-${r.slug}` : "—";
+ const project = r.slug ? projectName(r.slug) : "—";
const stack = !r.slug
? pc.gray("not initialized")
: r.dockerState === "running"
diff --git a/packages/dev/src/worktree.ts b/packages/dev/src/worktree.ts
index 0ad3f99804..1dec0efb99 100644
--- a/packages/dev/src/worktree.ts
+++ b/packages/dev/src/worktree.ts
@@ -72,7 +72,7 @@ export async function getWorktreeRoot(): Promise<string> {
}
export function projectName(slug: string): string {
- return `carbon-${slug}`;
+ return `crbn-${slug}`;
}
// Resolve symlinks + normalize separators / trailing slashes so two strings