diff --git a/motoko/cert-var/frontend/src/actor.js b/motoko/cert-var/frontend/src/actor.js index 5c9079f4c2..d3098a6c28 100644 --- a/motoko/cert-var/frontend/src/actor.js +++ b/motoko/cert-var/frontend/src/actor.js @@ -7,14 +7,11 @@ import { createActor } from "./bindings/backend"; // via Set-Cookie header (see vite.config.js). const canisterEnv = safeGetCanisterEnv(); -// Resolve canister ID: cookie (icp-cli + dev server) → env var (dfx build-time) -const canisterId = - canisterEnv?.["PUBLIC_CANISTER_ID:backend"] ?? - process.env.CANISTER_ID_BACKEND; +const canisterId = canisterEnv?.["PUBLIC_CANISTER_ID:backend"]; if (!canisterId) { throw new Error( - "Canister ID for 'backend' not found. Run 'icp deploy' or 'dfx deploy' first." + "Canister ID for 'backend' not found. Run 'icp deploy' first." ); } diff --git a/motoko/cert-var/frontend/vite.config.js b/motoko/cert-var/frontend/vite.config.js index 4e49c4705d..c2d473eec3 100644 --- a/motoko/cert-var/frontend/vite.config.js +++ b/motoko/cert-var/frontend/vite.config.js @@ -1,70 +1,60 @@ -import { defineConfig, loadEnv } from "vite"; +import { defineConfig } from "vite"; import { execSync } from "child_process"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { - // Try icp-cli first + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } - // Try dfx + let canisterId; try { - const pingResult = JSON.parse( - execSync("dfx ping", { encoding: "utf-8", stdio: "pipe" }) + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` ); - const rootKeyHex = Buffer.from(pingResult.root_key).toString("hex"); - const canisterId = execSync("dfx canister id backend", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${rootKeyHex}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { - target: "http://127.0.0.1:4943", - changeOrigin: true, - }, - }, - host: "127.0.0.1", - }; - } catch {} + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy\nor:\n dfx start --background && dfx deploy" - ); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } -export default defineConfig(({ command, mode }) => { - // dfx generates ../.env with CANISTER_ID_* vars on deploy. Bake them into the - // bundle so actor.js can fall back to them when the ic_env cookie does not - // contain canister IDs (dfx does not inject PUBLIC_CANISTER_ID:* env vars - // into the asset canister, unlike icp-cli). - const env = loadEnv(mode, "..", ["CANISTER_"]); - +export default defineConfig(({ command }) => { return { base: "./", plugins: [ @@ -73,11 +63,6 @@ export default defineConfig(({ command, mode }) => { outDir: "./src/bindings", }), ], - define: { - "process.env.CANISTER_ID_BACKEND": JSON.stringify( - env.CANISTER_ID_BACKEND - ), - }, optimizeDeps: { esbuildOptions: { define: { global: "globalThis" } }, }, diff --git a/motoko/daily_planner/frontend/vite.config.js b/motoko/daily_planner/frontend/vite.config.js index 1b9599b191..e62b05d93d 100644 --- a/motoko/daily_planner/frontend/vite.config.js +++ b/motoko/daily_planner/frontend/vite.config.js @@ -3,33 +3,56 @@ import { execSync } from "child_process"; import react from "@vitejs/plugin-react"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } export default defineConfig(({ command }) => { diff --git a/motoko/evm_block_explorer/frontend/vite.config.js b/motoko/evm_block_explorer/frontend/vite.config.js index faf4c2b869..75825dac6d 100644 --- a/motoko/evm_block_explorer/frontend/vite.config.js +++ b/motoko/evm_block_explorer/frontend/vite.config.js @@ -3,6 +3,58 @@ import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import react from "@vitejs/plugin-react"; import { execSync } from "child_process"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + +function getDevServerConfig() { + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } + + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; +} + export default defineConfig(({ command }) => { const plugins = [ react(), @@ -16,43 +68,8 @@ export default defineConfig(({ command }) => { return { plugins }; } - const environment = process.env.ICP_ENVIRONMENT || "local"; - const CANISTER_NAME = "backend"; - - const networkStatus = JSON.parse( - execSync(`icp network status -e ${environment} --json`, { encoding: "utf-8" }) - ); - const rootKey = networkStatus.root_key; - const proxyTarget = networkStatus.api_url; - - let canisterId; - try { - canisterId = execSync( - `icp canister status ${CANISTER_NAME} -e ${environment} -i`, - { encoding: "utf-8" } - ).trim(); - } catch { - console.error(` - Backend canister "${CANISTER_NAME}" not found in environment "${environment}" - - Before running the dev server, deploy the backend canister: - - icp deploy ${CANISTER_NAME} -e ${environment} - `); - process.exit(1); - } - return { plugins, - server: { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}&ic_root_key=${rootKey}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: proxyTarget, changeOrigin: true }, - }, - }, + server: getDevServerConfig(), }; }); diff --git a/motoko/filevault/frontend/src/actor.js b/motoko/filevault/frontend/src/actor.js index 0f364d30ff..e35f888b2d 100644 --- a/motoko/filevault/frontend/src/actor.js +++ b/motoko/filevault/frontend/src/actor.js @@ -3,9 +3,7 @@ import { createActor } from "./bindings/backend"; const canisterEnv = safeGetCanisterEnv(); -const canisterId = - canisterEnv?.["PUBLIC_CANISTER_ID:backend"] ?? - process.env.CANISTER_ID_BACKEND; +const canisterId = canisterEnv?.["PUBLIC_CANISTER_ID:backend"]; if (!canisterId) { throw new Error( diff --git a/motoko/filevault/frontend/vite.config.js b/motoko/filevault/frontend/vite.config.js index 0e61e065ac..46d000e42b 100644 --- a/motoko/filevault/frontend/vite.config.js +++ b/motoko/filevault/frontend/vite.config.js @@ -1,40 +1,62 @@ -import { defineConfig, loadEnv } from "vite"; +import { defineConfig } from "vite"; import { execSync } from "child_process"; import react from "@vitejs/plugin-react"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - replicaPort: "8000", - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + replicaPort: new URL(networkStatus.api_url).port, + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } -export default defineConfig(({ command, mode }) => { - const env = loadEnv(mode, "..", ["CANISTER_"]); +export default defineConfig(({ command }) => { const devConfig = command === "serve" ? getDevServerConfig() : undefined; return { @@ -47,9 +69,6 @@ export default defineConfig(({ command, mode }) => { }), ], define: { - "process.env.CANISTER_ID_BACKEND": JSON.stringify( - env.CANISTER_ID_BACKEND - ), "process.env.REPLICA_PORT": JSON.stringify(devConfig?.replicaPort ?? ""), }, optimizeDeps: { diff --git a/motoko/flying_ninja/frontend/vite.config.js b/motoko/flying_ninja/frontend/vite.config.js index d1495b37ce..a9edbaa764 100644 --- a/motoko/flying_ninja/frontend/vite.config.js +++ b/motoko/flying_ninja/frontend/vite.config.js @@ -3,34 +3,56 @@ import react from "@vitejs/plugin-react"; import { execSync } from "child_process"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { - // Try icp-cli first + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } export default defineConfig(({ command }) => { diff --git a/motoko/hello_world/frontend/vite.config.js b/motoko/hello_world/frontend/vite.config.js index d804fa696d..c0f5f4f211 100644 --- a/motoko/hello_world/frontend/vite.config.js +++ b/motoko/hello_world/frontend/vite.config.js @@ -2,66 +2,73 @@ import { defineConfig } from "vite"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import { execSync } from "child_process"; -export default defineConfig(({ command }) => { - const plugins = [ - icpBindgen({ - didFile: "../backend/backend.did", - outDir: "./src/bindings", - }), - ]; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; - // If we're only building this is enough - if (command !== "serve") { - return { plugins }; +function getDevServerConfig() { + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); } - // If we're running the local npm dev server, we're going to look up the - // local network's root key and the relevant canister ids. - const environment = process.env.ICP_ENVIRONMENT || "local"; - const CANISTER_NAME = "backend"; - - const networkStatus = JSON.parse( - execSync(`icp network status -e ${environment} --json`, { - encoding: "utf-8", - }) - ); - const rootKey = networkStatus.root_key; - const proxyTarget = networkStatus.api_url; - - // Backend must be deployed before starting dev server let canisterId; try { canisterId = execSync( - `icp canister status ${CANISTER_NAME} -e ${environment} -i`, - { encoding: "utf-8" } + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } ).trim(); } catch { - console.error(` - Backend canister "${CANISTER_NAME}" not found in environment "${environment}" - - Before running the dev server, deploy the backend canister: - - icp deploy ${CANISTER_NAME} -e ${environment} - `); + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); process.exit(1); } - const server = { + return { headers: { "Set-Cookie": `ic_env=${encodeURIComponent( - `PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}&ic_root_key=${rootKey}` + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` )}; SameSite=Lax;`, }, proxy: { - "/api": { - target: proxyTarget, - changeOrigin: true, - }, + "/api": { target: networkStatus.api_url, changeOrigin: true }, }, }; +} + +export default defineConfig(({ command }) => { + const plugins = [ + icpBindgen({ + didFile: "../backend/backend.did", + outDir: "./src/bindings", + }), + ]; + + // If we're only building this is enough + if (command !== "serve") { + return { plugins }; + } return { plugins, - server, + server: getDevServerConfig(), }; }); diff --git a/motoko/llm_chatbot/frontend/vite.config.js b/motoko/llm_chatbot/frontend/vite.config.js index be7ec53902..da95039aa5 100644 --- a/motoko/llm_chatbot/frontend/vite.config.js +++ b/motoko/llm_chatbot/frontend/vite.config.js @@ -3,65 +3,75 @@ import react from "@vitejs/plugin-react"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import { execSync } from "child_process"; -export default defineConfig(({ command }) => { - const plugins = [ - react(), - icpBindgen({ - didFile: "../backend/backend.did", - outDir: "./src/bindings", - }), - ]; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; - // Build only — no dev-server setup needed - if (command !== "serve") { - return { plugins }; +function getDevServerConfig() { + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); } - // Dev server: look up the local network root key and backend canister ID - const environment = process.env.ICP_ENVIRONMENT || "local"; - const CANISTER_NAME = "backend"; - - const networkStatus = JSON.parse( - execSync(`icp network status -e ${environment} --json`, { - encoding: "utf-8", - }) - ); - const rootKey = networkStatus.root_key; - const proxyTarget = networkStatus.api_url; - let canisterId; try { canisterId = execSync( - `icp canister status ${CANISTER_NAME} -e ${environment} -i`, - { encoding: "utf-8" } + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } ).trim(); } catch { - console.error(` - Backend canister "${CANISTER_NAME}" not found in environment "${environment}" - - Before running the dev server, deploy the backend canister: - - icp deploy ${CANISTER_NAME} -e ${environment} - `); + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); process.exit(1); } - const server = { + return { headers: { "Set-Cookie": `ic_env=${encodeURIComponent( - `PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}&ic_root_key=${rootKey}` + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` )}; SameSite=Lax;`, }, proxy: { - "/api": { - target: proxyTarget, - changeOrigin: true, - }, + "/api": { target: networkStatus.api_url, changeOrigin: true }, }, }; +} +export default defineConfig(({ command }) => { + const plugins = [ + react(), + icpBindgen({ + didFile: "../backend/backend.did", + outDir: "./src/bindings", + }), + ]; + + // Build only — no dev-server setup needed + if (command !== "serve") { + return { plugins }; + } + + // Dev server: look up the local network root key and backend canister ID return { plugins, - server, + server: getDevServerConfig(), }; }); diff --git a/motoko/random_maze/frontend/vite.config.js b/motoko/random_maze/frontend/vite.config.js index 1b9599b191..e62b05d93d 100644 --- a/motoko/random_maze/frontend/vite.config.js +++ b/motoko/random_maze/frontend/vite.config.js @@ -3,33 +3,56 @@ import { execSync } from "child_process"; import react from "@vitejs/plugin-react"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } export default defineConfig(({ command }) => { diff --git a/motoko/superheroes/frontend/vite.config.js b/motoko/superheroes/frontend/vite.config.js index fcc1308780..f982dfd3fd 100644 --- a/motoko/superheroes/frontend/vite.config.js +++ b/motoko/superheroes/frontend/vite.config.js @@ -3,34 +3,56 @@ import { execSync } from "child_process"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import react from "@vitejs/plugin-react"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { - // Try icp-cli first + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } export default defineConfig(({ command }) => { diff --git a/motoko/vetkeys/basic_vetkd/frontend/vite.config.js b/motoko/vetkeys/basic_vetkd/frontend/vite.config.js index 72777742b0..7763e05fef 100644 --- a/motoko/vetkeys/basic_vetkd/frontend/vite.config.js +++ b/motoko/vetkeys/basic_vetkd/frontend/vite.config.js @@ -2,33 +2,56 @@ import { defineConfig } from "vite"; import { execSync } from "child_process"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } export default defineConfig(({ command }) => ({ diff --git a/motoko/vetkeys/password_manager_with_metadata/frontend/vite.config.js b/motoko/vetkeys/password_manager_with_metadata/frontend/vite.config.js index 169539f3bb..b37e530972 100644 --- a/motoko/vetkeys/password_manager_with_metadata/frontend/vite.config.js +++ b/motoko/vetkeys/password_manager_with_metadata/frontend/vite.config.js @@ -6,33 +6,56 @@ import css from "rollup-plugin-css-only"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import { execSync } from "child_process"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { - try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { - encoding: "utf-8", - stdio: "pipe", - }) - ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` ); + process.exit(1); + } + + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } export default defineConfig(({ command }) => ({ diff --git a/motoko/who_am_i/src/frontend/src/actor.js b/motoko/who_am_i/src/frontend/src/actor.js index 1e4ac04add..e3a4d83306 100644 --- a/motoko/who_am_i/src/frontend/src/actor.js +++ b/motoko/who_am_i/src/frontend/src/actor.js @@ -3,9 +3,7 @@ import { createActor } from "./bindings/backend"; const canisterEnv = safeGetCanisterEnv(); -const canisterId = - canisterEnv?.["PUBLIC_CANISTER_ID:backend"] ?? - process.env.CANISTER_ID_BACKEND; +const canisterId = canisterEnv?.["PUBLIC_CANISTER_ID:backend"]; if (!canisterId) { throw new Error( diff --git a/motoko/who_am_i/src/frontend/vite.config.js b/motoko/who_am_i/src/frontend/vite.config.js index 8bb840ac36..46d000e42b 100644 --- a/motoko/who_am_i/src/frontend/vite.config.js +++ b/motoko/who_am_i/src/frontend/vite.config.js @@ -1,40 +1,62 @@ -import { defineConfig, loadEnv } from "vite"; +import { defineConfig } from "vite"; import { execSync } from "child_process"; import react from "@vitejs/plugin-react"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - replicaPort: "8000", - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + replicaPort: new URL(networkStatus.api_url).port, + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } -export default defineConfig(({ command, mode }) => { - const env = loadEnv(mode, "../..", ["CANISTER_"]); +export default defineConfig(({ command }) => { const devConfig = command === "serve" ? getDevServerConfig() : undefined; return { @@ -47,9 +69,6 @@ export default defineConfig(({ command, mode }) => { }), ], define: { - "process.env.CANISTER_ID_BACKEND": JSON.stringify( - env.CANISTER_ID_BACKEND - ), "process.env.REPLICA_PORT": JSON.stringify(devConfig?.replicaPort ?? ""), }, optimizeDeps: { @@ -59,7 +78,6 @@ export default defineConfig(({ command, mode }) => { ? { headers: devConfig.headers, proxy: devConfig.proxy, - host: devConfig.host, } : undefined, }; diff --git a/rust/basic_ethereum/backend/lib.rs b/rust/basic_ethereum/backend/lib.rs index 6d8d0fdadb..d67e0a695d 100644 --- a/rust/basic_ethereum/backend/lib.rs +++ b/rust/basic_ethereum/backend/lib.rs @@ -256,9 +256,9 @@ fn estimate_transaction_fees() -> (u128, u128, u128) { #[derive(CandidType, Deserialize, Debug, Default, PartialEq, Eq)] pub struct InitArg { pub ethereum_network: Option, - /// ECDSA key name as used by the IC management canister: "dfx_test_key" (local dfx), - /// "test_key_1" (ICP mainnet testing), or "key_1" (ICP mainnet production). - /// Defaults to "test_key_1". + /// ECDSA key name as used by the IC management canister: "test_key_1" (test key, + /// works on both the local network and ICP mainnet) or "key_1" (ICP mainnet + /// production). Defaults to "test_key_1". pub ecdsa_key_name: Option, } diff --git a/rust/daily_planner/frontend/vite.config.js b/rust/daily_planner/frontend/vite.config.js index 1b9599b191..e62b05d93d 100644 --- a/rust/daily_planner/frontend/vite.config.js +++ b/rust/daily_planner/frontend/vite.config.js @@ -3,33 +3,56 @@ import { execSync } from "child_process"; import react from "@vitejs/plugin-react"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } export default defineConfig(({ command }) => { diff --git a/rust/evm_block_explorer/frontend/vite.config.js b/rust/evm_block_explorer/frontend/vite.config.js index faf4c2b869..75825dac6d 100644 --- a/rust/evm_block_explorer/frontend/vite.config.js +++ b/rust/evm_block_explorer/frontend/vite.config.js @@ -3,6 +3,58 @@ import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import react from "@vitejs/plugin-react"; import { execSync } from "child_process"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + +function getDevServerConfig() { + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } + + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; +} + export default defineConfig(({ command }) => { const plugins = [ react(), @@ -16,43 +68,8 @@ export default defineConfig(({ command }) => { return { plugins }; } - const environment = process.env.ICP_ENVIRONMENT || "local"; - const CANISTER_NAME = "backend"; - - const networkStatus = JSON.parse( - execSync(`icp network status -e ${environment} --json`, { encoding: "utf-8" }) - ); - const rootKey = networkStatus.root_key; - const proxyTarget = networkStatus.api_url; - - let canisterId; - try { - canisterId = execSync( - `icp canister status ${CANISTER_NAME} -e ${environment} -i`, - { encoding: "utf-8" } - ).trim(); - } catch { - console.error(` - Backend canister "${CANISTER_NAME}" not found in environment "${environment}" - - Before running the dev server, deploy the backend canister: - - icp deploy ${CANISTER_NAME} -e ${environment} - `); - process.exit(1); - } - return { plugins, - server: { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}&ic_root_key=${rootKey}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: proxyTarget, changeOrigin: true }, - }, - }, + server: getDevServerConfig(), }; }); diff --git a/rust/face-recognition/frontend/vite.config.js b/rust/face-recognition/frontend/vite.config.js index d804fa696d..c0f5f4f211 100644 --- a/rust/face-recognition/frontend/vite.config.js +++ b/rust/face-recognition/frontend/vite.config.js @@ -2,66 +2,73 @@ import { defineConfig } from "vite"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import { execSync } from "child_process"; -export default defineConfig(({ command }) => { - const plugins = [ - icpBindgen({ - didFile: "../backend/backend.did", - outDir: "./src/bindings", - }), - ]; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; - // If we're only building this is enough - if (command !== "serve") { - return { plugins }; +function getDevServerConfig() { + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); } - // If we're running the local npm dev server, we're going to look up the - // local network's root key and the relevant canister ids. - const environment = process.env.ICP_ENVIRONMENT || "local"; - const CANISTER_NAME = "backend"; - - const networkStatus = JSON.parse( - execSync(`icp network status -e ${environment} --json`, { - encoding: "utf-8", - }) - ); - const rootKey = networkStatus.root_key; - const proxyTarget = networkStatus.api_url; - - // Backend must be deployed before starting dev server let canisterId; try { canisterId = execSync( - `icp canister status ${CANISTER_NAME} -e ${environment} -i`, - { encoding: "utf-8" } + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } ).trim(); } catch { - console.error(` - Backend canister "${CANISTER_NAME}" not found in environment "${environment}" - - Before running the dev server, deploy the backend canister: - - icp deploy ${CANISTER_NAME} -e ${environment} - `); + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); process.exit(1); } - const server = { + return { headers: { "Set-Cookie": `ic_env=${encodeURIComponent( - `PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}&ic_root_key=${rootKey}` + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` )}; SameSite=Lax;`, }, proxy: { - "/api": { - target: proxyTarget, - changeOrigin: true, - }, + "/api": { target: networkStatus.api_url, changeOrigin: true }, }, }; +} + +export default defineConfig(({ command }) => { + const plugins = [ + icpBindgen({ + didFile: "../backend/backend.did", + outDir: "./src/bindings", + }), + ]; + + // If we're only building this is enough + if (command !== "serve") { + return { plugins }; + } return { plugins, - server, + server: getDevServerConfig(), }; }); diff --git a/rust/flying_ninja/frontend/src/actor.js b/rust/flying_ninja/frontend/src/actor.js index 2f487dbdb5..b21deeefd5 100644 --- a/rust/flying_ninja/frontend/src/actor.js +++ b/rust/flying_ninja/frontend/src/actor.js @@ -7,14 +7,11 @@ import { createActor } from "./bindings/backend"; // via Set-Cookie header (see vite.config.js). const canisterEnv = safeGetCanisterEnv(); -// Resolve canister ID: cookie (icp-cli + dev server) → env var (dfx build-time) -const canisterId = - canisterEnv?.["PUBLIC_CANISTER_ID:backend"] ?? - process.env.CANISTER_ID_BACKEND; +const canisterId = canisterEnv?.["PUBLIC_CANISTER_ID:backend"]; if (!canisterId) { throw new Error( - "Canister ID for 'backend' not found. Run 'icp deploy' or 'dfx deploy' first." + "Canister ID for 'backend' not found. Run 'icp deploy' first." ); } diff --git a/rust/flying_ninja/frontend/vite.config.js b/rust/flying_ninja/frontend/vite.config.js index b5ac17930c..a9edbaa764 100644 --- a/rust/flying_ninja/frontend/vite.config.js +++ b/rust/flying_ninja/frontend/vite.config.js @@ -1,71 +1,61 @@ -import { defineConfig, loadEnv } from "vite"; +import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import { execSync } from "child_process"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { - // Try icp-cli first + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } - // Try dfx + let canisterId; try { - const pingResult = JSON.parse( - execSync("dfx ping", { encoding: "utf-8", stdio: "pipe" }) + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` ); - const rootKeyHex = Buffer.from(pingResult.root_key).toString("hex"); - const canisterId = execSync("dfx canister id backend", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${rootKeyHex}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { - target: "http://127.0.0.1:4943", - changeOrigin: true, - }, - }, - host: "127.0.0.1", - }; - } catch {} + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy\nor:\n dfx start --background && dfx deploy" - ); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } -export default defineConfig(({ command, mode }) => { - // dfx generates ../.env with CANISTER_ID_* vars on deploy. Bake them into the - // bundle so actor.js can fall back to them when the ic_env cookie does not - // contain canister IDs (dfx does not inject PUBLIC_CANISTER_ID:* env vars - // into the asset canister, unlike icp-cli). - const env = loadEnv(mode, "..", ["CANISTER_"]); - +export default defineConfig(({ command }) => { return { base: "./", plugins: [ @@ -75,11 +65,6 @@ export default defineConfig(({ command, mode }) => { outDir: "./src/bindings", }), ], - define: { - "process.env.CANISTER_ID_BACKEND": JSON.stringify( - env.CANISTER_ID_BACKEND - ), - }, optimizeDeps: { esbuildOptions: { define: { global: "globalThis" } }, }, diff --git a/rust/hello_world/frontend/vite.config.js b/rust/hello_world/frontend/vite.config.js index d804fa696d..c0f5f4f211 100644 --- a/rust/hello_world/frontend/vite.config.js +++ b/rust/hello_world/frontend/vite.config.js @@ -2,66 +2,73 @@ import { defineConfig } from "vite"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import { execSync } from "child_process"; -export default defineConfig(({ command }) => { - const plugins = [ - icpBindgen({ - didFile: "../backend/backend.did", - outDir: "./src/bindings", - }), - ]; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; - // If we're only building this is enough - if (command !== "serve") { - return { plugins }; +function getDevServerConfig() { + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); } - // If we're running the local npm dev server, we're going to look up the - // local network's root key and the relevant canister ids. - const environment = process.env.ICP_ENVIRONMENT || "local"; - const CANISTER_NAME = "backend"; - - const networkStatus = JSON.parse( - execSync(`icp network status -e ${environment} --json`, { - encoding: "utf-8", - }) - ); - const rootKey = networkStatus.root_key; - const proxyTarget = networkStatus.api_url; - - // Backend must be deployed before starting dev server let canisterId; try { canisterId = execSync( - `icp canister status ${CANISTER_NAME} -e ${environment} -i`, - { encoding: "utf-8" } + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } ).trim(); } catch { - console.error(` - Backend canister "${CANISTER_NAME}" not found in environment "${environment}" - - Before running the dev server, deploy the backend canister: - - icp deploy ${CANISTER_NAME} -e ${environment} - `); + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); process.exit(1); } - const server = { + return { headers: { "Set-Cookie": `ic_env=${encodeURIComponent( - `PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}&ic_root_key=${rootKey}` + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` )}; SameSite=Lax;`, }, proxy: { - "/api": { - target: proxyTarget, - changeOrigin: true, - }, + "/api": { target: networkStatus.api_url, changeOrigin: true }, }, }; +} + +export default defineConfig(({ command }) => { + const plugins = [ + icpBindgen({ + didFile: "../backend/backend.did", + outDir: "./src/bindings", + }), + ]; + + // If we're only building this is enough + if (command !== "serve") { + return { plugins }; + } return { plugins, - server, + server: getDevServerConfig(), }; }); diff --git a/rust/image-classification/frontend/vite.config.js b/rust/image-classification/frontend/vite.config.js index d804fa696d..c0f5f4f211 100644 --- a/rust/image-classification/frontend/vite.config.js +++ b/rust/image-classification/frontend/vite.config.js @@ -2,66 +2,73 @@ import { defineConfig } from "vite"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import { execSync } from "child_process"; -export default defineConfig(({ command }) => { - const plugins = [ - icpBindgen({ - didFile: "../backend/backend.did", - outDir: "./src/bindings", - }), - ]; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; - // If we're only building this is enough - if (command !== "serve") { - return { plugins }; +function getDevServerConfig() { + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); } - // If we're running the local npm dev server, we're going to look up the - // local network's root key and the relevant canister ids. - const environment = process.env.ICP_ENVIRONMENT || "local"; - const CANISTER_NAME = "backend"; - - const networkStatus = JSON.parse( - execSync(`icp network status -e ${environment} --json`, { - encoding: "utf-8", - }) - ); - const rootKey = networkStatus.root_key; - const proxyTarget = networkStatus.api_url; - - // Backend must be deployed before starting dev server let canisterId; try { canisterId = execSync( - `icp canister status ${CANISTER_NAME} -e ${environment} -i`, - { encoding: "utf-8" } + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } ).trim(); } catch { - console.error(` - Backend canister "${CANISTER_NAME}" not found in environment "${environment}" - - Before running the dev server, deploy the backend canister: - - icp deploy ${CANISTER_NAME} -e ${environment} - `); + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); process.exit(1); } - const server = { + return { headers: { "Set-Cookie": `ic_env=${encodeURIComponent( - `PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}&ic_root_key=${rootKey}` + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` )}; SameSite=Lax;`, }, proxy: { - "/api": { - target: proxyTarget, - changeOrigin: true, - }, + "/api": { target: networkStatus.api_url, changeOrigin: true }, }, }; +} + +export default defineConfig(({ command }) => { + const plugins = [ + icpBindgen({ + didFile: "../backend/backend.did", + outDir: "./src/bindings", + }), + ]; + + // If we're only building this is enough + if (command !== "serve") { + return { plugins }; + } return { plugins, - server, + server: getDevServerConfig(), }; }); diff --git a/rust/llm_chatbot/frontend/vite.config.js b/rust/llm_chatbot/frontend/vite.config.js index be7ec53902..da95039aa5 100644 --- a/rust/llm_chatbot/frontend/vite.config.js +++ b/rust/llm_chatbot/frontend/vite.config.js @@ -3,65 +3,75 @@ import react from "@vitejs/plugin-react"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import { execSync } from "child_process"; -export default defineConfig(({ command }) => { - const plugins = [ - react(), - icpBindgen({ - didFile: "../backend/backend.did", - outDir: "./src/bindings", - }), - ]; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; - // Build only — no dev-server setup needed - if (command !== "serve") { - return { plugins }; +function getDevServerConfig() { + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); } - // Dev server: look up the local network root key and backend canister ID - const environment = process.env.ICP_ENVIRONMENT || "local"; - const CANISTER_NAME = "backend"; - - const networkStatus = JSON.parse( - execSync(`icp network status -e ${environment} --json`, { - encoding: "utf-8", - }) - ); - const rootKey = networkStatus.root_key; - const proxyTarget = networkStatus.api_url; - let canisterId; try { canisterId = execSync( - `icp canister status ${CANISTER_NAME} -e ${environment} -i`, - { encoding: "utf-8" } + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } ).trim(); } catch { - console.error(` - Backend canister "${CANISTER_NAME}" not found in environment "${environment}" - - Before running the dev server, deploy the backend canister: - - icp deploy ${CANISTER_NAME} -e ${environment} - `); + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); process.exit(1); } - const server = { + return { headers: { "Set-Cookie": `ic_env=${encodeURIComponent( - `PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}&ic_root_key=${rootKey}` + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` )}; SameSite=Lax;`, }, proxy: { - "/api": { - target: proxyTarget, - changeOrigin: true, - }, + "/api": { target: networkStatus.api_url, changeOrigin: true }, }, }; +} +export default defineConfig(({ command }) => { + const plugins = [ + react(), + icpBindgen({ + didFile: "../backend/backend.did", + outDir: "./src/bindings", + }), + ]; + + // Build only — no dev-server setup needed + if (command !== "serve") { + return { plugins }; + } + + // Dev server: look up the local network root key and backend canister ID return { plugins, - server, + server: getDevServerConfig(), }; }); diff --git a/rust/qrcode/frontend/src/actor.js b/rust/qrcode/frontend/src/actor.js index 322a0601b4..9667a63427 100644 --- a/rust/qrcode/frontend/src/actor.js +++ b/rust/qrcode/frontend/src/actor.js @@ -7,10 +7,7 @@ import { createActor } from "./bindings/backend"; // via Set-Cookie header (see vite.config.js). const canisterEnv = safeGetCanisterEnv(); -// Resolve canister ID: cookie (icp-cli + dev server) → env var (dfx build-time) -const canisterId = - canisterEnv?.["PUBLIC_CANISTER_ID:backend"] ?? - process.env.CANISTER_ID_BACKEND; +const canisterId = canisterEnv?.["PUBLIC_CANISTER_ID:backend"]; if (!canisterId) { throw new Error( diff --git a/rust/qrcode/frontend/vite.config.js b/rust/qrcode/frontend/vite.config.js index cfa898e9d0..c2d473eec3 100644 --- a/rust/qrcode/frontend/vite.config.js +++ b/rust/qrcode/frontend/vite.config.js @@ -1,40 +1,60 @@ -import { defineConfig, loadEnv } from "vite"; +import { defineConfig } from "vite"; import { execSync } from "child_process"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { - // Try icp-cli first + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); -} + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } -export default defineConfig(({ command, mode }) => { - const env = loadEnv(mode, "..", ["CANISTER_"]); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; +} +export default defineConfig(({ command }) => { return { base: "./", plugins: [ @@ -43,11 +63,6 @@ export default defineConfig(({ command, mode }) => { outDir: "./src/bindings", }), ], - define: { - "process.env.CANISTER_ID_BACKEND": JSON.stringify( - env.CANISTER_ID_BACKEND - ), - }, optimizeDeps: { esbuildOptions: { define: { global: "globalThis" } }, }, diff --git a/rust/threshold-schnorr/backend/tests/integration_tests.rs b/rust/threshold-schnorr/backend/tests/integration_tests.rs index 0079b81168..1528a9a5a9 100644 --- a/rust/threshold-schnorr/backend/tests/integration_tests.rs +++ b/rust/threshold-schnorr/backend/tests/integration_tests.rs @@ -8,8 +8,7 @@ use serde::Deserialize; /// (absent, empty, invalid length, valid 32 bytes) for sign+verify, plus negative cases. /// /// The PocketIC instance is reused across iterations for speed. -/// The test threshold keys subnet provides `test_key_1` and `dfx_test_key`, matching -/// the canister's default key (`test_key_1`). +/// The test threshold keys subnet provides `test_key_1`, the canister's default key. #[test] fn signing_and_verification_should_work_correctly() { const ALGORITHMS: [SchnorrAlgorithm; 2] = diff --git a/rust/vetkeys/basic_vetkd/frontend/vite.config.js b/rust/vetkeys/basic_vetkd/frontend/vite.config.js index 72777742b0..7763e05fef 100644 --- a/rust/vetkeys/basic_vetkd/frontend/vite.config.js +++ b/rust/vetkeys/basic_vetkd/frontend/vite.config.js @@ -2,33 +2,56 @@ import { defineConfig } from "vite"; import { execSync } from "child_process"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } export default defineConfig(({ command }) => ({ diff --git a/rust/vetkeys/password_manager_with_metadata/frontend/vite.config.js b/rust/vetkeys/password_manager_with_metadata/frontend/vite.config.js index 169539f3bb..b37e530972 100644 --- a/rust/vetkeys/password_manager_with_metadata/frontend/vite.config.js +++ b/rust/vetkeys/password_manager_with_metadata/frontend/vite.config.js @@ -6,33 +6,56 @@ import css from "rollup-plugin-css-only"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; import { execSync } from "child_process"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { - try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { - encoding: "utf-8", - stdio: "pipe", - }) - ); - return { - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + let networkStatus; + try { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { + encoding: "utf-8", + stdio: "pipe", + }) + ); + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` ); + process.exit(1); + } + + return { + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } export default defineConfig(({ command }) => ({ diff --git a/rust/who_am_i/src/frontend/src/actor.js b/rust/who_am_i/src/frontend/src/actor.js index 1e4ac04add..e3a4d83306 100644 --- a/rust/who_am_i/src/frontend/src/actor.js +++ b/rust/who_am_i/src/frontend/src/actor.js @@ -3,9 +3,7 @@ import { createActor } from "./bindings/backend"; const canisterEnv = safeGetCanisterEnv(); -const canisterId = - canisterEnv?.["PUBLIC_CANISTER_ID:backend"] ?? - process.env.CANISTER_ID_BACKEND; +const canisterId = canisterEnv?.["PUBLIC_CANISTER_ID:backend"]; if (!canisterId) { throw new Error( diff --git a/rust/who_am_i/src/frontend/vite.config.js b/rust/who_am_i/src/frontend/vite.config.js index 8bb840ac36..46d000e42b 100644 --- a/rust/who_am_i/src/frontend/vite.config.js +++ b/rust/who_am_i/src/frontend/vite.config.js @@ -1,40 +1,62 @@ -import { defineConfig, loadEnv } from "vite"; +import { defineConfig } from "vite"; import { execSync } from "child_process"; import react from "@vitejs/plugin-react"; import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite"; +const CANISTER_NAME = "backend"; +const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local"; + function getDevServerConfig() { + let networkStatus; try { - const canisterId = execSync("icp canister status backend -e local -i", { - encoding: "utf-8", - stdio: "pipe", - }).trim(); - const networkStatus = JSON.parse( - execSync("icp network status --json", { + networkStatus = JSON.parse( + execSync(`icp network status -e ${ENVIRONMENT} --json`, { encoding: "utf-8", stdio: "pipe", }) ); - return { - replicaPort: "8000", - headers: { - "Set-Cookie": `ic_env=${encodeURIComponent( - `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}` - )}; SameSite=Lax;`, - }, - proxy: { - "/api": { target: "http://127.0.0.1:8000", changeOrigin: true }, - }, - }; - } catch {} + } catch (error) { + if (String(error.stderr).includes("does not contain an environment named")) { + console.error( + `Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.` + ); + } else { + console.error( + `No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` + + ` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}` + ); + } + process.exit(1); + } + + let canisterId; + try { + canisterId = execSync( + `icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`, + { encoding: "utf-8", stdio: "pipe" } + ).trim(); + } catch { + console.error( + `Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` + + ` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}` + ); + process.exit(1); + } - throw new Error( - "No local network running. Start with:\n icp network start -d && icp deploy" - ); + return { + replicaPort: new URL(networkStatus.api_url).port, + headers: { + "Set-Cookie": `ic_env=${encodeURIComponent( + `ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}` + )}; SameSite=Lax;`, + }, + proxy: { + "/api": { target: networkStatus.api_url, changeOrigin: true }, + }, + }; } -export default defineConfig(({ command, mode }) => { - const env = loadEnv(mode, "../..", ["CANISTER_"]); +export default defineConfig(({ command }) => { const devConfig = command === "serve" ? getDevServerConfig() : undefined; return { @@ -47,9 +69,6 @@ export default defineConfig(({ command, mode }) => { }), ], define: { - "process.env.CANISTER_ID_BACKEND": JSON.stringify( - env.CANISTER_ID_BACKEND - ), "process.env.REPLICA_PORT": JSON.stringify(devConfig?.replicaPort ?? ""), }, optimizeDeps: { @@ -59,7 +78,6 @@ export default defineConfig(({ command, mode }) => { ? { headers: devConfig.headers, proxy: devConfig.proxy, - host: devConfig.host, } : undefined, }; diff --git a/rust/x509/backend/tests/integration_tests.rs b/rust/x509/backend/tests/integration_tests.rs index 139974aa7c..9fccc8f45c 100644 --- a/rust/x509/backend/tests/integration_tests.rs +++ b/rust/x509/backend/tests/integration_tests.rs @@ -19,7 +19,7 @@ fn load_wasm() -> Vec { } fn pic_and_canister_id(ca_key_information: CaKeyInformation) -> (pocket_ic::PocketIc, Principal) { - // with_test_threshold_keys_subnet provides test_key_1 and dfx_test_key for all algorithms. + // with_test_threshold_keys_subnet provides test_key_1 for all algorithms. let pic = PocketIcBuilder::new() .with_application_subnet() .with_test_threshold_keys_subnet()