Skip to content

fix: allow zsh to source bin/activate - #417

Merged
ekalinin merged 1 commit into
ekalinin:masterfrom
r3wretrhy:fix/zsh-activate-source-guard
Sep 26, 2026
Merged

ekalinin merged 1 commit into
ekalinin:masterfrom
r3wretrhy:fix/zsh-activate-source-guard

Conversation

@r3wretrhy

Copy link
Copy Markdown
Contributor

Summary

Sourcing bin/activate from zsh prints the "do not call directly" error and aborts, so activation never happens. The guard added in #384 checks $0, but zsh's FUNCTION_ARGZERO sets $0 to the sourced file path, so every source bin/activate looks like a direct run.

Fix

Detect execution vs sourcing with the usual portable probes:

  • bash: BASH_SOURCE
  • zsh: ZSH_EVAL_CONTEXT (contains file when sourced)
  • fallback: compare $0 to the script path only when those are unavailable

Also drop the ZSH_SOURCE_GUARD xfails that were papering over this.

Fixes #398.

Test plan

  • zsh -c 'source …/bin/activate; echo SURVIVE' — survives (was blocked on stock)
  • bash / zsh / sh executing bin/activate directly still exit 1
  • pytest tests/test_activate_shells.py::test_syntax[plain-zsh] PASSED
  • pytest tests/test_activate_shells.py::test_freeze_lists_packages[plain-zsh] PASSED
  • pytest tests/test_activate_shells.py::test_freeze_local[plain-zsh] PASSED
  • pytest tests/test_activate_shells.py::test_freeze_writes_file[plain-zsh] PASSED
  • pytest tests/test_install_activate.py::test_win_activate_refuses_to_be_run_directly PASSED

The direct-call guard matched on $0, but zsh's FUNCTION_ARGZERO sets
$0 to the sourced file, so every `source bin/activate` aborted.
Detect execution vs sourcing with BASH_SOURCE / ZSH_EVAL_CONTEXT /
$0 as appropriate, and drop the ZSH_SOURCE_GUARD xfails.

Fixes ekalinin#398.
@ekalinin
ekalinin merged commit 55df3ce into ekalinin:master Sep 26, 2026
25 checks passed
@ekalinin

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zsh cannot source bin/activate: the direct-call guard misfires

2 participants