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

Make sure builtins are properly set after modules reload #858

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8bca56b
Fix #668 (partial)
Mar 1, 2019
a731c2a
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 1, 2019
069910b
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 5, 2019
7ffc9db
Tests
Mar 5, 2019
6303c45
Revert "Tests"
Mar 5, 2019
945451c
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 5, 2019
034e437
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 5, 2019
f997d68
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 6, 2019
0e4ff95
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 11, 2019
878c8c5
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 13, 2019
35e1033
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 13, 2019
1073711
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 14, 2019
86028da
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 14, 2019
4d4bca7
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 18, 2019
ac04f76
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 18, 2019
bab03da
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 20, 2019
85ec685
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 27, 2019
be7466d
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 28, 2019
01781c0
Exp
Mar 27, 2019
f544f8a
Limit concurrency
Mar 28, 2019
01bd719
Concurrency limit
Mar 28, 2019
2f8e564
Drop cache after analysis
Mar 28, 2019
9a933bd
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 28, 2019
4ad56bb
Fix regression
Mar 28, 2019
979efe9
Set builtins after reload
Apr 1, 2019
fce9cb0
Change time
Apr 1, 2019
3db52cc
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Analysis/Ast/Impl/Modules/PythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ protected virtual string LoadContent() {
return null; // Keep content as null so module can be loaded later.
}

private void InitializeContent(string content) {
private void InitializeContent(string content, int version = 0) {
bool startParse;
lock (AnalysisLock) {
LoadContent(content);
LoadContent(content, version);

startParse = ContentState < State.Parsing && _parsingTask == null;
if (startParse) {
Expand All @@ -231,11 +231,11 @@ private void InitializeContent(string content) {
}
}

private void LoadContent(string content) {
private void LoadContent(string content, int version) {
if (ContentState < State.Loading) {
try {
content = content ?? LoadContent();
_buffer.Reset(0, content);
_buffer.Reset(version, content);
ContentState = State.Loaded;
} catch (IOException) { } catch (UnauthorizedAccessException) { }
}
Expand Down Expand Up @@ -330,7 +330,8 @@ public void Update(IEnumerable<DocumentChange> changes) {
public void Reset(string content) {
lock (AnalysisLock) {
if (content != Content) {
InitializeContent(content);
ContentState = State.None;
InitializeContent(content, _buffer.Version + 1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ internal async Task LoadBuiltinTypesAsync(CancellationToken cancellationToken =
await BuiltinsModule.LoadAndAnalyzeAsync(cancellationToken);

Check.InvalidOperation(!(BuiltinsModule.Analysis is EmptyAnalysis), "After await");
// Add built-in module names
SetBuiltinModuleNames();
}

private void SetBuiltinModuleNames() {
// Add built-in module names
var builtinModuleNamesMember = BuiltinsModule.GetAnyMember("__builtin_module_names__");
if (builtinModuleNamesMember.TryGetConstant<string>(out var s)) {
Expand All @@ -203,6 +207,7 @@ public async Task ReloadAsync(CancellationToken cancellationToken = default) {
Modules[BuiltinModuleName] = builtins;

PathResolver = new PathResolver(_interpreter.LanguageVersion);
SetBuiltinModuleNames();

var addedRoots = new HashSet<string>();
addedRoots.UnionWith(PathResolver.SetRoot(_root));
Expand Down
58 changes: 36 additions & 22 deletions src/LanguageServer/Impl/Implementation/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ public sealed partial class Server : IDisposable {
private readonly DisposableBag _disposableBag = DisposableBag.Create<Server>();
private readonly CancellationTokenSource _shutdownCts = new CancellationTokenSource();
private readonly IServiceManager _services;
private readonly object _reloadLock = new object();

private IPythonInterpreter _interpreter;
private IRunningDocumentTable _rdt;
private ClientCapabilities _clientCaps;
private ILogger _log;
private IIndexManager _indexManager;
private string _rootDir;
private DateTime _lastPackageReload = DateTime.Now;


public Server(IServiceManager services) {
_services = services;
Expand All @@ -65,25 +69,26 @@ public Server(IServiceManager services) {
public void Dispose() => _disposableBag.TryDispose();

#region Client message handling

private InitializeResult GetInitializeResult() => new InitializeResult {
capabilities = new ServerCapabilities {
textDocumentSync = new TextDocumentSyncOptions {
openClose = true,
change = TextDocumentSyncKind.Incremental
},
completionProvider = new CompletionOptions {
triggerCharacters = new[] { "." }
triggerCharacters = new[] {"."}
},
hoverProvider = true,
signatureHelpProvider = new SignatureHelpOptions { triggerCharacters = new[] { "(", ",", ")" } },
signatureHelpProvider = new SignatureHelpOptions {triggerCharacters = new[] {"(", ",", ")"}},
definitionProvider = true,
referencesProvider = true,
workspaceSymbolProvider = true,
documentSymbolProvider = true,
renameProvider = true,
documentOnTypeFormattingProvider = new DocumentOnTypeFormattingOptions {
firstTriggerCharacter = "\n",
moreTriggerCharacter = new[] { ";", ":" }
moreTriggerCharacter = new[] {";", ":"}
},
}
};
Expand Down Expand Up @@ -121,10 +126,10 @@ public async Task<InitializeResult> InitializeAsync(InitializeParams @params, Ca
_services.AddService(_interpreter);

var fileSystem = _services.GetService<IFileSystem>();
_indexManager = new IndexManager(fileSystem, _interpreter.LanguageVersion, rootDir,
@params.initializationOptions.includeFiles,
@params.initializationOptions.excludeFiles,
_services.GetService<IIdleTimeService>());
_indexManager = new IndexManager(fileSystem, _interpreter.LanguageVersion, _rootDir,
@params.initializationOptions.includeFiles,
@params.initializationOptions.excludeFiles,
_services.GetService<IIdleTimeService>());
_services.AddService(_indexManager);
_disposableBag.Add(_indexManager);

Expand Down Expand Up @@ -158,22 +163,25 @@ public void DidChangeConfiguration(DidChangeConfigurationParams @params, Cancell
if (HandleConfigurationChanges(settings)) {
RestartAnalysis();
}

break;
}
default:
_log?.Log(TraceEventType.Error, "change configuration notification sent unsupported settings");
break;
}
}

#endregion

#region Private Helpers

private void DisplayStartupInfo() {
_log?.Log(TraceEventType.Information, Resources.LanguageServerVersion.FormatInvariant(Assembly.GetExecutingAssembly().GetName().Version));
_log?.Log(TraceEventType.Information,
string.IsNullOrEmpty(_interpreter.Configuration.InterpreterPath)
? Resources.InitializingForGenericInterpreter
: Resources.InitializingForPythonInterpreter.FormatInvariant(_interpreter.Configuration.InterpreterPath));
? Resources.InitializingForGenericInterpreter
: Resources.InitializingForPythonInterpreter.FormatInvariant(_interpreter.Configuration.InterpreterPath));
}

private bool HandleConfigurationChanges(ServerSettings newSettings) {
Expand Down Expand Up @@ -212,25 +220,31 @@ private IDocumentationSource ChooseDocumentationSource(string[] kinds) {

return new PlainTextDocumentationSource();
}

#endregion

public void NotifyPackagesChanged(CancellationToken cancellationToken) {
var interpreter = _services.GetService<IPythonInterpreter>();
_log?.Log(TraceEventType.Information, Resources.ReloadingModules);
// No need to reload typeshed resolution since it is a static storage.
// User does can add stubs while application is running, but it is
// by design at this time that the app should be restarted.
interpreter.ModuleResolution.ReloadAsync(cancellationToken).ContinueWith(t => {
_log?.Log(TraceEventType.Information, Resources.Done);
_log?.Log(TraceEventType.Information, Resources.AnalysisRestarted);
RestartAnalysis();
}, cancellationToken).DoNotWait();

lock (_reloadLock) {
if ((DateTime.Now - _lastPackageReload).TotalMilliseconds > 10000) {
var interpreter = _services.GetService<IPythonInterpreter>();
_log?.Log(TraceEventType.Information, Resources.ReloadingModules);
// No need to reload typeshed resolution since it is a static storage.
// User does can add stubs while application is running, but it is
// by design at this time that the app should be restarted.
interpreter.ModuleResolution.ReloadAsync(cancellationToken).ContinueWith(t => {
_log?.Log(TraceEventType.Information, Resources.Done);
_log?.Log(TraceEventType.Information, Resources.AnalysisRestarted);
RestartAnalysis();
}, cancellationToken).DoNotWait();
}
}
}

private void RestartAnalysis() {
foreach (var doc in _rdt) {
doc.Reset(null);
lock (_reloadLock) {
foreach (var doc in _rdt) {
doc.Reset(null);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/LanguageServer/Impl/PathsWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void OnChanged(object sender, FileSystemEventArgs e) {
// if there is massive change to the file structure.
lock (_lock) {
_changedSinceLastTick = true;
_throttleTimer = _throttleTimer ?? new Timer(TimerProc, null, 500, 500);
_throttleTimer = _throttleTimer ?? new Timer(TimerProc, null, 2000, 2000);
}
}

Expand Down