Fix bash completion script location in the Debian package

It was ending up installed as /bazel.

Also, the documented way of telling pkg_tar not to strip off the prefix
didn't work.

--
Change-Id: I593d17690526c614697369cab543aff1ba67de0a
Reviewed-on: https://bazel-review.googlesource.com/#/c/2222/
MOS_MIGRATED_REVID=107229260
diff --git a/scripts/packages/BUILD b/scripts/packages/BUILD
index 0e47b20..d9ac0ed 100644
--- a/scripts/packages/BUILD
+++ b/scripts/packages/BUILD
@@ -71,6 +71,7 @@
     name = "bazel-completion",
     files = [":etc/bash_completion.d/bazel"],
     mode = "0644",
+    strip_prefix = ".",
 )
 
 pkg_tar(
diff --git a/tools/build_defs/pkg/BUILD b/tools/build_defs/pkg/BUILD
index 22aef61..cc6a7a0 100644
--- a/tools/build_defs/pkg/BUILD
+++ b/tools/build_defs/pkg/BUILD
@@ -80,6 +80,37 @@
     ".bz2",
 ]]
 
+pkg_tar(
+    name = "test-tar-strip_prefix-empty",
+    files = [
+        ":etc/nsswitch.conf",
+    ],
+    strip_prefix = "",
+)
+
+pkg_tar(
+    name = "test-tar-strip_prefix-none",
+    files = [
+        ":etc/nsswitch.conf",
+    ],
+)
+
+pkg_tar(
+    name = "test-tar-strip_prefix-etc",
+    files = [
+        ":etc/nsswitch.conf",
+    ],
+    strip_prefix = "etc",
+)
+
+pkg_tar(
+    name = "test-tar-strip_prefix-dot",
+    files = [
+        ":etc/nsswitch.conf",
+    ],
+    strip_prefix = ".",
+)
+
 pkg_deb(
     name = "test-deb",
     data = ":test-tar-gz.tar.gz",
@@ -105,6 +136,10 @@
         ":test-tar-.tar",
         ":test-tar-bz2.tar.bz2",
         ":test-tar-gz.tar.gz",
+        ":test-tar-strip_prefix-dot.tar",
+        ":test-tar-strip_prefix-empty.tar",
+        ":test-tar-strip_prefix-etc.tar",
+        ":test-tar-strip_prefix-none.tar",
     ],
     deps = [
         "//src/test/shell:bashunit",
diff --git a/tools/build_defs/pkg/README.md b/tools/build_defs/pkg/README.md
index eaa8f80..384043d 100644
--- a/tools/build_defs/pkg/README.md
+++ b/tools/build_defs/pkg/README.md
@@ -135,10 +135,11 @@
           tarball but a prefix path determined by <code>strip_prefix</code>
           is removed from the directory structure. This path can
           be absolute from the workspace root if starting with a <code>/</code> or
-          relative to the rule's directory. A relative path may starts with "./"
-          (or be ".") but cannot use go up with "..". By default, the
+          relative to the rule's directory. A relative path may start with "./"
+          (or be ".") but cannot use ".." to go up level(s). By default, the
           <code>data_path</code> attribute is unused and all files are supposed to have no
-          prefix.
+          prefix. A <code>data_path</code> of "" (the empty string) means the
+          same as the default.
         </p>
       </td>
     </tr>
diff --git a/tools/build_defs/pkg/build_test.sh b/tools/build_defs/pkg/build_test.sh
index daa6125..d0caa2c 100755
--- a/tools/build_defs/pkg/build_test.sh
+++ b/tools/build_defs/pkg/build_test.sh
@@ -61,6 +61,11 @@
     check_eq "-rwxr-xr-x" "$(get_tar_permission test-tar-${i:1}.tar$i ./usr/titi)"
     check_eq "-rw-r--r--" "$(get_tar_permission test-tar-${i:1}.tar$i ./etc/nsswitch.conf)"
   done;
+
+  check_eq "./nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-empty.tar)"
+  check_eq "./nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-none.tar)"
+  check_eq "./nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-empty.tar)"
+  check_eq "./etc/nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-dot.tar)"
 }
 
 function test_deb() {
diff --git a/tools/build_defs/pkg/pkg.bzl b/tools/build_defs/pkg/pkg.bzl
index d8a6964..399369c 100644
--- a/tools/build_defs/pkg/pkg.bzl
+++ b/tools/build_defs/pkg/pkg.bzl
@@ -41,12 +41,12 @@
     # There is no way to handle .// correctly (no function that would make
     # that possible and Skylark is not turing complete) so just consider it
     # as an absolute path.
-    if data_path[0:2] == "./":
+    if len(data_path) >= 2 and data_path[0:2] == "./":
       data_path = data_path[2:]
-    if data_path[0] == "/":  # Absolute path
-      return data_path[1:]
-    elif not data_path or data_path == ".":  # Relative to current package
+    if not data_path or data_path == ".":  # Relative to current package
       return _short_path_dirname(out)
+    elif data_path[0] == "/":  # Absolute path
+      return data_path[1:]
     else:  # Relative to a sub-directory
       return _short_path_dirname(out) + "/" + data_path
   else: