Skip to content

Flaky tests due occasional error from ViteRuby::Manifest#path_for #376

Closed

Description

@mattbrictson
  • I have tried upgrading by running bundle update vite_ruby.
  • I have read the troubleshooting section before opening an issue.

Description 📖

The following test fails intermittently in my local and CI environments: https://github.com/mattbrictson/rails-template/blob/main/test/helpers/inline_svg_helper_test.rb

The test passes 95% of the time, so this appears to be a race condition issue. I have hard-coded the test seed to eliminate the possibility of test order causing the problem. Even with the seed fixed, the test fails about 5% of the time.

The errors I see are:

JSON::ParserError: unexpected token at ''
vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:123:in `block in load_manifest'
[vite-plugin-manifest-sri] Unexpected end of JSON input
error during build:
SyntaxError: Unexpected end of JSON input
    at parse (<anonymous>)
    at async augmentManifest (/Users/mbrictson/Code/blog/node_modules/vite-plugin-manifest-sri/dist/index.cjs:50:20)
ViteRuby::MissingEntrypointError: Vite Ruby can't find images/tabler-icon-star.svg in public/vite-test/manifest.json or public/vite-test/manifest-assets.json.

So it appears that the JSON manifest is empty or incomplete at the time it is being parsed.

👀 As a workaround, I am disabling autoBuild in the test mode, and manually running ViteRuby.instance.builder.build at the start of tests.

Reproduction 🐞

Vite Ruby Info

Run bin/rake vite:info and provide the output:

bin/vite present?: true
vite_ruby: 3.3.2
vite_rails: 3.0.14
rails: 7.0.5
node: v18.15.0
npm: 9.5.0
yarn: 1.22.19
pnpm: 
ruby: ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-darwin22]

installed packages:
blog@ /Users/mbrictson/Code/blog
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected] deduped
│ ├─┬ [email protected]
│ │ └── [email protected] deduped
│ ├─┬ [email protected]
│ │ └── [email protected] deduped
│ └── [email protected] deduped
└── [email protected]

Logs 📜

Output
Error:
InlineSvgHelperTest#test_inline_svg_tag_returns_contents_of_svg_file_as_html_safe_string:
ViteRuby::MissingEntrypointError: Vite Ruby can't find images/tabler-icon-star.svg in public/vite-test/manifest.json or public/vite-test/manifest-assets.json.

Possible causes:
  - The file path is incorrect.
  - The file is not in the entrypoints directory.
  - Some files are outside the sourceCodeDir, and have not been added to watchAdditionalPaths.

Visit the Troubleshooting guide for more information:
  https://vite-ruby.netlify.app/guide/troubleshooting.html#troubleshooting


Content in your manifests:
null

Last build in test mode:
{
  "success": true,
  "errors": "",
  "timestamp": "2023-06-22 09:48:16",
  "vite_ruby": "3.3.2",
  "digest": "88e5a29074593a261334d0510591ece396731e4f"
}


    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:206:in `missing_entry_error'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:70:in `lookup!'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:22:in `path_for'
    /Users/mbrictson/Code/blog/app/lib/vite_inline_svg_file_loader.rb:5:in `named'
    /Users/mbrictson/Code/blog/app/helpers/inline_svg_helper.rb:3:in `inline_svg_tag'
    /Users/mbrictson/Code/blog/test/helpers/inline_svg_helper_test.rb:5:in `block in <class:InlineSvgHelperTest>'


===============================


Error:
InlineSvgHelperTest#test_inline_svg_tag_returns_contents_of_svg_file_as_html_safe_string:
JSON::ParserError: unexpected token at ''
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/3.2.0/json/common.rb:216:in `parse'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/3.2.0/json/common.rb:216:in `parse'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:123:in `block in load_manifest'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:123:in `map'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:123:in `load_manifest'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:41:in `refresh'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:115:in `manifest'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:106:in `find_manifest_entry'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:83:in `lookup'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:70:in `lookup!'
    /Users/mbrictson/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/vite_ruby-3.3.2/lib/vite_ruby/manifest.rb:22:in `path_for'
    /Users/mbrictson/Code/blog/app/lib/vite_inline_svg_file_loader.rb:5:in `named'
    /Users/mbrictson/Code/blog/app/helpers/inline_svg_helper.rb:3:in `inline_svg_tag'
    /Users/mbrictson/Code/blog/test/helpers/inline_svg_helper_test.rb:5:in `block in <class:InlineSvgHelperTest>'


rails test test/helpers/inline_svg_helper_test.rb:4

Screenshots 📷

Screenshot 2023-06-22 at 10 00 49 AM

Activity

  1. added
    bug: pending triage
    on Jun 22, 2023
  2. mattbrictson commented on Jun 22, 2023

    @mattbrictson
    ContributorAuthor

    Hmm. I see in the test output:

    Running 52 tests in parallel using 8 processes
                                       ^^^^^^^^^^^
    

    Is it possible that autoBuild and Rails parallel testing don't play nicely?

  3. ElMassimo commented on Jun 22, 2023

    @ElMassimo
    Owner
  4. ElMassimo commented on Jun 22, 2023

    @ElMassimo
    Owner

    That said, it would be nice to fail early in the CI if precompiled assets are not available, or if detecting that a runner like parallel is being used.

    The main downside is that it might be too opinionated, and is backwards incompatible with the current behavior, so I'm not sure I'd go down that route even in the next major version.

    A warning on the other hand might be a good balance? Thinking of all the potential wasted hours, given the flaky aspect of this problem.

  5. mattbrictson commented on Jun 22, 2023

    @mattbrictson
    ContributorAuthor

    Is it possible to make autoBuild work reliably in a parallelized test environment? That would be ideal.

    The problem with explicitly precompiling before tests is that you have to pay the vite build penalty even if the tests you are running don't end up needing any assets. For example if you are developing locally and using a tool like retest or guard to run tests interactively, you want the boot-time for the test framework to be as fast as possible.

    Telling developers to turn autoBuild on or off (e.g. with an env var) depending on what test they are running or how they are running it seems like a frustrating user experience.

    I suppose you could try to auto-detect whether parallelization is being used, but I think there are many test running tools out there. It might be hard to support them all. Rails has its own parallelization system, but I don't think it works with RSpec. There is flatware and I'm sure many others.

    Could File#flock be used as an inter-process lock to defeat the race condition?

  6. mattbrictson commented on Jun 22, 2023

    @mattbrictson
    ContributorAuthor

    For me, the solution is:

    1. Set autoBuild: false in the test environment in config/vite.json.
    2. Add the following to test/test_helper.rb:
    # Compile assets at the start of testing if autoBuild is off
    ViteRuby.commands.build unless ViteRuby.config.auto_build

    Now I don't have to remember to run bin/rake assets:precompile in CI or set any special env vars.

    With test parallelization, ViteRuby.commands.build is run once when the test framework loads, before the fork happens, eliminating any race conditions.

    The downside is that it slightly complicates the ViteRuby installation process, since the vite install script (or the developer) will have to modify test/test_helper.rb or spec/rails_helper.rb to add the explicit build step. Whereas having autoBuild: true means that tests will magically just work.

  7. ElMassimo commented on Jun 22, 2023

    @ElMassimo
    Owner

    Thanks for the feedback!

    Add the following to test/test_helper.rb

    As you mentioned in the previous comment, a downside is that it would build even when running individual tests which might not depend on Vite assets.

    Overall, autoBuild is an important of the DX, because it only builds on demand, and right on time.

    Could File#flock be used as an inter-process lock to defeat the race condition?

    I like this idea, definitely worth a try to make Vite Ruby compatible with parallel tests, especially in development 👍

    In addition, disabling auto_build in the CI, and throwing a NoBuildError when no manifest is found (along with instructions), seems like a reasonable default. Will experiment with adding a warning message instead of a hard error, and hear back from users.

  8. top-sigrid commented on Aug 18, 2026

    @top-sigrid

    I just ran into a similar error when using autoBuild: true and runnung minitest via parallel_tests.

    Building the assets manually after turning off autoBuild is a little cumbersome as it should work for single runs and parallel runs and requires a solution for using ViteRuby.commands.build in different processes without race conditions.

    I found @mattbrictson idea of using File#flock a good idea and you seemed to find it viable aswell, @ElMassimo.

    Is this something you would find a good idea to integrate into the repository to make vite_ruby better suited for parallel testing? Or are there maybe other plans or is this something you wouldn't find worthwhile?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug: pending triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions