Skip to content

Commit

Permalink
Add debug() step in R binary search to help with finding problems
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv committed Jul 11, 2023
1 parent a5a81f5 commit bba12ab
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/core/resources.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
* resources.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/
* resources.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/

import { existsSync, walkSync } from "fs/mod.ts";
import { dirname, join } from "path/mod.ts";
Expand All @@ -15,6 +14,7 @@ import {
kHKeyLocalMachine,
registryReadString,
} from "./registry.ts";
import { debug } from "log/mod.ts";

export function resourcePath(resource?: string): string {
const sharePath = quartoConfig.sharePath();
Expand Down Expand Up @@ -75,36 +75,49 @@ export function pandocBinaryPath(): string {
}

export async function rBinaryPath(binary: string): Promise<string> {
debug(`-- Searching for R binary --`);
// if there is a QUARTO_R environment variable then respect that
const quartoR = Deno.env.get("QUARTO_R");
debug(`Looking for '${binary}' in QUARTO_R: ${quartoR}`);
if (quartoR && existsSync(quartoR)) {
const rBinDir = Deno.statSync(quartoR).isDirectory
? quartoR
: dirname(quartoR);
debug(`Found in ${rBinDir}`);
return join(rBinDir, binary);
}

// if there is an R_HOME then respect that
const rHome = Deno.env.get("R_HOME");
debug(`Looking for '${binary}' in R_HOME: ${rHome}`);
if (rHome) {
let rHomeBin = join(rHome, "bin", binary);
if (safeExistsSync(rHomeBin)) return rHomeBin;
if (safeExistsSync(rHomeBin)) {
debug(`Found in ${rHomeBin}`);
return rHomeBin;
}
if (Deno.build.os === "windows") {
// Some installation have binaries in the sub folder only
rHomeBin = join(rHome, "bin", "x64", binary);
if (safeExistsSync(rHomeBin)) return rHomeBin;
if (safeExistsSync(rHomeBin)) {
debug(`Found in ${rHomeBin}`);
return rHomeBin;
}
}
}

// then check the path
debug(`Looking for '${binary}' in PATH.`);
const path = await which(binary);
if (path) {
debug(`Found in PATH at ${path}`);
return path;
}

// on windows check the registry for a current version
if (Deno.build.os === "windows") {
// determine current version
debug(`Looking for '${binary}' in Windows Registry.`);
const version = await registryReadString(
[kHKeyCurrentUser, kHKeyLocalMachine],
"Software\\R-core\\R",
Expand All @@ -118,10 +131,12 @@ export async function rBinaryPath(binary: string): Promise<string> {
"InstallPath",
);
if (installPath) {
debug(`Found in PATH at ${join(installPath, "bin")}`);
return join(installPath, "bin", binary);
}
}
// last ditch, try to find R in program files
debug(`Looking for '${binary}' in Windows PROGRAMFILES.`);
const progFiles = Deno.env.get("programfiles");
if (progFiles) {
// Search program files for the binary
Expand All @@ -130,6 +145,7 @@ export async function rBinaryPath(binary: string): Promise<string> {
// found the R directory, now walk to find bin directory
for (const walk of walkSync(join(progFiles, "R"))) {
if (walk.isDirectory && walk.name === "bin") {
debug(`Found ${walk.path}`);
return join(walk.path, binary);
}
}
Expand All @@ -139,6 +155,7 @@ export async function rBinaryPath(binary: string): Promise<string> {
}

// We couldn't find R, just pass the binary itself and hope it works out!
debug(`Quarto did no found ${binary} and will try to use it directly.`);
return binary;
}

Expand Down

0 comments on commit bba12ab

Please sign in to comment.