microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.72k stars 769 forks source link

PYTHONPATH has unintended consequences #6599

Open rchiodo opened 1 month ago

rchiodo commented 1 month ago

Repro Steps

  1. set PYTHONPATH=.
  2. Open pylance-release/testing/single folder
  3. Try to bring up the code action for this code:
# place cursor on `userModule` and confirm lightbulb shows up
# and trigger quick fix and confirm `Convert to relative path` is listed
# execute the code action and confirm it works as expected
from lib.userModule import ConvertImportPath

Expected behavior

Code action appears

Actual behavior

Nothing appears

Logs

I debugged it. It's because of this code here:


const envPYTHONPATH = pythonSection._envPYTHONPATH;
        if (envPYTHONPATH && isString(envPYTHONPATH)) {
            serverSettings.extraPaths = envPYTHONPATH
                .split(path.delimiter)
                .filter((p) => p && isString(p))
                .map((p) => resolvePathWithEnvVariables(workspace, p, workspaces))
                .filter(isDefined);
        }
rchiodo commented 1 month ago

The argument is whether or not this should behave this way. PYTHONPATH modifies how things are imported and a relative import might not work in this case.

rchiodo commented 1 month ago

Maybe we just put up a warning or something that says why you can't change to a relative import.