Skip to content

getFileName does not work in electron #29

Closed

Description

@xianjimli

getFileName does not work in electron, In Electron, filename starts with "file://", it should be removed, or bind will failed.

exports.getFileName = function getFileName (calling_file) {
  var origPST = Error.prepareStackTrace
    , origSTL = Error.stackTraceLimit
    , dummy = {}
    , fileName

  Error.stackTraceLimit = 10

  Error.prepareStackTrace = function (e, st) {
    for (var i=0, l=st.length; i<l; i++) {
      fileName = st[i].getFileName()
      if (fileName !== __filename) {
        if (calling_file) {
            if (fileName !== calling_file) {
              return
            }
        } else {
          return
        }
      }
    }
  }

  // run the 'prepareStackTrace' function above
  Error.captureStackTrace(dummy)
  dummy.stack

 // cleanup
  Error.prepareStackTrace = origPST
  Error.stackTraceLimit = origSTL

  //In Electron, filename starts with "file://"
  var fileSchema = "file://";
  if(fileName.indexOf(fileSchema) === 0) {
     fileName = fileName.substr(fileSchema.length);
     //on windows
     if(fileName.indexOf(":/") == 2){
         fileName = fileName.substr(1);
     }
  }

  return fileName
}

Activity

  1. perymimon commented on Dec 12, 2018

    @perymimon

    i still reproduce it when i use leveldown package

  2. 1009466147 commented on Jan 21, 2019

    @1009466147

    hi I have the same problem,This code returns undefined,In nw.js version 0.14.7 , use ffi
    Error.prepareStackTrace = function (e, st) { for (var i=0, l=st.length; i<l; i++) { fileName = st[i].getFileName() if (fileName !== __filename) { if (calling_file) { if (fileName !== calling_file) { return } } else { return } } } }

  3. E3V3A commented on Jan 23, 2019

    @E3V3A

    It seem that @TooTallNate is too concerned telling people about his height. 🤔 A PR has fixed the issue but has not been merged...

  4. thisTom commented on Aug 27, 2019

    @thisTom

    hi I have the same problem,This code returns undefined,In nw.js version 0.14.7 , use ffi
    Error.prepareStackTrace = function (e, st) { for (var i=0, l=st.length; i<l; i++) { fileName = st[i].getFileName() if (fileName !== __filename) { if (calling_file) { if (fileName !== calling_file) { return } } else { return } } } }

    i have the same problem ,when i use electron and ffi

  5. pjebs commented on Apr 16, 2020

    @pjebs

    This issue is still not fixed for electron -- at least when bindings package is bundled using rollup.

    /**
     * Gets the filename of the JavaScript file that invokes this function.
     * Used to help find the root directory of a module.
     * Optionally accepts an filename argument to skip when searching for the invoking filename
     */
    

    When bundled, it's already embedded in the javascript. There is no other file that calls it.

    This is the error:

    Uncaught Exception:
    TypeError: Cannot read property 'indexOf' of undefined
        at Function.getFileName (/xxx/my-app/out/theCucumber.app.x/the-great-app-darwin-x64/the-great-app.app/Contents/Resources/app/src/bundle.js:260:16)
        at bindings (/xxx/my-app/out/theCucumber.app.x/the-great-app-darwin-x64/the-great-app.app/Contents/Resources/app/src/bundle.js:165:48)
        at Object.<anonymous> (/xxx/my-app/out/theCucumber.app.x/the-great-app-darwin-x64/the-great-app.app/Contents/Resources/app/src/bundle.js:1290:16)
        at Module._compile (internal/modules/cjs/loader.js:968:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:986:10)
        at Module.load (internal/modules/cjs/loader.js:816:32)
        at Module._load (internal/modules/cjs/loader.js:728:14)
        at Module._load (electron/js2c/asar.js:717:26)
        at Function.Module._load (electron/js2c/asar.js:717:26)
        at Object.<anonymous> (electron/js2c/browser_init.js:5785:12)
    

    This is the location of error:

      // handle filename that starts with "file://"
      var fileSchema = 'file://';
      if (fileName.indexOf(fileSchema) === 0) {
        fileName = fileUriToPath_1(fileName);
      }
    
  6. pjebs commented on Apr 16, 2020

    @pjebs

    Is it not standard practice to bundle your javascript files into 1 in electron?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions