Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableList;
import com.google.idea.blaze.base.io.FileOperationProvider;
import com.google.idea.blaze.base.run.RuntimeArtifactCache;
import com.google.idea.blaze.base.run.RuntimeArtifactKind;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.google.idea.blaze.common.Label;
import com.google.idea.blaze.common.artifact.BlazeArtifact;
Expand All @@ -29,7 +30,9 @@
import java.nio.file.Path;
import java.util.Collection;

/** A file artifact available on the local file system. */
/**
* A file artifact available on the local file system.
*/
public interface LocalFileArtifact extends BlazeArtifact {

/**
Expand All @@ -39,9 +42,11 @@ public interface LocalFileArtifact extends BlazeArtifact {
* runfiles directories).
*/
static ImmutableList<File> getLocalFiles(Label target, Collection<? extends OutputArtifact> artifacts, BlazeContext context,
Project project) {
Project project, RuntimeArtifactKind artifactKind) {
var runtimeArtifactCache = RuntimeArtifactCache.getInstance(project);
return runtimeArtifactCache.fetchArtifacts(target, artifacts.stream().toList(), context).stream().map(Path::toFile).collect(toImmutableList());
return runtimeArtifactCache.fetchArtifacts(target, artifacts.stream().toList(), context, artifactKind).stream()
.map(Path::toFile)
.collect(toImmutableList());
}

/**
Expand All @@ -53,7 +58,7 @@ static ImmutableList<File> getLocalFiles(Label target, Collection<? extends Outp
static ImmutableList<File> getLocalFilesForLegacySync(Collection<? extends BlazeArtifact> artifacts) {
return artifacts.stream()
.filter(a -> a instanceof LocalFileArtifact)
.map(a -> ((LocalFileArtifact) a).getFile())
.map(a -> ((LocalFileArtifact)a).getFile())
.collect(toImmutableList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ static RuntimeArtifactCache getInstance(Project project) {
* build.
*/
ImmutableList<Path> fetchArtifacts(
Label target, List<? extends OutputArtifact> artifacts, BlazeContext context);
Label target, List<? extends OutputArtifact> artifacts, BlazeContext context, RuntimeArtifactKind artifactKind);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.idea.blaze.qsync.project.ProjectProto.ProjectArtifact;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -43,7 +44,7 @@

public final class RuntimeArtifactCacheImpl implements RuntimeArtifactCache {
private static final Logger logger = Logger.getInstance(RuntimeArtifactCacheImpl.class);
private final Map<Label, Map<Path, ProjectProto.ProjectArtifact>> artifactCacheMap = new HashMap<>();
private final Map<Pair<Label, RuntimeArtifactKind>, Map<Path, ProjectArtifact>> artifactCacheMap = new HashMap<>();
private final Path runfilesDirectory;
private static final String SEPARATOR_DIR_NAME = "_";
private final BuildArtifactCache buildArtifactCache;
Expand All @@ -67,20 +68,21 @@ private RuntimeArtifactCacheImpl(Project project) throws IOException {

@Override
public ImmutableList<Path> fetchArtifacts(
Label target, List<? extends OutputArtifact> artifacts, BlazeContext context) {
Label target, List<? extends OutputArtifact> artifacts, BlazeContext context, RuntimeArtifactKind artifactKind) {
var targetKind = Pair.create(target, artifactKind);
final var artifactsCachedFuture =
buildArtifactCache.addAll(artifacts.stream().collect(toImmutableList()), context);
artifactCacheMap.put(target, buildArtifactLayout(target, artifacts));
artifactCacheMap.put(targetKind, buildArtifactLayout(target, artifacts));
final var artifactDirectoryContents = buildArtifactDirectoryContents(artifactCacheMap);
waitForArtifacts(artifactsCachedFuture);
updateArtifactDirectory(artifactDirectoryContents);

return resolveArtifactLayoutPaths(target, artifactCacheMap.get(target).keySet());
return resolveArtifactLayoutPaths(target, artifactKind, artifactCacheMap.get(targetKind).keySet());
}

private ImmutableList<Path> resolveArtifactLayoutPaths(Label target, Set<Path> artifactPaths) {
private ImmutableList<Path> resolveArtifactLayoutPaths(Label target, RuntimeArtifactKind artifactKind, Set<Path> artifactPaths) {
return artifactPaths.stream()
.map(artifactPath -> runfilesDirectory.resolve(getArtifactLocalPath(target, artifactPath)))
.map(artifactPath -> runfilesDirectory.resolve(getArtifactLocalPath(target, artifactKind, artifactPath)))
.collect(toImmutableList());
}

Expand Down Expand Up @@ -129,15 +131,15 @@ private void updateArtifactDirectory(
* path -> project artifact.
*/
private static ProjectProto.ArtifactDirectoryContents buildArtifactDirectoryContents(
Map<Label, Map<Path, ProjectProto.ProjectArtifact>> artifacts) {
Map<Pair<Label, RuntimeArtifactKind>, Map<Path, ProjectProto.ProjectArtifact>> artifacts) {
final var artifactDirectoryContents = ProjectProto.ArtifactDirectoryContents.newBuilder();
for (final var entry : artifacts.entrySet()) {
final var target = entry.getKey();
final var key = entry.getKey();
for (final var artifactPathAndDigest : entry.getValue().entrySet()) {
final var artifactPath = artifactPathAndDigest.getKey();
final var artifact = artifactPathAndDigest.getValue();
artifactDirectoryContents.putContents(
getArtifactLocalPath(target, artifactPath).toString(), artifact);
getArtifactLocalPath(key.first, key.second, artifactPath).toString(), artifact);
}
}
return artifactDirectoryContents.build();
Expand All @@ -147,7 +149,7 @@ private static ProjectProto.ArtifactDirectoryContents buildArtifactDirectoryCont
* Generates the local artifact path from the target and artifact path. Local Artifact path ->
* Target + SEPARATOR_DIR_NAME + artifactPath.
*/
public static Path getArtifactLocalPath(Label target, Path artifactPath) {
return target.toFilePath().resolve(Path.of(SEPARATOR_DIR_NAME)).resolve(artifactPath);
public static Path getArtifactLocalPath(Label target, RuntimeArtifactKind artifactKind, Path artifactPath) {
return target.toFilePath().resolve(Path.of(artifactKind.name())).resolve(Path.of(SEPARATOR_DIR_NAME)).resolve(artifactPath);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2025 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.idea.blaze.base.run;

public enum RuntimeArtifactKind {
APK(".apk"),
SYMBOL_FILE(".so"),
JAR(".jar");

public final String extension;

private RuntimeArtifactKind(String extension) {
this.extension = extension;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public void fetchArtifacts() throws Exception {
Label target = Label.of("//some/label:target");
List<Path> paths =
runtimeArtifactCache.fetchArtifacts(
target, ImmutableList.of(artifact1, artifact2), BlazeContext.create());
target, ImmutableList.of(artifact1, artifact2), BlazeContext.create(), RuntimeArtifactKind.JAR);
assertThat(paths)
.containsExactly(
runfilesDirectory.resolve(
RuntimeArtifactCacheImpl.getArtifactLocalPath(
target, Path.of("out/test1.jar"))),
target, RuntimeArtifactKind.JAR, Path.of("out/test1.jar"))),
runfilesDirectory.resolve(
RuntimeArtifactCacheImpl.getArtifactLocalPath(
target, Path.of("out/test2.jar"))));
target, RuntimeArtifactKind.JAR, Path.of("out/test2.jar"))));
assertThat(Files.readAllBytes(paths.get(0))).isEqualTo("abc".getBytes());
assertThat(Files.readAllBytes(paths.get(1))).isEqualTo("def".getBytes());
assertThat(testArtifactFetcher.getCopiedArtifacts()).isEqualTo(List.of(artifact1, artifact2));
Expand All @@ -112,7 +112,7 @@ public void fetchArtifacts_failed() throws Exception {
.build();
Label target = Label.of("//some/label:target");
assertThrows(IllegalStateException.class, () -> runtimeArtifactCache.fetchArtifacts(
target, ImmutableList.of(artifact1, artifact2), BlazeContext.create()));
target, ImmutableList.of(artifact1, artifact2), BlazeContext.create(), RuntimeArtifactKind.JAR));
}

private BuildArtifactCacheDirectory createBuildArtifactCache(RuntimeArtifactCacheImplTest.TestArtifactFetcher artifactFetcher) throws
Expand Down
Loading