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: fix `portless trust` and auto env sync

Sidwebworks3b5edbd39143
diff --git a/packages/dev/src/commands/copy.ts b/packages/dev/src/commands/copy.ts
index f80864bc69..bd134a861d 100644
--- a/packages/dev/src/commands/copy.ts
+++ b/packages/dev/src/commands/copy.ts
@@ -1,4 +1,4 @@
-import { copyFileSync, existsSync, readFileSync } from "node:fs";
+import { copyFileSync, existsSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
import { intro, log, outro } from "@clack/prompts";
import pc from "picocolors";
@@ -6,6 +6,30 @@ import { mainCheckoutRoot } from "../lib/git.js";
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
+// 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);
+ if (mainRoot === cwd) return [];
+
+ const includes = readIncludes(mainRoot);
+ const copied: string[] = [];
+ for (const rel of includes) {
+ const src = join(mainRoot, rel);
+ const dest = join(cwd, rel);
+ if (!existsSync(src)) continue;
+ const srcMtime = statSync(src).mtimeMs;
+ const destMtime = existsSync(dest) ? statSync(dest).mtimeMs : 0;
+ if (destMtime >= srcMtime) continue;
+ copyFileSync(src, dest);
+ copied.push(rel);
+ }
+ return copied;
+}
+
// Copy files listed in package.json#crbn.copy from main checkout into cwd.
export async function copy() {
intro("Carbon · copy");
diff --git a/packages/dev/src/commands/up.ts b/packages/dev/src/commands/up.ts
index f7d658d244..f98e518d8e 100644
--- a/packages/dev/src/commands/up.ts
+++ b/packages/dev/src/commands/up.ts
@@ -43,6 +43,7 @@ import {
} from "../services/portless.js";
import { pickApps } from "../ui/prompts.js";
import { summaryLines } from "../ui/summary.js";
+import { syncStaleCopyFiles } from "./copy.js";
import { down } from "./down.js";
export async function up(opts: { migrate?: boolean; regen?: boolean } = {}) {
@@ -63,6 +64,18 @@ export async function up(opts: { migrate?: boolean; regen?: boolean } = {}) {
persistSlug(root, slug);
log.info(`worktree: ${slug} (project ${projectName(slug)})`);
+ // Auto-heal stale `.env` (and other package.json#crbn.copy entries) from
+ // main checkout. `crbn checkout <existing-branch>` skips do_post_create →
+ // worktrees drift from main when new env vars land. Mtime-gated, so
+ // unchanged files are untouched and local edits made *after* main's last
+ // change are preserved.
+ const refreshed = await syncStaleCopyFiles(root);
+ if (refreshed.length > 0) {
+ log.info(
+ `refreshed ${refreshed.join(", ")} from main checkout (stale vs main)`
+ );
+ }
+
// Outside `tasks` so pnpm progress streams directly when install runs.
const ran = await installDeps(root);
if (ran) log.step("pnpm install");
diff --git a/setup.sh b/setup.sh
index 6f987166e8..7f78bccb2b 100755
--- a/setup.sh
+++ b/setup.sh
@@ -292,6 +292,26 @@ EOF
fi
ok "portless LaunchDaemon installed; proxy starts at boot"
+
+ run_portless_trust
+}
+
+# LaunchDaemon plist runs with --skip-trust, and the CLI's ensureProxyPrivileges
+# only triggers trust as a side-effect of fixing a privilege issue — neither
+# path runs trust on its own. Users who pre-installed portless never end up
+# with the CA in their keychain, so *.dev TLS fails in the browser.
+# `portless trust` is idempotent.
+run_portless_trust() {
+ if [[ "$(uname -s)" != "Darwin" ]]; then
+ return 0
+ fi
+ hdr "Trust portless CA"
+ info "Installs root CA into system keychain so *.dev TLS is trusted."
+ if ! sudo "HOME=$HOME" portless trust; then
+ warn "portless trust failed — browsers may show cert warnings until you run \`sudo portless trust\` manually."
+ return 0
+ fi
+ ok "portless CA trusted"
}
uninstall_proxy_daemon() {