Robust Python Tasks for NX
Status
Implemented
Date
2025-04-01
Context
Our monorepo was experiencing significant flakiness with Python-related NX tasks such as lock
, install
, and deps-check
in projects like gleam
, chainpyon
, and others. This flakiness was causing:
- CI failures requiring manual intervention or retries
- Developer frustration and reduced productivity
- Inconsistent local development environments
The main causes of flakiness were identified as:
- Poetry virtual environment inconsistencies
- Broken or incorrect virtual environment references
- Lack of robust error handling and retry mechanisms
- Non-standardized tags in project.json files
Decision
We have implemented a comprehensive solution to make Python tasks more reliable:
-
Create a robust Poetry wrapper script (
scripts/python/robust-poetry.sh
) that: -
Implements retry logic with exponential backoff
- Automatically cleans up broken environments
- Provides clear logging for debugging
-
Falls back to a clean install when needed
-
Update all Python project.json files to:
-
Use the robust script for
lock
andinstall
operations - Add an explicit
install:retry
target for clean installations -
Make
deps-check
non-blocking but still informative -
Standardize project tags across all Python projects:
- Use
language:python
(notlanguage-python
) - Use
type:library
(nottype-library
)
Consequences
Positive
- Python tasks are now significantly more reliable
- CI builds succeed more consistently
- Developers can work more efficiently without manual interventions
- New projects can adopt the pattern for consistent behavior
Negative
- Added complexity with additional scripts
- Potential increased build times due to retries and clean installations
Neutral
- Some warnings may still appear but won't block execution
Implementation
The implementation consists of:
-
A core helper script:
-
scripts/python/robust-poetry.sh
: Robust wrapper for Poetry operations -
A configuration script:
-
scripts/python/update-python-projects.sh
: Updates project.json files to use the robust approach -
Updates to all Python project.json files:
- Modified lock, install, and deps-check targets
- Standardized tags across projects
To apply the solution to a new Python project, run: