Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Native loading strategy to support TRUFFLE_NATIVE_SOLC_PATH #6007

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/compile-solidity/src/compilerSupplier/index.ts
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
16 changes: 10 additions & 6 deletions packages/compile-solidity/src/compilerSupplier/rangeUtils.ts
Expand Up @@ -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
Expand All @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this always uses the default path. Should this function take an optional second argument so it can be more accurate, and the uses of it updated as necessary/possible?

} else if (fs.existsSync(version) && path.isAbsolute(version)) {
return new Local().load(version).version();
}
Expand All @@ -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;
}
1 change: 1 addition & 0 deletions packages/config/src/configDefaults.ts
Expand Up @@ -62,6 +62,7 @@ export const getInitialConfig = ({
},
compilers: {
solc: {
nativePath: `${process.env.TRUFFLE_NATIVE_SOLC_PATH || "solc"}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I snuck this env override w/ my changes, and want to be sure @eggplantzzz and @haltman-at are ok with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eech. I am quite wary of that... @gnidan?

settings: {
//Note: The default solc version is *not* set here!
//It's set in compilerSupplier/index.js in compile-solidity
Expand Down