Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1501 from hyperledger/build-improvements
Browse files Browse the repository at this point in the history
Ensure build errors are surfaced from burrow.js
  • Loading branch information
Silas Davis committed Jul 9, 2021
2 parents 137976e + 8176f4b commit 2d30136
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
13 changes: 10 additions & 3 deletions js/src/contracts/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import { ResolvedImport } from 'solc';
import solc from 'solc_v5';
import { format } from 'util';
import { ABI } from './abi';
import { Contract } from './contract';

Expand Down Expand Up @@ -130,9 +131,15 @@ export function inputDescriptionFromFiles(names: string[]): Solidity.InputDescri
return desc;
}

export function importLocal(path: string): ResolvedImport {
return {
contents: fs.readFileSync(path).toString(),
export function importLocalResolver(basePath: string): (path: string) => ResolvedImport {
return (path) => {
try {
return {
contents: fs.readFileSync(path).toString(),
};
} catch (err) {
throw new Error(`could not import path '${path}': ${format(err)}`);
}
};
}

Expand Down
2 changes: 1 addition & 1 deletion js/src/solts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getContractMethods } from './lib/solidity';
import { declareConstant, ExportToken, importBurrow, importReadable } from './lib/syntax';
import Func = ABI.Func;

export { decodeOutput, encodeInput, importLocal, inputDescriptionFromFiles, tokenizeLinks } from '../contracts/compile';
export { decodeOutput, encodeInput, importLocalResolver, inputDescriptionFromFiles, tokenizeLinks } from '../contracts/compile';

export type Compiled = {
name: string;
Expand Down
26 changes: 19 additions & 7 deletions js/src/solts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { promises as fs } from 'fs';
import * as path from 'path';
import * as solcv5 from 'solc_v5';
import * as solcv8 from 'solc_v8';
import { decodeOutput, encodeInput, importLocal, inputDescriptionFromFiles, Solidity } from '../contracts/compile';
import {
decodeOutput,
encodeInput,
importLocalResolver,
inputDescriptionFromFiles,
Solidity,
} from '../contracts/compile';
import { Compiled, newFile, printNodes, tokenizeLinks } from './api';

const solcCompilers = {
Expand Down Expand Up @@ -33,18 +39,24 @@ export async function build(srcPathOrFiles: string | string[], opts?: Partial<Bu
...defaultBuildOptions,
...opts,
};
const basePathPrefix = new RegExp(
'^' + path.resolve(basePath ?? (typeof srcPathOrFiles === 'string' ? srcPathOrFiles : process.cwd())),
);
const resolvedBasePath = basePath ?? (typeof srcPathOrFiles === 'string' ? srcPathOrFiles : process.cwd());
process.chdir(resolvedBasePath);
const basePathPrefix = new RegExp('^' + path.resolve(resolvedBasePath));
await fs.mkdir(binPath, { recursive: true });
const solidityFiles = await getSourceFilesList(srcPathOrFiles);
const inputDescription = inputDescriptionFromFiles(solidityFiles);
const inputDescription = inputDescriptionFromFiles(
// solidityFiles.map((f) => path.resolve(resolvedBasePath, f.replace(basePathPrefix, ''))),
solidityFiles,
);
const input = encodeInput(inputDescription);
const solc = solcCompilers[solcVersion];
const solcOutput = solc.compile(input, { import: importLocal });

const solcOutput = solc.compile(input, { import: importLocalResolver(resolvedBasePath) });
const output = decodeOutput(solcOutput);
if (output.errors && output.errors.length > 0) {
throw new Error(output.errors.map((err) => err.formattedMessage).join('\n'));
throw new Error(
'Solidity compiler errors: ' + output.errors.map((err) => err.formattedMessage || err.message).join(', '),
);
}

const plan = Object.keys(output.contracts).map((filename) => ({
Expand Down

0 comments on commit 2d30136

Please sign in to comment.