Skip to content

fix(uv): allow environment setting for the update action#3800

Merged
aignas merged 6 commits into
bazel-contrib:mainfrom
aignas:aignas.fix.uv-allow-env-setting
Jun 3, 2026
Merged

fix(uv): allow environment setting for the update action#3800
aignas merged 6 commits into
bazel-contrib:mainfrom
aignas:aignas.fix.uv-allow-env-setting

Conversation

@aignas
Copy link
Copy Markdown
Collaborator

@aignas aignas commented May 29, 2026

With this PR we allow users to set extra environment variables using .bazelrc
and to ensure that we can pass UV extra parameters. This should enable users use
this with private indexes in a diff_test usage scenario.

Whilst at it, add integration tests to actually verify that passing works via
env vars. Whilst at it also fix the Windows support for the uv lock rule.

Fixes #3405

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enables uv lock updates to inherit the default shell environment by setting use_default_shell_env = True in lock.bzl, and introduces a new integration test to verify lock updates with custom index URLs. The review feedback suggests improving the robustness of the test server by explicitly binding to 127.0.0.1 instead of localhost to prevent IPv4/IPv6 resolution mismatches, and properly shutting down the HTTP server in tearDown to avoid resource leaks.

Comment thread tests/integration/uv_lock_test.py Outdated
Comment on lines +107 to +113
self.server_url = "http://localhost:{port}".format(port=self.port)
self.index_url = "http://{user}:{passwd}@localhost:{port}".format(
user=self.username,
passwd=self.password,
port=self.port,
)
self.auth_url = self.index_url
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using localhost can lead to flaky tests on dual-stack (IPv4/IPv6) systems or restricted CI environments where localhost resolves to ::1 (IPv6) but the HTTPServer binds to 127.0.0.1 (IPv4). It is safer and more robust to use 127.0.0.1 explicitly.

Suggested change
self.server_url = "http://localhost:{port}".format(port=self.port)
self.index_url = "http://{user}:{passwd}@localhost:{port}".format(
user=self.username,
passwd=self.password,
port=self.port,
)
self.auth_url = self.index_url
self.server_url = "http://127.0.0.1:{port}".format(port=self.port)
self.index_url = "http://{user}:{passwd}@127.0.0.1:{port}".format(
user=self.username,
passwd=self.password,
port=self.port,
)
self.auth_url = self.index_url

Comment thread tests/integration/uv_lock_test.py Outdated
Comment on lines +150 to +151
# Start the HTTP server
self._start_server()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The HTTP server is started in a background thread but is never shut down. This can leak sockets and threads, potentially causing port conflicts or resource exhaustion in environments where multiple tests run sequentially in the same process. We should store the server instance and shut it down in tearDown.

        # Start the HTTP server
        self._start_server()

    def tearDown(self):
        if hasattr(self, "server"):
            self.server.shutdown()
            self.server.server_close()
        super().tearDown()

Comment thread tests/integration/uv_lock_test.py Outdated
Comment on lines +154 to +164
server = http.server.HTTPServer(
("localhost", self.port),
lambda *args, **kwargs: AuthSimpleAPIHandler(
*args,
username=self.username,
password=self.password,
directory=str(self.docroot),
**kwargs,
),
)
self._server_thread = threading.Thread(target=server.serve_forever)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Bind the HTTP server to 127.0.0.1 instead of localhost to match the client connection URL and avoid IPv4/IPv6 resolution mismatches. Also, store the server instance as self.server so it can be shut down in tearDown.

Suggested change
server = http.server.HTTPServer(
("localhost", self.port),
lambda *args, **kwargs: AuthSimpleAPIHandler(
*args,
username=self.username,
password=self.password,
directory=str(self.docroot),
**kwargs,
),
)
self._server_thread = threading.Thread(target=server.serve_forever)
self.server = http.server.HTTPServer(
("127.0.0.1", self.port),
lambda *args, **kwargs: AuthSimpleAPIHandler(
*args,
username=self.username,
password=self.password,
directory=str(self.docroot),
**kwargs,
),
)
self._server_thread = threading.Thread(target=self.server.serve_forever)

Comment thread docs/pypi/download.md Outdated

See the [Credential Helper Spec][cred-helper-spec] for more details.

### Using a credential helper with the lock rule
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I'm +1 on changing the bazel run //foo.lock target to run uv directly so that it uses the user's permissions. For casual use, its more convenient than having to setup the mount pair etc

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but if you want a diff_test here, then everything runs as an action, so we would then need to mount the credential helper inside. Maybe the docs should be updated to reflect that better?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah doh, //requirements.run already works the way I described.

Actually, re-reading the uv docs you linked to, since uv has its own credential store, is what needs to be mounted in the sandbox the uv credential store? And then uv just has to be told where that is.

Comment thread docs/pypi/download.md Outdated
```
# .bazelrc
build --sandbox_add_mount_pair=/path/to/cred_helper.sh=/cred_helper.sh
build --action_env=UV_CREDENTIAL_HELPER=/cred_helper.sh
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saw a clever alternative: Use a custom workspace status to put an extra key which is the path to what you want. Then pass version_file to the action and parse out the value. The nominal benefit is it doesn't become part of the cache key.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread docs/pypi/download.md Outdated

```
# .bazelrc
build --sandbox_add_mount_pair=/path/to/cred_helper.sh=/cred_helper.sh
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the LHS have to be an absolute path? If its relative, what is it relative to? does it support e.e. %workspace% interpolation?

If you don't know the answer, thats OK. Mostly just for my own edification.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does not need to be an absolute path.

Comment thread python/uv/private/lock.bzl
Comment thread docs/pypi/download.md Outdated

The same credential helper pattern can be used with the {obj}`@rules_python//python/uv:lock.bzl`
`lock` macro. Instead of embedding credentials in `UV_EXTRA_INDEX_URL`, set
`UV_CREDENTIAL_HELPER` to point to a credential helper script and use
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is UV_CREDENTIAL_HELPER part of uv itself? I don't see it in the uv docs, nor the rules_python code.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm I think it might be our own, I think I should review this once more.

@aignas aignas marked this pull request as ready for review May 29, 2026 09:08
@aignas
Copy link
Copy Markdown
Collaborator Author

aignas commented Jun 1, 2026

OK, PTAL, I think this can be merged if there are no extra comments.

@aignas aignas force-pushed the aignas.fix.uv-allow-env-setting branch 3 times, most recently from aba3e83 to 23843e4 Compare June 2, 2026 11:58
@aignas aignas force-pushed the aignas.fix.uv-allow-env-setting branch from bd55709 to 8581201 Compare June 3, 2026 12:46
@aignas aignas enabled auto-merge June 3, 2026 13:01
@aignas aignas force-pushed the aignas.fix.uv-allow-env-setting branch from 2c39e90 to 2928d36 Compare June 3, 2026 13:37
@aignas aignas added this pull request to the merge queue Jun 3, 2026
Merged via the queue into bazel-contrib:main with commit 25ad57c Jun 3, 2026
5 checks passed
@aignas aignas deleted the aignas.fix.uv-allow-env-setting branch June 3, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support authentication in lock rule in sandbox

2 participants