{"record":{"id":"56efc0205007d419","repo":"bazelbuild/bazel","slug":"cannot-find-runfiles-directory-for-s","errorCode":null,"errorMessage":"Cannot find .runfiles directory for %s","messagePattern":"Cannot find \\.runfiles directory for (.+?)","errorType":"panic","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"tools/python/python_bootstrap_template.txt","lineNumber":172,"sourceCode":"  while True:\n    module_space = stub_filename + ('.exe' if IsWindows() else '') + '.runfiles'\n    if os.path.isdir(module_space):\n      return module_space\n\n    runfiles_pattern = r'(.*\\.runfiles)' + (r'\\\\' if IsWindows() else '/') + '.*'\n    matchobj = re.match(runfiles_pattern, stub_filename)\n    if matchobj:\n      return matchobj.group(1)\n\n    if not os.path.islink(stub_filename):\n      break\n    target = os.readlink(stub_filename)\n    if os.path.isabs(target):\n      stub_filename = target\n    else:\n      stub_filename = os.path.join(os.path.dirname(stub_filename), target)\n\n  raise AssertionError('Cannot find .runfiles directory for %s' % sys.argv[0])\n\ndef ExtractZip(zip_path, dest_dir):\n  \"\"\"Extracts the contents of a zip file, preserving the unix file mode bits.\n\n  These include the permission bits, and in particular, the executable bit.\n\n  Ideally the zipfile module should set these bits, but it doesn't. See:\n  https://bugs.python.org/issue15795.\n\n  Args:\n      zip_path: The path to the zip file to extract\n      dest_dir: The path to the destination directory\n  \"\"\"\n  zip_path = GetWindowsPathWithUNCPrefix(zip_path)\n  dest_dir = GetWindowsPathWithUNCPrefix(dest_dir)\n  with zipfile.ZipFile(zip_path) as zf:\n    for info in zf.infolist():\n      zf.extract(info, dest_dir)","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/tools/python/python_bootstrap_template.txt#L154-L190","documentation":"Raised in the Python launcher stub while locating the .runfiles directory: it repeatedly matches the stub filename against a '*_runfiles' pattern and follows symlinks. If neither the pattern nor symlink traversal yields a runfiles dir, it raises AssertionError naming sys.argv[0].","triggerScenarios":"Executing the generated python stub after its runfiles tree/manifest was deleted or never created — e.g. the binary was copied out of bazel-out without its .runfiles sibling, or runfiles are disabled (Windows without --enable_runfiles) and the manifest is missing.","commonSituations":"Copying a bazel-built python binary elsewhere and running it standalone; 'bazel clean' removing the runfiles tree while the stub remains; Windows builds where only the manifest mode is available but the manifest was not passed.","solutions":["Run the binary via its bazel-generated location (bazel run / bazel-bin path with the adjacent .runfiles tree intact).","On Windows ensure --enable_runfiles is set (or that the .runfiles manifest is next to the stub) so resolution can succeed.","If you must relocate the binary, move the whole _runfiles directory alongside it and preserve the '*_runfiles' naming."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import os, sys\nstub = os.path.abspath(sys.argv[0])\nrf = stub + '.runfiles'\nif not (os.path.isdir(rf) or os.path.exists(stub + '.runfiles_manifest')):\n    raise RuntimeError('no runfiles next to %s; run via bazel' % stub)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Execute bazel-built python binaries in place with their _runfiles sibling directory.","Enable runfiles (or ship the manifest) on Windows deployments."],"tags":["python","runfiles","bazel","bootstrap"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}