Browse Source

fix: allow renaming electron.exe (backport: 4-0-x) (#15249)

* fix: allow renaming electron.exe

* add test

* fix lf-at-eol

* review comments

* fix lint

* fix lint-sam
trop[bot] 6 years ago
parent
commit
96a4fce100
4 changed files with 39 additions and 0 deletions
  1. 10 0
      BUILD.gn
  2. 6 0
      build/electron.def
  3. 6 0
      spec/fixtures/module/runas-renamed.js
  4. 17 0
      spec/modules-spec.js

+ 10 - 0
BUILD.gn

@@ -750,6 +750,16 @@ if (is_mac) {
         "/DELAYLOAD:api-ms-win-core-winrt-l1-1-0.dll",
         "/DELAYLOAD:api-ms-win-core-winrt-string-l1-1-0.dll",
       ]
+
+      # This is to support renaming of electron.exe. node-gyp has hard-coded
+      # executable names which it will recognise as node. This module definition
+      # file claims that the electron executable is in fact named "node.exe",
+      # which is one of the executable names that node-gyp recognizes.
+      # See https://github.com/nodejs/node-gyp/commit/52ceec3a6d15de3a8f385f43dbe5ecf5456ad07a
+      ldflags += [ "/DEF:" + rebase_path("build/electron.def", root_build_dir) ]
+      inputs = [
+        "build/electron.def",
+      ]
     }
     if (is_linux) {
       ldflags = [ "-pie" ]

+ 6 - 0
build/electron.def

@@ -0,0 +1,6 @@
+; This is to support renaming of electron.exe. node-gyp has hard-coded
+; executable names which it will recognise as node. This module definition
+; file claims that the electron executable is in fact named "node.exe",
+; which is one of the executable names that node-gyp recognizes.
+; See https://github.com/nodejs/node-gyp/commit/52ceec3a6d15de3a8f385f43dbe5ecf5456ad07a
+NAME node.exe

+ 6 - 0
spec/fixtures/module/runas-renamed.js

@@ -0,0 +1,6 @@
+try {
+  require('runas')
+} catch (e) {
+  process.exit(1)
+}
+process.exit(0)

+ 17 - 0
spec/modules-spec.js

@@ -1,6 +1,7 @@
 const assert = require('assert')
 const Module = require('module')
 const path = require('path')
+const fs = require('fs')
 const { remote } = require('electron')
 const { BrowserWindow } = remote
 const { closeWindow } = require('./window-helpers')
@@ -24,6 +25,22 @@ describe('modules support', () => {
           done()
         })
       })
+
+      if (process.platform === 'win32') {
+        it('can be required if electron.exe is renamed', () => {
+          const { execPath } = remote.process
+          const testExecPath = path.join(path.dirname(execPath), 'test.exe')
+          fs.copyFileSync(execPath, testExecPath)
+          try {
+            const runasFixture = path.join(fixtures, 'module', 'runas-renamed.js')
+            assert.ok(fs.existsSync(runasFixture))
+            const child = require('child_process').spawnSync(testExecPath, [runasFixture])
+            assert.strictEqual(child.status, 0)
+          } finally {
+            fs.unlinkSync(testExecPath)
+          }
+        })
+      }
     })
 
     // TODO(alexeykuzmin): Disabled during the Chromium 62 (Node.js 9) upgrade.