Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(cursor-cloud): docker networking, and dev env (#5814)

teo85150d57bd72
diff --git a/.cursor/cloud-lib.sh b/.cursor/cloud-lib.sh
index 06c1e0ca5a..914960fd68 100755
--- a/.cursor/cloud-lib.sh
+++ b/.cursor/cloud-lib.sh
@@ -111,29 +111,74 @@ ensure_nix_daemon() {
}
ensure_docker_iptables_backend() {
- if [ ! -x /usr/bin/update-alternatives ] \
- || [ ! -x "${DOCKER_IPTABLES_BACKEND}" ] \
+ # Compose puts Postgres/Redis/services on custom bridges. With
+ # bridge-nf-call-iptables=1, that ICC walks iptables FORWARD. Dockerd
+ # programs the legacy table; if iptables-legacy is missing it only
+ # wires docker0, and auth/storage time out reaching postgres.
+ if [ ! -x "${DOCKER_IPTABLES_BACKEND}" ] \
|| [ ! -x "${DOCKER_IP6TABLES_BACKEND}" ]; then
- return 0
+ if [ ! -x /usr/bin/apt-get ]; then
+ echo "cursor-cloud: iptables-legacy is required for Docker ICC" >&2
+ return 1
+ fi
+ echo "cursor-cloud: installing iptables (legacy backend)"
+ sudo DEBIAN_FRONTEND=noninteractive apt-get update -qq
+ sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq iptables
fi
- local desired_backend
- desired_backend="$(readlink -f "${DOCKER_IPTABLES_BACKEND}")"
- local desired_ip6_backend
- desired_ip6_backend="$(readlink -f "${DOCKER_IP6TABLES_BACKEND}")"
- local current_backend
- current_backend="$(readlink -f /etc/alternatives/iptables 2>/dev/null || true)"
+ if [ -x /usr/bin/update-alternatives ]; then
+ local desired_backend
+ desired_backend="$(readlink -f "${DOCKER_IPTABLES_BACKEND}")"
+ local desired_ip6_backend
+ desired_ip6_backend="$(readlink -f "${DOCKER_IP6TABLES_BACKEND}")"
+ local current_backend
+ current_backend="$(readlink -f /etc/alternatives/iptables 2>/dev/null || true)"
+
+ if [ "${current_backend}" != "${desired_backend}" ]; then
+ sudo /usr/bin/update-alternatives \
+ --set iptables "${DOCKER_IPTABLES_BACKEND}"
+ fi
+ if [ "$(readlink -f /etc/alternatives/ip6tables 2>/dev/null || true)" \
+ != "${desired_ip6_backend}" ]; then
+ sudo /usr/bin/update-alternatives \
+ --set ip6tables "${DOCKER_IP6TABLES_BACKEND}"
+ fi
+ fi
+}
- # Reconcile the backend with a preserved legacy FORWARD policy.
- if [ "${current_backend}" != "${desired_backend}" ]; then
- sudo /usr/bin/update-alternatives \
- --set iptables "${DOCKER_IPTABLES_BACKEND}"
+# Allow container-to-container traffic on every Docker bridge. Needed when
+# networks were created before iptables-legacy existed (dockerd then only
+# programmed docker0). Safe to repeat: each rule is added only if missing.
+ensure_docker_bridge_forwarding() {
+ if [ ! -x "${DOCKER_IPTABLES_BACKEND}" ]; then
+ return 0
fi
- if [ "$(readlink -f /etc/alternatives/ip6tables 2>/dev/null || true)" \
- != "${desired_ip6_backend}" ]; then
- sudo /usr/bin/update-alternatives \
- --set ip6tables "${DOCKER_IP6TABLES_BACKEND}"
+ if ! sudo "${DOCKER_IPTABLES_BACKEND}" -nL DOCKER-FORWARD >/dev/null 2>&1; then
+ return 0
fi
+
+ local br name
+ for br in /sys/class/net/br-*; do
+ [ -e "${br}" ] || continue
+ name="$(basename "${br}")"
+ if ! sudo "${DOCKER_IPTABLES_BACKEND}" -C DOCKER-FORWARD \
+ -i "${name}" -j ACCEPT >/dev/null 2>&1; then
+ sudo "${DOCKER_IPTABLES_BACKEND}" -A DOCKER-FORWARD \
+ -i "${name}" -j ACCEPT
+ fi
+ if sudo "${DOCKER_IPTABLES_BACKEND}" -nL DOCKER-CT >/dev/null 2>&1 \
+ && ! sudo "${DOCKER_IPTABLES_BACKEND}" -C DOCKER-CT -o "${name}" \
+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT >/dev/null 2>&1; then
+ sudo "${DOCKER_IPTABLES_BACKEND}" -A DOCKER-CT -o "${name}" \
+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT || true
+ fi
+ if sudo "${DOCKER_IPTABLES_BACKEND}" -nL DOCKER-BRIDGE >/dev/null 2>&1 \
+ && ! sudo "${DOCKER_IPTABLES_BACKEND}" -C DOCKER-BRIDGE \
+ -o "${name}" -j DOCKER >/dev/null 2>&1; then
+ sudo "${DOCKER_IPTABLES_BACKEND}" -A DOCKER-BRIDGE \
+ -o "${name}" -j DOCKER || true
+ fi
+ done
}
ensure_dockerd() {
@@ -161,6 +206,7 @@ ensure_dockerd() {
if [ -S "${DOCKER_SOCK}" ]; then
sudo chmod 666 "${DOCKER_SOCK}"
if docker info >/dev/null 2>&1; then
+ ensure_docker_bridge_forwarding
return 0
fi
fi
@@ -174,6 +220,7 @@ ensure_dockerd() {
if [ -S "${DOCKER_SOCK}" ]; then
sudo chmod 666 "${DOCKER_SOCK}"
if docker info >/dev/null 2>&1; then
+ ensure_docker_bridge_forwarding
return 0
fi
fi
diff --git a/.cursor/infra.sh b/.cursor/infra.sh
index 16115b5810..0870e53c2c 100644
--- a/.cursor/infra.sh
+++ b/.cursor/infra.sh
@@ -19,6 +19,7 @@ ensure_persistent_caches
\cd "${WORKSPACE_ROOT}"
just run_dbs -d
+ensure_docker_bridge_forwarding
pg_isready -h 127.0.0.1 -p 5432
diff --git a/.cursor/stack.sh b/.cursor/stack.sh
index fa6177f42a..b61db38653 100755
--- a/.cursor/stack.sh
+++ b/.cursor/stack.sh
@@ -19,5 +19,6 @@ fi
\cd "${WORKSPACE_ROOT}"
build_local_stack_binaries
just stack up --no-doppler --build-aux-services --binaries-dir "${LOCAL_STACK_BINS}/bin"
+ensure_docker_bridge_forwarding
echo "cursor-cloud stack: app ready"
diff --git a/apps/web/scripts/keep-import-meta-dev.test.ts b/apps/web/scripts/keep-import-meta-dev.test.ts
new file mode 100644
index 0000000000..600f6835e5
--- /dev/null
+++ b/apps/web/scripts/keep-import-meta-dev.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it } from 'vitest';
+import { keepImportMetaDev } from './keep-import-meta-dev';
+
+describe('keepImportMetaDev', () => {
+ it('keeps DEV for local-backend static builds (stack up, Fly preview)', () => {
+ expect(
+ keepImportMetaDev({
+ command: 'build',
+ mode: 'development',
+ localBackendOrigin: 'same-origin',
+ })
+ ).toBe(true);
+ });
+
+ it('does not keep DEV for hosted build-dev', () => {
+ expect(
+ keepImportMetaDev({
+ command: 'build',
+ mode: 'development',
+ localBackendOrigin: undefined,
+ })
+ ).toBe(false);
+ });
+
+ it('does not keep DEV for vite serve (already true natively)', () => {
+ expect(
+ keepImportMetaDev({
+ command: 'serve',
+ mode: 'development',
+ localBackendOrigin: 'http://localhost:8090',
+ })
+ ).toBe(false);
+ });
+
+ it('refuses a local-backend origin on a non-development MODE', () => {
+ expect(() =>
+ keepImportMetaDev({
+ command: 'build',
+ mode: 'production',
+ localBackendOrigin: 'same-origin',
+ })
+ ).toThrow(/MODE=production/);
+ });
+});
diff --git a/apps/web/scripts/keep-import-meta-dev.ts b/apps/web/scripts/keep-import-meta-dev.ts
new file mode 100644
index 0000000000..3bfd74bc93
--- /dev/null
+++ b/apps/web/scripts/keep-import-meta-dev.ts
@@ -0,0 +1,23 @@
+/**
+ * Whether a `vite build` should keep `import.meta.env.DEV` true.
+ *
+ * Vite compiles DEV from NODE_ENV, not MODE, so
+ * `MODE=development NODE_ENV=production vite build` would otherwise ship
+ * DEV=false. Local-backend bundles (`just stack up`, Fly preview) already set
+ * `VITE_LOCAL_BACKEND_ORIGIN`; hosted `just build-dev` / staging / prod do not.
+ *
+ * `vite serve` already has DEV=true, so this only applies to `build`.
+ */
+export function keepImportMetaDev(opts: {
+ command: string;
+ mode: string;
+ localBackendOrigin: string | undefined;
+}): boolean {
+ const hasOrigin = Boolean(opts.localBackendOrigin);
+ if (hasOrigin && opts.mode !== 'development') {
+ throw new Error(
+ `VITE_LOCAL_BACKEND_ORIGIN is set on MODE=${opts.mode}; refusing to keep import.meta.env.DEV`
+ );
+ }
+ return opts.command === 'build' && opts.mode === 'development' && hasOrigin;
+}
diff --git a/apps/web/src/lib/core/constant/featureFlags.ts b/apps/web/src/lib/core/constant/featureFlags.ts
index b75277236b..a9abfe7899 100644
--- a/apps/web/src/lib/core/constant/featureFlags.ts
+++ b/apps/web/src/lib/core/constant/featureFlags.ts
@@ -4,6 +4,10 @@ import { analytics } from '@app/lib/analytics';
* This constant reflects whether the app is running locally with hot reload enabled
*
* @returns true in bun run dev, false otherwise
+ *
+ * Distinct from `import.meta.env.DEV` (true under vite serve *and* local-backend
+ * static bundles) and `DEV_MODE_ENV` (true whenever MODE=development, including
+ * dev.macro.com).
*/
export const LOCAL_ONLY = !!import.meta.hot;
diff --git a/apps/web/vite.base.ts b/apps/web/vite.base.ts
index c0914692ec..1f57061906 100644
--- a/apps/web/vite.base.ts
+++ b/apps/web/vite.base.ts
@@ -8,9 +8,9 @@ import solid from 'vite-plugin-solid';
import solidSvg from 'vite-plugin-solid-svg';
import wasm from 'vite-plugin-wasm';
import tsconfigpaths from 'vite-tsconfig-paths';
-
// @ts-ignore
import { version } from './package.json';
+import { keepImportMetaDev } from './scripts/keep-import-meta-dev';
function readShortSha(): string {
try {
@@ -230,6 +230,15 @@ function getAssetsPath(mode: string, command: string): string {
}
function defineEnv(mode: string, command: string) {
+ // `vite build` compiles DEV from NODE_ENV, not MODE. Local-backend static
+ // bundles already set VITE_LOCAL_BACKEND_ORIGIN (stack up, Fly preview);
+ // keep DEV so those artifacts match `just run_local` (vite serve). Hosted
+ // `just build-dev` does not set the origin, so DEV stays false.
+ const keepDev = keepImportMetaDev({
+ command,
+ mode,
+ localBackendOrigin: process.env.VITE_LOCAL_BACKEND_ORIGIN,
+ });
return {
'import.meta.env.__APP_VERSION__': JSON.stringify(appVersion),
'import.meta.env.ASSETS_PATH': JSON.stringify(getAssetsPath(mode, command)),
@@ -238,5 +247,11 @@ function defineEnv(mode: string, command: string) {
'import.meta.env.__GIT_BRANCH__': JSON.stringify(
command === 'serve' ? readGitBranch() : ''
),
+ ...(keepDev
+ ? {
+ 'import.meta.env.DEV': true,
+ 'import.meta.env.PROD': false,
+ }
+ : {}),
};
}
diff --git a/tooling/xtask/crates/xtask_local/src/local/frontend.rs b/tooling/xtask/crates/xtask_local/src/local/frontend.rs
index c55509cfec..87f2982cfb 100644
--- a/tooling/xtask/crates/xtask_local/src/local/frontend.rs
+++ b/tooling/xtask/crates/xtask_local/src/local/frontend.rs
@@ -45,6 +45,13 @@ pub fn static_dir(instance: &Instance) -> std::path::PathBuf {
/// (dev-mode bundle, production optimizations), except the backend origin is
/// the `same-origin` sentinel — resolved from `location.origin` at runtime — so
/// the one bundle works on localhost and through any tunnel/preview hostname.
+///
+/// Setting `VITE_LOCAL_BACKEND_ORIGIN` also keeps `import.meta.env.DEV` true
+/// in the static bundle (see `keepImportMetaDev` in apps/web/scripts). `vite build`
+/// otherwise compiles DEV from `NODE_ENV=production`, which drops local-only
+/// paths such as passwordless auto-login (`just run_local` uses `vite serve`,
+/// where DEV is already true). Fly preview sets the same origin env, so it
+/// gets the override without a second flag.
pub fn build_static(
stage: &Stage,
instance: &Instance,