Skip to content

Commit

Permalink
Fix editable installs when using extraPaths/PYTHONPATH (microsoft#1183)
Browse files Browse the repository at this point in the history
* Allow interpreter paths to be reclassified as user code if specified in user search paths

* log raw search path results only in verbose mode

* update TROUBLESHOOTING
  • Loading branch information
jakebailey committed Jun 7, 2019
1 parent d22adab commit 9c0ae95
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
46 changes: 41 additions & 5 deletions TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ in [Filing an issue](#filing-an-issue).

There are a few known issues in the current version of the language server:

- Import statement handling may be too strict, leading to "unresolved import" messages and the lack of analysis.
- The language server considers the workspace root to be the root of user code imports. For the most part, this can be modified by adding additional folders to the `python.autoComplete.extraPaths` setting, for example, `"python.autoComplete.extraPaths": ["./src"]`, if `src` contains the user code. A `.env` file with `PYTHONPATH` set may also help.
- Editable installs (`pip install -e` or `setup.py develop`) are known not to work with `extraPaths` when the package is installed. (#1139, #1013, #1137, #989, others).
- Not all `__all__` statements can be handled.
- Some modules may have an incorrect list of exported names.
See [#620](https://github.com/Microsoft/python-language-server/issues/620),
[#619](https://github.com/Microsoft/python-language-server/issues/619).
- Persistent issues with high memory consumption for users.
- In some contexts, users are experiencing higher than average amounts of memory being consumed. See [#832](https://github.com/Microsoft/python-language-server/issues/832).


## Requirements
Expand All @@ -38,6 +33,47 @@ but may require outside libraries such as OpenSSL 1.0 or `libicu` on Linux.

## Common questions and issues

### Unresolved import warnings

If you're getting a warning about an unresolved import, first ensure that the
package is installed into your environment if it is a library (`pip`, `pipenv`, etc).
If the warning is about importing _your own_ code (and not a library), continue reading.

The language server treats the workspace root (i.e. folder you have opened) as
the main root of user module imports. This means that if your imports are not relative
to this path, the language server will not be able to find them. This is common
for users who have a `src` directory which contains their code, a directory for
an installable package, etc.

These extra roots must be specified to the language server. The easiest way to
do this (with the VS Code Python extension) is to create a workspace configuration
which sets `python.autoComplete.extraPaths`. For example, if a project uses a
`src` directory, then create a file `.vscode/settings.json` in the workspace
with the contents:


```json
{
"python.autoComplete.extraPaths": ["./src"]
}
```

This list can be extended to other paths within the workspace (or even with
code outside the workspace in more complicated setups). Relative paths will
be taken as relative to the workspace root.

This list may also be configured using the `PYTHONPATH` environment variable,
either set directly, or via a `.env` file in the workspace root (if using the
Python extension):

```
PYTHONPATH=./src
```

For more examples, see issues:
[#1085](https://github.com/microsoft/python-language-server/issues/1085#issuecomment-492919382),
[#1169](https://github.com/microsoft/python-language-server/issues/1169#issuecomment-499998928)

### "Server initialization failed"

If you see this message, ensure that an interpreter has been selected. (See [Requirements](#requirements)).
Expand Down
21 changes: 16 additions & 5 deletions src/Analysis/Ast/Impl/Modules/Resolution/MainModuleResolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ public async Task<IReadOnlyList<string>> GetSearchPathsAsync(CancellationToken c
Debug.Assert(_searchPaths != null, "Should have search paths");
_searchPaths = _searchPaths ?? Array.Empty<string>();

_log?.Log(TraceEventType.Information, "Python search paths:");
_log?.Log(TraceEventType.Verbose, "Python search paths:");
foreach (var s in _searchPaths) {
_log?.Log(TraceEventType.Information, $" {s}");
_log?.Log(TraceEventType.Verbose, $" {s}");
}

var configurationSearchPaths = Configuration.SearchPaths ?? Array.Empty<string>();

_log?.Log(TraceEventType.Information, "Configuration search paths:");
_log?.Log(TraceEventType.Verbose, "Configuration search paths:");
foreach (var s in configurationSearchPaths) {
_log?.Log(TraceEventType.Information, $" {s}");
_log?.Log(TraceEventType.Verbose, $" {s}");
}
return _searchPaths;
}
Expand Down Expand Up @@ -213,14 +213,25 @@ public async Task ReloadAsync(CancellationToken cancellationToken = default) {

InterpreterPaths = await GetSearchPathsAsync(cancellationToken);

var userSearchPaths = _interpreter.Configuration.SearchPaths.Except(InterpreterPaths, StringExtensions.PathsStringComparer);
IEnumerable<string> userSearchPaths = Configuration.SearchPaths;
InterpreterPaths = InterpreterPaths.Except(userSearchPaths, StringExtensions.PathsStringComparer);

if (Root != null) {
var underRoot = userSearchPaths.ToLookup(p => _fs.IsPathUnderRoot(Root, p));
userSearchPaths = underRoot[true];
InterpreterPaths = underRoot[false].Concat(InterpreterPaths);
}

_log?.Log(TraceEventType.Information, "Interpreter search paths:");
foreach (var s in InterpreterPaths) {
_log?.Log(TraceEventType.Information, $" {s}");
}

_log?.Log(TraceEventType.Information, "User search paths:");
foreach (var s in userSearchPaths) {
_log?.Log(TraceEventType.Information, $" {s}");
}

addedRoots.UnionWith(PathResolver.SetInterpreterSearchPaths(InterpreterPaths));
addedRoots.UnionWith(SetUserSearchPaths(userSearchPaths));
ReloadModulePaths(addedRoots);
Expand Down

0 comments on commit 9c0ae95

Please sign in to comment.