1717import static com .salesforce .bazel .eclipse .core .BazelCoreSharedContstants .FILE_NAME_REPO_BAZEL ;
1818import static com .salesforce .bazel .eclipse .core .BazelCoreSharedContstants .FILE_NAME_WORKSPACE ;
1919import static com .salesforce .bazel .eclipse .core .BazelCoreSharedContstants .FILE_NAME_WORKSPACE_BAZEL ;
20- import static com .salesforce .bazel .eclipse .core .model .BazelPackageInfo .queryForTargets ;
2120import static java .lang .String .format ;
2221import static java .nio .file .Files .isDirectory ;
2322import static java .nio .file .Files .isRegularFile ;
2423import static java .util .Objects .requireNonNull ;
2524import static java .util .function .Predicate .not ;
25+ import static java .util .stream .Collectors .joining ;
2626import static java .util .stream .Collectors .toList ;
2727
2828import java .nio .file .Path ;
2929import java .util .Collection ;
3030import java .util .Collections ;
31+ import java .util .HashMap ;
3132import java .util .List ;
33+ import java .util .Map ;
3234import java .util .Objects ;
3335import java .util .function .Predicate ;
3436import java .util .stream .Stream ;
3537
38+ import org .eclipse .core .resources .IProject ;
3639import org .eclipse .core .runtime .CoreException ;
3740import org .eclipse .core .runtime .IPath ;
3841import org .eclipse .core .runtime .Status ;
4346import com .salesforce .bazel .eclipse .core .projectview .BazelProjectView ;
4447import com .salesforce .bazel .sdk .BazelVersion ;
4548import com .salesforce .bazel .sdk .command .BazelBinary ;
49+ import com .salesforce .bazel .sdk .command .BazelQueryForTargetProtoCommand ;
50+ import com .salesforce .bazel .sdk .command .querylight .Target ;
4651import com .salesforce .bazel .sdk .model .BazelLabel ;
4752
4853/**
@@ -218,6 +223,11 @@ public boolean exists() {
218223 return isDirectory (path ) && (findWorkspaceFile (path ) != null );
219224 }
220225
226+ public IProject findProject (BazelPackage bazelPackage ) throws CoreException {
227+ var info = bazelPackage .getBazelWorkspace ().getInfo ();
228+ return info .getProject (bazelPackage .getLabel ());
229+ }
230+
221231 /**
222232 * @return a collection of all targets loaded in memory
223233 */
@@ -616,7 +626,7 @@ public void open(Collection<BazelPackage> bazelPackages) throws CoreException {
616626 }
617627
618628 // open all closed projects
619- var targetsByPackage = queryForTargets (this , closedPackages , getCommandExecutor ());
629+ var targetsByPackage = queryForTargetsWithDependencies (this , closedPackages , getCommandExecutor ());
620630 for (BazelPackage bazelPackage : closedPackages ) {
621631 if (bazelPackage .hasInfo ()) {
622632 continue ;
@@ -634,6 +644,11 @@ public void open(Collection<BazelPackage> bazelPackages) throws CoreException {
634644 LOG .warn ("Package '{}' does not have a BUILD file, skipping" , bazelPackage .getLabel ());
635645 continue ;
636646 }
647+ bazelPackage .setTargets (targets );
648+ if (!bazelPackage .hasInfo ()) {
649+ // getting the info loads the package avoiding unnecessary double loads
650+ bazelPackage .getInfo ();
651+ }
637652
638653 // Create PackageInfo directly with the batched query results
639654 var packageInfo = new BazelPackageInfo (buildFile , bazelPackage , targets );
@@ -644,7 +659,77 @@ public void open(Collection<BazelPackage> bazelPackages) throws CoreException {
644659 }
645660 }
646661
662+ public Map <BazelPackage , Map <String , Target >> queryForTargetsWithDependencies (BazelWorkspace bazelWorkspace ,
663+ Collection <BazelPackage > bazelPackages , BazelElementCommandExecutor bazelElementCommandExecutor )
664+ throws CoreException {
665+ // bazel query 'kind(rule, deps(//foo:all + //bar:all))"'
666+
667+ if (bazelPackages .isEmpty ()) {
668+ return Collections .emptyMap ();
669+ }
670+ var workspaceRoot = bazelWorkspace .getLocation ().toPath ();
671+ var query = bazelPackages .stream ()
672+ .map (bazelPackage -> format ("//%s:all" , bazelPackage .getWorkspaceRelativePath ()))
673+ .collect (joining (" + " ));
674+
675+ Map <String , BazelPackage > bazelPackageByWorkspaceRelativePath = new HashMap <>();
676+ bazelPackages .stream ()
677+ .forEach (p -> bazelPackageByWorkspaceRelativePath .put (p .getWorkspaceRelativePath ().toString (), p ));
678+
679+ query = "kind(rule, deps(" + query + "))" ;
680+ Map <BazelPackage , Map <String , Target >> result = new HashMap <>();
681+ LOG .debug ("{}: querying Bazel for list of targets from: {}" , bazelWorkspace , query );
682+ var queryResult = bazelElementCommandExecutor .runQueryWithoutLock (
683+ new BazelQueryForTargetProtoCommand (
684+ workspaceRoot ,
685+ query ,
686+ true /* keep going */ ,
687+ List .of ("--noproto:locations" , "--noproto:default_values" , "--noimplicit_deps" , "--notool_deps" ),
688+ format (
689+ "Loading targets for %d %s" ,
690+ bazelPackages .size (),
691+ bazelPackages .size () == 1 ? "package" : "packages" )));
692+ for (Target target : queryResult ) {
693+ if (!target .hasRule ()) {
694+ LOG .trace ("{}: ignoring target: {}" , bazelWorkspace , target );
695+ System .out .println ();
696+ continue ;
697+ }
698+
699+ try {
700+ BazelLabel .validateLabelPath (target .rule ().name (), true );
701+ } catch (Exception e ) {
702+ LOG .trace ("{}: ignoring target: {}" , bazelWorkspace , target );
703+ continue ;
704+ }
705+ LOG .trace ("{}: found target: {}" , bazelWorkspace , target );
706+ var targetLabel = new BazelLabel (target .rule ().name ());
707+
708+ var bazelPackage = bazelPackageByWorkspaceRelativePath .get (targetLabel .getPackagePath ());
709+ if (bazelPackage == null ) {
710+ // LOG.debug("{}: ignoring target for unknown package: {}", bazelWorkspace, targetLabel);
711+ // continue;
712+ var packageLabel = targetLabel .getPackageLabel ();
713+ if (bazelWorkspace .isRootedAtThisWorkspace (packageLabel )) {
714+ bazelPackage = bazelWorkspace .getBazelPackage (packageLabel );
715+ }
716+ }
717+ if (bazelPackage == null ) {
718+ LOG .debug ("{}: ignoring target for unknown package: {}" , bazelWorkspace , targetLabel );
719+ continue ;
720+ }
721+ if (!result .containsKey (bazelPackage )) {
722+ result .put (bazelPackage , new HashMap <>());
723+ }
724+
725+ var targetName = targetLabel .getTargetName ();
726+ result .get (bazelPackage ).put (targetName , target );
727+ }
728+ return result ;
729+ }
730+
647731 Path workspacePath () {
648732 return root .toPath ();
649733 }
734+
650735}
0 commit comments