Skip to content

cmdline: Allow overriding -jsD directives#26639

Open
subhaushsingh wants to merge 1 commit intoemscripten-core:mainfrom
subhaushsingh:fix-jsd-override-26576
Open

cmdline: Allow overriding -jsD directives#26639
subhaushsingh wants to merge 1 commit intoemscripten-core:mainfrom
subhaushsingh:fix-jsd-override-26576

Conversation

@subhaushsingh
Copy link
Copy Markdown
Contributor

@subhaushsingh subhaushsingh commented Apr 7, 2026

The Problem

  • Currently, passing -jsDFOO=1 -jsDFOO=2 triggers an error: cannot change built-in settings values with a -jsD directive.
  • This happens because the first flag adds FOO to the internal settings dictionary. When the second flag appears, the compiler mistakenly thinks a protected system setting (like WASM) is being overwritten.

The Fix

  • Added user_js_defines: A set in tools/cmdline.py to track which keys were introduced by the user during the current run.
  • Smart Validation: The error now only triggers if a key is a built-in and hasn't already been defined by the user via -jsD in this session.

Verification

  • Regression Test: Added a case to test/test_jslib.py that specifically tests duplicate/overridden -jsD flags.
  • Safety Check: Confirmed that attempting to override actual system settings (e.g., -jsDWASM=0) still correctly fails as intended.

Fixes: #26576

@subhaushsingh subhaushsingh force-pushed the fix-jsd-override-26576 branch from aff073f to 02426d8 Compare April 7, 2026 14:42
Copy link
Copy Markdown
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

Thanks!

lgtm % test comments

self.cflags += ['--js-library', test_file('jslib/test_jslib_custom_settings.js'), '-jsDCUSTOM_JS_OPTION=1']
self.do_run_in_out_file_test('jslib/test_jslib_custom_settings.c')

self.run_process([EMCC, test_file('hello_world.c'), '-jsDCUSTOM_JS_OPTION=1', '-jsDCUSTOM_JS_OPTION=2'])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you replace these two lines with:

self.do_runf('jslib/test_jslib_custom_settings.c', '1\n')

# verify that the settings can be specified more then once, and that the last one wins.
self.do_runf('jslib/test_jslib_custom_settings.c', '2\n', cflags=['-jsDCUSTOM_JS_OPTION=2'])

Then you can also delete the test/jslib/test_jslib_custom_settings.out file

@subhaushsingh subhaushsingh force-pushed the fix-jsd-override-26576 branch from 73088d2 to 9855548 Compare April 8, 2026 04:41
@subhaushsingh
Copy link
Copy Markdown
Contributor Author

@sbc100 It looks like the Codesize Checks are failing due to a pre-existing stale baseline on main (specifically test_codesize_cxx_lto.json showing a +206 byte drift). This seems unrelated to my -jsD logic. Could you please run the rebaseline-tests workflow on main to unblock the CI? Thanks!

@subhaushsingh subhaushsingh requested a review from sbc100 April 8, 2026 12:55
self.do_runf('jslib/test_jslib_custom_settings.c', '1\n')

# verify that the settings can be specified more than once, and that the last one wins.
self.do_runf('jslib/test_jslib_custom_settings.c', '2\n', cflags=['-jsDCUSTOM_JS_OPTION=2'])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe the oder of the cflags is not correct there.

How about adding cflags=['-jsDCUSTOM_JS_OPTION=1'] to the first run then cflags=['-jsDCUSTOM_JS_OPTION=1', '-jsDCUSTOM_JS_OPTION=2'] to the second run.

Then the order will be obvious.

@subhaushsingh subhaushsingh force-pushed the fix-jsd-override-26576 branch from 9855548 to 5da5234 Compare April 9, 2026 15:33
@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Apr 9, 2026

Looks like the override test is still failing for some reason?

@subhaushsingh subhaushsingh force-pushed the fix-jsd-override-26576 branch 2 times, most recently from eb5e07a to ad32fb1 Compare April 9, 2026 17:14
tools/cmdline.py Outdated

# Apply -jsD defines last so they win over -s args
for key, value in options.user_js_defines.items():
settings[key] = value
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why can't think line be up on line 581? User defined settings cannot overlap with normal -s settings so I don't see why we would care about competing with them, right?

@subhaushsingh subhaushsingh force-pushed the fix-jsd-override-26576 branch 2 times, most recently from 70faeb6 to 1c13494 Compare April 9, 2026 17:52
tools/cmdline.py Outdated
should_exit = False
skip = False
builtin_settings = set(settings.keys())
user_js_defines = set()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think maybe we no longer require user_js_defines now?

@subhaushsingh subhaushsingh force-pushed the fix-jsd-override-26576 branch 3 times, most recently from d0e21ba to 2a9cf37 Compare April 9, 2026 18:36
tools/cmdline.py Outdated
exit_with_error(f'{arg}: cannot change built-in settings values with a -jsD directive. Pass -s{key}={value} instead!')
# Apply user -jsD settings
if key in builtin_settings and key in settings:
if str(settings[key]) != value:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think we need to compare the value here.

I think just if key in builtin_settings: should be enough. i.e. it should always be not allowed to do -jsDASSERTIONS=value regardless of the value passed

@subhaushsingh subhaushsingh force-pushed the fix-jsd-override-26576 branch 2 times, most recently from 28591cd to 059ee71 Compare April 9, 2026 18:53
Copy link
Copy Markdown
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

Great! Thanks

@sbc100 sbc100 changed the title cmdline: allow overriding -jsD directives (#26576) cmdline: Allow overriding -jsD directives Apr 9, 2026
@sbc100 sbc100 enabled auto-merge (squash) April 9, 2026 19:08
auto-merge was automatically disabled April 10, 2026 05:22

Head branch was pushed to by a user without write access

@subhaushsingh subhaushsingh force-pushed the fix-jsd-override-26576 branch 2 times, most recently from 83dde76 to 7bd8181 Compare April 10, 2026 06:35
@subhaushsingh subhaushsingh force-pushed the fix-jsd-override-26576 branch from 7bd8181 to 4c47d6a Compare April 10, 2026 07:23
@subhaushsingh
Copy link
Copy Markdown
Contributor Author

Sorry for the messy commit history, still getting familiar with this codebase. Thanks for the patience @sbc100. The fix captures builtin_settings before processing any -jsD flags to allow duplicates, and the JS test fixture now uses {{{ CUSTOM_JS_OPTION }}} to return the actual value.

@subhaushsingh subhaushsingh requested a review from sbc100 April 10, 2026 08:32
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.

Overriding -jsD directives causes an error

2 participants