Skip to content

Commit

Permalink
Handle jars missing from the cache more gracefully.
Browse files Browse the repository at this point in the history
Also force-enable the local jar cache when syncing remotely.

PiperOrigin-RevId: 249869185
  • Loading branch information
brendandouglas authored and copybara-github committed May 24, 2019
1 parent 4e97244 commit 6fd5f09
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public RemoteOutputArtifacts appendNewOutputs(Set<RemoteOutputArtifact> outputs)
return new RemoteOutputArtifacts(ImmutableMap.copyOf(map));
}

public boolean isEmpty() {
return remoteOutputArtifacts.isEmpty();
}

/**
* Looks for a {@link RemoteOutputArtifact} with a given genfiles-relative path, returning the
* first such match, or null if none can be found.
Expand Down
28 changes: 22 additions & 6 deletions java/src/com/google/idea/blaze/java/libraries/JarCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.idea.blaze.base.scope.BlazeContext;
import com.google.idea.blaze.base.scope.output.IssueOutput;
import com.google.idea.blaze.base.scope.output.PrintOutput;
import com.google.idea.blaze.base.settings.Blaze;
import com.google.idea.blaze.base.settings.BlazeImportSettings;
import com.google.idea.blaze.base.settings.BlazeImportSettingsManager;
import com.google.idea.blaze.base.sync.SyncMode;
Expand Down Expand Up @@ -93,6 +94,7 @@ private static class InMemoryState {
}
}

private final Project project;
private final File cacheDir;

@Nullable private volatile InMemoryState inMemoryState = null;
Expand All @@ -102,6 +104,7 @@ private static class InMemoryState {
public JarCache(Project project) {
BlazeImportSettings importSettings =
BlazeImportSettingsManager.getInstance(project).getImportSettings();
this.project = project;
this.cacheDir = getCacheDir(importSettings);
}

Expand All @@ -110,9 +113,11 @@ public boolean isEnabled() {
}

private boolean updateEnabled() {
// force-enable the jar cache if syncing remotely
this.enabled =
BlazeJavaUserSettings.getInstance().getUseJarCache()
&& !ApplicationManager.getApplication().isUnitTestMode();
!ApplicationManager.getApplication().isUnitTestMode()
&& (BlazeJavaUserSettings.getInstance().getUseJarCache()
|| Blaze.getBuildSystemProvider(project).syncingRemotely());
return enabled;
}

Expand Down Expand Up @@ -155,9 +160,11 @@ private void onSync(

private void refresh(BlazeContext context, @Nullable BlazeProjectData projectData) {
InMemoryState inMemoryState = this.inMemoryState;
RemoteOutputArtifacts previousOutputs = RemoteOutputArtifacts.fromProjectData(projectData);
if (inMemoryState == null
|| inMemoryState.projectOutputs.values().stream()
.anyMatch(a -> a instanceof RemoteOutputArtifact)) {
.anyMatch(a -> a instanceof RemoteOutputArtifact)
|| !previousOutputs.isEmpty()) {
// if we have remote artifacts, only refresh during sync
return;
}
Expand Down Expand Up @@ -305,7 +312,11 @@ private void clearCache() {
inMemoryState = null;
}

/** Gets the cached file for a jar. If it doesn't exist, we return the file from the library. */
/**
* Gets the cached file for a jar. If it doesn't exist, we return the file from the library, or
* null if that also can't be accessed locally.
*/
@Nullable
public File getCachedJar(ArtifactLocationDecoder decoder, BlazeJarLibrary library) {
boolean enabled = isEnabled();
OutputArtifact artifact =
Expand All @@ -317,7 +328,11 @@ public File getCachedJar(ArtifactLocationDecoder decoder, BlazeJarLibrary librar
return getCacheFile(cacheKey).orElseGet(() -> getFallbackFile(artifact));
}

/** Gets the cached file for a source jar. */
/**
* Gets the cached file for a source jar. If it doesn't exist, we return the file from the
* library, or null if that also can't be accessed locally.
*/
@Nullable
public File getCachedSourceJar(ArtifactLocationDecoder decoder, ArtifactLocation sourceJar) {
boolean enabled = isEnabled();
OutputArtifact artifact = decoder.resolveOutput(sourceJar);
Expand All @@ -337,10 +352,11 @@ private Optional<File> getCacheFile(String cacheKey) {
}

/** The file to return if there's no locally cached version. */
@Nullable
private static File getFallbackFile(OutputArtifact output) {
if (output instanceof RemoteOutputArtifact) {
// TODO(brendandouglas): copy locally on the fly?
throw new RuntimeException("The jar cache must be enabled when syncing remotely");
return null;
}
return ((LocalFileOutputArtifact) output).getFile();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder;
import com.google.idea.blaze.java.libraries.AttachedSourceJarManager;
import com.google.idea.blaze.java.libraries.JarCache;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.libraries.Library;
Expand All @@ -33,6 +34,9 @@
/** An immutable reference to a .jar required by a rule. */
@Immutable
public final class BlazeJarLibrary extends BlazeLibrary {

private static final Logger logger = Logger.getInstance(BlazeJarLibrary.class);

public final LibraryArtifact libraryArtifact;

public BlazeJarLibrary(LibraryArtifact libraryArtifact) {
Expand Down Expand Up @@ -63,7 +67,11 @@ public void modifyLibraryModel(
Library.ModifiableModel libraryModel) {
JarCache jarCache = JarCache.getInstance(project);
File jar = jarCache.getCachedJar(artifactLocationDecoder, this);
libraryModel.addRoot(pathToUrl(jar), OrderRootType.CLASSES);
if (jar != null) {
libraryModel.addRoot(pathToUrl(jar), OrderRootType.CLASSES);
} else {
logger.error("No local jar file found for " + libraryArtifact.jarForIntellijLibrary());
}

AttachedSourceJarManager sourceJarManager = AttachedSourceJarManager.getInstance(project);
if (!sourceJarManager.hasSourceJarAttached(key)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import com.google.idea.blaze.base.util.PackagePrefixCalculator;
import com.google.idea.blaze.java.sync.model.BlazeContentEntry;
import com.google.idea.blaze.java.sync.model.BlazeSourceDirectory;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import java.io.File;
import java.util.Collection;
Expand All @@ -66,8 +65,6 @@
*/
public final class SourceDirectoryCalculator {

private static final Logger logger = Logger.getInstance(SourceDirectoryCalculator.class);

private static final Splitter PACKAGE_SPLITTER = Splitter.on('.');
private static final Splitter PATH_SPLITTER = Splitter.on('/');
private static final Joiner PACKAGE_JOINER = Joiner.on('.');
Expand Down Expand Up @@ -243,8 +240,7 @@ private void calculateJavaSourceDirectories(
}
}
} catch (ExecutionException | InterruptedException e) {
logger.error(e);
throw new IllegalStateException("Could not read sources");
throw new IllegalStateException("Could not read sources", e);
}

// Sort source roots into their respective directories
Expand Down

0 comments on commit 6fd5f09

Please sign in to comment.