nystudio107 / craft-vite

Allows the use of the Vite.js next generation frontend tooling with Craft CMS
MIT License
52 stars 16 forks source link

ValueError: Path cannot be empty #47

Closed transopticmedia closed 2 years ago

transopticmedia commented 2 years ago

Hi, I'm not having any luck getting craft-vite to work on my server. Locally in DDEV, it's working perfectly and the configuration is working – it will use HMR version when I set the env variable, and it will use the manifest to load built css and JS when I set it to.

On the server, it keep getting the error: ValueError: Path cannot be empty.

I suspected that it's not finding the manifest.json file, but I've even tried hard coding it and it doesn't fixe the error. I've checked the permissions on the manifest.json, and it's fine. I'm stumped -- have you see this error?

Sorry for the data dump, here's my config and stack trace. I truly appreciate any assistance or tips you can provide.

vite.php

<?php

use craft\helpers\App;

return [
    'useDevServer' => (bool) App::env('VITE_USE_DEV_SERVER'),
    'checkDevServer' => true,
    'manifestPath' => '@webroot/dist/manifest.json',
    'devServerPublic' => App::env('PRIMARY_SITE_URL') . ':3000',
    'serverPublic' => App::env('PRIMARY_SITE_URL') . '/dist/',
    'errorEntry' => 'src/js/app.js',
    'devServerInternal' => 'http://localhost:3000',
];
];

vite.config.js:

import ViteRestart from 'vite-plugin-restart';
import {defineConfig} from 'vite';
import path from 'path';
import viteCompression from 'vite-plugin-compression';
import mkcert from'vite-plugin-mkcert'

export default defineConfig(({command}) => ({
  base: command === 'serve' ? '' : '/dist/',
  build: {
    commonjsOptions: {
        transformMixedEsModules: true,
    },
    manifest: true,
    outDir: 'web/dist/',
    rollupOptions: {
      input: {
        app: 'src/js/global.js',
      },
      output: {
        sourcemap: true
      },
    },
  },
  plugins: [
    viteCompression({
        filter: /\.(js|mjs|json|css|map)$/i
    }),
    ViteRestart({
      reload: [
        'templates/**/*',
      ],
    }),
  ],
  publicDir: path.resolve(__dirname, 'src/public'),
  resolve: {
      alias: {
          '@': path.resolve(__dirname, 'src'),
          '@css': path.resolve(__dirname, 'src/css'),
          '@js': path.resolve(__dirname, 'src/js'),
      },
  },
  server: {
    host: '0.0.0.0',
    port: 3000,
    strictPort: true,
  }
}));

Stack Trace:

ValueError: Path cannot be empty in <my-server-info>/public_html/vendor/nystudio107/craft-plugin-vite/src/helpers/FileHelper.php:88
Stack trace:
#0 <my-server-info>/public_html/vendor/nystudio107/craft-plugin-vite/src/helpers/FileHelper.php(88): file_get_contents()
#1 [internal function]: nystudio107\pluginvite\helpers\FileHelper::nystudio107\pluginvite\helpers\{closure}()
#2 <my-server-info>/public_html/vendor/yiisoft/yii2/caching/Cache.php(607): call_user_func()
#3 <my-server-info>/public_html/vendor/nystudio107/craft-plugin-vite/src/helpers/FileHelper.php(79): yii\caching\Cache->getOrSet()
#4 <my-server-info>/public_html/vendor/nystudio107/craft-plugin-vite/src/helpers/ManifestHelper.php(54): nystudio107\pluginvite\helpers\FileHelper::fetch()
#5 <my-server-info>/public_html/vendor/nystudio107/craft-plugin-vite/src/services/ViteService.php(455): nystudio107\pluginvite\helpers\ManifestHelper::fetchManifest()
#6 <my-server-info>/public_html/vendor/nystudio107/craft-plugin-vite/src/services/ViteService.php(399): nystudio107\pluginvite\services\ViteService->manifestScript()
#7 <my-server-info>/public_html/vendor/nystudio107/craft-plugin-vite/src/variables/ViteVariableTrait.php(50): nystudio107\pluginvite\services\ViteService->script()
#8 <my-server-info>/public_html/vendor/twig/twig/src/Extension/CoreExtension.php(1607): nystudio107\vite\variables\ViteVariable->script()
#9 <my-server-info>/public_html/vendor/craftcms/cms/src/helpers/Template.php(101): twig_get_attribute()
#10 <my-server-info>/public_html/storage/runtime/compiled_templates/46/4613608a083214d1755c6101b69cd321.php(60): craft\helpers\Template::attribute()
#11 <my-server-info>/public_html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_bb0c5fcc94d421977e1313a3f173e20c->doDisplay()
#12 <my-server-info>/public_html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling()
#13 <my-server-info>/public_html/storage/runtime/compiled_templates/94/944f24e116ca9f9a1d3d4fb61237dbd5.php(48): Twig\Template->display()
#14 <my-server-info>/public_html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_411e8e08f966546ad5c724cf28c02b94->doDisplay()
#15 <my-server-info>/public_html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling()
#16 <my-server-info>/public_html/vendor/twig/twig/src/Template.php(379): Twig\Template->display()
#17 <my-server-info>/public_html/vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render()
#18 <my-server-info>/public_html/vendor/twig/twig/src/Environment.php(277): Twig\TemplateWrapper->render()
#19 <my-server-info>/public_html/vendor/craftcms/cms/src/web/View.php(428): Twig\Environment->render()
#20 <my-server-info>/public_html/vendor/craftcms/cms/src/web/View.php(481): craft\web\View->renderTemplate()
#21 <my-server-info>/public_html/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(56): craft\web\View->renderPageTemplate()
#22 <my-server-info>/public_html/vendor/yiisoft/yii2/web/Response.php(1095): craft\web\TemplateResponseFormatter->format()
#23 <my-server-info>/public_html/vendor/craftcms/cms/src/web/Response.php(286): yii\web\Response->prepare()
#24 <my-server-info>/public_html/vendor/yiisoft/yii2/web/Response.php(339): craft\web\Response->prepare()
#25 <my-server-info>/public_html/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#26 <my-server-info>/public_html/web/index.php(12): yii\base\Application->run()
#27 {main}

.env

# The environment Craft is currently running in (dev, staging, production, etc.)
ENVIRONMENT=production

# The application ID used to to uniquely store session and cache data, mutex locks, and more
APP_ID=CraftCMS--###########

# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY=###########

# The URI segment that tells Craft to load the control panel
CP_TRIGGER=admin

PRIMARY_SITE_URL=https://staging.mysite.com
BRAND_SITE_URL=https://brand.staging.mysite.com
OMNI_SITE_URL=https://portal.staging.mysite.com
VITE_USE_DEV_SERVER=0
khalwat commented 2 years ago

I'd try the same thing in local dev -- do a production build, and disable the dev server, to see if you can get it to reproduce locally not using the dev server.

Somehow, some way, the manifestPath is ending up being empty. This is the line of code where it is erroring:

                    $contents = @file_get_contents($pathOrUrl);

https://github.com/nystudio107/craft-plugin-vite/blob/develop/src/helpers/FileHelper.php#L88

transopticmedia commented 2 years ago

Thanks for taking a look! yeah, I tried that locally, and it's working fine, with and without the dev server. I'll keep poking round.

khalwat commented 2 years ago

Yeah so if it's working fine locally both with a production build, and with the dev server... it's almost always going to be something environmental.

Make sure that your .env is right on production, your aliases are properly set on production (ensure you are explicitly setting the @webroot alias), make sure config/vite.php is deployed on production, etc.

I can tell you that the error is that an entirely empty path is being passed into manifestPath, which is causing the error. How, I don't know.

transopticmedia commented 2 years ago

Oh jeez.. it was a permission mismatch on vite.php. You comment "Somehow, some way, the manifestPath is ending up being empty." made me check... thanks!!

khalwat commented 2 years ago

That makes total sense. Always does, in retrospect! :)

vaughn-taylor commented 1 year ago

@transopticmedia I'd love to know how you determined that you had a permission mismatch, and how you resolved it exactly? I've been struggling with this same issue "ValueError: Path cannot be empty" with the Vite plugin. I have yet to resolve it after months of dipping in and out of a new build.