Skip to content

Commit

Permalink
Fixes issue resolving nim executable path when nimble nimDir is defin…
Browse files Browse the repository at this point in the history
…ed (#86)

* Fixed extension loading issue during development - don't use HOME environment variable to resolve paths to binaries on Windows.

* Fixed nim executable loading bug - search for nim executable in the nimble nimDir instead of returning the nimDir directly
  • Loading branch information
scupit authored Aug 11, 2024
1 parent 4960115 commit d7e7c54
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/tools/nimBinTools.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
import platform/js/[jsNode, jsNodePath, jsString, jsNodeFs, jsNodeCp]

import std/jsffi
from std/sequtils import mapIt, foldl, filterIt
from std/sequtils import mapIt, foldl, filterIt, concat
import ../[spec, nimUtils]
var binPathsCache = newMap[cstring, cstring]()

proc getBinPath*(tool: cstring): cstring =
proc getBinPath*(tool: cstring, initialSearchPaths: openArray[cstring] = []): cstring =
if binPathsCache[tool].toJs().to(bool): return binPathsCache[tool]
if not process.env["PATH"].isNil():
# USERPROFILE is the standard equivalent of HOME on windows.
let userHomeVarName = if process.platform == "win32": "USERPROFILE" else: "HOME"

# add support for choosenim
process.env["PATH"] = path.join(process.env["HOME"], ".nimble", "bin") &
let fullEnvPath = path.join(process.env[userHomeVarName], ".nimble", "bin") &
path.delimiter & process.env["PATH"]
if process.platform == "win32":
# USERPROFILE is the standard equivalent of HOME on windows.
process.env["PATH"] = path.join(
process.env["PATH"] & path.delimiter & process.env["USERPROFILE"],
".nimble",
"bin")
var pathParts = process.env["PATH"].split(path.delimiter)
var endings = if process.platform == "win32": @[".exe", ".cmd", ""]

let pathParts: seq[cstring] = concat(
@initialSearchPaths,
fullEnvPath.split(path.delimiter)
)

let endings = if process.platform == "win32": @[".exe", ".cmd", ""]
else: @[""]

let paths = pathParts.mapIt(
Expand Down Expand Up @@ -57,11 +59,13 @@ proc getBinPath*(tool: cstring): cstring =
proc getNimExecPath*(executable: cstring = "nim"): cstring =
## returns the path to the an executable by name, defaults to nim, returns an
## empty string in case it wasn't found.
if executable == "nim":
if ext.nimDir != "":
return ext.nimDir #use the nimDir from nimble when is set instead of the path
var initialPaths = newSeq[cstring]()

if executable == "nim" and ext.nimDir != "":
# use the nimDir from nimble as the initial search path when it's set.
initialPaths.add(ext.nimDir.cstring)

result = getBinPath(executable)
result = getBinPath(executable, initialPaths)
if result.isNil():
result = ""

Expand Down

0 comments on commit d7e7c54

Please sign in to comment.