Skip to content

uv run --with packages invisible to project entrypoints when the venv path forces the /bin/sh shebang wrapper #22323

Open

Description

@ismailkattakath

Summary

uv run --with <req> <entrypoint> stops seeing the --with packages when the project venv's path is long. A >127-byte shebang is enough.

  • In that case format_shebang (crates/uv-install-wheel/src/wheel.rs) writes the launcher as an absolute /bin/sh wrapper (#!/bin/sh / '''exec' '/abs/.venv/bin/python' "$0" "$@").
  • copy_entrypoint (crates/uv-project-commands/src/run.rs) only recognises the relocatable /bin/sh form and a bare #!<python> line, so it skips this form. The trace shows: Skipping copy of entrypoint .venv/bin/pytest: does not start with expected shebang.
  • The base launcher then runs the base interpreter, and the ephemeral --with environment is invisible to it.

The same project at a short path works. The bug shows up only at long paths, e.g. deep git worktrees or CI workspaces.

Reproduction

d="$TMPDIR/$(python3 -c 'print("a"*200)')/proj"   # any path giving a >127-byte shebang
mkdir -p "$d" && cd "$d"
uv init -q --bare --name repro
uv add -q pytest
printf 'def test_x():\n    import six\n' > test_x.py
head -1 .venv/bin/pytest                    # -> #!/bin/sh
uv run --with six pytest -q test_x.py       # -> ModuleNotFoundError: No module named 'six'
uv run --with six python -m pytest -q test_x.py   # -> 1 passed (workaround)

Run the same steps under a short directory (shebang ≤127 bytes) and uv run --with six pytest passes.

Expected

copy_entrypoint should also accept the absolute /bin/sh wrapper that uv itself writes for long paths, and rewrite it to point at the ephemeral interpreter.

Platform / version

  • macOS aarch64 (Darwin 27.0.0); uv 0.11.32 and 0.12.17 (same behaviour).
  • I read the code on main @ c0110fc, and copy_entrypoint still matches only the two forms above.

Activity

  1. kidskoding commented on Oct 9, 2026

    @kidskoding

    Hi!

    I was able to reproduce this issue on Linux, with uv release version 0.12.17 and with main at commit e8c3128.

    Alongside this issue happening when the shebang is longer than 127 bytes, this same bug also occurs on shebang paths that have a space in them.

    d="/tmp/my proj"
    mkdir -p "$d" && cd "$d"
    uv init -q --bare --name repro
    uv add -q pytest
    printf 'def test_x():\n    import six\n' > test_x.py
    uv run --with six pytest -q test_x.py   # -> error saying no module named 'six'

    By running this example and taking the first two lines of the launcher file for pytest, which start with the /bin/sh wrapper as seen below, we can see that the same issue is happening, where python launchers don't start with the expected shebang.

    $ head -2 .venv/bin/pytest
    #!/bin/sh
    '''exec' '/tmp/my proj/.venv/bin/python' "$0" "$@"
    $ RUST_LOG=uv=trace uv run --with six pytest -q test_x.py 2>&1 | grep "expected shebang"
    TRACE Skipping copy of entrypoint `.venv/bin/py.test`: does not start with expected shebang
    TRACE Skipping copy of entrypoint `.venv/bin/pytest`: does not start with expected shebang
    TRACE Skipping copy of entrypoint `.venv/bin/pygmentize`: does not start with expected shebang

    Thereby, causing the issue that module six cannot be found


    format_shebang ends up choosing the /bin/sh wrapper when the shebang is longer than 127 bytes, has a space, or the venv is relocatable

    if shebang_length > 127 || executable.contains(' ') || relocatable {
    let prefix = if relocatable {
    r#""$(dirname -- "$(realpath -- "$0")")"/"#
    } else {
    ""
    };
    let executable = format!(
    "{}'{}'",
    prefix,
    escape_posix_for_single_quotes(&executable)
    );
    return format!("#!/bin/sh\n'''exec' {executable} \"$0\" \"$@\"\n' '''");

    copy_entrypoint only accepts the relocatable /bin/sh wrapper or a plain #!<python> line, and then skips everything else

    let Some(contents) = contents
    // Check for a relative path or relocatable shebang
    .strip_prefix(
    r#"#!/bin/sh
    '''exec' "$(dirname -- "$(realpath -- "$0")")"/'python' "$0" "$@"
    ' '''
    "#,
    )
    // Or, an absolute path shebang
    .or_else(|| contents.strip_prefix(&format!("#!{}\n", previous_executable.display())))
    // If the previous executable ends with `python3`, check for a shebang with `python` too
    .or_else(|| {
    previous_executable
    .to_str()
    .and_then(|path| path.strip_suffix("3"))
    .and_then(|path| contents.strip_prefix(&format!("#!{path}\n")))
    })
    else {
    // If it's not a Python shebang, we'll skip it
    trace!(
    "Skipping copy of entrypoint `{}`: does not start with expected shebang",
    source.user_display()
    );

    One way to fix this is to have copy_entrypoint also accept the absolute /bin/sh wrapper (which is the exact text format_shebang would produce for the project's Python), strip it, and then write the launcher pointing at the --with environment's Python

    Would be happy to open a PR if that is welcome!

  2. PrithwishDas101 commented on Oct 18, 2026

    @PrithwishDas101

    I traced this to a mismatch between the launcher format generated by format_shebang and the formats accepted by copy_entrypoint.

    crates/uv-install-wheel/src/wheel.rs emits an absolute /bin/sh wrapper when the executable path exceeds the shebang length limit or contains spaces. However, crates/uv-project-commands/src/run.rs only recognizes the relocatable /bin/sh wrapper and direct Python shebang forms. The generated absolute wrapper is therefore skipped, leaving the entrypoint to run outside the ephemeral --with environment.

    A targeted fix would be to recognize the absolute wrapper format that format_shebang generates, extract its original launcher payload, and rewrite it to invoke the ephemeral environment's interpreter. I would match the expected wrapper structure rather than treating arbitrary /bin/sh scripts as Python entrypoints.

    Regression tests should cover:

    • A normal Python shebang.
    • The existing relocatable /bin/sh wrapper.
    • An absolute /bin/sh wrapper caused by a long executable path.
    • An absolute wrapper caused by spaces in the path.
    • A non-Python shell script that must continue to be skipped.
    • An end-to-end uv run --with six pytest case proving the extra dependency is importable through the copied entrypoint.

    The reported uv run --with six python -m pytest workaround is also a useful control case: it should continue to pass independently of the entrypoint-copying path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions