Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache source packages when reference/revision is available #367

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions fixtures/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"example/example-version": "1.0.7",
"example/example-reference": "dev-master"
}
}
55 changes: 55 additions & 0 deletions fixtures/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fixtures/vendor/example/example-reference/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
3 changes: 3 additions & 0 deletions fixtures/vendor/example/example-version/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php


17 changes: 14 additions & 3 deletions src/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,26 @@ public function index(): Promise
$cacheKey = null;
$index = null;
foreach (array_merge($this->composerLock->packages, $this->composerLock->{'packages-dev'}) as $package) {
// Check if package name matches and version is absolute
// Dynamic constraints are not cached, because they can change every time
if ($package->name !== $packageName) {
continue;
}
// Check if package can be cached.
$packageVersion = ltrim($package->version, 'v');
if ($package->name === $packageName && strpos($packageVersion, 'dev') === false) {
// If package is anchored to a version
if (strpos($packageVersion, 'dev') === false) {
$packageKey = $packageName . ':' . $packageVersion;
$cacheKey = self::CACHE_VERSION . ':' . $packageKey;
// Check cache
$index = yield $this->cache->get($cacheKey);
break;

// If package is checked out
} else if (isset($package->source->reference)) {
$packageKey = $packageName . ':' . $package->source->reference;
$cacheKey = self::CACHE_VERSION . ':' . $packageKey;
// Check cache
$index = yield $this->cache->get($cacheKey);
break;
}
}
if ($index !== null) {
Expand Down
14 changes: 11 additions & 3 deletions tests/LanguageServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,27 @@ public function testIndexingWithDirectFileAccess()
$promise = new Promise;
$input = new MockProtocolStream;
$output = new MockProtocolStream;
$output->on('message', function (Message $msg) use ($promise) {
$cacheVersionCalled = false;
$cacheReferenceCalled = false;
$output->on('message', function (Message $msg) use ($promise, &$cacheVersionCalled, &$cacheReferenceCalled) {
if ($msg->body->method === 'window/logMessage' && $promise->state === Promise::PENDING) {
if ($msg->body->params->type === MessageType::ERROR) {
$promise->reject(new Exception($msg->body->params->message));
} else if (strpos($msg->body->params->message, 'All 27 PHP files parsed') !== false) {
} else if (preg_match('/All [0-9]+ PHP files parsed/', $msg->body->params->message)) {
$promise->fulfill();
} else if (preg_match('#(Storing|Restored) example/example-version:.* (in|from) cache#', $msg->body->params->message)) {
$cacheVersionCalled = true;
} else if (preg_match('#(Storing|Restored) example/example-reference:.* (in|from) cache#', $msg->body->params->message)) {
$cacheReferenceCalled = true;
}
}
});
$server = new LanguageServer($input, $output);
$capabilities = new ClientCapabilities;
$server->initialize($capabilities, realpath(__DIR__ . '/../fixtures'), getmypid());
$promise->wait();
$this->assertTrue($cacheVersionCalled);
$this->assertTrue($cacheReferenceCalled);
}

public function testIndexingWithFilesAndContentRequests()
Expand Down Expand Up @@ -103,7 +111,7 @@ public function testIndexingWithFilesAndContentRequests()
if ($promise->state === Promise::PENDING) {
$promise->reject(new Exception($msg->body->params->message));
}
} else if (strpos($msg->body->params->message, 'All 27 PHP files parsed') !== false) {
} else if (preg_match('/All [0-9]+ PHP files parsed/', $msg->body->params->message)) {
$promise->fulfill();
}
}
Expand Down