RangerMauve / hyper-sdk

Make your own hyper apps!
https://www.youtube.com/watch?v=HyHk4aImd_I&list=PL7sG5SCUNyeYx8wnfMOUpsh7rM_g0w_cu&index=20
MIT License
292 stars 45 forks source link

Electron build failing #56

Closed hexadecible closed 4 years ago

hexadecible commented 4 years ago

It looks like the SDK now officially supports Electron, but when I attempt to build the package I get the following errors. Is this the correct way to include the sdk into Electron, or are we supposed to build a browser bundle?

Electron React Boilerplate

✖ Rebuild Failed

An unhandled error occurred inside electron-rebuild
gyp info it worked if it ends with ok
gyp info using node-gyp@6.1.0
gyp info using node@12.8.1 | darwin | x64
gyp info find Python using Python version 2.7.5 found at "/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python"
gyp info spawn /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
gyp info spawn args [
gyp info spawn args   '/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/carolbaskins/Documents/electron-mail-client/app/node_modules/sodium-native/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/carolbaskins/.electron-gyp/7.1.13/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/carolbaskins/.electron-gyp/7.1.13',
gyp info spawn args   '-Dnode_gyp_dir=/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/carolbaskins/.electron-gyp/7.1.13/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/carolbaskins/Documents/electron-mail-client/app/node_modules/sodium-native',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  CC(target) Release/obj.target/sodium/binding.o
../binding.c:70:20: warning: implicit declaration of function 'napi_detach_arraybuffer' is invalid in C99 [-Wimplicit-function-declaration]
  SN_STATUS_THROWS(napi_detach_arraybuffer(env, array_buf), "failed to detach array buffer");
                   ^
1 warning generated.
  SOLINK_MODULE(target) Release/sodium.node
clang: error: no such file or directory: '../lib/libsodium-x64.dylib'
make: *** [Release/sodium.node] Error 1
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:203:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
gyp ERR! System Darwin 19.4.0
gyp ERR! command "/Users/carolbaskins/.nvm/versions/node/v12.8.1/bin/node" "/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/.bin/node-gyp" "rebuild" "--target=7.1.13" "--arch=x64" "--dist-url=https://www.electronjs.org/headers" "--build-from-source"
gyp ERR! cwd /Users/carolbaskins/Documents/electron-mail-client/app/node_modules/sodium-native
gyp ERR! node -v v12.8.1
gyp ERR! node-gyp -v v6.1.0
gyp ERR! not ok 

Failed with exit code: 1

Error: gyp info it worked if it ends with ok
gyp info using node-gyp@6.1.0
gyp info using node@12.8.1 | darwin | x64
gyp info find Python using Python version 2.7.5 found at "/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python"
gyp info spawn /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
gyp info spawn args [
gyp info spawn args   '/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/carolbaskins/Documents/electron-mail-client/app/node_modules/sodium-native/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/carolbaskins/.electron-gyp/7.1.13/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/carolbaskins/.electron-gyp/7.1.13',
gyp info spawn args   '-Dnode_gyp_dir=/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/carolbaskins/.electron-gyp/7.1.13/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/carolbaskins/Documents/electron-mail-client/app/node_modules/sodium-native',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  CC(target) Release/obj.target/sodium/binding.o
../binding.c:70:20: warning: implicit declaration of function 'napi_detach_arraybuffer' is invalid in C99 [-Wimplicit-function-declaration]
  SN_STATUS_THROWS(napi_detach_arraybuffer(env, array_buf), "failed to detach array buffer");
                   ^
1 warning generated.
  SOLINK_MODULE(target) Release/sodium.node
clang: error: no such file or directory: '../lib/libsodium-x64.dylib'
make: *** [Release/sodium.node] Error 1
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:203:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
gyp ERR! System Darwin 19.4.0
gyp ERR! command "/Users/carolbaskins/.nvm/versions/node/v12.8.1/bin/node" "/Users/carolbaskins/Documents/electron-mail-client/node_modules/electron-rebuild/node_modules/.bin/node-gyp" "rebuild" "--target=7.1.13" "--arch=x64" "--dist-url=https://www.electronjs.org/headers" "--build-from-source"
gyp ERR! cwd /Users/carolbaskins/Documents/electron-mail-client/app/node_modules/sodium-native
gyp ERR! node -v v12.8.1
gyp ERR! node-gyp -v v6.1.0
gyp ERR! not ok 

Failed with exit code: 1
    at SafeSubscriber._error (/Users/carolbaskins/Documents/electron-mail-client/node_modules/spawn-rx/lib/src/index.js:267:84)
    at SafeSubscriber.__tryOrUnsub (/Users/carolbaskins/Documents/electron-mail-client/node_modules/rxjs/internal/Subscriber.js:205:16)
    at SafeSubscriber.error (/Users/carolbaskins/Documents/electron-mail-client/node_modules/rxjs/internal/Subscriber.js:156:26)
    at Subscriber._error (/Users/carolbaskins/Documents/electron-mail-client/node_modules/rxjs/internal/Subscriber.js:92:26)
    at Subscriber.error (/Users/carolbaskins/Documents/electron-mail-client/node_modules/rxjs/internal/Subscriber.js:72:18)
    at MapSubscriber.Subscriber._error (/Users/carolbaskins/Documents/electron-mail-client/node_modules/rxjs/internal/Subscriber.js:92:26)
    at MapSubscriber.Subscriber.error (/Users/carolbaskins/Documents/electron-mail-client/node_modules/rxjs/internal/Subscriber.js:72:18)
    at SafeSubscriber._next (/Users/carolbaskins/Documents/electron-mail-client/node_modules/spawn-rx/lib/src/index.js:242:65)
    at SafeSubscriber.__tryOrUnsub (/Users/carolbaskins/Documents/electron-mail-client/node_modules/rxjs/internal/Subscriber.js:205:16)
    at SafeSubscriber.next (/Users/carolbaskins/Documents/electron-mail-client/node_modules/rxjs/internal/Subscriber.js:143:22)
child_process.js:660
    throw err;
    ^

Error: Command failed: ../node_modules/.bin/electron-rebuild --parallel --force --types prod,dev,optional --module-dir .
    at checkExecSyncError (child_process.js:621:11)
    at execSync (child_process.js:657:15)
    at Object.<anonymous> (/Users/carolbaskins/Documents/electron-mail-client/internals/scripts/ElectronRebuild.js:18:3)
    at Module._compile (internal/modules/cjs/loader.js:868:30)
    at Module._compile (/Users/carolbaskins/Documents/electron-mail-client/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:879:10)
    at Object.newLoader [as .js] (/Users/carolbaskins/Documents/electron-mail-client/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:731:32)
    at Function.Module._load (internal/modules/cjs/loader.js:644:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:931:10) {
  status: 255,
  signal: null,
  output: [ null, null, null ],
  pid: 52280,
  stdout: null,
  stderr: null
}
RangerMauve commented 4 years ago

Interesting, it looks like the trouble is coming from the sodium-native module. Could you double check what the requirements are for that and that your setup meets them? It could be that the version of electron you're using isn't compatible with them.

I got it working in electron@7.1.4 with electron-react-boilerplate@0.18.1

RangerMauve commented 4 years ago

One caveat is that you should be running the dat-sdk inside the main thread rather than within a browser window for the best results. I combine that with electron's remote API and a wrapper over the main dat interactions. (sadly this project isn't open source yet)

hexadecible commented 4 years ago

This ended up being an issue with electron-rebuild. It was attempting to rebuild sodium-native and should have been skipping over that module since it was already prebuilt. @mafintosh opened an issue a while back which was resolved, but it appears electron-rebuild is looking for

fs.exists(path.resolve(modulePath, 'prebuilds', `${process.platform}-${this.arch}`, `electron-${this.ABI}.node`))

It looks like this file no longer exists inside of sodium-native's prebuild directory. I had to add the following as a workaround inside of internals/scripts/ElectronRebuild.js.

import path from 'path';
import { execSync } from 'child_process';
import fs, { readdirSync } from 'fs';
import { dependencies } from '../../app/package.json';

const nodeModulesPath = path.join(__dirname, '..', '..', 'app', 'node_modules');

const getModules = (source, exclude) =>
  readdirSync(source, { withFileTypes: true })
    .filter(dirent => dirent.isDirectory() && !exclude.includes(dirent.name))
    .map(dirent => dirent.name);

if (
  Object.keys(dependencies || {}).length > 0 &&
  fs.existsSync(nodeModulesPath)
) {
  // Skip modules that are already prebuilt
  const exclude = ['sodium-native', 'utp-native', '.bin'];
  const modules = getModules(nodeModulesPath, exclude);

  const electronRebuildCmd = `../node_modules/.bin/electron-rebuild --parallel --types prod,dev,optional --only ${modules.toString()} --module-dir .`;

  const cmd =
    process.platform === 'win32'
      ? electronRebuildCmd.replace(/\//g, '\\')
      : electronRebuildCmd;
  execSync(cmd, {
    cwd: path.join(__dirname, '..', '..', 'app'),
    stdio: 'inherit'
  });
}
RangerMauve commented 4 years ago

I just ran into this too, I went with the approach of making a rebuild.sh script in the root of my project (not using electron starter for this).

https://www.electronjs.org/docs/tutorial/using-native-node-modules#using-npm

mafintosh commented 4 years ago

Unsure if you already know this but just to make it clear: all native modules used by hyper* have prebuilds bundled for electron, so do not rebuild any of those :)

RangerMauve commented 4 years ago

@mafintosh What should I be doing about the prebuilds not working with a regular npm install?

mafintosh commented 4 years ago

@RangerMauve open an issue on the relevant native repo. any latest major is supported through n-api on node 10+, recent electrons

RangerMauve commented 3 years ago

I'm having a bunch of other issues, but adding "npmRebuild": false, to my package.json helped. 🤷