Use --override_repository instead of --package_path to inject the aspect

--package_path invalidates package loading when switching between command line
and IDE invocation. --override_repository was specifically designed not to have
that drawback

Change-Id: I0c0d10aaf23bc2b8035e2e7262584eacae9c82b6
diff --git a/BUILD b/BUILD
index 932e07a..3534b37 100644
--- a/BUILD
+++ b/BUILD
@@ -12,7 +12,7 @@
     srcs = glob(["java/**/*.java"]),
     activator = "com.google.devtools.bazel.e4b.Activator",
     bundle_name = "Eclipse 4 Bazel",
-    resources = glob(["resources/**"]) + ["//resources/tools/must/be/unique:srcs"],
+    resources = ["//resources:srcs"],
     vendor = "The Bazel Authors",
     version = VERSION,
     visibility = ["//visibility:public"],
diff --git a/WORKSPACE b/WORKSPACE
index a91ed5f..4fd9945 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -2,7 +2,7 @@
 
 load("//tools/build_defs:eclipse.bzl", "load_eclipse_deps")
 load(":bazel_version.bzl", "check_bazel_version")
-check_bazel_version("0.4.5")
+check_bazel_version("0.5.0")
 
 load_eclipse_deps()
 
diff --git a/java/com/google/devtools/bazel/e4b/BazelAspectLocationImpl.java b/java/com/google/devtools/bazel/e4b/BazelAspectLocationImpl.java
index a14cdf3..b07f605 100644
--- a/java/com/google/devtools/bazel/e4b/BazelAspectLocationImpl.java
+++ b/java/com/google/devtools/bazel/e4b/BazelAspectLocationImpl.java
@@ -14,13 +14,15 @@
 
 package com.google.devtools.bazel.e4b;
 
-import com.google.devtools.bazel.e4b.command.BazelAspectLocation;
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+
 import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.Platform;
 
+import com.google.devtools.bazel.e4b.command.BazelAspectLocation;
+
 /** Implementation of {@link BazelAspectLocation} using Eclipse OSGi Bundle locations */
 class BazelAspectLocationImpl implements BazelAspectLocation {
 
@@ -44,7 +46,7 @@
 
   @Override
   public String getAspectLabel() {
-    return "//tools/must/be/unique:e4b_aspect.bzl%e4b_aspect";
+    return "//:e4b_aspect.bzl%e4b_aspect";
   }
 
 }
diff --git a/java/com/google/devtools/bazel/e4b/command/BazelCommand.java b/java/com/google/devtools/bazel/e4b/command/BazelCommand.java
index 28fae71..85c9282 100644
--- a/java/com/google/devtools/bazel/e4b/command/BazelCommand.java
+++ b/java/com/google/devtools/bazel/e4b/command/BazelCommand.java
@@ -14,10 +14,6 @@
 
 package com.google.devtools.bazel.e4b.command;
 
-import com.google.common.base.Joiner;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.devtools.bazel.e4b.command.CommandConsole.CommandConsoleFactory;
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -29,6 +25,11 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.devtools.bazel.e4b.command.CommandConsole.CommandConsoleFactory;
+
 /**
  * Main utility to call bazel commands, wrapping its input and output to the message console.
  */
@@ -38,8 +39,8 @@
   private static Pattern VERSION_PATTERN =
       Pattern.compile("^([0-9]+)\\.([0-9]+)\\.([0-9]+)([^0-9].*)?$");
 
-  // Minimum bazel version needed to work with this plugin (currently 0.4.0)
-  private static int[] MINIMUM_BAZEL_VERSION = {0, 4, 0};
+  // Minimum bazel version needed to work with this plugin (currently 0.5.0)
+  private static int[] MINIMUM_BAZEL_VERSION = {0, 5, 0};
 
   private static enum ConsoleType {
     NO_CONSOLE, SYSTEM, WORKSPACE
@@ -62,7 +63,9 @@
     this.aspectLocation = aspectLocation;
     this.consoleFactory = consoleFactory;
     this.buildOptions =
-        ImmutableList.of("--watchfs", "--aspects=" + aspectLocation.getAspectLabel());
+        ImmutableList.of("--watchfs",
+            "--override_repository=local_eclipse_aspect=" + aspectLocation.getWorkspaceDirectory(),
+            "--aspects=@local_eclipse_aspect" + aspectLocation.getAspectLabel());
     this.aspectOptions = ImmutableList.<String>builder().addAll(buildOptions).add("-k",
         "--output_groups=ide-info-text,ide-resolve,-_,-defaults", "--experimental_show_artifacts")
         .build();
@@ -153,7 +156,6 @@
    */
   public class BazelInstance {
     private final File workspaceRoot;
-    private final String packagePath;
     private final File execRoot;
 
     private final Map<String, ImmutableMap<String, IdeBuildInfo>> buildInfoCache = new HashMap<>();
@@ -161,8 +163,6 @@
     private BazelInstance(File workspaceRoot)
         throws IOException, InterruptedException, BazelNotFoundException {
       this.workspaceRoot = workspaceRoot;
-      this.packagePath = String.join("", runBazel("info", "package_path")) + ":"
-          + aspectLocation.getWorkspaceDirectory().toString();
       this.execRoot = new File(String.join("", runBazel("info", "execution_root")));
     }
 
@@ -199,8 +199,8 @@
     private synchronized List<String> buildIdeInfo(Collection<String> targets)
         throws IOException, InterruptedException, BazelNotFoundException {
       return BazelCommand.this.runBazelAndGetErrorLines(ConsoleType.WORKSPACE, workspaceRoot,
-          ImmutableList.<String>builder().add("build").add("--package_path", packagePath)
-              .addAll(aspectOptions).addAll(targets).build(),
+          ImmutableList.<String>builder().add("build").addAll(aspectOptions).addAll(targets)
+              .build(),
           // Strip out the artifact list, keeping the e4b-build.json files.
           t -> t.startsWith(">>>") ? (t.endsWith(".e4b-build.json") ? t.substring(3) : "") : null);
     }
@@ -245,7 +245,7 @@
     public synchronized int build(List<String> targets, String... extraArgs)
         throws IOException, InterruptedException, BazelNotFoundException {
       return BazelCommand.this.runBazel(workspaceRoot,
-          ImmutableList.<String>builder().add("build", "--package_path", packagePath)
+          ImmutableList.<String>builder().add("build")
               .addAll(buildOptions).add(extraArgs).addAll(targets).build());
     }
 
@@ -257,7 +257,7 @@
     public synchronized int tests(List<String> targets, String... extraArgs)
         throws IOException, InterruptedException, BazelNotFoundException {
       return BazelCommand.this.runBazel(workspaceRoot,
-          ImmutableList.<String>builder().add("test").add("--package_path", packagePath)
+          ImmutableList.<String>builder().add("test")
               .addAll(buildOptions).add(extraArgs).addAll(targets).build());
     }
 
diff --git a/resources/BUILD b/resources/BUILD
new file mode 100644
index 0000000..78e2457
--- /dev/null
+++ b/resources/BUILD
@@ -0,0 +1,9 @@
+# This is a build file for creating a corresponding package. This package will be a local repository
+# added with --override_repository when using the aspect so we can load the e4b_aspect.bzl Skylark
+# aspect extension when calling build from Eclipse.
+
+filegroup(
+   name = "srcs",
+   srcs = glob(["**"]),
+   visibility = ["//visibility:public"],
+)
diff --git a/resources/WORKSPACE b/resources/WORKSPACE
new file mode 100644
index 0000000..11cc71a
--- /dev/null
+++ b/resources/WORKSPACE
@@ -0,0 +1,3 @@
+# This is a workspace file for creating a local repository added with --override_repository when
+# using the aspect so we can load the e4b_aspect.bzl Skylark aspect extension when calling build
+# from Eclipse.
\ No newline at end of file
diff --git a/resources/tools/must/be/unique/e4b_aspect.bzl b/resources/e4b_aspect.bzl
similarity index 100%
rename from resources/tools/must/be/unique/e4b_aspect.bzl
rename to resources/e4b_aspect.bzl
diff --git a/resources/tools/must/be/unique/BUILD b/resources/tools/must/be/unique/BUILD
deleted file mode 100644
index 3b68e55..0000000
--- a/resources/tools/must/be/unique/BUILD
+++ /dev/null
@@ -1,10 +0,0 @@
-# This is a build file for creating a corresponding package. This package will be added to the
-# package path when using the aspect so we can load the e4b_aspect.bzl Skylark aspect extension
-# when calling build from Eclipse. This package has an improbable name to avoid collision with
-# packages in the main repository.
-
-filegroup(
-   name = "srcs",
-   srcs = glob(["**"]),
-   visibility = ["//visibility:public"],
-)