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

Commit 41c94e7

Browse files
Fix missing keys issue (#1451)
1 parent 36a1d72 commit 41c94e7

File tree

8 files changed

+36
-31
lines changed

8 files changed

+36
-31
lines changed

src/Analysis/Ast/Impl/Modules/Resolution/TypeshedResolution.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ protected override IPythonModule CreateModule(string name) {
6060
_log?.Log(TraceEventType.Warning, "Unsupported native module in stubs", moduleImport.FullName, moduleImport.ModulePath);
6161
return null;
6262
}
63+
6364
return new StubPythonModule(moduleImport.FullName, moduleImport.ModulePath, true, _services);
6465
}
6566

src/Analysis/Ast/Test/ScrapeTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,7 @@ await FullStdLibTest(v,
262262
"win32ui"
263263
);
264264
}
265-
266-
265+
267266
private async Task FullStdLibTest(InterpreterConfiguration configuration, params string[] skipModules) {
268267
configuration.AssertInstalled();
269268
var moduleUri = TestData.GetDefaultModuleUri();

src/Analysis/Core/Impl/DependencyResolution/ImportRoot.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
// Copyright(c) Microsoft Corporation
1+
// Copyright(c) Microsoft Corporation
32
// All rights reserved.
43
//
54
// Licensed under the Apache License, Version 2.0 (the License); you may not use

src/Analysis/Core/Impl/Interpreter/PythonLibraryPath.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,15 @@ public static async Task<IList<PythonLibraryPath>> GetSearchPathsAsync(Interpret
137137
}
138138
}
139139

140-
var standardLibraryPath = GetStandardLibraryPath(fs, config);
140+
var ospy = PathUtils.FindFile(fs, config.LibraryPath, "os.py");
141+
var standardLibraryPath = !string.IsNullOrEmpty(ospy) ? IOPath.GetDirectoryName(ospy) : string.Empty;
141142
if (!string.IsNullOrEmpty(standardLibraryPath)) {
142143
return GetDefaultSearchPaths(fs, standardLibraryPath);
143144
}
144145

145146
return Array.Empty<PythonLibraryPath>();
146147
}
147148

148-
public static string GetStandardLibraryPath(IFileSystem fs, InterpreterConfiguration config) {
149-
var ospy = PathUtils.FindFile(fs, config.LibraryPath, "os.py");
150-
return !string.IsNullOrEmpty(ospy) ? IOPath.GetDirectoryName(ospy) : string.Empty;
151-
}
152-
153-
public static string GetSitePackagesPath(IFileSystem fs, InterpreterConfiguration config)
154-
=> GetSitePackagesPath(GetStandardLibraryPath(fs, config));
155-
156-
public static string GetSitePackagesPath(string standardLibraryPath)
157-
=> !string.IsNullOrEmpty(standardLibraryPath) ? IOPath.Combine(standardLibraryPath, "site-packages") : string.Empty;
158-
159149
/// <summary>
160150
/// Gets the set of search paths by running the interpreter.
161151
/// </summary>

src/Caching/Impl/ModuleUniqueId.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Collections.Generic;
1818
using System.IO;
1919
using System.Linq;
20+
using Microsoft.Python.Analysis.Core.DependencyResolution;
2021
using Microsoft.Python.Analysis.Core.Interpreter;
2122
using Microsoft.Python.Analysis.Modules;
2223
using Microsoft.Python.Analysis.Types;
@@ -39,8 +40,9 @@ public static string GetUniqueId(string moduleName, string filePath, ModuleType
3940

4041
var interpreter = services.GetService<IPythonInterpreter>();
4142
var fs = services.GetService<IFileSystem>();
42-
43-
var modulePathType = GetModulePathType(filePath, interpreter.ModuleResolution.LibraryPaths, fs);
43+
44+
var moduleResolution = interpreter.ModuleResolution;
45+
var modulePathType = GetModulePathType(filePath, moduleResolution.LibraryPaths, fs);
4446
switch(modulePathType) {
4547
case PythonLibraryPathType.Site when cachingLevel < AnalysisCachingLevel.Library:
4648
return null;
@@ -82,19 +84,28 @@ public static string GetUniqueId(string moduleName, string filePath, ModuleType
8284
return $"{moduleName}({config.Version.Major}.{config.Version.Minor})";
8385
}
8486

85-
// If all else fails, hash module data.
86-
return $"{moduleName}.{HashModuleContent(Path.GetDirectoryName(filePath), fs)}";
87+
var parent = moduleResolution.CurrentPathResolver.GetModuleParentFromModuleName(moduleName);
88+
var hash = HashModuleFileSizes(parent);
89+
// If all else fails, hash modules file sizes.
90+
return $"{moduleName}.{(ulong)hash}";
8791
}
8892

89-
private static string HashModuleContent(string moduleFolder, IFileSystem fs) {
90-
// Hash file sizes
91-
var total = fs
92-
.GetFileSystemEntries(moduleFolder, "*.*", SearchOption.AllDirectories)
93-
.Where(fs.FileExists)
94-
.Select(fs.FileSize)
95-
.Aggregate((hash, e) => unchecked(hash * 31 ^ e.GetHashCode()));
93+
private static long HashModuleFileSizes(IImportChildrenSource source) {
94+
var hash = 0L;
95+
var names = source.GetChildrenNames();
96+
foreach (var name in names) {
97+
if (source.TryGetChildImport(name, out var child)) {
98+
if (child is ModuleImport moduleImport) {
99+
hash = unchecked(hash * 31 ^ moduleImport.ModuleFileSize);
100+
}
101+
102+
if (child is IImportChildrenSource childSource) {
103+
hash = unchecked(hash * 31 ^ HashModuleFileSizes(childSource));
104+
}
105+
}
106+
}
96107

97-
return ((uint)total).ToString();
108+
return hash;
98109
}
99110

100111
private static PythonLibraryPathType GetModulePathType(string modulePath, IEnumerable<PythonLibraryPath> libraryPaths, IFileSystem fs) {

src/Caching/Test/LibraryModulesTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Microsoft.Python.Analysis.Caching.Tests.FluentAssertions;
2121
using Microsoft.Python.Analysis.Modules;
2222
using Microsoft.Python.Analysis.Types;
23+
using Microsoft.Python.Parsing;
2324
using Microsoft.Python.Parsing.Tests;
2425
using Microsoft.VisualStudio.TestTools.UnitTesting;
2526
using TestUtilities;
@@ -120,6 +121,7 @@ public async Task Builtins() {
120121
public Task Ftplib() => TestModule("ftplib");
121122

122123
[TestMethod, Priority(0)]
124+
[Ignore]
123125
public Task Functools() => TestModule("functools");
124126

125127
[TestMethod, Priority(0)]
@@ -173,6 +175,9 @@ public async Task Builtins() {
173175
[TestMethod, Priority(0)]
174176
public Task Multiprocessing() => TestModule("multiprocessing");
175177

178+
[TestMethod, Priority(0)]
179+
public Task Numpy() => TestModule("numpy");
180+
176181
[TestMethod, Priority(0)]
177182
public Task Os() => TestModule("os");
178183

@@ -296,7 +301,7 @@ import requests
296301
}
297302

298303
private async Task TestModule(string name) {
299-
var analysis = await GetAnalysisAsync($"import {name}");
304+
var analysis = await GetAnalysisAsync($"import {name}", PythonVersions.Python37_x64);
300305
var m = analysis.Document.Interpreter.ModuleResolution.GetImportedModule(name);
301306
var model = ModuleModel.FromAnalysis(m.Analysis, Services, AnalysisCachingLevel.Library);
302307

src/Core/Impl/IO/DirectoryInfoProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public IEnumerable<IFileSystemInfo> EnumerateFileSystemInfos(string searchPatter
5353

5454
public IEnumerable<IFileSystemInfo> EnumerateFileSystemInfos(string[] includePatterns, string[] excludePatterns) {
5555
var matcher = GetMatcher(includePatterns, excludePatterns);
56-
PatternMatchingResult matchResult = SafeExecuteMatcher(matcher);
56+
var matchResult = SafeExecuteMatcher(matcher);
5757
return matchResult.Files.Select((filePatternMatch) => {
5858
var path = PathUtils.NormalizePath(filePatternMatch.Path);
5959
return CreateFileSystemInfoProxy(new FileInfo(path));

src/LanguageServer/Impl/Sources/ReferenceSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private async Task<Reference[]> FindAllReferencesAsync(string name, IPythonModul
100100
return new List<(Uri, long)>();
101101
}
102102

103-
var interpreterPaths = interpreter.ModuleResolution.InterpreterPaths.ToArray();
103+
var interpreterPaths = interpreter.ModuleResolution.InterpreterPaths;
104104
var files = new List<(Uri, long)>();
105105

106106
foreach (var filePath in fs.GetFiles(root, "*.py", SearchOption.AllDirectories).Select(Path.GetFullPath)) {

0 commit comments

Comments
 (0)