Repository navigation
Description
- 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

Activity
mattbrictson commentedon Jun 22, 2023
Hmm. I see in the test output:
Is it possible that
autoBuildand Rails parallel testing don't play nicely?ElMassimo commentedon Jun 22, 2023
Correct, see this discussion:
and the Integration Tests in the CI section of the guide.
ElMassimo commentedon Jun 22, 2023
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.
mattbrictson commentedon Jun 22, 2023
Is it possible to make
autoBuildwork 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 buildpenalty 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 likeretestorguardto 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#flockbe used as an inter-process lock to defeat the race condition?mattbrictson commentedon Jun 22, 2023
For me, the solution is:
autoBuild: falsein the test environment inconfig/vite.json.test/test_helper.rb:Now I don't have to remember to run
bin/rake assets:precompilein CI or set any special env vars.With test parallelization,
ViteRuby.commands.buildis 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 installscript (or the developer) will have to modifytest/test_helper.rborspec/rails_helper.rbto add the explicit build step. Whereas havingautoBuild: truemeans that tests will magically just work.ElMassimo commentedon Jun 22, 2023
Thanks for the feedback!
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,
autoBuildis an important of the DX, because it only builds on demand, and right on time.I like this idea, definitely worth a try to make Vite Ruby compatible with parallel tests, especially in development 👍
In addition, disabling
auto_buildin the CI, and throwing aNoBuildErrorwhen 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.top-sigrid commentedon Aug 18, 2026
I just ran into a similar error when using
autoBuild: trueand runnungminitestviaparallel_tests.Building the assets manually after turning off
autoBuildis a little cumbersome as it should work for single runs and parallel runs and requires a solution for usingViteRuby.commands.buildin different processes without race conditions.I found @mattbrictson idea of using
File#flocka 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_rubybetter suited for parallel testing? Or are there maybe other plans or is this something you wouldn't find worthwhile?