-
Notifications
You must be signed in to change notification settings - Fork 1
feat: detect project from script location, not just CWD #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Previously, auto-uv only looked for project markers (pyproject.toml, .venv,
uv.lock) starting from the current working directory. This caused issues when
running scripts from a different directory:
cd /some/dir
python /path/to/project/script.py # auto-uv wouldn't detect the project
This change adds script-path-based detection:
1. First try to find project root from the script's directory
2. Fall back to CWD for backwards compatibility
This enables the use case of running Python scripts from any directory while
still having auto-uv intercept and use `uv run`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Reviewer's GuideRefactors project-root detection into a reusable helper and updates the auto-uv interception logic to prefer detecting projects from the script’s directory (with CWD as fallback), along with tests verifying the new behavior and preserving existing path-normalization checks. Sequence diagram for auto_uv interception using script_path and CWD fallbacksequenceDiagram
actor User
participant Python as PythonInterpreter
participant AutoUV as auto_use_uv
participant Should as should_use_uv
participant Finder as _find_project_root
User->>Python: python script.py
Python->>AutoUV: auto_use_uv()
AutoUV->>Python: inspect sys.argv / sys.path
AutoUV->>AutoUV: resolve script_path
AutoUV->>Should: should_use_uv(script_path)
Should->>Should: check UV_RUN_ACTIVE
Should->>Should: check AUTO_UV_DISABLE
Should->>Python: subprocess.run("uv", "--version")
Python-->>Should: uv available?
Should->>Should: handle errors, maybe return False
alt uv_available
alt script_path_provided
Should->>Finder: _find_project_root(script_dir)
Finder-->>Should: project_root or None
end
alt no_project_root_from_script_dir
Should->>Finder: _find_project_root(os_getcwd)
Finder-->>Should: project_root or None
end
alt project_root_found
Should-->>AutoUV: True
AutoUV->>Python: set UV_RUN_ACTIVE
AutoUV->>Python: exec uv run script
else no_project_root_found
Should-->>AutoUV: False
AutoUV-->>Python: return without interception
end
else uv_not_available
Should-->>AutoUV: False
AutoUV-->>Python: return without interception
end
Flow diagram for _find_project_root directory walkflowchart TD
start["Start with start_dir"] --> absdir["check_dir = abs(start_dir)"]
absdir --> loop_start["For up to max_depth levels"]
loop_start --> check_markers{"Does check_dir contain pyproject.toml or .venv or uv.lock?"}
check_markers -->|Yes| found["Return check_dir as project_root"]
check_markers -->|No| parent_step["parent = dirname(check_dir)"]
parent_step --> reached_root{"Is parent == check_dir?"}
reached_root -->|Yes| no_root["Break loop (filesystem root reached)"]
reached_root -->|No| update_dir["check_dir = parent"]
update_dir --> loop_start
no_root --> return_none["Return None (no project markers found)"]
style start fill:#e3f2fd,stroke:#1e88e5
style found fill:#c8e6c9,stroke:#43a047
style return_none fill:#ffcdd2,stroke:#e53935
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- The tests currently import and assert against the private helper
_find_project_root; consider either testing this behavior only throughshould_use_uv/auto_use_uvor promoting_find_project_rootto a public helper if you intend it to be part of the supported surface. - In
test_script_path_detection, you're manually saving/restoringos.environand callingos.environ.clear(), which is easy to get wrong and can interfere with other tests; usingmonkeypatch(or a similar fixture) to isolate env changes would make this safer and more readable.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The tests currently import and assert against the private helper `_find_project_root`; consider either testing this behavior only through `should_use_uv`/`auto_use_uv` or promoting `_find_project_root` to a public helper if you intend it to be part of the supported surface.
- In `test_script_path_detection`, you're manually saving/restoring `os.environ` and calling `os.environ.clear()`, which is easy to get wrong and can interfere with other tests; using `monkeypatch` (or a similar fixture) to isolate env changes would make this safer and more readable.
## Individual Comments
### Comment 1
<location> `tests/test_auto_uv.py:489` </location>
<code_context>
+ print(f"From unrelated dir, no script_path: should_use_uv = {result}")
+ assert result is False, "Should NOT detect project when CWD has no markers and no script_path"
+
+ # Test 2: From unrelated dir, should_use_uv(script_path) returns True
+ result = should_use_uv(script_path)
+ print(f"From unrelated dir, with script_path: should_use_uv = {result}")
</code_context>
<issue_to_address>
**suggestion (testing):** Add a test for the case where CWD is a project but the script_path points outside it
Currently this only asserts: (a) CWD has no markers and no script_path ⇒ False, and (b) CWD has no markers but script_path is inside a project ⇒ True. It would be valuable to also cover the case where CWD *is* a project dir but `script_path` points outside any project, to verify that CWD-based detection still applies and that a non-project `script_path` doesn’t override it. For example:
- create `project_dir` with markers
- `os.chdir(project_dir)`
- choose `script_path` in `unrelated_dir`
- assert `should_use_uv(script_path)` matches the intended behavior (likely `True`, or explicitly `False` if that’s the spec).
This will clearly document and lock in the precedence between script-based and CWD-based detection.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| print(f"From unrelated dir, no script_path: should_use_uv = {result}") | ||
| assert result is False, "Should NOT detect project when CWD has no markers and no script_path" | ||
|
|
||
| # Test 2: From unrelated dir, should_use_uv(script_path) returns True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Add a test for the case where CWD is a project but the script_path points outside it
Currently this only asserts: (a) CWD has no markers and no script_path ⇒ False, and (b) CWD has no markers but script_path is inside a project ⇒ True. It would be valuable to also cover the case where CWD is a project dir but script_path points outside any project, to verify that CWD-based detection still applies and that a non-project script_path doesn’t override it. For example:
- create
project_dirwith markers os.chdir(project_dir)- choose
script_pathinunrelated_dir - assert
should_use_uv(script_path)matches the intended behavior (likelyTrue, or explicitlyFalseif that’s the spec).
This will clearly document and lock in the precedence between script-based and CWD-based detection.
Summary
Previously, auto-uv only looked for project markers (
pyproject.toml,.venv,uv.lock) starting from the current working directory. This caused issues when running scripts from a different directory:This change adds script-path-based detection:
Changes
_find_project_root()helper function that walks up directories looking for project markersshould_use_uv()to accept optionalscript_pathparameterauto_use_uv()to pass the script path toshould_use_uv()Use Case
This enables running Python scripts from any directory while still having auto-uv intercept and use
uv run:Test Plan
test_script_path_detectiontest added and passing🤖 Generated with Claude Code
Summary by Sourcery
Detect uv projects based on the executed script’s directory, falling back to the current working directory for compatibility.
New Features:
Enhancements:
Tests: