0

I'm trying to play with xhp and I'm finding that running the one xhp example I could find https://github.com/hhvm/xhp-js-example is issuing an error \nFatal error: Uncaught Error: Found top-level code in :\nStack trace:\n#0 {main} when following the README as is on HHVM 4.71. Removing require_once(__DIR__.'/vendor/autoload.php'); resolves the top-level code error but I'm now stuck with the error \nFatal error: Uncaught Error: unknown class xhp_x__element in :\nStack trace:\n#0 /Users/user/code/xhp-js-example/example.php(12): <unknown>()\n#1 {main}. I've tried to change the code in example.php to the one found here:

class :introduction extends :x:element {
  protected function render(): \XHPRoot {
    return <strong>Hello!</strong>;
  }
}

class :intro-plain-str extends :x:element {
  protected function render(): \XHPRoot {
    // Since this function returns an XHPRoot, if you want to return a primitive
    // like a string, wrap it around <x:frag>
    return <x:frag>Hello!</x:frag>;
  }
}
echo <introduction />;
echo PHP_EOL.PHP_EOL;
echo <intro-plain-str />;

since that didn't work, I also tried the simple example from here:

<?hh // strict
$user_name = 'Fred';
echo <tt>Hello <strong>{$user_name}</strong></tt>;

but altered to resolve the top-level error by wrapping it in a function and annotating it as follows:

<?hh // strict

<<__EntryPoint>>
function main(): void {
  $user_name = 'Fred';
  echo <tt>Hello <strong>{$user_name}</strong></tt>;
}

I'd get a very similar error \nFatal error: Uncaught Error: Class undefined: xhp_tt in /Users/user/code/xhp-js-example/example.php:6\nStack trace:\n#0 (): main()\n#1 {main}

Any help with getting these seemingly simple examples to work would be much appreciated since it's very painful and discouraging for trying this tech when the basic examples don't seem to run. this is using the macos package of hhvm on Catalina.

EDIT: I've also tried this without the use of xhp-js, which is included in the example repo and am getting the same error message.

reactor
  • 1,722
  • 1
  • 14
  • 34
  • 1
    The example you're using is outdated as `xhp-js` is very stale, having been [last updated in 2017](https://github.com/hhvm/xhp-js/commits/master). Since then, many breaking changes have been introduced to Hack, so we can't even compile the latest `xhp-js` as is. If you just want to see XHP work at all, try [this example in the docs](https://docs.hhvm.com/hack/XHP/some-basics#why-use-xhp). As you've noted, you must require xhp-lib _as a library_ (via autoloading) as it provides all the tag definitions and whatnot. – concat Aug 20 '20 at 22:18
  • I tried that without `xhp-js` and unfortunately am getting the same issue. I'll update the original post to reflect that I've tried this without `xhp-js` as well – reactor Aug 20 '20 at 23:52
  • 1
    Ah, I missed the fact that you were using simpler examples. With these examples, with xhp-js removed and all the code in an entrypoint function, you need to require the autoload as to had before. As I said, XHP as a library provides all the needed tag definitions, which is why you're getting "class undefined: xhp_tt" at the moment. – concat Aug 21 '20 at 00:29
  • I just tried this: > function main(): void { $user_name = 'Fred'; echo Hello {$user_name}; } and am now getting \nFatal error: Uncaught Error: A .php file must begin with ' – reactor Aug 21 '20 at 01:04
  • Is your only dependency XHP? What does the type checker say? (`hh_client`) – concat Aug 21 '20 at 02:07
  • yes, it is. `hh_client` issues a bunch of errors as described here: https://stackoverflow.com/questions/62978039/hh-client-reports-errors-on-package. Reverting to a version older than 4.62 as mentioned in the thread is the only way I was able to get the errors to clear, while the advised changes to .hhconfig did not. I tried running the code in 4.61 as well in case versions newer than 4.61 introduced breaking changes to XHP. I'm hoping that hack and hhvm are still seriously maintained with xhp in mind and that what I stumbled upon isn't their abandoning support for it. – reactor Aug 21 '20 at 02:30
  • 1
    You may need to use version 4.0.0rc1. [Its .hhconfig was updated with the list of fixme codes in June 2020](https://github.com/hhvm/xhp-lib/pull/259) – concat Aug 21 '20 at 04:27
  • yes! that was it. I lost two days because of this so I very much appreciate your filling in the gap to get this resolved – reactor Aug 21 '20 at 17:49
  • No problem! I'll summarize this in an answer. – concat Aug 22 '20 at 01:26

1 Answers1

1

There are a few roadblocks that the OP encountered to get XHP running with HHVM 4.62, as discussed in the comments.

  1. xhp-js and xhp-js-example are both outdated by a few years so they can't compile due to breaking changes to HHVM itself, as the OP saw.
  2. While the XHP syntax is built into Hack, all the standard implementations are provided by xhp-lib, so it is necessary to autoload that in order to use the standard library of tags and tag classes.
  3. New to HHVM 4.62 is mandatory FIXME whitelisting, which requires package writers to explicitly specify allowed HH_FIXME codes in the .hhconfig. These were added to XHP-lib in 4.0.0rc1, so this version is necessary when running on HHVM 4.62.

Therefore, a minimal project with XHP on 4.62 looks like this:

composer.json

{
  "require": {
    "hhvm": "~4.62",
    "hhvm/hhvm-autoload": "^3.1.3",
    "facebook/xhp-lib": "~4.0.0rc1"
  }
}

hh_autoload.json

{ "roots": [ "src/" ] }

src/example.hack

require_once(__DIR__ . "/../vendor/hh_autoload.hh");
<<__EntryPoint>>
function main(): void {
  echo <div>{1 + 2}</div>;
}
concat
  • 3,107
  • 16
  • 30
  • 1
    hi again. I tried to do the same on hhvm v 4.83.1 but going to localhost:8080/example.hack gives me the `\nFatal error: Uncaught Error: Found top-level code in :\nStack trace:\n#0 {main}` error again. Everything is as above except with different version numbers for the composer dependencies – reactor Nov 16 '20 at 00:10