ykmnkmi / jinja.dart

Jinja2 template engine port for Dart.
https://pub.dev/packages/jinja
MIT License
51 stars 11 forks source link

Lexer's `if (rule case ...)` not working on web #26

Closed jpohhhh closed 2 months ago

jpohhhh commented 8 months ago

I found a really interesting issue that looks more like a Dart problem than a package problem.

TL;DR: the (rule case MultiTokenRule rule) and (rule case SingleTokenRule rule) lines in lexer.dart never return true on web. Replacing them with (rule is MultiTokenRule) and (rule is SingleTokenRule) work properly.

I looked into this from every angle, logging all the steps along Lexer.scan. I assumed it must be a regex difference between web and native. Tried a bunch of stuff. I then added print()'s to every branch entry in the while loop, and then to every data mutation. That made it clear everything was the same, until it ran those if (rule case ...) statements. Changing them to if (rule is ...) worked, surprising me. I'm not sure why the case version doesn't work but is does. It almost seems like it must be a Dart issue. But, I'm confident it's the cause, not only from debugging, but also because the last commit I had in my fork from ~May was the commit before the if (rule case...) statements were added, which also explains why it worked in May, but not now.

Test case

Method

String expandPossibleJinja2Variables(
    Map<String, Object?> jinjaContext, String text) {
  print('jinjaContext: $jinjaContext');
  print('text: $text');
  if (text.isEmpty) {
    return text;
  }
  final template = env.fromString(text);
  return template.render(jinjaContext);
}

Arguments

jinjaContext

{prompt: how old is barack, datetime: 12:20 PM EST on Thursday, December 14, 2023.} both values are String

text

The above is search results you can use to help us answer:
{{prompt}}

Today is {{datetime}}
ykmnkmi commented 8 months ago

Thanks for the analysis. Which version of Dart do you use? Strange, there should be no differences between platforms; it looks like a Dart issue. I'll try to check this on both the 3.0.0, stable and dev versions.

ykmnkmi commented 2 months ago

Fixed by fbc53e6 in 0.6.0.