Skip to content

Fix env var restoration on error path to prevent test pollution#14245

Open
cderv wants to merge 1 commit intomainfrom
fix/env-restore-finally
Open

Fix env var restoration on error path to prevent test pollution#14245
cderv wants to merge 1 commit intomainfrom
fix/env-restore-finally

Conversation

@cderv
Copy link
Collaborator

@cderv cderv commented Mar 20, 2026

When tests pass environment variables via TestContext.env, the quarto() function sets them globally with Deno.env.set() and restores the original values after execution. However, the restore code only runs on the success path — if await promise throws (e.g. a CommandError from a failed render, or Deno's exit sanitizer intercepting a Deno.exit() call inside the command), execution jumps to the catch block and the env vars are never restored.

In the sequential test runner (deno test with all files in one process), this means env vars from a failing test leak to all subsequent tests.

Fix

Move the env restoration into a finally block so it runs on both success and error paths.

Context

Discovered while reviewing #14241, which adds a test that sets QUARTO_PDF_STANDARD via TestContext.env. Same class of env pollution as #14218, though #14218 is a separate issue where crossref.ts sets env vars directly without any cleanup mechanism.

Related: #12621 (broader Deno.env cleanup effort)

Move environment variable restoration from the try block to a finally
block so env vars passed via the test harness are always cleaned up,
even when the command fails and Deno's exit sanitizer intercepts the
Deno.exit() call.

Without this, a test that sets env vars (e.g. QUARTO_PDF_STANDARD)
via TestContext.env could leak those values to subsequent tests when
running in the same deno test process.
@posit-snyk-bot
Copy link
Collaborator

posit-snyk-bot commented Mar 20, 2026

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@cscheid
Copy link
Collaborator

cscheid commented Mar 20, 2026

This is another instance of the class of bugs "we should set up env variables first and never touch them again". Not something we'll be able to fully fix before Quarto 2, and this is a good PR - I'm just pointing out the general principle.

@cscheid cscheid added this to the v1.10 milestone Mar 20, 2026
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.

3 participants