diff --git a/packages/compile-solidity/src/compilerSupplier/index.ts b/packages/compile-solidity/src/compilerSupplier/index.ts index f24f688087a..b8954d801d1 100644 --- a/packages/compile-solidity/src/compilerSupplier/index.ts +++ b/packages/compile-solidity/src/compilerSupplier/index.ts @@ -17,11 +17,14 @@ export class CompilerSupplier { private version: string; private docker: boolean; private strategyOptions: StrategyOptions; + private nativePath: string; constructor({ events, solcConfig }) { - const { version, docker, compilerRoots, dockerTagsUrl, spawn } = solcConfig; + const { version, docker, compilerRoots, dockerTagsUrl, spawn, nativePath } = + solcConfig; this.version = version ? version : defaultSolcVersion; this.docker = docker; + this.nativePath = nativePath; this.strategyOptions = {}; if (version) this.strategyOptions.version = this.version; if (dockerTagsUrl) this.strategyOptions.dockerTagsUrl = dockerTagsUrl; @@ -44,7 +47,7 @@ export class CompilerSupplier { if (useDocker) { strategy = new Docker(this.strategyOptions); } else if (useNative) { - strategy = new Native(); + strategy = new Native(this.nativePath); } else if (useSpecifiedLocal) { strategy = new Local(); } else if (isValidVersionRange) { @@ -83,7 +86,7 @@ export class CompilerSupplier { if (useDocker) { strategy = new Docker(this.strategyOptions); } else if (useNative) { - strategy = new Native(); + strategy = new Native(this.nativePath); } else if (useSpecifiedLocal) { strategy = new Local(); } else if (isValidVersionRange) { diff --git a/packages/compile-solidity/src/compilerSupplier/loadingStrategies/Native.ts b/packages/compile-solidity/src/compilerSupplier/loadingStrategies/Native.ts index 249173d021a..b73e841b298 100644 --- a/packages/compile-solidity/src/compilerSupplier/loadingStrategies/Native.ts +++ b/packages/compile-solidity/src/compilerSupplier/loadingStrategies/Native.ts @@ -3,9 +3,15 @@ const { normalizeSolcVersion } = require("../normalizeSolcVersion"); const { NoVersionError } = require("../errors"); export class Native { + private solcPath: string; + + constructor(solcPath: string) { + this.solcPath = solcPath; + } + load() { const versionString = this.validateAndGetSolcVersion(); - const command = "solc --standard-json"; + const command = `${this.solcPath} --standard-json`; const maxBuffer = 1024 * 1024 * 50; try { @@ -25,7 +31,7 @@ export class Native { validateAndGetSolcVersion() { let version; try { - version = execSync("solc --version"); + version = execSync(`${this.solcPath} --version`); } catch (error) { throw new NoNativeError(error); } diff --git a/packages/compile-solidity/src/compilerSupplier/rangeUtils.ts b/packages/compile-solidity/src/compilerSupplier/rangeUtils.ts index 9bc410d6a70..2f266710a43 100644 --- a/packages/compile-solidity/src/compilerSupplier/rangeUtils.ts +++ b/packages/compile-solidity/src/compilerSupplier/rangeUtils.ts @@ -3,6 +3,7 @@ import fs from "fs-extra"; import semver from "semver"; import { Native, Local } from "./loadingStrategies"; import { CompilerSupplier } from "./index"; +import TruffleConfig from "@truffle/config"; /** * takes a version string which may be native or local, and resolves @@ -16,7 +17,8 @@ export function resolveToRange(version?: string): string { //if version was native or local, must determine what version that //actually corresponds to if (version === "native") { - return new Native().load().version(); + const nativePath = TruffleConfig.default().compilers.solc.nativePath; + return new Native(nativePath).load().version(); } else if (fs.existsSync(version) && path.isAbsolute(version)) { return new Local().load(version).version(); } @@ -40,11 +42,13 @@ export function rangeContainsAtLeast( ); //the following line doesn't, despite the flag, but does work with version ranges const rangeAtLeast = !!( - semver.validRange(range, { loose: true }) && - !semver.gtr(comparisonVersion, range, { - includePrerelease: true, - loose: true - }) //intersects will throw if given undefined so must ward against + ( + semver.validRange(range, { loose: true }) && + !semver.gtr(comparisonVersion, range, { + includePrerelease: true, + loose: true + }) + ) //intersects will throw if given undefined so must ward against ); return individualAtLeast || rangeAtLeast; } diff --git a/packages/config/src/configDefaults.ts b/packages/config/src/configDefaults.ts index 3c499284824..e27dad4331e 100644 --- a/packages/config/src/configDefaults.ts +++ b/packages/config/src/configDefaults.ts @@ -62,6 +62,7 @@ export const getInitialConfig = ({ }, compilers: { solc: { + nativePath: `${process.env.TRUFFLE_NATIVE_SOLC_PATH || "solc"}`, settings: { //Note: The default solc version is *not* set here! //It's set in compilerSupplier/index.js in compile-solidity