Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

Use PathResolverSnapshot to find modules. #361

Merged
merged 8 commits into from
Dec 4, 2018

Conversation

AlexanderSher
Copy link
Contributor

No description provided.

var rootNodes = searchPaths.Where(Path.IsPathRooted).Select(Node.CreateRoot).Prepend(_roots[0]).ToArray();
return new PathResolverSnapshot(_pythonLanguageVersion, rootNodes, Version + 1);
public PathResolverSnapshot SetUserSearchPaths(in IEnumerable<string> searchPaths) {
var userSearchPaths = searchPaths.ToArray();
Copy link

@MikhailArkhipov MikhailArkhipov Nov 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User search paths can contain anything, like . for example. VSC passed . as one of the paths normally. Also, since we pass them down verbatim from user settings, I'd try/catch IOException since they can contain random characters and may be malformed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PathResolver never accesses file system, so there can't be any IOExceptions. Search paths are preserved in their form cause when _rootDirectory is changed, we can try to convert relative path to absolute.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Path.Combine(rootDirectory, p) etc also throw when path parts are invalid, but that is ArgumentException

@MikhailArkhipov
Copy link

MikhailArkhipov commented Nov 9, 2018

BTW, how does it work with AstAnalysisWalker? It currently goes to AstNestedPythonModule which calls _interpreter.ImportModule bypassing analyzer. Should we have a service for the name resolution rather than stick it to the analyzer? The Ast* one is actually sees more complicated cases since it handles library modules imports.

@@ -382,6 +408,46 @@ static class PathUtils {
return null;
}

public static bool IsNormalizedPath(string path) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can just PathUtil.NormalizePath(path) == path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PathUtil.NormalizePath(path) produces enormous amount of garbage. During initial load, some of that garbage even ends up in Gen2. So the goal was the opposite - don't create new path if the original one is normalized already.

@AlexanderSher
Copy link
Contributor Author

Unfortunately, AstAnalysisWalker doesn't have a lot to share with DDG, cause they work differently:

  • DDG immediately tries to load module while walking through ImportStatement and FromImportStatement and assign all imported values to the variables. If module can't be loaded immediately because of circular dependency, it will use a sentinel, but because of that dependency module will be reanalyzed, and sentinel will be (most likely) replaced with the real module.
  • AstAnalysisWalker doesn't load anything (with exception for from module import * case). It creates AstNestedPythonModule for imported modules and AstNestedPythonModuleMember instance for imported members, which later resolve dependencies through lazy loading when variable is accessed. If module isn't imported at the moment of resolution, AstNestedPythonModule preserves sentinel module, and it will never be replaced with a real one. It is probably done intentionally to avoid complexity.

So the only thing I can share is PathResolver itself. I'll submit changes to AstAnalysisWalker in the next commit to this [WIP].

- Fixed namespace package resolution in completions (microsoft#12)
- Fixed support for sys.modules (microsoft#363)
- Added several tests (microsoft#365)
- Fixed support for implicit relative module names (microsoft#366)
- Added support for multiple matching rules in PathResolver (microsoft#367)

Not yet implemented:
- Fix imports of binary modules (microsoft#362)
- Fix stubs handling
- AstAnalysisWalker
- More tests
- Code cleanup
@@ -14,132 +14,344 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using Microsoft.PythonTools.Analysis.Infrastructure;
using Microsoft.PythonTools.Parsing;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorting

@MikhailArkhipov
Copy link

This needs tons of tests :-)

@MikhailArkhipov
Copy link

I assume we merge it post Nov release?

- Use PathResolverSnapshot in AstAnalysisWalker
- More cleanup

Bug to fix: PathResolverSnapshot.AddModulePath should add path to all roots where it matches, not just the first one.
… is created

- Added support for native module file names with versions
@AlexanderSher AlexanderSher changed the title [WIP]: Use PathResolverSnapshot to find modules. Use PathResolverSnapshot to find modules. Dec 3, 2018
@AlexanderSher
Copy link
Contributor Author

Ok, I've removed the WIP prefix. There are some known bugs, but they can be fixed separately.

# Conflicts:
#	src/Analysis/Engine/Impl/PythonAnalyzer.cs
…-server into NamespacePackages

# Conflicts:
#	src/Analysis/Engine/Impl/Interpreter/Ast/AstBuiltinsPythonModule.cs
@jakebailey
Copy link
Member

Given that this dependency resolver is independent from the analyzer and really only makes use of the AST, would it make more sense to move this into one of the new assemblies Mikhail is planning on creating in a future PR?

The only using from Analysis is Microsoft.PythonTools.Analysis.Infrastructure, which I would expect to be moved/reworked to a more common location anyway since it contains a lot of useful extensions.

@MikhailArkhipov
Copy link

@jakebailey - I will move the resolver to the proper assembly with my PR so new code can access it. Currently there is no separate assembly yet.

@AlexanderSher AlexanderSher merged commit d811d02 into microsoft:master Dec 4, 2018
@AlexanderSher AlexanderSher deleted the NamespacePackages branch December 4, 2018 18:35
jakebailey pushed a commit to jakebailey/python-language-server that referenced this pull request Nov 1, 2019
Use PathResolverSnapshot to find modules.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants