Repository navigation
[WIP] Support Namespace Packages. Part 1: path resolver - #327
Alexander Sher (AlexanderSher) merged 5 commits into
Conversation
|
Test changes will follow |
…-server into NamespacePackages # Conflicts: # src/Analysis/Engine/Test/CompletionTests.cs # src/LanguageServer/Impl/LanguageServer.cs
| } | ||
|
|
||
| public int GetChildIndex(string childName) { | ||
| for (var index = 0; index < _children.Length; index++) { |
There was a problem hiding this comment.
I think we had IndexWhere in RTVS
There was a problem hiding this comment.
Good point, I'll port it.
|
|
||
| namespace Microsoft.PythonTools.Analysis.DependencyResolution { | ||
| internal partial struct PathResolverSnapshot { | ||
| private static readonly bool IgnoreCase = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); |
There was a problem hiding this comment.
And Mac. Although it is possible to have case-sensitive MacOS FS, it is rare I believe
There was a problem hiding this comment.
Looks like it can be more complicated: https://www.howtogeek.com/354220/how-to-enable-case-sensitive-folders-on-windows-10/
Or we can simply assume the "80%" case.
There was a problem hiding this comment.
I guess on Windows it is more like 0.01% since setting appears to be per folder.
| public PathResolverSnapshot(PythonLanguageVersion pythonLanguageVersion) { | ||
| _pythonLanguageVersion = pythonLanguageVersion; | ||
| _roots = Array.Empty<Node>(); | ||
| Version = 0; |
| private readonly Node[] _roots; | ||
| public int Version { get; } | ||
|
|
||
| public PathResolverSnapshot(PythonLanguageVersion pythonLanguageVersion) { |
There was a problem hiding this comment.
: this(Array.Empty<Node>(), 0) ?
There was a problem hiding this comment.
Yep
| namespace Microsoft.PythonTools.Analysis.DependencyResolution { | ||
| internal partial struct PathResolverSnapshot { | ||
| /// <summary> | ||
| /// Represents arc between two nodes in the tree |
There was a problem hiding this comment.
I think it is called edge in https://en.wikipedia.org/wiki/Glossary_of_graph_theory_terms#edge. Or path since it represents multiple nodes?
There was a problem hiding this comment.
Also in the same vein of naming changes would be vertexes -> vertices, but I think "arc" is much less common relative to "edge" than "vertexes" is to "vertices".
(EDIT: Ah, Mikhail said that below. Github emails are lagging behind...)
There was a problem hiding this comment.
I am fine either way, just that more commonly used term is easier to understand for newcomers.
There was a problem hiding this comment.
Renamed
| return GetAvailableImports(lastArc); | ||
| } | ||
|
|
||
| private IAvailableImports GetAvailableImports(in Arc lastArc, in bool requireInitPy = true) { |
There was a problem hiding this comment.
Do we need requireInitPy? The object appears to use specific language version that defines the rule.
There was a problem hiding this comment.
Yep, not required.
| using System; | ||
|
|
||
| namespace Microsoft.PythonTools.Analysis.Infrastructure { | ||
| internal static class ArrayExtensions { |
There was a problem hiding this comment.
I think System.Collections.Immutable has all of this stuff. Would there be any benefit to using that (specifically ImmutableArray) instead of our own custom extensions to the builtin Array? I'd think it might help make clear what actually has to be immutable (and what doesn't), and it claims to optimize things to prevent unneeded copies when possible.
There was a problem hiding this comment.
Yes, ImmutableArray is almost free (it is a thin struct wrapper), but other types are rather slow and expensive. Referencing new package for a single type is an overkill, especially for a reference-free assembly. Also ImmutableArray stops being cheap when any custom change operation is required.
We can grab the sources of the ImmutableArray.
There was a problem hiding this comment.
I guess I'm a bit ignorant of the implications of adding a reference when we currently have none. I'm personally less satisfied by copying the source code of another library rather than using it the normal way (especially since its code seems to be non-trivial), or even leaving it how it is with some indication that uses shouldn't modify it.
There was a problem hiding this comment.
The problem may happen if several versions of assembly has to referenced through different code paths. This particular assembly (System.Collections.Immutable) has caused problems in RTVS because of the threading package that required special version of assembly that we couldn't use in our extension.
So being dependent only on .net standard is better for our clients.
|
|
||
| namespace Microsoft.PythonTools.Analysis.DependencyResolution { | ||
| internal interface IAvailableImports { | ||
| string Name { get; } |
There was a problem hiding this comment.
Only Name? What about Modules and Packages in AvailablePackageImports? Also, should it be public?
There was a problem hiding this comment.
AvailableModuleImports doesn't have Modules and Packages. It only has ModulePath which should be used to list variables that are defined in that module (if AST is available).
There was a problem hiding this comment.
I guess I am confused by plural Imports while there is only single Name
| // List of vertexes. | ||
| // To provide immutability, it must be changed only by calling List<T>.Add | ||
| // Better to be replaced with some immutable structure | ||
| private readonly List<(int index, Node node)> _vertexes; |
There was a problem hiding this comment.
Good point.
|
|
||
| normalizedPath = PathUtils.NormalizePath(modulePath); | ||
| var rootIndex = 0; | ||
| while (rootIndex < _roots.Length && !normalizedPath.StartsWithOrdinal(_roots[rootIndex].Name)) { |
There was a problem hiding this comment.
OS-specific case sensitivity comparison aka IgnoreCase?
There was a problem hiding this comment.
Python is case sensitive in terms of names, need to check how it works with root locations
There was a problem hiding this comment.
There was a problem hiding this comment.
PEP 235 doesn't say anything about root locations. Experiment shows that it depends on file system, so with our approximation we can use OS.
| } | ||
|
|
||
| private bool TryFindModule(string modulePath, out Arc lastArc, out string normalizedPath, out (int start, int length) unmatchedPathSpan) { | ||
| if (!Path.IsPathRooted(modulePath)) { |
There was a problem hiding this comment.
Is it time to introduce IFileSystem for testability along with service container?... So we can run Mac path tests on Windows and the other way around?
There was a problem hiding this comment.
Yes it is. Do you want me to add it to this [WIP]?
There was a problem hiding this comment.
Sure, prob will need for tests anyway.
| var uri = GetDocument(Path.Combine("TestData", "AstAnalysis", "TopLevelCompletions.py")); | ||
| await s.LoadFileAsync(uri); | ||
| using (var server = await CreateServerAsync(PythonVersions.LatestAvailable3X)) { | ||
| var uri = GetDocument(@"TestData\AstAnalysis\TopLevelCompletions.py"); |
There was a problem hiding this comment.
I don't think this path should be getting changed back to \.
There was a problem hiding this comment.
Sorry, merge conflict.
|
|
||
| private IServiceContainer _services; | ||
| private IUIService _ui; | ||
| private ITelemetryService2 _telemetry; |
There was a problem hiding this comment.
Is there a reason why you're removing this? The RPC trace listener isn't going to be the only thing that needs to access telemetry (and I don't think it has anything to do with this PR's intent).
There was a problem hiding this comment.
Well, I don't like fields that are just hanging around.
There was a problem hiding this comment.
I think Jake is now using it ;-)
There was a problem hiding this comment.
He moved the GetService down into the constructor call for the class I added, so it'll still function correctly. My statement was more about how if/when we start sending more telemetry than just for RPC method exceptions, then this change will just be undone anyway.
There was a problem hiding this comment.
Not exactly. If all the usages of ITelemetryService2 instance in LanguageServer is limited to passing it to another constructors, we won't need a field as well. We will need a field only if LanguageServer actually uses the ITelemetryService2, but for now it isn't used.
| // List of vertexes. | ||
| // To provide immutability, it must be changed only by calling List<T>.Add | ||
| // Better to be replaced with some immutable structure | ||
| private readonly List<(int index, Node node)> _vertexes; |
There was a problem hiding this comment.
Following offline discussion - comment may come handy what is that index represents and how it is used since structure is a bit different from typical node with children.
There was a problem hiding this comment.
Yep, will add it. And add a DebuggerDisplay as well.
- Fix unit tests - Some refactorings
# Conflicts: # src/Analysis/Engine/Impl/Parsing/Ast/PythonNameBinder.cs # src/Analysis/Engine/Impl/PythonAnalyzer.cs # src/Analysis/Engine/Test/AstAnalysisTests.cs
…ed files) - Fix module names in tests
[WIP] Support Namespace Packages. Part 1: path resolver

This PR adds a structure that holds references to all python modules in a form of a tree that can be searched for packages and names that are available for each import. This is not equivalent to exact set of names, which is affected by previous imports (see, for instance, #288 (comment)).