Skip to content

Add virtualEnvName config to Python hook executor #7693

Description

@wbreza

Add virtualEnvName config to Python hook executor

Background & Motivation
Python hook scripts default to {baseName}_env for virtual environments, but many Python projects use .venv by convention. Issue #7653 explicitly calls out virtualEnvName as a key config property. The Python framework service (framework_service_python.go) also hardcodes the venv name with no config override — hooks should lead in configurability here.

User Story
As a Python developer, I want to specify virtualEnvName in my hook config so that my Python hooks use my preferred virtual environment directory.

Solution Approach

  • Read Config["virtualEnvName"] in python_executor.go Prepare() phase via execCtx.Config
  • Define a strongly-typed pythonHookConfig struct and unmarshal from execCtx.Config:
    type pythonHookConfig struct {
        VirtualEnvName string `json:"virtualEnvName"` // override venv dir name
    }
  • If set, use as the venv name instead of VenvNameForDir() default
  • Validate it's a string, non-empty, and doesn't contain path separators (security: prevent path traversal)
  • Pass to existing EnsureVirtualEnv() and InstallDependencies() methods

Example azure.yaml

hooks:
  postprovision:
    run: ./hooks/seed.py
    kind: python
    config:
      virtualEnvName: .venv

Acceptance Criteria

  • config.virtualEnvName: .venv creates/uses .venv directory for the hook
  • config.virtualEnvName: my_env creates/uses my_env directory
  • Omitting virtualEnvName preserves current behavior (search .venv/venv, fall back to {baseName}_env)
  • Values with path separators (/, \) are rejected with a clear error
  • Empty string values are rejected with a clear error
  • Config is unmarshalled into a strongly-typed struct (not raw map access)
  • Unit tests covering override, fallback, and validation

Default when omitted: Existing behavior — search for .venv/venv in project dir, then fall back to {baseName}_env

Out of Scope

  • Python package manager selection (uv, poetry, pdm) — future enhancement
  • Custom requirements file name — future enhancement
  • Python binary path override — future enhancement

Testing Expectations

  • Unit tests: table-driven tests for config unmarshalling, validation, and venv name resolution
  • Test path traversal rejection (e.g., ../evil, foo/bar)
  • Test empty string rejection

Related Issues

Metadata

Metadata

Assignees

Labels

Fields

No fields configured for Feature.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions