summaryrefslogtreecommitdiff
path: root/patches/rstudio-server-multi-version/0002-sessionProcessConfig-Configure-R-version-from-active.patch
blob: 51050015505cb4cee052b964226580cef418c624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
From 6172ab0908e8b80bd5838ed4336335d224b80941 Mon Sep 17 00:00:00 2001
From: Ricardo Wurmus <rekado@elephly.net>
Date: Fri, 18 Dec 2020 10:32:01 +0100
Subject: [PATCH 2/7] sessionProcessConfig: Configure R version from active
 session.

When switching projects the R version is recorded in the active
session.  Before launching a new rsession process R environment
variables need to be set to select the variant of libR.so that should
be used to launch the embedded R.

* src/cpp/server/ServerSessionManager.cpp (sessionProcessConfig):
Configure R environment using the R version that has been recorded in
the active session.
---
 src/cpp/server/ServerSessionManager.cpp | 26 ++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/src/cpp/server/ServerSessionManager.cpp b/src/cpp/server/ServerSessionManager.cpp
index 6e37578cab..86d212779a 100644
--- a/src/cpp/server/ServerSessionManager.cpp
+++ b/src/cpp/server/ServerSessionManager.cpp
@@ -2,6 +2,7 @@
  * ServerSessionManager.cpp
  *
  * Copyright (C) 2021 by RStudio, PBC
+ * Copyright (C) 2020-2021 Ricardo Wurmus
  *
  * Unless you have received this program directly from RStudio pursuant
  * to the terms of a commercial license agreement with RStudio, then
@@ -21,10 +22,14 @@
 
 #include <shared_core/SafeConvert.hpp>
 #include <core/system/Process.hpp>
+#include <shared_core/system/User.hpp>
 #include <core/system/PosixUser.hpp>
 #include <core/system/Environment.hpp>
 #include <core/json/JsonRpc.hpp>
 
+#include <core/r_util/RActiveSessions.hpp>
+#include <core/r_util/RUserData.hpp>
+
 #include <monitor/MonitorClient.hpp>
 #include <session/SessionConstants.hpp>
 
@@ -156,7 +161,26 @@ core::system::ProcessConfig sessionProcessConfig(
    std::copy(extraArgs.begin(), extraArgs.end(), std::back_inserter(args));
 
    // append R environment variables
-   r_util::RVersion rVersion = r_environment::rVersion();
+   // Get the active session (e.g. after switching projects) and set R
+   // variables according to the configured R version.
+   core::system::User user;
+   core::system::User::getUserFromIdentifier(context.username, user);
+   FilePath userScratchPath_ = core::system::xdg::userDataDir(context.username);
+
+   r_util::ActiveSessions activeSessions(userScratchPath_);
+   std::vector<boost::shared_ptr<r_util::ActiveSession> > sessions =
+     activeSessions.list(user.getHomePath(), false);
+
+   r_util::RVersion rVersion;
+   if (sessions.size() > 0) {
+     // Get the R version from the active user session.  This will
+     // have been set earlier when the user switched to a project.
+     std::string errorMsg;
+     bool success = r_environment::detectRVersion(FilePath((*sessions.front()).rVersionHome()).completePath("bin/R"),
+                                   &rVersion, &errorMsg);
+   } else {
+     rVersion = r_environment::rVersion();
+   }
    core::system::Options rEnvVars = rVersion.environment();
    environment.insert(environment.end(), rEnvVars.begin(), rEnvVars.end());
    
-- 
2.31.1