3030import java .util .HashSet ;
3131import java .util .LinkedHashSet ;
3232import java .util .List ;
33+ import java .util .Map ;
3334import java .util .Set ;
3435
3536import org .eclipse .core .resources .IWorkspaceRoot ;
6162import com .google .idea .blaze .java .sync .model .BlazeJarLibrary ;
6263import com .salesforce .bazel .eclipse .core .classpath .CompileAndRuntimeClasspath ;
6364import com .salesforce .bazel .eclipse .core .classpath .CompileAndRuntimeClasspath .Builder ;
65+ import com .salesforce .bazel .eclipse .core .model .BazelPackage ;
6466import com .salesforce .bazel .eclipse .core .model .BazelProject ;
6567import com .salesforce .bazel .eclipse .core .model .BazelTarget ;
6668import com .salesforce .bazel .eclipse .core .model .BazelWorkspace ;
6769import com .salesforce .bazel .eclipse .core .model .discovery .classpath .AccessRule ;
6870import com .salesforce .bazel .eclipse .core .model .discovery .classpath .ClasspathEntry ;
6971import com .salesforce .bazel .sdk .command .BazelBuildWithIntelliJAspectsCommand ;
7072import com .salesforce .bazel .sdk .command .querylight .BazelRuleAttribute ;
73+ import com .salesforce .bazel .sdk .command .querylight .Target ;
7174import com .salesforce .bazel .sdk .model .BazelLabel ;
7275
7376/**
@@ -331,6 +334,11 @@ public IStatus addTarget(BazelTarget bazelTarget) throws CoreException {
331334 * @throws CoreException
332335 */
333336 public CompileAndRuntimeClasspath compute () throws CoreException {
337+ return compute (null );
338+ }
339+
340+ public CompileAndRuntimeClasspath compute (Map <BazelPackage , Map <String , Target >> targetsByPackage )
341+ throws CoreException {
334342 // the code below is copied and adapted from BlazeJavaWorkspaceImporter
335343
336344 var classpathBuilder = new Builder ();
@@ -359,7 +367,7 @@ public CompileAndRuntimeClasspath compute() throws CoreException {
359367 var targetKey = targetLabel != null ? TargetKey .forPlainTarget (targetLabel ) : null ;
360368 library = new BlazeJarLibrary (libraryArtifact , targetKey );
361369 }
362- var entry = resolveLibrary (library );
370+ var entry = resolveLibrary (library , targetsByPackage );
363371 if (entry != null ) {
364372 if (!validateEntry (entry )) {
365373 continue ;
@@ -400,7 +408,7 @@ public CompileAndRuntimeClasspath compute() throws CoreException {
400408
401409 // Collect jars referenced by direct deps
402410 for (TargetKey targetKey : directDeps ) {
403- var entries = resolveDependency (targetKey );
411+ var entries = resolveDependency (targetKey , targetsByPackage );
404412 for (ClasspathEntry entry : entries ) {
405413 if (validateEntry (entry )) {
406414 classpathBuilder .addCompileEntry (entry );
@@ -410,7 +418,7 @@ public CompileAndRuntimeClasspath compute() throws CoreException {
410418
411419 // Collect jars referenced by runtime deps
412420 for (TargetKey targetKey : runtimeDeps ) {
413- var entries = resolveDependency (targetKey );
421+ var entries = resolveDependency (targetKey , targetsByPackage );
414422 var addRuntimeDependencyAsCompileEntry = includeRuntimeDependencyAsProjectCompileDependency (targetKey );
415423
416424 for (ClasspathEntry entry : entries ) {
@@ -585,8 +593,9 @@ protected boolean relevantDep(Deps.Dependency dep) {
585593 return (dep .getKind () == Deps .Dependency .Kind .EXPLICIT ) || (dep .getKind () == Deps .Dependency .Kind .IMPLICIT );
586594 }
587595
588- protected Collection <ClasspathEntry > resolveDependency (TargetKey targetKey ) throws CoreException {
589- var projectEntry = resolveProject (targetKey );
596+ protected Collection <ClasspathEntry > resolveDependency (TargetKey targetKey ,
597+ Map <BazelPackage , Map <String , Target >> targetsByPackage ) throws CoreException {
598+ var projectEntry = resolveProject (targetKey , targetsByPackage );
590599 if (projectEntry != null ) {
591600 return Set .of (projectEntry );
592601 }
@@ -623,10 +632,11 @@ protected BlazeArtifact resolveJdepsOutput(TargetIdeInfo target) {
623632 return locationDecoder .resolveOutput (javaIdeInfo .getJdepsFile ());
624633 }
625634
626- private ClasspathEntry resolveLibrary (BlazeJarLibrary library ) throws CoreException {
635+ private ClasspathEntry resolveLibrary (BlazeJarLibrary library ,
636+ Map <BazelPackage , Map <String , Target >> targetsByPackage ) throws CoreException {
627637 // find project in workspace if possible
628638 if (library .targetKey != null ) {
629- var projectEntry = resolveProject (library .targetKey );
639+ var projectEntry = resolveProject (library .targetKey , targetsByPackage );
630640 if (projectEntry != null ) {
631641 return projectEntry ;
632642 }
@@ -636,7 +646,8 @@ private ClasspathEntry resolveLibrary(BlazeJarLibrary library) throws CoreExcept
636646 return resolveJar (library .libraryArtifact );
637647 }
638648
639- private ClasspathEntry resolveProject (final Label targetLabel ) throws CoreException {
649+ private ClasspathEntry resolveProject (final Label targetLabel ,
650+ Map <BazelPackage , Map <String , Target >> targetsByPackage ) throws CoreException {
640651 var workspace = bazelWorkspace ;
641652
642653 // check for project mapping (it trumps everything)
@@ -686,10 +697,18 @@ private ClasspathEntry resolveProject(final Label targetLabel) throws CoreExcept
686697 }
687698 }
688699 var bazelPackage = workspace .getBazelPackage (forPosix (targetLabel .blazePackage ().relativePath ()));
689- var bazelTarget = bazelPackage .getBazelTarget (targetLabel .targetName ().toString ());
690- if (bazelTarget .hasBazelProject () && bazelTarget .getBazelProject ().getProject ().isAccessible ()) {
691- // a direct target match is preferred
692- return newProjectReference (targetLabel , bazelTarget .getBazelProject ());
700+ if (targetsByPackage != null ) {
701+ var targets = targetsByPackage .get (bazelPackage );
702+ bazelPackage .setTargets (targets );
703+ }
704+ var strategy = new TargetDiscoveryAndProvisioningExtensionLookup ()
705+ .createTargetProvisioningStrategy (bazelWorkspace .getBazelProjectView ());
706+ if (strategy instanceof ProjectPerTargetProvisioningStrategy ) {
707+ var bazelTarget = bazelPackage .getBazelTarget (targetLabel .targetName ().toString ());
708+ if (bazelTarget .hasBazelProject () && bazelTarget .getBazelProject ().getProject ().isAccessible ()) {
709+ // a direct target match is preferred
710+ return newProjectReference (targetLabel , bazelTarget .getBazelProject ());
711+ }
693712 }
694713 if (bazelPackage .hasBazelProject () && bazelPackage .getBazelProject ().getProject ().isAccessible ()) {
695714 // we have to check the target name is part of the enabled project list
@@ -701,7 +720,7 @@ private ClasspathEntry resolveProject(final Label targetLabel) throws CoreExcept
701720 .anyMatch (t -> t .getTargetName ().equals (targetName ))) {
702721 return newProjectReference (targetLabel , bazelPackage .getBazelProject ());
703722 }
704-
723+ var bazelTarget = bazelPackage . getBazelTarget ( targetLabel . targetName (). toString ());
705724 // it may be possible that the target is explicitly hidden from IDEs
706725 // in this case, it won't match in the above condition, however, we still want to make it a project references
707726 // the reason is that we do expect the project to represent the package adequately
@@ -715,12 +734,13 @@ private ClasspathEntry resolveProject(final Label targetLabel) throws CoreExcept
715734 return null ;
716735 }
717736
718- protected ClasspathEntry resolveProject (TargetKey targetKey ) throws CoreException {
737+ protected ClasspathEntry resolveProject (TargetKey targetKey ,
738+ Map <BazelPackage , Map <String , Target >> targetsByPackage ) throws CoreException {
719739 if (!targetKey .isPlainTarget ()) {
720740 return null ;
721741 }
722742
723- return resolveProject (targetKey .getLabel ());
743+ return resolveProject (targetKey .getLabel (), targetsByPackage );
724744 }
725745
726746 /**
0 commit comments