Skip to content

[Bug]: startup script, virtual env path starts with // #538

Open
@hariom-qure

Description

@hariom-qure

What happened?

Calling the startup script from root directory from its relative path has buggy path resolution, the path to the virtual environment starts with // instead of /.
This has a bigger consequence: __file__ values inside python files starts with //

Version

bazel 8.0.0
python version = 3.9.18
Aspect rules: Patched ruleset with some of our changes, cut from tag v1.0.0

Issue found on linux containers using base image pulled with this snippet

oci.pull(
    name = "ubuntu2004cuda116",
    image = "index.docker.io/nvidia/cuda/amd64",
    tag = "11.6.1-base-ubuntu20.04",
)
use_repo(oci, "ubuntu2004cuda116")

Detailed setup

  • We use rules_py with rules_oci and py_image_layer to generate our docker images
    The final image target is like this
oci_image(
    name="docker_image",
    base="...",
    tars=[...],
    entrypoint = ["services/app/binary"],
)

This basically starts the container by calling the script services/app/binary from / inside the container

The path to the VIRTUAL_ENV variable in run.sh is //services/app/binary.runfiles/.binary.venv (note the slashes in the beginning)

The main problem is that __file__ returns the absolute path of that python file, but with 2 forward slashes instead of a single one for the root directory
I'm assuming python uses the interpreter path, along with the path from which it was called to determine __file__ variable values. The version is python 3.9 for my code

The culprit is this function in run.sh

# Returns an absolute path to the given location if the path is relative, otherwise return
# the path unchanged.
function alocation {
  local P=$1
  if [[ "${P:0:1}" == "/" ]]; then
    echo -n "${P}"
  else
    echo -n "${PWD}/${P}"
  fi
}

If $PWD == "/" and $P is relative, then it would return //$P, making big potential breaking changes

For now, I'm patching the rules in my monorepo with a small patch, and using absolute paths for entry

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingneed: investigationInvestigation required to proceed further

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions