From 131f634028a572b206742697d5cad2122ca940b1 Mon Sep 17 00:00:00 2001
From: Ricardo Wurmus <rekado@elephly.net>
Date: Fri, 18 Dec 2020 10:26:44 +0100
Subject: [PATCH 1/7] handleClientInit: Store R versions in sessionInfo.

This unlocks the multi-version feature needed to initialize version
selection widgets.

* src/cpp/session/SessionClientInit.cpp (handleClientInit): Read
version entries from /etc/rstudio/r-versions via the RVersionsScanner.
Add discovered entries to a JSON array at the sessionInfo object
r_versions_info->available_r_versions.
---
 src/cpp/session/SessionClientInit.cpp | 31 ++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/cpp/session/SessionClientInit.cpp b/src/cpp/session/SessionClientInit.cpp
index ab1228096f..6d9e5a064b 100644
--- a/src/cpp/session/SessionClientInit.cpp
+++ b/src/cpp/session/SessionClientInit.cpp
@@ -2,6 +2,7 @@
  * SessionClientInit.hpp
  *
  * Copyright (C) 2021 by RStudio, PBC
+ * Copyright (C) 2020 Ricardo Wurmus
  *
  * Unless you have received this program directly from RStudio pursuant
  * to the terms of a commercial license agreement with RStudio, then
@@ -63,6 +64,8 @@
 #include <core/http/CSRFToken.hpp>
 #include <core/system/Environment.hpp>
 
+#include <server_core/RVersionsScanner.hpp>
+
 #include <session/SessionConsoleProcess.hpp>
 #include <session/SessionClientEventService.hpp>
 #include <session/SessionHttpConnection.hpp>
@@ -511,11 +514,29 @@ void handleClientInit(const boost::function<void()>& initFunction,
 
    sessionInfo["multi_session"] = options.multiSession();
 
-   json::Object rVersionsJson;
-   rVersionsJson["r_version"] = module_context::rVersion();
-   rVersionsJson["r_version_label"] = module_context::rVersionLabel();
-   rVersionsJson["r_home_dir"] = module_context::rHomeDir();
-   sessionInfo["r_versions_info"] = rVersionsJson;
+   // Read versions from /etc/rstudio/r-versions
+   json::Array availableRVersionsJson;
+   std::vector<r_util::RVersion> versions = RVersionsScanner().getRVersions();
+   for (r_util::RVersion& rEntry : versions)
+     {
+       json::Object rVersionJson;
+       rVersionJson["version"] = rEntry.number();
+       rVersionJson["label"] = rEntry.label();
+       rVersionJson["r_home"] = rEntry.homeDir().getAbsolutePath();
+
+       availableRVersionsJson.push_back(rVersionJson);
+     }
+
+   json::Object rVersionsInfo;
+   rVersionsInfo["available_r_versions"] = availableRVersionsJson;
+   rVersionsInfo["r_version"] = module_context::rVersion();
+   rVersionsInfo["r_version_label"] = module_context::rVersionLabel();
+   rVersionsInfo["r_home"] = module_context::rHomeDir();
+   rVersionsInfo["default_r_version"] = module_context::rVersion();
+   rVersionsInfo["default_r_version_label"] = module_context::rVersionLabel();
+   rVersionsInfo["default_r_home_dir"] = module_context::rHomeDir();
+   rVersionsInfo["restore_project_r_version"] = true;
+   sessionInfo["r_versions_info"] = rVersionsInfo;
 
    sessionInfo["show_user_home_page"] = options.showUserHomePage();
    sessionInfo["user_home_page_url"] = json::Value();
-- 
2.33.0

