Skip to content

Enhance variable handling for Shell and Script tasks#57

Merged
8bitAlex merged 3 commits intomainfrom
bugbash
Apr 29, 2026
Merged

Enhance variable handling for Shell and Script tasks#57
8bitAlex merged 3 commits intomainfrom
bugbash

Conversation

@8bitAlex
Copy link
Copy Markdown
Owner

This pull request ensures that variables set by Set tasks ("raid variables") are exported as environment variables to subprocesses spawned by Script and Shell tasks. This allows scripts and child processes to access these variables directly via $VAR, and guarantees that raid variables override any OS environment variable with the same name. The implementation includes a new helper to merge environments in the correct order and comprehensive tests to validate the behavior.

The most important changes include:

Environment Variable Handling for Subprocesses:

  • Added a new buildSubprocessEnv helper function in task_runner.go to merge the OS environment, session variables, and raid variables, ensuring raid variables take precedence when names collide. This function is now used for all Script and Shell subprocesses. [1] [2] [3]
  • Updated documentation in variables.mdx to explain that raid variables are available in the environment of subprocesses spawned by Script and Shell tasks, and that raid variables override OS environment variables with the same name.

Testing and Validation:

  • Added new helper functions and tests in task_runner_test.go to verify that raid variables are correctly exported to subprocess environments, that they override OS variables, and that the environment order is correct. [1] [2]

These changes ensure more predictable and powerful scripting capabilities when using raid tasks.…riables are accessible in subprocesses and maintain precedence over OS environment variables

…riables are accessible in subprocesses and maintain precedence over OS environment variables
Copilot AI review requested due to automatic review settings April 28, 2026 23:31
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.07%. Comparing base (b8024d5) to head (261dbb6).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/internal/lib/task_runner.go 75.00% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #57      +/-   ##
==========================================
- Coverage   92.33%   92.07%   -0.27%     
==========================================
  Files          32       32              
  Lines        2309     2333      +24     
==========================================
+ Hits         2132     2148      +16     
- Misses        111      115       +4     
- Partials       66       70       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Exports raid “Set” variables into the environment of subprocesses spawned by Shell and Script tasks so scripts/child processes can read them via $VAR, with raid variables taking precedence over OS env on key collisions.

Changes:

  • Add buildSubprocessEnv() to merge OS env + session exports + raidVars in precedence order.
  • Apply the merged env to execShell and execScript subprocesses.
  • Add tests and docs describing/validating subprocess env propagation and precedence.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/internal/lib/task_runner.go Introduces env-merging helper and sets cmd.Env for Shell/Script subprocesses.
src/internal/lib/task_runner_test.go Adds helpers + tests asserting raid var inheritance/precedence in subprocess env.
site/docs/features/variables.mdx Documents availability/precedence of raid variables inside Shell/Script subprocesses.

Comment on lines +340 to +365
// buildSubprocessEnv returns the OS environment merged with the current
// commandSession exports and raidVars (Set tasks). Order matters: exec.Cmd
// uses the LAST occurrence of a duplicate key, so we append in increasing
// priority — OS env first, session next, raidVars last so they win on
// collision. Mirrors the lookup order used by expandRaid.
//
// Applied to Shell and Script tasks so a `Set FOO bar` task earlier in the
// sequence is visible to the spawned process (and any children it spawns)
// as $FOO. Without this, raidVars only resolved via raid's pre-expansion of
// the cmd string — which couldn't reach into a Script's source or into a
// child process spawned from inside a Shell command.
func buildSubprocessEnv() []string {
env := os.Environ()
if commandSession != nil {
commandSession.mu.RLock()
for k, v := range commandSession.vars {
env = append(env, k+"="+v)
}
commandSession.mu.RUnlock()
}
raidVarsMu.RLock()
for k, v := range raidVars {
env = append(env, k+"="+v)
}
raidVarsMu.RUnlock()
return env
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buildSubprocessEnv blindly appends raidVars into cmd.Env. Since Set tasks currently only validate non-empty names (and the JSON schema doesn’t constrain var), a user could set an invalid env key/value (e.g., containing NUL, or a name containing '=' which would be interpreted as a different key at exec time). With this change, a single bad raidVar could cause subsequent Shell/Script tasks to fail at process start or behave unexpectedly. Consider validating task.Var/task.Value in Set tasks (preferred) or filtering/guarding in buildSubprocessEnv before appending.

Copilot uses AI. Check for mistakes.
Comment on lines +1322 to +1326
scriptPath := writeTempScript(t, "#!/bin/sh\nprintf 'got=%s' \"$ISSUE20_FOO\"\n")

if err := execScript(Task{Type: Script, Path: scriptPath}); err != nil {
t.Fatalf("execScript: %v", err)
}
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new tests call execScript() with direct script execution (no Runner). That path is already skipped elsewhere on Windows because executing a .sh file directly isn’t supported there; CI runs windows-latest, so this test will fail. Consider either skipping this test on Windows (like TestExecuteTask_script_directExecution) or running the script via a runner (e.g., set Runner to "bash" and keep asserting the env behavior).

Copilot uses AI. Check for mistakes.
Comment on lines +1341 to +1345
scriptPath := writeTempScript(t, "#!/bin/sh\nprintf 'got=%s' \"$ISSUE20_OVERRIDE\"\n")

if err := execScript(Task{Type: Script, Path: scriptPath}); err != nil {
t.Fatalf("execScript: %v", err)
}
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same Windows issue as the previous test: execScript(Task{Path: scriptPath}) triggers direct .sh execution which isn’t supported on Windows, so this will fail in the Windows CI job. Please skip on Windows or set a Runner (e.g., "bash") so the test is portable while still validating raid var precedence in subprocess env.

Copilot uses AI. Check for mistakes.
Comment on lines +1366 to +1369
task := Task{Type: Shell, Cmd: childPath, Literal: true}
if err := execShell(task); err != nil {
t.Fatalf("execShell: %v", err)
}
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows, execShell defaults to cmd /c, which won’t execute a .sh child script path like this. Since CI includes windows-latest, this test is likely to fail there. Options: skip on Windows, or force a POSIX shell in the task (e.g., Shell: "bash") and invoke the child script in a bash-friendly way so the child still inherits the environment.

Copilot uses AI. Check for mistakes.
… entries and ensure proper inheritance in Script tasks
@8bitAlex 8bitAlex merged commit 4615531 into main Apr 29, 2026
11 of 13 checks passed
@8bitAlex 8bitAlex deleted the bugbash branch April 29, 2026 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants