Bug Report
Project: cortex
Description
In CommandBuilder::build() (parse_command.rs lines 628–650), environment variable assignments are emitted before the cd subcommand when a working_dir is set. In POSIX shell, a bare VAR=value cmd assignment is scoped only to that single command. This means the env var is passed to cd, not to the intended command that follows the &&.
Error Message
No panic — silently wrong behavior: the env var is never visible to the actual command.
Debug Logs
Concrete trace for:
CommandBuilder::new("git")
.env("GIT_SSH_COMMAND", "ssh -i /key")
.working_dir("/repo")
.arg("fetch")
.build()
Generated shell string:
GIT_SSH_COMMAND='ssh -i /key' cd '/repo' && git fetch
In POSIX shell, GIT_SSH_COMMAND='ssh -i /key' cd '/repo' sets GIT_SSH_COMMAND only for the cd builtin, not for git fetch. The git fetch command runs without GIT_SSH_COMMAND set.
The correct output should be:
cd '/repo' && GIT_SSH_COMMAND='ssh -i /key' git fetch
The relevant code:
// parse_command.rs:631-643
// Add environment variables
for (k, v) in &self.env {
parts.push(format!("{}={}", k, shell_escape(v))); // ← emitted first
}
// Add cd if working directory is set
if let Some(ref wd) = self.working_dir {
parts.push(format!("cd {} &&", shell_escape(&wd.display().to_string()))); // ← cd comes after env vars
}
// Add command
parts.push(shell_escape(&self.executable)); // ← actual command comes last
The env var assignments must be placed immediately before the actual command, not before the cd.
System Information
OS: Linux (Ubuntu)
Affected file: src/cortex-engine/src/parse_command.rs
Lines: 628-650 (CommandBuilder::build)
Steps to Reproduce
- Create a
CommandBuilder with both .env() and .working_dir() set
- Call
.build()
- Observe that the generated shell string has env vars before
cd, not before the actual command
- Run the generated string in a shell — the env var is not visible to the intended command
Expected Behavior
Env var assignments should appear immediately before the actual command:
cd '/repo' && FOO=bar git fetch
Actual Behavior
Env var assignments appear before cd, scoping them to cd only:
FOO=bar cd '/repo' && git fetch
Additional Context
This affects any caller that uses both .env() and .working_dir() on a CommandBuilder. The env var is silently dropped from the intended command's environment.
Bug Report
Project: cortex
Description
In
CommandBuilder::build()(parse_command.rs lines 628–650), environment variable assignments are emitted before thecdsubcommand when aworking_diris set. In POSIX shell, a bareVAR=value cmdassignment is scoped only to that single command. This means the env var is passed tocd, not to the intended command that follows the&&.Error Message
No panic — silently wrong behavior: the env var is never visible to the actual command.
Debug Logs
Concrete trace for:
Generated shell string:
In POSIX shell,
GIT_SSH_COMMAND='ssh -i /key' cd '/repo'setsGIT_SSH_COMMANDonly for thecdbuiltin, not forgit fetch. Thegit fetchcommand runs withoutGIT_SSH_COMMANDset.The correct output should be:
The relevant code:
The env var assignments must be placed immediately before the actual command, not before the
cd.System Information
Steps to Reproduce
CommandBuilderwith both.env()and.working_dir()set.build()cd, not before the actual commandExpected Behavior
Env var assignments should appear immediately before the actual command:
Actual Behavior
Env var assignments appear before
cd, scoping them tocdonly:Additional Context
This affects any caller that uses both
.env()and.working_dir()on aCommandBuilder. The env var is silently dropped from the intended command's environment.