php-wasm icon indicating copy to clipboard operation
php-wasm copied to clipboard

PhpNode and PhpCgiNode locateFile breaks dynamic imports

Open mglaman opened this issue 1 year ago • 8 comments

I'm trying out PhpNode and the following:

        const php = new PhpNode({
            persist: {
                mountPath: '/persist',
                localPath: process.cwd() + '/tests/fixtures/'
            },
            sharedLibs: [
                await import('php-wasm-zlib'),
                await import('php-wasm-libzip')

All is good until the following: See following screenshot

		args.locateFile = path => {
			let located = userLocateFile(path);
			if(located !== undefined)
			{
				return located;
			}
			if(urlLibs[path])
			{
				return urlLibs[path];
			}
		};

Screenshot 2024-08-21 at 4 23 28 PM

The path is php8.3-zlib.so, but urlLibs[path] has the proper URL. But located returns a value even though it is wrong.

locateFile never returns undefined when using PhpNode and returns files which do not exist. See result in the next screenshot.

Screenshot 2024-08-21 at 4 26 24 PM

mglaman avatar Aug 21 '24 21:08 mglaman

Possible workaround:

import { PhpBase } from 'php-wasm/PhpBase.mjs';
import PhpBinary from 'php-wasm/php-node.mjs';

        const php = new PhpBase(PhpBinary, {
            persist: {
                mountPath: '/persist',
                localPath: process.cwd() + '/tests/fixtures/'
            },
            sharedLibs: [
                await import('php-wasm-zlib'),
                await import('php-wasm-libzip')
``

mglaman avatar Aug 21 '24 21:08 mglaman

I don't think this is quite working either as when I debug I find it trying to load php83-zlib.so.so

mglaman avatar Aug 21 '24 21:08 mglaman

WIP branch where I'm trying this out: https://github.com/mglaman/wasm-drupal/blob/9768d93d0b4bca056a87d40dfc5a06264b2c5190/tests/init-phpcode.test.js

Warning: PHP Startup: Invalid library (maybe not a PHP library) 'libzip.so' in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library 'php8.3-zlib.so' (tried: ./php8.3-zlib.so (Tried to lookup unknown symbol "_zend_extension_entry" in dynamic lib: libzip.so), ./php8.3-zlib.so.so ()) in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library 'zlib.so' (tried: ./zlib.so (), ./zlib.so.so ()) in Unknown on line 0

mglaman avatar Aug 21 '24 21:08 mglaman

Okay, I got it working this way: using PhpBase and not PhpNode

import { PhpBase } from 'php-wasm/PhpBase.mjs';
import PhpBinary from 'php-wasm/php-node.mjs';

        const php = new PhpBase(PhpBinary, {
            persist: {
                mountPath: '/persist',
                localPath: process.cwd() + '/tests/fixtures/'
            },
            sharedLibs: [
                {
                    name: `php${PhpNode.phpVersion}-zip.so`,
                    url: `${process.cwd()}/node_modules/php-wasm-libzip/php${PhpNode.phpVersion}-zip.so`,
                    ini: true
                },
                {
                    name: `libzip.so`,
                    url: `${process.cwd()}/node_modules/php-wasm-libzip/libzip.so`,
                    ini: false
                },
                {
                    name: `php${PhpNode.phpVersion}-zlib.so`,
                    url: `${process.cwd()}/node_modules/php-wasm-zlib/php${PhpNode.phpVersion}-zlib.so`,
                    ini: true
                },
                {
                    name: `libz.so`,
                    url: `${process.cwd()}/node_modules/php-wasm-zlib/libz.so`,
                    ini: false
                },
            ]
        });

mglaman avatar Aug 21 '24 21:08 mglaman

ah, this is my fault for getting "creative."

seanmorris avatar Sep 19 '24 01:09 seanmorris

@mglaman Can you confirm/deny this bug is fixed? I think this change cleared it up:

https://github.com/seanmorris/php-wasm/blob/68768e254ba1a9eb7ffe50fea50798d68f2b2e45/source/PhpNode.js#L28-L33

seanmorris avatar Apr 12 '25 19:04 seanmorris

I'll take some time on Monday to test it out! Started to dust off my work and I'll remove my patch and see how the tests go

mglaman avatar Apr 12 '25 19:04 mglaman

@mglaman Thanks man. Let me know how it goes. I put some other fixes out in the latest alpha.

seanmorris avatar Apr 13 '25 15:04 seanmorris