Skip to content

Conversation

@mikejoseph23
Copy link

@mikejoseph23 mikejoseph23 commented Oct 22, 2025

Summary

Fixes native module compilation issues on both macOS and Windows by patching native-keymap binding
configuration.

Issues Fixed

macOS (Electron 38)

  • Problem: Native modules failed to compile with Electron 38 due to missing C++20 standard configuration
  • Error: error: 'value' is unavailable: introduced in macOS 11.0
  • Solution: Added C++20 xcode_settings with macOS 11.0 deployment target

Windows

  • Problem: Native modules required Spectre-mitigated libraries not installed by default
  • Error: MSB8040: Spectre-mitigated libraries are required for this project
  • Solution: Disabled SpectreMitigation requirement in msvs_configuration_attributes

Changes

Added Dependencies

  • Added patch-package as devDependency to apply native module patches during installation

Patch File

Created patches/native-keymap+3.3.5.patch with:

  1. Line 10: Set 'SpectreMitigation': 'false' (Windows fix)
  2. Lines 18-22: Added xcode_settings for C++20 compilation (macOS fix):
    'CLANG_CXX_LANGUAGE_STANDARD': 'c++20'
    'CLANG_CXX_LIBRARY': 'libc++'
    'MACOSX_DEPLOYMENT_TARGET': '11.0'

Package.json

  • Added "postinstall": "patch-package" script to automatically apply patches after npm install

Build Requirements

⚠️ Node.js ≥20.19.0 required

  • Newer electron-vite v4.0.1 requires Node.js ^20.19.0 or >=22.12.0
  • Earlier versions (v20.18.0) will encounter ESM/CommonJS compatibility errors

Installation Instructions

Standard Installation (Recommended)

npm install
The patch will be applied automatically via the postinstall script.

Manual Installation (If needed)

npm install --ignore-scripts
npx patch-package
npm rebuild

Testing

macOS ✅

- Native modules compile successfully with Electron 38
- Dev and production builds work

Windows ✅

- Native modules compile without MSB8040 Spectre errors
- Full Windows installer built successfully: marktext-win-x64-0.18.5-setup.exe
- Dev and production builds work
- Tested on Windows with Node.js v20.19.5

Affected Modules

All native modules now compile successfully:
- [email protected]
- ced
- keytar

Fixes Issue #8

Resolves native-keymap build failures on macOS caused by missing C++20
compiler flags. The native-keymap dependency requires C++20 features
(constexpr, concept, requires) for Electron 38 compatibility, but the
macOS build configuration was missing the necessary xcode_settings.

Changes:
- Added patch-package to apply C++20 fix to native-keymap binding.gyp
- Created patches/native-keymap+3.3.5.patch with xcode_settings for macOS
- Enabled npmRebuild in electron-builder.yml to properly rebuild native
  modules for both x64 and arm64 architectures
- Added postinstall script to automatically apply patches

The patch adds CLANG_CXX_LANGUAGE_STANDARD: 'c++20' to the macOS build
configuration in native-keymap, allowing it to compile successfully
against Electron 38 headers.

Tested on:
- macOS Ventura 13.7 (Intel iMac 2017) - x64 build
- macOS Sequoia 15.x (M4 MacBook Air 2025) - arm64 build

Fixes Tkaixiang#8
The postinstall script requires patch-package to be installed
as a devDependency. Without it, npm install fails with
'patch-package: command not found'.

This commit completes the native module compilation fix by
ensuring patch-package is available to apply the native-keymap
patches during installation.
Extends the existing native-keymap patch to disable Spectre mitigation
requirements on Windows. This allows the native module to compile without
requiring Spectre-mitigated libraries from Visual Studio.

Changes:
- Set 'SpectreMitigation': 'false' in binding.gyp msvs_configuration_attributes
- Retains existing macOS C++20 fix

This is a test commit to verify the fix works on Windows before merging
into the main PR branch.
- Updated package-lock.json for Node.js 20.19.5 compatibility
- Added test results showing successful Windows build
- Confirmed Spectre mitigation bypass patch works correctly
- Windows installer created: marktext-win-x64-0.18.5-setup.exe (123.5 MB)

All native modules (native-keymap, ced, keytar) compiled successfully
without MSB8040 Spectre errors.
@mikejoseph23 mikejoseph23 changed the title Fix macOS native module compilation for Electron 38 Fix native module compilation for macOS (Electron 38) and Windows (Spectre mitigation) Oct 24, 2025
@mikejoseph23
Copy link
Author

Hey @Tkaixiang ... This PR should fix macOS! Let me know if you need me too change anything! I'd be glad to walk you through the other PR's I've submitted as well...

@Tkaixiang
Copy link
Owner

Hi Mike, thank you for your PRs. Unfortuantely I do not have the time to review them at the moment due to irl commitments. I should be more free in a few weeks time :)

Copy link
Owner

Choose a reason for hiding this comment

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

Hi, please remove this file as it is not related to the repository :)

Comment on lines +8 to +10
'msvs_configuration_attributes': {
- 'SpectreMitigation': 'Spectre'
+ 'SpectreMitigation': 'false'
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a need to remove SpectreMitigation when it can be installed easily via the Visual Studio Installer?

SpectreMitigation is a useful security feature with minimal performance impact.

Comment on lines 16 to +20
"build:unpack": "npm run minify-locales && npm run build && electron-builder --dir",
"build:win": "npm run minify-locales && npx @electron/rebuild && npm run build && electron-builder --win --publish never",
"build:mac": "npm run minify-locales && npx @electron/rebuild && npm run build && electron-builder --mac --publish never",
"build:linux": "npm run minify-locales && npx @electron/rebuild && npm run build && electron-builder --linux --publish never"
"build:mac": "npm run minify-locales && npm run build && electron-builder --mac --publish never",
"build:linux": "npm run minify-locales && npx @electron/rebuild && npm run build && electron-builder --linux --publish never",
"postinstall": "patch-package"
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a specific reason why we removed npx @electron/rebuild and instead relied on npmRebuild in electron-builder.yml? The GitHub Mac runners seem to compile the builds just fine?

The scratchpad file has been moved to a separate branch
(feature/scratchpad-notes) as it is not related to the
native module compilation fixes in this PR.
@mikejoseph23
Copy link
Author

Re: removing @electron/rebuild from build:mac

The @electron/rebuild was removed from build:mac because it was failing on macOS with the C++20 compilation issues that this PR fixes. Since electron-builder.yml already has npmRebuild: true (line 49), electron-builder handles the native module rebuild automatically during packaging. The GitHub Mac runners work fine because they likely have the correct toolchain already configured, but local macOS builds were failing before the patch was applied.

The current approach relies on:

  1. patch-package (postinstall) to apply the C++20 fix
  2. npmRebuild: true in electron-builder.yml to rebuild during packaging

This is cleaner and avoids the redundant @electron/rebuild step.

@mikejoseph23
Copy link
Author

I just removed those extraneous files. Sorry about that. I'm new to the git PR workflow since I mostly just work by myself most of the time. Apologies for the confusion.

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.

2 participants