{"record":{"id":"7f7499470a88f433","repo":"clockworklabs/SpacetimeDB","slug":"injected-commit-hash-is-not-valid-unicode-gross","errorCode":null,"errorMessage":"Injected commit hash is not valid unicode: {gross:?}","messagePattern":"Injected commit hash is not valid unicode: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cli/build.rs","lineNumber":32,"sourceCode":"\nfn nix_injected_commit_hash() -> Option<String> {\n    use std::env::VarError;\n    // Our flake.nix sets this environment variable to be our git commit hash during the build.\n    // This is important because git metadata is otherwise not available within the nix build sandbox,\n    // and we don't install the git command-line tool in our build.\n    match std::env::var(\"SPACETIMEDB_NIX_BUILD_GIT_COMMIT\") {\n        Ok(commit_sha) => {\n            // Var is set, we're building under Nix.\n            Some(commit_sha)\n        }\n\n        Err(VarError::NotPresent) => {\n            // Var is not set, we're not in Nix.\n            None\n        }\n        Err(VarError::NotUnicode(gross)) => {\n            // Var is set but is invalid unicode, something is very wrong.\n            panic!(\"Injected commit hash is not valid unicode: {gross:?}\")\n        }\n    }\n}\n\nfn is_nix_build() -> bool {\n    nix_injected_commit_hash().is_some()\n}\n\nfn find_git_hash() -> String {\n    nix_injected_commit_hash().unwrap_or_else(|| {\n        // When we're *not* building in Nix, we can assume that git metadata is still present in the filesystem,\n        // and that the git command-line tool is installed.\n        let output = Command::new(\"git\").args([\"rev-parse\", \"HEAD\"]).output().unwrap();\n        String::from_utf8(output.stdout).unwrap().trim().to_string()\n    })\n}\n\nfn get_manifest_dir() -> PathBuf {","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/cli/build.rs#L14-L50","documentation":"Build-time panic in the SpacetimeDB CLI's build.rs: the environment variable `SPACETIMEDB_NIX_BUILD_GIT_COMMIT` is set (marking a Nix build) but its bytes are not valid UTF-8. The flake.nix injects the git commit hash through this variable because git metadata is unavailable in the Nix sandbox; a non-UTF-8 value means the injection is corrupted, so the build aborts immediately rather than embedding a garbage hash.","triggerScenarios":"Building `spacetimedb-standalone`/CLI inside Nix where `SPACETIMEDB_NIX_BUILD_GIT_COMMIT` was set from a non-UTF-8 source, e.g. a misquoted shell interpolation, a binary blob, or a wrapper script exporting the variable with stray bytes.","commonSituations":"Custom Nix overlays or dev shells that hand-set the variable; CI pipelines that pass the hash through a tool that mangles encoding; copy-pasted env setup exporting a truncated hash.","solutions":["Inspect how the variable is produced: `printenv SPACETIMEDB_NIX_BUILD_GIT_COMMIT | xxd` — it must be pure ASCII hex (a 40/64-char commit sha).","Fix the injection site (flake.nix / wrapper) to export a plain UTF-8 hex string, e.g. `export SPACETIMEDB_NIX_BUILD_GIT_COMMIT=$(git rev-parse HEAD)`.","If you did not intend a Nix-style build, unset the variable so build.rs falls back to `git rev-parse`.","Re-run the Nix build after correcting the environment."],"exampleFix":"# before (broken: injects raw bytes)\nSPACETIMEDB_NIX_BUILD_GIT_COMMIT=$someBinaryRef nix build\n\n# after\nexport SPACETIMEDB_NIX_BUILD_GIT_COMMIT=\"$(git rev-parse HEAD)\"\nnix build","handlingStrategy":"validation","validationCode":"#!/usr/bin/env bash\n# Run before invoking the Nix build:\nif [[ -n \"${SPACETIMEDB_NIX_BUILD_GIT_COMMIT:-}\" ]]; then\n  if ! [[ \"$SPACETIMEDB_NIX_BUILD_GIT_COMMIT\" =~ ^[0-9a-fA-F]{7,64}$ ]]; then\n    echo \"SPACETIMEDB_NIX_BUILD_GIT_COMMIT is not a valid hex sha\" >&2\n    exit 1\n  fi\nfi","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always derive SPACETIMEDB_NIX_BUILD_GIT_COMMIT via `git rev-parse HEAD` inside the flake/wrapper.","Never hand-type or pipe binary data into the variable.","Unset the variable entirely when building outside Nix."],"tags":["spacetimedb","build-script","nix","environment-variable","encoding"],"backgroundTag":"invalid-env-var-encoding","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}