Skip to content

Commit

Permalink
multiple resolc fixes and improvements (#151)
Browse files Browse the repository at this point in the history
- Error out early on compiler invocations with invalid base path or include path flags.
- Do not error out if no files and no errors were produced. This aligns resolc closer to sloc.
- Add a CLI test with an involved fixture containing multiple contract remappings to properly testing the standard JSON path.
- Fixes input normalization in the Wasm version.



Co-authored-by: Cyrill Leutwiler <[email protected]>
  • Loading branch information
Brianspha and xermicus authored Jan 17, 2025
1 parent b78b2b2 commit 06f4308
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 23 deletions.
9 changes: 9 additions & 0 deletions crates/solidity/src/solc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub mod soljson_compiler;
pub mod standard_json;
pub mod version;

use once_cell::sync::Lazy;
use semver::VersionReq;
use std::path::Path;
use std::path::PathBuf;

Expand All @@ -29,6 +31,13 @@ pub const FIRST_VIA_IR_VERSION: semver::Version = semver::Version::new(0, 8, 13)

/// The last supported version of `solc`.
pub const LAST_SUPPORTED_VERSION: semver::Version = semver::Version::new(0, 8, 28);
/// `--base-path` was introduced in 0.6.9 <https://github.com/ethereum/solidity/releases/tag/v0.6.9>
pub static FIRST_SUPPORTS_BASE_PATH: Lazy<VersionReq> =
Lazy::new(|| VersionReq::parse(">=0.6.9").unwrap());

/// `--include-path` was introduced in 0.8.8 <https://github.com/ethereum/solidity/releases/tag/v0.8.8>
pub static FIRST_SUPPORTS_INCLUDE_PATH: Lazy<VersionReq> =
Lazy::new(|| VersionReq::parse(">=0.8.8").unwrap());

/// The Solidity compiler.
pub trait Compiler {
Expand Down
20 changes: 17 additions & 3 deletions crates/solidity/src/solc/solc_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::solc::standard_json::output::Output as StandardJsonOutput;
use crate::solc::version::Version;

use super::Compiler;
use crate::solc::{FIRST_SUPPORTS_BASE_PATH, FIRST_SUPPORTS_INCLUDE_PATH};

/// The Solidity compiler.
pub struct SolcCompiler {
Expand Down Expand Up @@ -58,10 +59,23 @@ impl Compiler for SolcCompiler {
command.stdout(std::process::Stdio::piped());
command.arg("--standard-json");

if let Some(base_path) = base_path {
command.arg("--base-path");
command.arg(base_path);
if let Some(base_path) = &base_path {
if !FIRST_SUPPORTS_BASE_PATH.matches(&version.default) {
anyhow::bail!(
"--base-path not supported this version {} of solc",
&version.default
);
}
command.arg("--base-path").arg(base_path);
}

if !include_paths.is_empty() && !FIRST_SUPPORTS_INCLUDE_PATH.matches(&version.default) {
anyhow::bail!(
"--include-path not supported this version {} of solc",
&version.default
);
}

for include_path in include_paths.into_iter() {
command.arg("--include-path");
command.arg(include_path);
Expand Down
1 change: 1 addition & 0 deletions crates/solidity/src/solc/soljson_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Compiler for SoljsonCompiler {
_allow_paths: Option<String>,
) -> anyhow::Result<StandardJsonOutput> {
let version = self.version()?;
input.normalize(&version.default);
let suppressed_warnings = input.suppressed_warnings.take().unwrap_or_default();

let input_json = serde_json::to_string(&input).expect("Always valid");
Expand Down
19 changes: 7 additions & 12 deletions crates/solidity/src/solc/standard_json/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ use crate::yul::parser::statement::object::Object;
use self::contract::Contract;
use self::error::Error as SolcStandardJsonOutputError;
use self::source::Source;

/// The `solc --standard-json` output.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct Output {
/// The file-contract hashmap.
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -64,15 +63,12 @@ impl Output {

let files = match self.contracts.as_ref() {
Some(files) => files,
None => {
anyhow::bail!(
"{}",
self.errors
.as_ref()
.map(|errors| serde_json::to_string_pretty(errors).expect("Always valid"))
.unwrap_or_else(|| "Unknown project assembling error".to_owned())
);
}
None => match &self.errors {
Some(errors) if errors.iter().any(|e| e.severity == "error") => {
anyhow::bail!(serde_json::to_string_pretty(errors).expect("Always valid"));
}
_ => &BTreeMap::new(),
},
};
let mut project_contracts = BTreeMap::new();

Expand Down Expand Up @@ -159,7 +155,6 @@ impl Output {
messages.extend(polkavm_messages);
}
}

self.errors = match self.errors.take() {
Some(mut errors) => {
errors.extend(messages);
Expand Down
103 changes: 103 additions & 0 deletions crates/solidity/src/tests/cli-tests/src/contracts/compiled/1.json

Large diffs are not rendered by default.

31 changes: 23 additions & 8 deletions crates/solidity/src/tests/cli-tests/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
import * as shell from 'shelljs';
import * as fs from 'fs';
import { spawnSync } from 'child_process';

interface CommandResult {
output: string;
exitCode: number;
}

export const executeCommand = (command: string): CommandResult => {
const result = shell.exec(command, {async: false});
export const executeCommand = (command: string, stdin?: string): CommandResult => {
if (stdin) {
const process = spawnSync(command, [], {
input: stdin,
shell: true,
encoding: 'utf8',
maxBuffer: 30 * 1024 * 1024
});

return {
exitCode: process.status || 0,
output: (process.stdout || process.stderr || '').toString()
};
}

const result = shell.exec(command, { silent: true, async: false });
return {
exitCode: result.code,
output: result.stdout.trim() || result.stderr.trim(),
output: result.stdout || result.stderr || ''
};
};

export const isFolderExist = (folder: string): boolean => {
export const isFolderExist = (folder: string): boolean => {
return shell.test('-d', folder);
};

export const isFileExist = (pathToFileDir: string, fileName: string, fileExtension:string): boolean => {
return shell.ls(pathToFileDir).stdout.includes(fileName + fileExtension);
export const isFileExist = (pathToFileDir: string, fileName: string, fileExtension: string): boolean => {
return shell.ls(pathToFileDir).stdout.includes(fileName + fileExtension);
};

export const isFileEmpty = (file: string): boolean => {
export const isFileEmpty = (file: string): boolean => {
if (fs.existsSync(file)) {
return (fs.readFileSync(file).length === 0);
}
}
};
47 changes: 47 additions & 0 deletions crates/solidity/src/tests/cli-tests/tests/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { executeCommand, isFolderExist, isFileExist, isFileEmpty } from "../src/helper";
import { paths } from '../src/entities';
import * as shell from 'shelljs';
import * as path from 'path';


//id1762
Expand Down Expand Up @@ -142,3 +144,48 @@ describe("Run resolc with source debug information, check LLVM debug-info", () =
});
}
});



describe("Standard JSON compilation with path options", () => {
const contractsDir = path.join(shell.tempdir(), 'contracts-test');
const inputFile = path.join(__dirname, '..', 'src/contracts/compiled/1.json');

beforeAll(() => {
shell.mkdir('-p', contractsDir);

const input = JSON.parse(shell.cat(inputFile).toString());

Object.entries(input.sources).forEach(([sourcePath, source]: [string, any]) => {
const filePath = path.join(contractsDir, sourcePath);
shell.mkdir('-p', path.dirname(filePath));
shell.ShellString(source.content).to(filePath);
});
});

afterAll(() => {
shell.rm('-rf', contractsDir);
});

describe("Output with all path options", () => {
let result: { exitCode: number; output: string };

beforeAll(() => {
const tempInputFile = path.join(contractsDir, 'temp-input.json');
shell.cp(inputFile, tempInputFile);
const inputContent = shell.cat(inputFile).toString();

const command = `resolc --standard-json --base-path "${contractsDir}" --include-path "${contractsDir}" --allow-paths "${contractsDir}"`;

result = executeCommand(command, inputContent);

shell.rm(tempInputFile);

});

it("Compiler run successful without emiting warnings", () => {
const parsedResults = JSON.parse(result.output)
expect(parsedResults.errors.filter((error: { type: string; }) => error.type != 'Warning')).toEqual([]);
});
});
});

0 comments on commit 06f4308

Please sign in to comment.