Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent treatment of python.analysis.extraPaths between pylint and pylance #511

Closed
couteau opened this issue Feb 8, 2024 · 2 comments · Fixed by #524
Closed

Inconsistent treatment of python.analysis.extraPaths between pylint and pylance #511

couteau opened this issue Feb 8, 2024 · 2 comments · Fixed by #524
Labels
bug Issue identified by VS Code Team member as probable bug good first issue Good for newcomers needs PR

Comments

@couteau
Copy link

couteau commented Feb 8, 2024

It appears that both pylance and the vscode pylint extension use the python.analysis.extraPaths setting when resolving imports, but that treat that setting differently with respect to relative paths and variable expansion. This is creating an issue for me in how to specify a path that is relative to the user's home directory.

In Unix-like systems (I'm on MacOS), the "~" character at the beginning of the path means the user's home directory. Pylance will treat a path that is prefixed with "~" correctly, and search the path relative to the user's home directory. Pylint, on the other hand, treats a path starting with "~" as a relative path, and specifically, a path relative to the workspace folder. That results in a search path that looks, for example, like /usr/src/project/~/anaconda3/envs/myenv/shared/package, which doesn't work, causing pylint to fail to resolve an import that pylance has no problem with.

On the other hand, pylint will recognize and expand the standard vscode settings variable ${userHome} when evaluating python.analysis.extraPaths and will successfully resolve the imports. In contrast, pylance does not recognize and expand these variables, and will choke on a path that starts with ${userHome}, and reports that imports in that path cannot be resolved.

I am currently working around this issue by including in the python.analysis.extraPaths the same path twice, once beginning with "~" and once beginning with ${userHome}.

I could just make them all absolute paths, starting with my home directory, but I don't like to include personal info like that in a settings file that will be public on GitHub, and even if I were ok with that, the settings file wouldn't work for anyone else who might clone my repo.

In any event, it seems like these two python-related parts of vscode that both use the same setting should treat it the same.

@github-actions github-actions bot added the triage-needed Issue is not triaged. label Feb 8, 2024
@karthiknadig karthiknadig added bug Issue identified by VS Code Team member as probable bug needs PR good first issue Good for newcomers and removed triage-needed Issue is not triaged. labels Feb 8, 2024
@karthiknadig
Copy link
Member

@couteau Thanks for reporting, this looks like something we can handle in the variable resolver.

Found here:

function resolveVariables(
value: string[],
workspace?: WorkspaceFolder,
interpreter?: string[],
env?: NodeJS.ProcessEnv,
): string[] {
const substitutions = new Map<string, string>();
const home = process.env.HOME || process.env.USERPROFILE;
if (home) {
substitutions.set('${userHome}', home);
}
if (workspace) {
substitutions.set('${workspaceFolder}', workspace.uri.fsPath);
}
substitutions.set('${cwd}', process.cwd());
getWorkspaceFolders().forEach((w) => {
substitutions.set('${workspaceFolder:' + w.name + '}', w.uri.fsPath);
});
env = env || process.env;
if (env) {
for (const [key, value] of Object.entries(env)) {
if (value) {
substitutions.set('${env:' + key + '}', value);
}
}
}
const modifiedValue = [];
for (const v of value) {
if (interpreter && v === '${interpreter}') {
modifiedValue.push(...interpreter);
} else {
modifiedValue.push(v);
}
}
return modifiedValue.map((s) => {
for (const [key, value] of substitutions) {
s = s.replace(key, value);
}
return s;
});
}

@MGasiewski
Copy link
Contributor

I have a fix for this, just working out the unit test changes. Will submit PR soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug good first issue Good for newcomers needs PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants