Two output files share the same path but have different contents

1.3k views Asked by At

I've come across this error when building grammar for a parser, using nearley.js. I have three files: grammar.ne, grammar.js, and parser.js. The full error is below:

$ ./.config/build.sh
 > error: Two output files share the same path but have different contents: .build/grammar.js.map

 > error: Two output files share the same path but have different contents: .build/grammar.js

exit status 1

Here are the contents of each of the files:

grammar.ne:

main -> (statement "\n"):+
statement -> "foo" | "bar"

grammar.js:

// Generated automatically by nearley, version 2.20.1
// http://github.com/Hardmath123/nearley
import Lexer from './lexer';

(function() {
  function id(x) { return x[0]; }
  var grammar = {
    Lexer: Lexer,
    ParserRules: [
      { "name": "main$ebnf$1$subexpression$1", "symbols": ["statement", { "literal": "\n" }] },
      { "name": "main$ebnf$1", "symbols": ["main$ebnf$1$subexpression$1"] },
      { "name": "main$ebnf$1$subexpression$2", "symbols": ["statement", { "literal": "\n" }] },
      { "name": "main$ebnf$1", "symbols": ["main$ebnf$1", "main$ebnf$1$subexpression$2"], "postprocess": function arrpush(d) { return d[0].concat([d[1]]); } },
      { "name": "main", "symbols": ["main$ebnf$1"] },
      { "name": "statement$string$1", "symbols": [{ "literal": "f" }, { "literal": "o" }, { "literal": "o" }], "postprocess": function joiner(d) { return d.join(''); } },
      { "name": "statement", "symbols": ["statement$string$1"] },
      { "name": "statement$string$2", "symbols": [{ "literal": "b" }, { "literal": "a" }, { "literal": "r" }], "postprocess": function joiner(d) { return d.join(''); } },
      { "name": "statement", "symbols": ["statement$string$2"] }
    ]
    , ParserStart: "main"
  }
  if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
    module.exports = grammar;
  } else {
    grammar = grammar;
  }
})();
const nearley = require("nearley");
const grammar = require("./grammar.js");

const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));

parser.feed("foo\n");

console.log(JSON.stringify(parser.results));

Nothing that I have found online has helped at all. This is built in a TypeScript repl, and I have a lexer written in TypeScript, if that helps any.

1

There are 1 answers

0
Archigan On BEST ANSWER

I figured out the issue. In my package.json, I had used the module "es2015" when I should have been using "commonjs". I then changed the file extension of grammar.js to .cjs, and that got rid of all of the automatically-generated code errors. I also added a script to the package json npx nearleyc grammar.ne -o grammar.cjs && node parser.cjs, which allows me to execute compiling the grammar file faster, and uses the new .cjs extension to compile it to a CommonJS module; this also allows me to run the test file at the same time.