Skip to content

Commit c352b36

Browse files
Improve RScript discover strategy in RPlotExporter
1 parent 7986cea commit c352b36

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/BenchmarkDotNet/Exporters/RPlotExporter.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,56 @@ public void ExportToLog(Summary summary, ILogger logger)
8080
throw new NotSupportedException();
8181
}
8282

83-
private static bool TryFindRScript(ILogger consoleLogger, out string rscriptPath)
83+
private static bool TryFindRScript(ILogger consoleLogger, out string? rscriptPath)
8484
{
8585
string rscriptExecutable = RuntimeInformation.IsWindows() ? "Rscript.exe" : "Rscript";
8686
rscriptPath = null;
87+
8788
string rHome = Environment.GetEnvironmentVariable("R_HOME");
8889
if (rHome != null)
8990
{
9091
rscriptPath = Path.Combine(rHome, "bin", rscriptExecutable);
9192
if (File.Exists(rscriptPath))
92-
{
9393
return true;
94-
}
9594

96-
consoleLogger.WriteLineError($"RPlotExporter requires R_HOME to point to the parent directory of the existing '{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}{rscriptExecutable} (currently points to {rHome})");
95+
consoleLogger.WriteLineError($"{nameof(RPlotExporter)} requires R_HOME to point to the parent directory of the existing '{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}{rscriptExecutable} (currently points to {rHome})");
96+
return false;
9797
}
9898

9999
// No R_HOME, or R_HOME points to a wrong folder, try the path
100100
rscriptPath = FindInPath(rscriptExecutable);
101-
if (rscriptPath == null)
101+
if (rscriptPath != null)
102+
return true;
103+
104+
if (RuntimeInformation.IsWindows())
102105
{
103-
consoleLogger.WriteLineError($"RPlotExporter couldn't find {rscriptExecutable} in your PATH and no R_HOME environment variable is defined");
104-
return false;
106+
string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
107+
string programFilesR = Path.Combine(programFiles, "R");
108+
if (Directory.Exists(programFilesR))
109+
{
110+
foreach (string rRootDirectory in Directory.EnumerateDirectories(programFilesR))
111+
{
112+
string rscriptPathCandidate = Path.Combine(rRootDirectory, "bin", rscriptExecutable);
113+
if (File.Exists(rscriptPathCandidate))
114+
{
115+
rscriptPath = rscriptPathCandidate;
116+
return true;
117+
}
118+
}
119+
}
105120
}
106121

107-
return true;
122+
consoleLogger.WriteLineError($"{nameof(RPlotExporter)} couldn't find {rscriptExecutable} in your PATH and no R_HOME environment variable is defined");
123+
return false;
108124
}
109125

110-
private static string FindInPath(string fileName)
126+
private static string? FindInPath(string fileName)
111127
{
112128
string path = Environment.GetEnvironmentVariable("PATH");
113129
if (path == null)
114130
return null;
115131

116-
var dirs = path.Split(Path.PathSeparator);
132+
string[] dirs = path.Split(Path.PathSeparator);
117133
foreach (string dir in dirs)
118134
{
119135
string trimmedDir = dir.Trim('\'', '"');

0 commit comments

Comments
 (0)