megahertz / electron-log

Simple logging module Electron/Node.js/NW.js application. No dependencies. No complicated configuration.
MIT License
1.27k stars 124 forks source link

Unhandled electron-log error TypeError: The "path" argument must be of type string. Received undefined #399

Closed douglascayers closed 5 months ago

douglascayers commented 5 months ago

Summary

👋 Hello, thanks for this great project!

I'm converting a typescript project from CJS to ESM (😭) and I encountered an issue in one of my tests that uses code that uses electron-log.

In my original code base, I dynamically imported electron-log/main when it was needed, but with the ESM version of my project I now statically import it import electronLogMain from 'electron-log/main.js';. Not sure if that's related, just giving some context.

In any case, it seems electron isn't initialized in my new world like it had been and so electron.app.name or electron.app.getName() return undefined, which led to the following error message:

Unhandled electron-log error TypeError: The "path" argument must be of type string. Received undefined
        at new NodeError (node:internal/errors:405:5)
        at validateString (node:internal/validators:162:11)
        at Object.join (node:path:1171:7)
        at ElectronExternalApi.getAppLogPath (/path/to/app/node_modules/electron-log/src/node/NodeExternalApi.js:17:19)
        at ElectronExternalApi.getPathVariables (/path/to/app/node_modules/electron-log/src/node/NodeExternalApi.js:95:31)
        at initializeOnFirstAccess (/path/to/app/node_modules/electron-log/src/node/transports/file/index.js:84:23)
        at getFile (/path/to/app/node_modules/electron-log/src/node/transports/file/index.js:117:5)
        at transport (/path/to/app/node_modules/electron-log/src/node/transports/file/index.js:60:18)
        at Logger.processMessage (/path/to/app/node_modules/electron-log/src/core/Logger.js:173:11)
        at Logger.logData (/path/to/app/node_modules/electron-log/src/core/Logger.js:129:10)
        at Object.newScope.<computed> (/path/to/app/node_modules/electron-log/src/core/scope.js:26:42)
        at Object.<anonymous> (/path/to/app/node_modules/jest-mock/build/index.js:794:25)
        at /path/to/app/node_modules/jest-mock/build/index.js:397:39
        at Object.<anonymous> (/path/to/app/node_modules/jest-mock/build/index.js:404:13)
        at Object.mockConstructor [as info] (/path/to/app/node_modules/jest-mock/build/index.js:148:19)

Digging into the electron-log code, I identified the two methods that were impacted:

class ElectronExternalApi extends NodeExternalApi {
  getAppName() {
    try {
      return electron.app?.name || electron.app?.getName();
    } catch {
      return super.getAppName();
    }
  }

  getAppVersion() {
    try {
      return electron.app?.getVersion();
    } catch {
      return super.getAppVersion();
    }
  }

  ...
}

Note that the super fallback is only called if an error was caught, not also if electron.app.xyz was undefined.

Expected Result

Actual Result

Versions

"electron-log": "^5.1.0"
"electron": "^28.2.0"
"jest": "^29.7.0"
"typescript": "^5.3.3"

Node v18.18.0