Truffle: Cannot tell which file failed compilation

Created on 12 Jun 2017  路  8Comments  路  Source: trufflesuite/truffle

  • [ x ] I've asked for help in the Truffle Gitter before filing this issue.

Issue

When certain types of compilation errors occur via truffle compile, you cannot know which file caused them. In more severe cases, you cannot even know what the error is.

IMO this is quite a serious issue, as it prevents one from sanely debugging the problem because they cannot even know where to start. In my experience, when this error occurs it is at least a 30 minute set back, and can be upwards of an hour depending on how many files were touched since the last compile, and how hard the compile error is to manually catch (could be a missing semicolon, for example).

Steps to Reproduce

Create a contract, include a syntax error (eg, omit a semicolon after import, or add a comma somewhere, or add an extra semicolon). Now compile. You'll get an error message like this:

$> truffle compile -all
SyntaxError: Expected "/*", "//", ";", "as", comment, end of line, or whitespace but "c" found.
    at peg$buildStructuredError (/usr/local/lib/node_modules/truffle/node_modules/solidity-parser/build/imports_parser.js:543:12)
    at Object.peg$parse [as parse] (/usr/local/lib/node_modules/truffle/node_modules/solidity-parser/build/imports_parser.js:4142:11)
    at Object.parse (/usr/local/lib/node_modules/truffle/node_modules/solidity-parser/index.js:34:23)
    at /usr/local/lib/node_modules/truffle/node_modules/truffle-compile/profiler.js:200:36
   ...

or like this

Compiling <previous contracts...>
Compiling ./contracts/roles/UsingTreasury.sol...
5
5

/usr/local/lib/node_modules/truffle/node_modules/solc/wrapper.js:48
        throw e;
        ^
abort(5) at Error
    at jsStackTrace (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:1:19718)
    at stackTrace (/usr/local/lib/node_modules/truffle/node_modules/solc/soljson.js:1:19901)
    ...
If this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.

Expected Behavior

Ideally this error would be caught and rethrown with a message indicating which file caused the SyntaxError. Or, truffle compile could take a --verbose flag that would output the files it is parsing as it goes.

Actual Results

A stacktrace that doesn't help me.

A much worse version of this error will pretend to have all files compiled correctly, then will barf out 5 5 and a solc error. (Update: I caused this by putting two semicolons where there should instead be one.). The user cannot even know where to begin to look, and what to look for.

Environment

  • Operating System:
  • Truffle version:
  • Ethereum client:
  • node version:
  • npm version:

Most helpful comment

@jjc12 I would prefer it not get closed until the PR is approved and pulled, and until this project updates its dependancies to include the fix.

IMO this is one of the most major issues right now. People get this error and have to spend so much time digging through all their code looking for one syntax error.

All 8 comments

I fixed one of the bugs by changing /truffle-compile/profiler.js line 200 to this:

        try {
          imports = SolidityParser.parse(resolved_body, "imports");
        } catch (e) {
          var error = new Error("Error parsing " + import_path + ": " + e.message);
          error.prev = e;
          return finished(error);
        }

Before this change:

SyntaxError: Expected "(", ",", "{", comment, end of line, or whitespace but "U" found.
 (stack trace)

After this change:

Error: Error parsing /path/to/Bad.sol: Expected "(", ",", "{", comment, end of line, or whitespace but "U" found. Line: 14, Column: 2
   (stack trace)

I don't think we care about the previous error, but in case we do it's in "prev". This will at least show which file failed parsing.

Unfortunately, some files will parse correctly, but will fail when truffle-compile calls solc.standardCompile() with all the file sources, causing the 5 5 error. I'm not sure how to fix that -- perhaps by compiling files one by one?

@tcoulter I don't have my dev env set-up to change this and push it as a branch -- could you review this and take care of that?

I've come up with my workaround for the 5 5 error, and it's already saved me HOURS.

I added this to truffle-compile/index.js on line 98:

  if (!options.dontparse){
    const SolidityParser = require("solidity-parser");
    Object.keys(operatingSystemIndependentSources).forEach(function(file_path) {
      try {
        var source = operatingSystemIndependentSources[file_path];
        SolidityParser.parse(source);
      } catch (e) {
        console.log("WARNING:  Parsing error in " + file_path + ":\n" + e.message);
      }
    });
  }

And added a flag in truffle-core. Works like a charm -- now I always see parsing errors before that 5 5 error.

One more note -- may want to add something to console.log that says "if you think this parsing error is incorrect, please notify solidity-parser" ... i've seen some false positives already.

@tcoulter what do you think?

I created a pull request in truffle-compile.

https://github.com/trufflesuite/truffle-compile/pull/9

Is this still an issue or can it be closed?

@jjc12 I would prefer it not get closed until the PR is approved and pulled, and until this project updates its dependancies to include the fix.

IMO this is one of the most major issues right now. People get this error and have to spend so much time digging through all their code looking for one syntax error.

This is a bug in solc-js. See here.

This will be fixed in the next version of solidity. Please track #484.

This error still seems to occur. I've upgraded truffle to the latest version, replaced solc with the latest one, everything is up to date on my system and my modules all point to the latest compiler - same issue. If it weren't for beether's clever fix I'd be completely lost right now. I've probably wasted hours on trying to figure out these utterly senseless error messages.

Why wasn't this fix merged into truffle? The 5 5 error and these syntax errors have cost myself and others hours of time and its a simple fix, too.

Edit: Also want to say that its 100 times harder now to apply these patches as truffle seems to be web packed now so instead of a nice collection of modules its a single 100k+ file you need to modify.... Incredible.

It's no wonder there's been so many hacks in the Ethereum space. It takes like 10 seconds to calculate a simple test for an addition operation... and missing a semi-colon can take you hours to find as there's often no debug info.... Maybe more people would want to test their software if the tools weren't all so shitty. Just saying...

Also seeing this bug still persist for certain types of errors. Here is a minimal example of a contract that causes the bug (note the mismatched braces):

pragma solidity ^0.4.10;

contract Test {

    uint[] test = [uint(0)};

}
Was this page helpful?
0 / 5 - 0 ratings