{"record":{"id":"a79ae79961bda122","repo":"oven-sh/bun","slug":"buildkite-agent-token-not-set-and-no-existing-buil","errorCode":null,"errorMessage":"BUILDKITE_AGENT_TOKEN not set and no existing buildkite-agent.cfg to reuse","messagePattern":"BUILDKITE_AGENT_TOKEN not set and no existing buildkite-agent\\.cfg to reuse","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/agent.mjs","lineNumber":136,"sourceCode":"          --background \\\\\n          --make-pidfile \\\\\n          --stdout ${escape(agentLogPath)} \\\\\n          --stderr ${escape(agentLogPath)}\"\n\n        depend() {\n          need net\n          use dns logger\n        }\n      `;\n      writeFile(servicePath, service, { mode: 0o755 });\n      await spawnSafe([\"rc-update\", \"add\", \"buildkite-agent\", \"default\"], { stdio: \"inherit\", privileged: true });\n    }\n\n    if (isMacOS) {\n      const queue = cliOptions.queue || getEnv(\"BUILDKITE_AGENT_QUEUE\", false) || \"test-darwin\";\n      const token = getEnv(\"BUILDKITE_AGENT_TOKEN\", false);\n      if (!token && !existsSync(cfgPath)) {\n        throw new Error(\"BUILDKITE_AGENT_TOKEN not set and no existing buildkite-agent.cfg to reuse\");\n      }\n\n      // `install` runs via sudo, so process.env.USER is \"root\". The launchd\n      // service must run as the real login user (whose ~/Library the cfg and\n      // build dirs live under), and the files we write here must be owned by\n      // them so the service can read them.\n      const runAsUser = process.env.SUDO_USER || process.env.USER || \"administrator\";\n\n      for (const dir of [homePath, cachePath, logsPath]) {\n        mkdir(dir);\n      }\n\n      // Copy this script and its imports into homePath so the launchd plist\n      // doesn't depend on the checkout that ran `install` sticking around.\n      const srcDir = fileURLToPath(new URL(\".\", import.meta.url));\n      for (const f of [\"agent.mjs\", \"utils.mjs\"]) {\n        copyFileSync(join(srcDir, f), join(homePath, f));\n      }","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/scripts/agent.mjs#L118-L154","documentation":"scripts/agent.mjs provisions a machine as a BuildKite agent; on macOS it needs either the BUILDKITE_AGENT_TOKEN environment variable or an existing buildkite-agent.cfg to reuse. With neither, it throws before writing any launchd configuration, so you cannot accidentally register an agent with no credentials.","triggerScenarios":"Running the script's install/setup command on a Mac without exporting BUILDKITE_AGENT_TOKEN and with no prior agent configuration at the expected cfgPath.","commonSituations":"Onboarding a new CI mac machine and forgetting the token from the BuildKite organization settings; running through sudo where the env var is stripped (env_reset); a fresh macOS runner image.","solutions":["Export the token first: BUILDKITE_AGENT_TOKEN=... (create one in BuildKite org settings > Agents)","Point the script at an existing buildkite-agent.cfg if the machine was previously registered","Pass sudo -E (or configure env_keep) if running the installer through sudo so the variable survives","Also set BUILDKITE_AGENT_QUEUE if you need a queue other than the default 'test-darwin'"],"exampleFix":"# before\nnode scripts/agent.mjs install   # no token, no cfg -> throws\n\n# after\nexport BUILDKITE_AGENT_TOKEN=...\nnode scripts/agent.mjs install","handlingStrategy":"validation","validationCode":"import { existsSync } from 'node:fs';\nconst hasToken = Boolean(process.env.BUILDKITE_AGENT_TOKEN);\nif (!hasToken && !existsSync(cfgPath)) {\n  throw new Error('set BUILDKITE_AGENT_TOKEN (BuildKite > Agents > New Agent) before running install');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Export BUILDKITE_AGENT_TOKEN in the shell/profile that runs agent.mjs","Use sudo -E or env_keep so sudo does not strip the variable during install","Point the script at an existing buildkite-agent.cfg to reuse prior registration"],"tags":["ci","buildkite","macos","setup"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}