Skip to content

Commit 39e2e3e

Browse files
shunsockzanieb
andauthored
Support UV_WORKING_DIRECTORY for setting --directory (#16125)
<!-- Thank you for contributing to uv! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> This pull request enables the `--directory` option to accept environment variable: `UV_DIRECTORY` ### Motivation Currently, the `--project` option already supports environment variables, but --directory does not. The motivation for this change is the same as for the --project option. When using this option, it’s likely that the project root and the directory containing the uv project differ. In such cases, allowing environment variables makes it easier to avoid repeatedly specifying the directory in commands or task runners. ### Other PRs - PR for create `--project` option: #12327 ## Test Plan <!-- How was it tested? --> ### no auto testing As with the --project option, no auto tests are included for this change. This is because the implementation relies on Clap’s built-in attribute functionality, and testing such behavior would effectively mean testing a third-party crate, which would be redundant. As long as the compiler accepts it, things should work as expected. ### testing manually i tested manually like [previous pull request](#12327) ```shell $ cargo build --locked ./target/debug/uv init uv_directory $ mkdir uv_directory $ UV_DIRECTORY=uv_directory ./target/debug/uv sync Using CPython 3.14.0rc3 Creating virtual environment at: .venv Resolved 1 package in 15ms Audited in 0.04ms $ UV_DIRECTORY=uv_directory ./target/debug/uv run main.py Hello from uv-directory! $ ./target/debug/uv run main.py error: Failed to spawn: `main.py` Caused by: No such file or directory (os error 2) ``` --------- Co-authored-by: Zanie Blue <[email protected]>
1 parent 2e180f5 commit 39e2e3e

File tree

5 files changed

+67
-56
lines changed

5 files changed

+67
-56
lines changed

crates/uv-cli/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ pub struct GlobalArgs {
339339
/// Relative paths are resolved with the given directory as the base.
340340
///
341341
/// See `--project` to only change the project root directory.
342-
#[arg(global = true, long)]
342+
#[arg(global = true, long, env = EnvVars::UV_WORKING_DIRECTORY)]
343343
pub directory: Option<PathBuf>,
344344

345345
/// Run the command within the given project directory.

crates/uv-static/src/env_vars.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,9 @@ impl EnvVars {
876876
/// Equivalent to the `--project` command-line argument.
877877
pub const UV_PROJECT: &'static str = "UV_PROJECT";
878878

879+
/// Equivalent to the `--directory` command-line argument.
880+
pub const UV_WORKING_DIRECTORY: &'static str = "UV_WORKING_DIRECTORY";
881+
879882
/// Disable GitHub-specific requests that allow uv to skip `git fetch` in some circumstances.
880883
pub const UV_NO_GITHUB_FAST_PATH: &'static str = "UV_NO_GITHUB_FAST_PATH";
881884

crates/uv/tests/it/help.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn help() {
6666
--no-progress
6767
Hide all progress outputs [env: UV_NO_PROGRESS=]
6868
--directory <DIRECTORY>
69-
Change to the given directory prior to running the command
69+
Change to the given directory prior to running the command [env: UV_WORKING_DIRECTORY=]
7070
--project <PROJECT>
7171
Run the command within the given project directory [env: UV_PROJECT=]
7272
--config-file <CONFIG_FILE>
@@ -147,7 +147,7 @@ fn help_flag() {
147147
--no-progress
148148
Hide all progress outputs [env: UV_NO_PROGRESS=]
149149
--directory <DIRECTORY>
150-
Change to the given directory prior to running the command
150+
Change to the given directory prior to running the command [env: UV_WORKING_DIRECTORY=]
151151
--project <PROJECT>
152152
Run the command within the given project directory [env: UV_PROJECT=]
153153
--config-file <CONFIG_FILE>
@@ -227,7 +227,7 @@ fn help_short_flag() {
227227
--no-progress
228228
Hide all progress outputs [env: UV_NO_PROGRESS=]
229229
--directory <DIRECTORY>
230-
Change to the given directory prior to running the command
230+
Change to the given directory prior to running the command [env: UV_WORKING_DIRECTORY=]
231231
--project <PROJECT>
232232
Run the command within the given project directory [env: UV_PROJECT=]
233233
--config-file <CONFIG_FILE>
@@ -415,6 +415,8 @@ fn help_subcommand() {
415415
Relative paths are resolved with the given directory as the base.
416416
417417
See `--project` to only change the project root directory.
418+
419+
[env: UV_WORKING_DIRECTORY=]
418420
419421
--project <PROJECT>
420422
Run the command within the given project directory.
@@ -676,6 +678,8 @@ fn help_subsubcommand() {
676678
Relative paths are resolved with the given directory as the base.
677679
678680
See `--project` to only change the project root directory.
681+
682+
[env: UV_WORKING_DIRECTORY=]
679683
680684
--project <PROJECT>
681685
Run the command within the given project directory.
@@ -767,7 +771,7 @@ fn help_flag_subcommand() {
767771
--no-progress
768772
Hide all progress outputs [env: UV_NO_PROGRESS=]
769773
--directory <DIRECTORY>
770-
Change to the given directory prior to running the command
774+
Change to the given directory prior to running the command [env: UV_WORKING_DIRECTORY=]
771775
--project <PROJECT>
772776
Run the command within the given project directory [env: UV_PROJECT=]
773777
--config-file <CONFIG_FILE>
@@ -846,7 +850,7 @@ fn help_flag_subsubcommand() {
846850
--no-progress
847851
Hide all progress outputs [env: UV_NO_PROGRESS=]
848852
--directory <DIRECTORY>
849-
Change to the given directory prior to running the command
853+
Change to the given directory prior to running the command [env: UV_WORKING_DIRECTORY=]
850854
--project <PROJECT>
851855
Run the command within the given project directory [env: UV_PROJECT=]
852856
--config-file <CONFIG_FILE>
@@ -1008,7 +1012,7 @@ fn help_with_global_option() {
10081012
--no-progress
10091013
Hide all progress outputs [env: UV_NO_PROGRESS=]
10101014
--directory <DIRECTORY>
1011-
Change to the given directory prior to running the command
1015+
Change to the given directory prior to running the command [env: UV_WORKING_DIRECTORY=]
10121016
--project <PROJECT>
10131017
Run the command within the given project directory [env: UV_PROJECT=]
10141018
--config-file <CONFIG_FILE>
@@ -1131,7 +1135,7 @@ fn help_with_no_pager() {
11311135
--no-progress
11321136
Hide all progress outputs [env: UV_NO_PROGRESS=]
11331137
--directory <DIRECTORY>
1134-
Change to the given directory prior to running the command
1138+
Change to the given directory prior to running the command [env: UV_WORKING_DIRECTORY=]
11351139
--project <PROJECT>
11361140
Run the command within the given project directory [env: UV_PROJECT=]
11371141
--config-file <CONFIG_FILE>

0 commit comments

Comments
 (0)