diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md index 10fa1b6ff..26dfceb52 100644 --- a/TROUBLESHOOTING.md +++ b/TROUBLESHOOTING.md @@ -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 @@ -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)). diff --git a/src/Analysis/Ast/Impl/Modules/Resolution/MainModuleResolution.cs b/src/Analysis/Ast/Impl/Modules/Resolution/MainModuleResolution.cs index 176f32a1a..20fe31435 100644 --- a/src/Analysis/Ast/Impl/Modules/Resolution/MainModuleResolution.cs +++ b/src/Analysis/Ast/Impl/Modules/Resolution/MainModuleResolution.cs @@ -71,16 +71,16 @@ public async Task> GetSearchPathsAsync(CancellationToken c Debug.Assert(_searchPaths != null, "Should have search paths"); _searchPaths = _searchPaths ?? Array.Empty(); - _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(); - _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; } @@ -213,7 +213,8 @@ public async Task ReloadAsync(CancellationToken cancellationToken = default) { InterpreterPaths = await GetSearchPathsAsync(cancellationToken); - var userSearchPaths = _interpreter.Configuration.SearchPaths.Except(InterpreterPaths, StringExtensions.PathsStringComparer); + IEnumerable userSearchPaths = Configuration.SearchPaths; + InterpreterPaths = InterpreterPaths.Except(userSearchPaths, StringExtensions.PathsStringComparer); if (Root != null) { var underRoot = userSearchPaths.ToLookup(p => _fs.IsPathUnderRoot(Root, p)); @@ -221,6 +222,16 @@ public async Task ReloadAsync(CancellationToken cancellationToken = default) { 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);