Skip to content

[FS] Export optional JS filesystem backends when linked in - #28302

Open
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:export-optional-fs
Open

sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:export-optional-fs

Conversation

@sbc100

@sbc100 sbc100 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

After FS.filesystems was removed in #27940 and toplevel: true was enabled for Terser in #27987, optional filesystem backends linked in via -lidbfs.js, -lworkerfs.js, -lproxyfs.js, -lopfs.js, etc. had no remaining references in the generated JS and were eliminated in -O2 and -O3 builds when referenced only from external JS (such as Module.preRun in HTML).

Mark these optional filesystem backend objects with __export: true so that when $FS pulls them in, they are exported and preserved across JS optimizations (including --closure=1, -sMODULARIZE, and -sMODULARIZE=instance), without adding any code size when $FS is not used or under default settings.

Fixes: #28072

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request ensures that various filesystem libraries (FetchFS, IcaseFS, IDBFS, JSFileFS, NodeFS, OPFS, ProxyFS, and WorkerFS) are explicitly exported by adding the corresponding __export: true property to their library definitions. It also updates existing tests and adds a new parameterized test test_fs_js_exports in test/test_jslib.py to verify that these filesystem objects are correctly exported and accessible under different compilation settings (such as with closure compiler, modularization, and WasmFS). I have no additional feedback to provide.

@sbc100
sbc100 force-pushed the export-optional-fs branch from 59eef46 to 487fbe7 Compare November 18, 2026 00:27
@sbc100
sbc100 requested review from hoodmane and kripken November 18, 2026 00:27
After `FS.filesystems` was removed in emscripten-core#27940 and `toplevel: true` was
enabled for Terser in emscripten-core#27987, optional filesystem backends linked in via
`-lidbfs.js`, `-lworkerfs.js`, `-lproxyfs.js`, `-lopfs.js`, etc. had no
remaining references in the generated JS and were eliminated in `-O2`
and `-O3` builds when referenced only from external JS (such as
`Module.preRun` in HTML).

Mark these optional filesystem backend objects with `__export: true` so
that when `$FS` pulls them in, they are exported and preserved across
JS optimizations (including `--closure=1`, `-sMODULARIZE`, and
`-sMODULARIZE=instance`), without adding any code size when `$FS` is not
used or under default settings.

Fixes: emscripten-core#28072
@sbc100
sbc100 force-pushed the export-optional-fs branch from 487fbe7 to b80d487 Compare November 18, 2026 00:37
Comment thread src/lib/libnodefs.js
addToLibrary({
#if !LibraryManager.has('libnoderawfs.js')
$NODEFS__export: true,
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this depend on noderawfs?

@kripken

kripken commented Nov 18, 2026

Copy link
Copy Markdown
Member

Another option could be to require the user to export them manually, what do you think about the tradeoffs?

@sbc100

sbc100 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

Another option could be to require the user to export them manually, what do you think about the tradeoffs?

I guess that is the status quo. It seems like if you include one of the filesystems you probably want use it though? I guess you could just use it on the inside of the module only.

@sbc100

sbc100 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

Another option could be to require the user to export them manually, what do you think about the tradeoffs?

I guess that is the status quo. It seems like if you include one of the filesystems you probably want use it though? I guess you could just use it on the inside of the module only.

But this change does specifically fix the breaking in #28072

@kripken

kripken commented Nov 18, 2026

Copy link
Copy Markdown
Member

Hmm, but there, the code is

var Module = {
        preRun: [function() {
            addRunDependency('syncfs')
            FS.mkdir('/preferences')
            FS.mount(IDBFS, {}, '/preferences');

Exporting won't help this, I think? It would need to be Module.IDBFS - ?

@sbc100

sbc100 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

Hmm, but there, the code is

var Module = {
preRun: [function() {
addRunDependency('syncfs')
FS.mkdir('/preferences')
FS.mount(IDBFS, {}, '/preferences');
Exporting won't help this, I think? It would need to be Module.IDBFS - ?

Ah yes. I does solve their problem, since exporting IDBFS also forces IDBFS to be included as an internal symbols. So I guess you could say it solves a superset of their problem.

I could focus on just including it, and not also exporting it?

@kripken

kripken commented Nov 18, 2026

Copy link
Copy Markdown
Member

How would including it but not export it work? JSDCE would remove it without an export, I think. At least I don't see a non-hackish way, off the top of my head.

The other option is to just document this. When you do -lSDL it does not imply any exports, for example, and users should be using exports for things they want to use externally.

@sbc100

sbc100 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

How would including it but not export it work? JSDCE would remove it without an export, I think. At least I don't see a non-hackish way, off the top of my head.

Oh I see.. because JSDCE has no access the Module.preRun (that is being injected at runtime) there is nothing that would keep IDBFS alive during DCE (unless we export it).

Previously it was being kept alive by the filesystems object (which was removed in #27940).

The other option is to just document this. When you do -lSDL it does not imply any exports, for example, and users should be using exports for things they want to use externally.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IDBFS no longer working between version 6.0.12 and 6.0.13

2 participants