{"record":{"id":"6411a11dceb01065","repo":"cross-rs/cross","slug":"expected-gha-envfile-to-exist","errorCode":null,"errorMessage":"expected GHA envfile to exist","messagePattern":"expected GHA envfile to exist","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"xtask/src/util.rs","lineNumber":363,"sourceCode":"}\n\npub fn write_to_string(path: &Path, contents: &str) -> cross::Result<()> {\n    let mut file = fs::OpenOptions::new()\n        .write(true)\n        .truncate(true)\n        .create(true)\n        .open(path)?;\n    writeln!(file, \"{}\", contents)?;\n    Ok(())\n}\n\n// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files\npub fn write_to_gha_env_file(env_name: &str, contents: &str) -> cross::Result<()> {\n    eprintln!(\"{contents}\");\n    let path = if let Ok(path) = env::var(env_name) {\n        PathBuf::from(path)\n    } else {\n        eyre::ensure!(\n            env::var(\"GITHUB_ACTIONS\").is_err(),\n            \"expected GHA envfile to exist\"\n        );\n        return Ok(());\n    };\n    let mut file = fs::OpenOptions::new().append(true).open(path)?;\n    writeln!(file, \"{}\", contents)?;\n    Ok(())\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    use cross::shell::Verbosity;\n    use std::collections::BTreeMap;\n\n    #[test]","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/cross-rs/cross/blob/8c1a8aa4b661711f4b7b6ac07c2e8929ce2f7d27/xtask/src/util.rs#L345-L381","documentation":"write_to_gha_env_file appends a line to a GitHub Actions environment file identified by an env var (e.g. GITHUB_OUTPUT). If that env var is not set, the function tolerates running outside GitHub Actions — but only when GITHUB_ACTIONS is also unset (local run). If the env var is missing while GITHUB_ACTIONS IS set (we are on a runner), it bails: a runner must always provide these files, so their absence is an error.","triggerScenarios":"Running gha_output (the only caller) inside a GitHub Actions runner where the GITHUB_OUTPUT environment variable is not present — e.g. running the xtask step in a container/job where GitHub's runner environment file setup did not happen, running via a wrapper that scrubs the env, or a custom runner image missing GITHUB_OUTPUT.","commonSituations":"A workflow step runs the xtask command inside `docker run`/a container action that does not forward GITHUB_OUTPUT and other GITHUB_* variables; a self-hosted runner with a stripped environment; using `env -i` or a sanitized exec context within an Actions job.","solutions":["Ensure the step runs directly on the runner (not in a nested container/sanitized env) so GitHub sets and exposes GITHUB_OUTPUT; or explicitly forward -e GITHUB_OUTPUT=... when invoking docker.","If the intent is a local run, unset/skip the GITHUB_ACTIONS variable (or pass a flag) so the function's local no-op path is taken instead of erroring.","Export GITHUB_OUTPUT to a writable file path in the job before invoking xtask if using a custom runner image."],"exampleFix":"// before (workflow)\n- run: docker run myimage xtask ci   # GITHUB_OUTPUT not forwarded\n\n// after\n- run: docker run -e GITHUB_OUTPUT -e GITHUB_ACTIONS myimage xtask ci","handlingStrategy":"try-catch","validationCode":"if std::env::var(\"GITHUB_ACTIONS\").is_ok() {\n    std::env::var(\"GITHUB_OUTPUT\").expect(\"GITHUB_OUTPUT must be set on a GHA runner\");\n}","typeGuard":null,"tryCatchPattern":"match gha_output(\"tag\", &value) {\n    Ok(()) => {},\n    Err(e) if e.to_string().contains(\"expected GHA envfile\") => {\n        eprintln!(\"not writing GHA output: environment file missing\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Run xtask CI steps directly on the runner, not inside nested containers without forwarded GITHUB_* vars.","When using docker in workflows, pass -e GITHUB_OUTPUT -e GITHUB_ACTIONS explicitly.","On self-hosted runners, verify the runner image provides the standard environment files."],"tags":["github-actions","environment","missing-env-var","ci"],"backgroundTag":"missing-env-var","analyzedSha":"8c1a8aa4b661711f4b7b6ac07c2e8929ce2f7d27","analyzedAt":"2026-09-13T15:10:43.988Z","contentChangedAt":"2026-09-13T15:10:43.988Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}