Skip to content

Support Bazel sandboxs for imports #478

Closed

Description

Pylance by default assumes that your entire source tree is visible to every python file, which may not be true in production. Integration of build systems and pylance may be out of scrobe, but perhaps this request will introduce functionality that would enable a build system aware extension to do the right thing.

If you are using the Bazel build system for each python build target it will create a directory that looks like the declared runtime environment. Specifically, it creates symlinks to all of the files declared as build dependencies. You can use vscode to test if the built python program using a launch.json file like:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${relativeFileDirname}/${fileBasename}",
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}",
            "envFile": "${workspaceFolder}/.vscode/python.env",
            "env": {
                "PYTHONPATH": "${workspaceFolder}/bazel-bin/${relativeFileDirname}/${fileBasenameNoExtension}.runfiles/__main__"
            },
            "preLaunchTask": "build_program",
        }
    ]
}

If there was a way to tell pylance to use a relative path like the one specified as the PYTHONPATH above as the root directory import lookup it would really amazing.

Activity

  1. jakebailey commented on Oct 12, 2020

    @jakebailey
    Member

    Just to check if this is different than #275, does this work as expected if you use MPLS (languageServer set to Microsoft), which does support PYTHONPATH?

  2. onet-git commented on Oct 12, 2020

    @onet-git
    Author

    I have not be able to get feature from MPLS, which is not to say it is not possible.

    I do not believe supporting .env files is not sufficient because there is a unique directory per input file. According to the documentation MPLS only supports environment variable substitution in environment files, not vscode variable substitution. https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file

    From a user perspective it would be having the ability to specify an extraPath with variable substitution, such as "bazel-bin/${relativeFileDirname}/${fileBasenameNoExtension}.runfiles/main", and disable other search paths other than the python environment search paths. I imagine that would be hard to handle efficiently on the language server side.

  3. AlexanderOtavka commented on Mar 25, 2022

    @AlexanderOtavka

    We also have this issue where I work. If full support of the variable expansion in python.analysis.extraPaths isn't feasible, supporting * and ** glob wildcards would be workable, since we could do something like this in our settings and get working import resolutions:

    "python.analysis.extraPaths": ["bazel-bin/**/*.runfiles/**/pypi__*/"]
    

    This is assuming that extraPaths is able to correctly follow symlinks.

  4. judej commented on Jun 13, 2022

    @judej

    Moving this issue to discussion as an enhancement request for comments and upvotes.

  5. locked and limited conversation to collaborators on Jun 13, 2022
  6. converted this issue into a discussion #2923 on Jun 13, 2022
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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions