Use the workspace name in the process title of the Bazel server.

This makes way more sense than using the name of the *parent* directory of the workspace.

Closes #4253 - thanks @akira-baruah for suggesting this change and making it happen!

PiperOrigin-RevId: 184147456
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 243df26..195134c 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -394,13 +394,14 @@
 }
 
 // Returns the JVM command argument array.
-static vector<string> GetArgumentArray() {
+static vector<string> GetArgumentArray(
+    const WorkspaceLayout *workspace_layout) {
   vector<string> result;
 
   // e.g. A Blaze server process running in ~/src/build_root (where there's a
   // ~/src/build_root/WORKSPACE file) will appear in ps(1) as "blaze(src)".
   string workspace =
-      blaze_util::Basename(blaze_util::Dirname(globals->workspace));
+      workspace_layout->GetPrettyWorkspaceName(globals->workspace);
   string product = globals->options->product_name;
   blaze_util::ToLower(&product);
   result.push_back(product + "(" + workspace + ")");
@@ -673,7 +674,7 @@
 // Starts the Blaze server.
 static int StartServer(const WorkspaceLayout *workspace_layout,
                         BlazeServerStartup **server_startup) {
-  vector<string> jvm_args_vector = GetArgumentArray();
+  vector<string> jvm_args_vector = GetArgumentArray(workspace_layout);
   string argument_string = GetArgumentString(jvm_args_vector);
   string server_dir =
       blaze_util::JoinPath(globals->options->output_base, "server");
@@ -734,7 +735,7 @@
         globals->options->product_name.c_str(),
         globals->options->product_name.c_str(), product.c_str());
   }
-  vector<string> jvm_args_vector = GetArgumentArray();
+  vector<string> jvm_args_vector = GetArgumentArray(workspace_layout);
   if (!command.empty()) {
     jvm_args_vector.push_back(command);
     AddLoggingArgs(&jvm_args_vector);
@@ -1134,7 +1135,8 @@
 }
 
 // Kills the running Blaze server, if any, if the startup options do not match.
-static void KillRunningServerIfDifferentStartupOptions(BlazeServer *server) {
+static void KillRunningServerIfDifferentStartupOptions(
+    const WorkspaceLayout *workspace_layout, BlazeServer *server) {
   if (!server->Connected()) {
     return;
   }
@@ -1153,7 +1155,7 @@
   // These strings contain null-separated command line arguments. If they are
   // the same, the server can stay alive, otherwise, it needs shuffle off this
   // mortal coil.
-  if (ServerNeedsToBeKilled(arguments, GetArgumentArray())) {
+  if (ServerNeedsToBeKilled(arguments, GetArgumentArray(workspace_layout))) {
     globals->restart_reason = NEW_OPTIONS;
     PrintWarning(
         "Running %s server needs to be killed, because the "
@@ -1493,7 +1495,7 @@
 
   blaze_server->Connect();
   EnsureCorrectRunningVersion(blaze_server);
-  KillRunningServerIfDifferentStartupOptions(blaze_server);
+  KillRunningServerIfDifferentStartupOptions(workspace_layout, blaze_server);
 
   if (globals->options->batch) {
     SetScheduling(globals->options->batch_cpu_scheduling,
diff --git a/src/main/cpp/workspace_layout.cc b/src/main/cpp/workspace_layout.cc
index 99083d2..cc02c78 100644
--- a/src/main/cpp/workspace_layout.cc
+++ b/src/main/cpp/workspace_layout.cc
@@ -49,6 +49,13 @@
   return "";
 }
 
+string WorkspaceLayout::GetPrettyWorkspaceName(
+    const std::string& workspace) const {
+  // e.g. A Bazel server process running in ~/src/myproject (where there's a
+  // ~/src/myproject/WORKSPACE file) will appear in ps(1) as "bazel(myproject)".
+  return blaze_util::Basename(workspace);
+}
+
 static string FindDepotBlazerc(const blaze::WorkspaceLayout* workspace_layout,
                                const string& workspace) {
   // Package semantics are ignored here, but that's acceptable because
diff --git a/src/main/cpp/workspace_layout.h b/src/main/cpp/workspace_layout.h
index 5f5a876..a8dd6d1 100644
--- a/src/main/cpp/workspace_layout.h
+++ b/src/main/cpp/workspace_layout.h
@@ -40,6 +40,11 @@
   // relative or absolute.
   virtual std::string GetWorkspace(const std::string& cwd) const;
 
+  // Given a result returned from GetWorkspace, returns a pretty workspace name
+  // than can e.g. be used in the process title of the Bazel server.
+  virtual std::string GetPrettyWorkspaceName(
+      const std::string& workspace) const;
+
   // Returns if workspace is a valid build workspace.
   virtual bool InWorkspace(const std::string& workspace) const;