Skip to content

Commit

Permalink
Modify contract for more flexibility
Browse files Browse the repository at this point in the history
Instead of using BazelPackage and BazelTarget the
TargetDiscoveryStrategy now uses more flexible Bazel IJ classes.

This allows an optimization for a future strategy that does ignore the
targets (to save one additional query operation).
  • Loading branch information
guw committed Nov 28, 2024
1 parent facf206 commit acfec2f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.eclipse.core.resources.IResource.DEPTH_ONE;
import static org.eclipse.core.resources.IResource.FORCE;
import static org.eclipse.core.resources.IResource.NEVER_DELETE_PROJECT_CONTENT;
import static org.eclipse.core.runtime.IPath.forPosix;

import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -62,6 +63,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot;
Expand All @@ -76,7 +78,6 @@
import com.salesforce.bazel.eclipse.core.util.trace.TraceGraphDumper;
import com.salesforce.bazel.eclipse.core.util.trace.TracingSubMonitor;
import com.salesforce.bazel.sdk.command.BazelQueryForLabelsCommand;
import com.salesforce.bazel.sdk.model.BazelLabel;
import com.salesforce.bazel.sdk.projectview.ImportRoots;

/**
Expand Down Expand Up @@ -270,11 +271,11 @@ public ISchedulingRule detectMissingRule() {
return null;
}

private Set<BazelTarget> detectTargetsToMaterializeInEclipse(IProject workspaceProject, TracingSubMonitor monitor,
int work) throws CoreException {
private Set<TargetExpression> detectTargetsToMaterializeInEclipse(IProject workspaceProject,
TracingSubMonitor monitor, int work) throws CoreException {
monitor = monitor.split(work, "Detecting targets");

Set<BazelTarget> result = new HashSet<>();
Set<TargetExpression> result = new HashSet<>();

if (projectView.deriveTargetsFromDirectories()) {
// use strategy configured for workspace
Expand All @@ -299,7 +300,7 @@ private Set<BazelTarget> detectTargetsToMaterializeInEclipse(IProject workspaceP
// filter packages to remove excludes
bazelPackages = bazelPackages.stream().filter(bazelPackage -> {
// filter packages based in includes
var directory = bazelPackage.getWorkspaceRelativePath();
var directory = forPosix(bazelPackage.relativePath());
if (!includeEverything && !findPathOrAnyParentInSet(directory, allowedDirectories)) {
return false;
}
Expand All @@ -315,8 +316,8 @@ private Set<BazelTarget> detectTargetsToMaterializeInEclipse(IProject workspaceP
var bazelTargets = targetDiscoveryStrategy.discoverTargets(workspace, bazelPackages, monitor.slice(1));

// add only targets not explicitly excluded
for (BazelTarget t : bazelTargets) {
if (!importRoots.targetInProject(t.getLabel().toPrimitive())) {
for (TargetExpression t : bazelTargets) {
if (t instanceof Label l && !importRoots.targetInProject(l)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Excluding target '{}' per project view exclusion", t);
}
Expand All @@ -336,8 +337,13 @@ private Set<BazelTarget> detectTargetsToMaterializeInEclipse(IProject workspaceP
"Identifying manual specified targets to synchronize");
Collection<String> labels = workspace.getCommandExecutor().runQueryWithoutLock(queryCommand);
for (String label : labels) {
var bazelLabel = new BazelLabel(label.toString());
result.add(workspace.getBazelTarget(bazelLabel));
var targetExpression = Label.validate(label) == null ? TargetExpression.fromStringSafe(label)
: TargetExpression.fromStringSafe(label);
if (targetExpression != null) {
result.add(targetExpression);
}
//var bazelLabel = new BazelLabel(label.toString());
//result.add(workspace.getBazelTarget(bazelLabel));
}
}

Expand Down Expand Up @@ -529,8 +535,8 @@ private void logSyncStats(String workspaceName, Duration duration, int projectsC
lines.stream().collect(joining(System.lineSeparator())));
}

private List<BazelProject> provisionProjectsForTarget(Set<BazelTarget> targets, TracingSubMonitor monitor, int work)
throws CoreException {
private List<BazelProject> provisionProjectsForTarget(Set<TargetExpression> targets, TracingSubMonitor monitor,
int work) throws CoreException {
return getTargetProvisioningStrategy()
.provisionProjectsForSelectedTargets(targets, workspace, monitor.split(work, "Provisioning Projects"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.function.Predicate;
Expand Down Expand Up @@ -88,7 +89,9 @@
import org.slf4j.LoggerFactory;

import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.model.primitives.TargetName;
import com.google.idea.blaze.base.model.primitives.WildcardTargetPattern;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.salesforce.bazel.eclipse.core.BazelCoreSharedContstants;
import com.salesforce.bazel.eclipse.core.model.BazelElement;
Expand Down Expand Up @@ -1666,13 +1669,38 @@ protected void linkSourcesIntoProject(BazelProject project, JavaProjectInfo java
}

@Override
public List<BazelProject> provisionProjectsForSelectedTargets(Collection<BazelTarget> targets,
public List<BazelProject> provisionProjectsForSelectedTargets(Collection<TargetExpression> targetsOrPackages,
BazelWorkspace workspace, IProgressMonitor progress) throws CoreException {
try {
var monitor = TracingSubMonitor.convert(progress, "Provisioning projects", 3);

// load all packages to be provisioned
workspace.open(targets.stream().map(BazelTarget::getBazelPackage).distinct().toList());
// open all packages at once
monitor.subTask("Loading packages");
var bazelPackages = targetsOrPackages.parallelStream().map(e -> {
if (e instanceof Label l) {
return new BazelLabel(l.toString());
}
var w = WildcardTargetPattern.stripWildcardSuffix(e.toString());
if (w != null) {
return new BazelLabel(w);
}
return null;
}).filter(Predicate.not(Objects::isNull)).map(workspace::getBazelPackage).distinct().toList();
workspace.open(bazelPackages);

// collect targets
monitor.subTask("Collecting targets");
List<BazelTarget> targets = new ArrayList<>();
for (TargetExpression targetExpression : targetsOrPackages) {
if (targetExpression instanceof Label l) {
// we don't check for no-ide tag here because we assume this was done already when discovering targets
targets.add(workspace.getBazelTarget(new BazelLabel(l.toString())));
} else {
LOG.warn(
"Ignoring target expression '{}' for provisioning because this is not supported by the current implementation.",
targetExpression);
}
}

// ensure there is a mapper
fileSystemMapper = new BazelProjectFileSystemMapper(workspace);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.salesforce.bazel.eclipse.core.model.discovery;

import static com.salesforce.bazel.eclipse.core.model.BazelWorkspace.findWorkspaceFile;
import static java.lang.String.format;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.joining;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
Expand All @@ -14,9 +17,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.salesforce.bazel.eclipse.core.model.BazelPackage;
import com.salesforce.bazel.eclipse.core.model.BazelTarget;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.salesforce.bazel.eclipse.core.model.BazelWorkspace;
import com.salesforce.bazel.sdk.command.BazelQueryForLabelsCommand;
import com.salesforce.bazel.sdk.command.BazelQueryForPackagesCommand;

/**
Expand All @@ -34,7 +39,7 @@ public class BazelQueryTargetDiscovery implements TargetDiscoveryStrategy {
private static Logger LOG = LoggerFactory.getLogger(BazelQueryTargetDiscovery.class);

@Override
public Collection<BazelPackage> discoverPackages(BazelWorkspace bazelWorkspace, IProgressMonitor progress)
public Collection<WorkspacePath> discoverPackages(BazelWorkspace bazelWorkspace, IProgressMonitor progress)
throws CoreException {
var monitor = SubMonitor.convert(progress, 100);

Expand All @@ -50,7 +55,7 @@ public Collection<BazelPackage> discoverPackages(BazelWorkspace bazelWorkspace,
monitor.worked(1);
monitor.setWorkRemaining(labels.size());

var result = new ArrayList<BazelPackage>();
var result = new ArrayList<WorkspacePath>();
for (String label : labels) {
monitor.worked(1);

Expand All @@ -72,41 +77,31 @@ public Collection<BazelPackage> discoverPackages(BazelWorkspace bazelWorkspace,
continue;
}

var bazelPackage = bazelWorkspace.getBazelPackage(packagePath);
if (bazelPackage.exists()) {
result.add(bazelPackage);
}

result.add(new WorkspacePath(packagePath.toString()));
}
return result;
}

@Override
public Collection<BazelTarget> discoverTargets(BazelWorkspace bazelWorkspace,
Collection<BazelPackage> bazelPackages, IProgressMonitor progress) throws CoreException {
public Collection<TargetExpression> discoverTargets(BazelWorkspace bazelWorkspace,
Collection<WorkspacePath> bazelPackages, IProgressMonitor progress) throws CoreException {
try {
var monitor = SubMonitor.convert(progress, "Querying targets", 1 + bazelPackages.size());

// open all packages at once
monitor.subTask("Loading package info");
bazelWorkspace.open(bazelPackages);

// collect targets
monitor.subTask("Collecting targets");
List<BazelTarget> targets = new ArrayList<>();
for (BazelPackage bazelPackage : bazelPackages) {
monitor.checkCanceled();
if (LOG.isDebugEnabled()) {
LOG.debug(
"Discovered targets in package '{}': {}",
bazelPackage.getLabel(),
bazelPackage.getBazelTargets());
}
bazelPackage.getBazelTargets().stream().filter(BazelTarget::isVisibleToIde).forEach(targets::add);
monitor.worked(1);
}

return targets;
var monitor = SubMonitor.convert(progress, "Discovering targets", 1 + bazelPackages.size());

monitor.subTask("Querying for targets");
Collection<String> labels = bazelWorkspace.getCommandExecutor()
.runQueryWithoutLock(
new BazelQueryForLabelsCommand(
bazelWorkspace.getLocation().toPath(),
format(
"let all_target = kind(.*rule, %s) in $all_target - attr(tags, 'no-ide', $all_target)",
bazelPackages.stream()
.map(p -> "//" + p.relativePath().toString() + ":all")
.collect(joining(" + "))),
true,
"Querying for targets to synchronize"));

return labels.parallelStream().map(Label::fromStringSafe).filter(not(Objects::isNull)).toList();
} finally {
if (progress != null) {
progress.done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.salesforce.bazel.eclipse.core.model.BazelPackage;
import com.salesforce.bazel.eclipse.core.model.BazelTarget;
import com.salesforce.bazel.eclipse.core.model.BazelWorkspace;
import com.salesforce.bazel.eclipse.core.model.SynchronizeProjectViewJob;

Expand Down Expand Up @@ -35,7 +36,8 @@ public interface TargetDiscoveryStrategy {
* <p>
* This method is typically called by {@link SynchronizeProjectViewJob} before
* {@link #discoverTargets(BazelWorkspace, Collection, IProgressMonitor)} is called. However, the list of packages
* may be further reduced.
* may be further reduced by the caller. Therefore implementors must not expect the return value to be passed to
* {@link #discoverTargets(BazelWorkspace, Collection, IProgressMonitor)}.
* </p>
*
* @param bazelWorkspace
Expand All @@ -45,7 +47,7 @@ public interface TargetDiscoveryStrategy {
* not need to call {@link IProgressMonitor#done()}
* @return the found packages (never <code>null</code>)
*/
Collection<BazelPackage> discoverPackages(BazelWorkspace bazelWorkspace, IProgressMonitor progress)
Collection<WorkspacePath> discoverPackages(BazelWorkspace bazelWorkspace, IProgressMonitor progress)
throws CoreException;

/**
Expand All @@ -70,6 +72,6 @@ Collection<BazelPackage> discoverPackages(BazelWorkspace bazelWorkspace, IProgre
*
* @return the found targets (never <code>null</code>)
*/
Collection<BazelTarget> discoverTargets(BazelWorkspace bazelWorkspace, Collection<BazelPackage> bazelPackages,
Collection<TargetExpression> discoverTargets(BazelWorkspace bazelWorkspace, Collection<WorkspacePath> bazelPackages,
IProgressMonitor progress) throws CoreException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.core.IClasspathEntry;

import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.salesforce.bazel.eclipse.core.classpath.BazelClasspathScope;
import com.salesforce.bazel.eclipse.core.classpath.CompileAndRuntimeClasspath;
import com.salesforce.bazel.eclipse.core.model.BazelProject;
import com.salesforce.bazel.eclipse.core.model.BazelTarget;
import com.salesforce.bazel.eclipse.core.model.BazelWorkspace;
import com.salesforce.bazel.eclipse.core.model.SynchronizeProjectViewJob;

Expand Down Expand Up @@ -128,7 +128,6 @@ Map<BazelProject, CompileAndRuntimeClasspath> computeClasspaths(Collection<Bazel
* a monitor for tracking progress and observing cancellations (never <code>null</code>)
* @return a list of provisioned projects (never <code>null</code>)
*/
List<BazelProject> provisionProjectsForSelectedTargets(Collection<BazelTarget> targets, BazelWorkspace workspace,
IProgressMonitor progress) throws CoreException;

List<BazelProject> provisionProjectsForSelectedTargets(Collection<TargetExpression> targets,
BazelWorkspace workspace, IProgressMonitor progress) throws CoreException;
}

0 comments on commit acfec2f

Please sign in to comment.