Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP Class finfo not found #1127

Closed
mho22 opened this issue Mar 21, 2024 · 20 comments · Fixed by #1179
Closed

PHP Class finfo not found #1127

mho22 opened this issue Mar 21, 2024 · 20 comments · Fixed by #1179
Assignees
Labels
[Feature] PHP.wasm [Type] Bug An existing feature does not function as intended
Milestone

Comments

@mho22
Copy link
Contributor

mho22 commented Mar 21, 2024

I encountered an issue while experimenting with PHP CLI.

node node_modules/.bin/cli test.php

> Fatal error: Uncaught Error: Class "finfo" not found in /mho22/works/php-wasm-issue/test.php:3
> Stack trace:
> #0 {main}
>   thrown in /mho22/works/php-wasm-issue/test.php on line 3

It seems like PHP's built-in extensions like fileinfo are not found. Is this correct ?

Here's step by step to reproduce the problem

mkdir php-wasm-issue

cd php-wasm-issue

npm install @php-wasm/cli

test.php

<?php

$finfo = new finfo( FILEINFO_MIME );

echo json_encode( $finfo );

node node_modules/.bin/cli test.php

@mho22 mho22 changed the title PHP Class "finfo" not found PHP Class finfo not found Mar 21, 2024
@adamziel
Copy link
Collaborator

Oh interesting! Are we missing a php.ini setting? Or perhaps the --disable-all flag during the compilation removes this extension from the build?

@adamziel adamziel added [Type] Bug An existing feature does not function as intended [Feature] PHP.wasm labels Mar 21, 2024
@adamziel adamziel added this to the Zero Crashes milestone Mar 21, 2024
@mho22
Copy link
Contributor Author

mho22 commented Mar 21, 2024

@adamziel Do you know a way for me to help find how to solve this ?

@adamziel
Copy link
Collaborator

@mho22 yes! One way would be to compare the phpinfo() output at https://playground.wordpress.net/?url=/phpinfo.php with a regular PHP installation that has that class and figure out what's different with respect to the finfo class. I think we need to add a flag to ./configure call that would be similar to --enable-finfo. I'm not sure what the exact name is, there should be some hints in https://github.com/php/php-src/tree/master/ext/fileinfo and https://github.com/php/php-src/ in general.

@mho22
Copy link
Contributor Author

mho22 commented Mar 21, 2024

@adamziel I suppose two screenshots with PHP8.2 binary phpinfo() method and PHP-WASM8.2 phpinfo() could be interesting to view here but these views are gigantic.

So, here is a little screenshot of the only thing different when I search for finfo or fileinfo between the two. This element appears on PHP8.2 binary, not PHP-WASM8.2.

Capture d’écran 2024-03-21 à 23 25 00

Next is the data from Configure Command. I think the best would be to paste what I have. And I hope this will be readable enough :

PHP8.2 binary:

'./configure' '--prefix=/usr/local/Cellar/php@8.2/8.2.14' '--localstatedir=/usr/local/var' '--sysconfdir=/usr/local/etc/php/8.2' '--with-config-file-path=/usr/local/etc/php/8.2' '--with-config-file-scan-dir=/usr/local/etc/php/8.2/conf.d' '--with-pear=/usr/local/Cellar/php@8.2/8.2.14/share/php@8.2/pear' '--enable-bcmath' '--enable-calendar' '--enable-dba' '--enable-exif' '--enable-ftp' '--enable-fpm' '--enable-gd' '--enable-intl' '--enable-mbregex' '--enable-mbstring' '--enable-mysqlnd' '--enable-pcntl' '--enable-phpdbg' '--enable-phpdbg-readline' '--enable-shmop' '--enable-soap' '--enable-sockets' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--with-apxs2=/usr/local/opt/httpd/bin/apxs' '--with-bz2=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr' '--with-curl' '--with-external-gd' '--with-external-pcre' '--with-ffi' '--with-fpm-user=_www' '--with-fpm-group=_www' '--with-gettext=/usr/local/opt/gettext' '--with-gmp=/usr/local/opt/gmp' '--with-iconv=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr' '--with-kerberos' '--with-layout=GNU' '--with-ldap=/usr/local/opt/openldap' '--with-libxml' '--with-libedit' '--with-mhash=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr' '--with-mysql-sock=/tmp/mysql.sock' '--with-mysqli=mysqlnd' '--with-ndbm=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr' '--with-openssl' '--with-password-argon2=/usr/local/opt/argon2' '--with-pdo-dblib=/usr/local/opt/freetds' '--with-pdo-mysql=mysqlnd' '--with-pdo-odbc=unixODBC,/usr/local/opt/unixodbc' '--with-pdo-pgsql=/usr/local/opt/libpq' '--with-pdo-sqlite' '--with-pgsql=/usr/local/opt/libpq' '--with-pic' '--with-pspell=/usr/local/opt/aspell' '--with-sodium' '--with-sqlite3' '--with-tidy=/usr/local/opt/tidy-html5' '--with-unixODBC' '--with-xsl' '--with-zip' '--with-zlib' '--enable-dtrace' '--with-ldap-sasl' '--with-os-sdkpath=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk' 'PKG_CONFIG_PATH=/usr/local/opt/apr/lib/pkgconfig:/usr/local/opt/openssl@3/lib/pkgconfig:/usr/local/opt/apr-util/lib/pkgconfig:/usr/local/opt/argon2/lib/pkgconfig:/usr/local/opt/brotli/lib/pkgconfig:/usr/local/opt/libidn2/lib/pkgconfig:/usr/local/opt/libnghttp2/lib/pkgconfig:/usr/local/opt/libssh2/lib/pkgconfig:/usr/local/opt/openldap/lib/pkgconfig:/usr/local/opt/rtmpdump/lib/pkgconfig:/usr/local/opt/lz4/lib/pkgconfig:/usr/local/opt/xz/lib/pkgconfig:/usr/local/opt/zstd/lib/pkgconfig:/usr/local/opt/curl/lib/pkgconfig:/usr/local/opt/unixodbc/lib/pkgconfig:/usr/local/opt/libpng/lib/pkgconfig:/usr/local/opt/freetype/lib/pkgconfig:/usr/local/opt/fontconfig/lib/pkgconfig:/usr/local/opt/jpeg-turbo/lib/pkgconfig:/usr/local/opt/highway/lib/pkgconfig:/usr/local/opt/imath/lib/pkgconfig:/usr/local/opt/libtiff/lib/pkgconfig:/usr/local/opt/little-cms2/lib/pkgconfig:/usr/local/opt/openexr/lib/pkgconfig:/usr/local/opt/webp/lib/pkgconfig:/usr/local/opt/jpeg-xl/lib/pkgconfig:/usr/local/opt/libvmaf/lib/pkgconfig:/usr/local/opt/aom/lib/pkgconfig:/usr/local/opt/libavif/lib/pkgconfig:/usr/local/opt/gd/lib/pkgconfig:/usr/local/opt/gmp/lib/pkgconfig:/usr/local/opt/icu4c/lib/pkgconfig:/usr/local/opt/krb5/lib/pkgconfig:/usr/local/opt/libpq/lib/pkgconfig:/usr/local/opt/libsodium/lib/pkgconfig:/usr/local/opt/libzip/lib/pkgconfig:/usr/local/opt/oniguruma/lib/pkgconfig:/usr/local/opt/pcre2/lib/pkgconfig:/usr/local/opt/readline/lib/pkgconfig:/usr/local/opt/sqlite/lib/pkgconfig:/usr/local/opt/tidy-html5/lib/pkgconfig' 'PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig:/usr/local/Homebrew/Library/Homebrew/os/mac/pkgconfig/10.15' 'KERBEROS_CFLAGS= ' 'SASL_CFLAGS=-I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sasl' 'SASL_LIBS=-lsasl2'

PHP-WASM8.2 :

'./configure' 'PKG_CONFIG_PATH=/root/lib/lib/pkgconfig' '--disable-fiber-asm' '--enable-json' '--enable-embed=static' '--with-layout=GNU' '--disable-cgi' '--disable-all' '--enable-hash' '--enable-static=yes' '--enable-shared=no' '--enable-session' '--enable-filter' '--enable-calendar' '--disable-rpath' '--disable-phpdbg' '--without-pear' '--with-valgrind=no' '--without-pcre-jit' '--enable-bcmath' '--enable-ctype' '--enable-tokenizer' '--with-zlib' '--with-zlib-dir=/root/lib' '--with-zip' '--enable-phar' '--enable-cli=static' '--enable-readline' '--with-libedit=/root/lib' '--enable-libxml' '--with-libxml' '--with-libxml-dir=/root/lib' '--enable-dom' '--enable-xml' '--enable-simplexml' '--enable-xmlreader' '--enable-xmlwriter' '--with-sqlite3' '--with-pdo-sqlite=/root/lib' '--with-png-dir=/root/lib' '--with-gd' '--enable-gd' '--with-openssl' '--with-openssl-dir=/root/lib' '--with-iconv=/root/lib' '--enable-mbstring' '--disable-mbregex' '--enable-mysql' '--enable-pdo' '--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' 'PKG_CONFIG_LIBDIR=/root/emsdk/upstream/emscripten/cache/sysroot/local/lib/pkgconfig:/root/emsdk/upstream/emscripten/cache/sysroot/lib/pkgconfig'

Now is time to investigate.

@adamziel
Copy link
Collaborator

This could be relevant:

https://github.com/php/php-src/blob/530e0d68ebee5b21ad10311115c392072b8b4168/ext/fileinfo/config.m4#L1-L5

I'm not sure if --disable-all impacts that flag at all.

@mho22
Copy link
Contributor Author

mho22 commented Mar 21, 2024

It seems like --disable-all disables all extensions which are enabled by default :

https://github.com/php/php-src/blob/530e0d68ebee5b21ad10311115c392072b8b4168/configure.ac#L1099

This does not confirm that it disables fileinfo either.

That said, I can't find anything about "re-enable" disabled default extensions, like :

--disable-all
--enable-fileinfo
--enable-curl

@adamziel
Copy link
Collaborator

Perhaps it's time to remove --disable-all and tactically disable specific extensions instead, then.

@mho22
Copy link
Contributor Author

mho22 commented Mar 21, 2024

@adamziel Alright, I'm working on it.

@mho22
Copy link
Contributor Author

mho22 commented Mar 22, 2024

@adamziel I tried removing --disable-all and recompiled only in 8.3 but an error occurred quickly. opcache was responsible of that error. I then added --disable-opcache and the recompilation of 8.3 succeeded.

I ran a npm run php -m to list the available php-wasm extensions and two new extensions appeared :

  • fileinfo
  • posix

I suppose this could be the solution. I am currently busy recompiling every PHP versions before launching the tests.

By the way, I was wondering : could there be a way to make --with-curl work for php-wasm ?

@adamziel
Copy link
Collaborator

Lovely! 🤞it will just work for all the PHP versions :)

About curl, you’d need to add libcurl directory here and provide a wasm build of the library:

https://github.com/WordPress/wordpress-playground/tree/trunk/packages/php-wasm/compile

From there, you could add —with-curl to ./configure in the same way as other extensions are provided.

@mho22
Copy link
Contributor Author

mho22 commented Mar 22, 2024

@adamziel It seems when i npm run recompile:php:web I get the given error :

#38 34.22 configure: error: iconv does not support errno

And this has been said about that :

As of PHP 8.0.0, iconv implementation which do not support errno
(i.e. non POSIX conforming implementations) are no longer
supported.

https://bugs.php.net/bug.php?id=80585

I added this :

# Add iconv if needed
RUN if [ "$WITH_ICONV" = "yes" ]; \
	then \
		echo -n ' --with-iconv=/root/lib ' >> /root/.php-configure-flags; \
		echo -n ' /root/lib/lib/libiconv.a' >> /root/.emcc-php-wasm-sources; \
		# PHP >= 8.0 posix is no longer supported.
		# PHP ≤= 7.4 posix is supported.
		if [[ "${PHP_VERSION:0:1}" -ge "8" ]]; then \
			echo -n ' --disable-iconv ' >> /root/.php-configure-flags; \
		fi; \
	fi;

Not working at the moment. Anyways, I am on it.

Concerning --with-curl do you mind if I add it to php-wasm ? Or you suggested me doing this locally ?

@adamziel
Copy link
Collaborator

@mho22 aha, WITH_ICONV is only set to yes for the kitchen-sink build but not for the light build. without the --disable-all that part of the DOCKERFILE might need a top-level else as follows:

RUN if [ "$WITH_ICONV" = "yes" ]; \
	then \
		echo -n ' --with-iconv=/root/lib ' >> /root/.php-configure-flags; \
		echo -n ' /root/lib/lib/libiconv.a' >> /root/.emcc-php-wasm-sources; \
		# PHP >= 8.0 posix is no longer supported.
		# PHP ≤= 7.4 posix is supported.
	else \
		echo -n ' --disable-iconv ' >> /root/.php-configure-flags; \
	fi;

@mho22
Copy link
Contributor Author

mho22 commented Mar 22, 2024

There are also two WITH_ICONV for node build. I suppose I can "clean" this ?

@adamziel
Copy link
Collaborator

Concerning --with-curl do you mind if I add it to php-wasm ? Or you suggested me doing this locally ?

@mho22 I would be stoked to have this in php-wasm, you would enable a lot of developers to explore new use-cases and run their code in Playground 🔥

If you can get it to build, this PR should give curl access to the network.

@mho22
Copy link
Contributor Author

mho22 commented Mar 22, 2024

@adamziel It seems npm run recompile:php:web:light:8.3 [ who has no WITH_ICONV enabled ] kept throwing :

#38 34.22 configure: error: iconv does not support errno

I added --without-iconv and there was no iconv error anymore. Instead, I had :

44.14 checking whether to enable XMLReader support... yes
44.14 configure: error: XMLReader extension requires LIBXML extension, add --with-libxml

So I added --disable-xmlreader in the RUN if [ "$WITH_LIBXML" = "yes" ]; else [ it seemed to be missing ].

I also tried by removing --disable-iconv and it worked too. I suppose this is correct.

Running every recompile and then testing. Let's find out if everything works fine. I keep you posted.

P.S.: I am glad to hear that lib-curl could probably be added, but I should mention that I will try to add it. I am absolutely not confident in my success.

@mho22
Copy link
Contributor Author

mho22 commented Mar 22, 2024

@adamziel Only one test failed at this point :

FAIL  src/lib/steps/wp-cli.spec.ts > Blueprint step wpCLI > should run wp-cli commands
    AssertionError: expected '#!/usr/bin/env php\n' to match /Success: Created post/
        ❯ src/lib/steps/wp-cli.spec.ts:35:23

The expected value is too specific to be a "real" error. I suppose the result.text returns the first line of the file here #!/usr/bin/env php\n instead of the result that could possibly be /Success: Created post/.

But I will wait for your insight about this.

Anyways, this sounds promising I suppose. Let's make the Pull Request now.

@mho22
Copy link
Contributor Author

mho22 commented Mar 25, 2024

@adamziel I am trying to list the PHP extensions from @php-wasm/web but this code gives me a module error :

npm install @php-wasm/web

index.js

import { WebPHP } from '@php-wasm/web';

const php = await WebPHP.load( '8.3' );

php.writeFile( '/index.php', `<?php echo phpinfo(); ?>` );

await php.run( { scriptPath : './index.php' } );

node index.js

node:internal/modules/esm/get_format:160
  throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath);
        ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".wasm" for /wasm/node_modules/@php-wasm/web/light/8_3_0/php_8_3.wasm
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:403:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:285:45) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Node.js v21.7.1

I am probably missing something obvious. But I need some enlightenments.

I tried this :

node --experimental-wasm-modules index.js

(node:67155) ExperimentalWarning: Importing WebAssembly modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
node:internal/modules/esm/resolve:845
  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
        ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'a' imported from /Users/mho/Work/Sandbox/Web/php/php-wasm/wasm/node_modules/@php-wasm/web/light/8_3_0/php_8_3.wasm
    at packageResolve (node:internal/modules/esm/resolve:845:9)
    at moduleResolve (node:internal/modules/esm/resolve:918:18)
    at defaultResolve (node:internal/modules/esm/resolve:1148:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
    at link (node:internal/modules/esm/module_job:86:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v21.7.1

@adamziel
Copy link
Collaborator

@php-wasm/web is meant to be used in web browsers. Perhaps @php-wasm/browser would be a better package name. What you're looking for is likely:

import { NodePHP } from '@php-wasm/node';

@mho22
Copy link
Contributor Author

mho22 commented Mar 25, 2024

Yeah, that was indeed obvious. Apologies for the silly question. I should probably ask you what the quickest way is to list the PHP extensions of php-wasm/web/light using the '@php-wasm/web' package instead.

In other words, how can I list the php extensions present in the light version when I recompile it and rebuild it ?

@adamziel
Copy link
Collaborator

Aha! I'd go to http://localhost:5400/website-server/, load the PHP version you've rebuilt, and run this in the dev tools:

document.body.innerHTML = (await playground.run({ code: `<?php phpinfo(); ?>` })).text

adamziel pushed a commit that referenced this issue Mar 28, 2024
…1132)

Based on #1127. This PR enables FileInfo and Posix extensions in PHP
previously disabled by `--disable-all`.

## What problem is it solving?

It allows the use of `new finfo()`. Previously, the following error
occurred :

```
node node_modules/.bin/cli test.php

> Fatal error: Uncaught Error: Class "finfo" not found in /mho22/works/php-wasm-issue/test.php:3
> Stack trace:
> #0 {main}
>   thrown in /mho22/works/php-wasm-issue/test.php on line 3
```

## Testing Instructions

1. `mkdir php-wasm-issue && cd php-wasm-issue` 

2. `npm install @php-wasm/cli`

3. `touch test.php` :

```php
<?php

$finfo = new finfo( FILEINFO_MIME );

echo json_encode( $finfo );
```

4. `node node_modules/.bin/cli test.php`
bgrgicak added a commit that referenced this issue Apr 3, 2024
Fixes #1127

## What is this PR doing?

It enables `fileinfo` support in kitchen-sink and Node and adds tests
for it.

## What problem is it solving?

Allows users to use `fileinfo`.

## How is the problem addressed?

By fixing a bug in the build script that prevented `fileinfo` from being
enabled.

## Testing Instructions

- ensure tests pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] PHP.wasm [Type] Bug An existing feature does not function as intended
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants