Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

electron-updater throws "Cannot find namespace 'debug'" on TypeScript compile #1405

Closed
tiangolo opened this issue Mar 24, 2017 · 12 comments · May be fixed by qcif/data-curator#563
Closed

electron-updater throws "Cannot find namespace 'debug'" on TypeScript compile #1405

tiangolo opened this issue Mar 24, 2017 · 12 comments · May be fixed by qcif/data-curator#563
Assignees

Comments

@tiangolo
Copy link

  • Version: 16.3.0
  • electron-updater Version: 1.11.0
  • Target: Linux / Windows (the error is thrown by TypeScript, before any compilation can happen)

Description

I'm creating an Electron app using (:star2: the awesome :tada:) electron-builder.

My app is written in TypeScript, I think that is (ironically) causing the problem.

When I try to compile with tsc (./node_modules/.bin/tsc`) I get the error:

node_modules/electron-builder-http/out/electron-builder-http.d.ts(257,31): error TS2503: Cannot find namespace 'debug'.

When I open that file in the editor I see a:

protected readonly debug: debug.Debugger

with the debug.Debugger showing the error: Cannot find namespace 'debug'.

Looking at the official types for debug I see that there's a declaration compatible to the one in this repo under "typings". But it is not being used, and during compiling it seems like none of those two is used.

Minimal steps to reproduce

  • Create a directory for the test and enter it, in my case:
mkdir test-electron-builder
cd test-electron-builder
  • Put a file package.json with:
{
  "name": "test-electron-updater",
  "version": "1.0.0",
  "main": "out/index.js",
  "license": "MIT",
  "devDependencies": {
    "@types/electron": "^1.4.34",
    "electron": "^1.6.2",
    "electron-builder": "^16.3.0",
    "typescript": "^2.2.1"
  },
  "dependencies": {
    "electron-updater": "^1.11.0"
  },
  "build": {
    "appId": "com.example.test-electron-updater"
  }
}
  • Put a file tsconfig.json with:
{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "out"
    },
    "include": [
        "src/**/*"
    ]
}
  • Create a file in src/index.ts with:
import * as electron from 'electron';
import { autoUpdater } from 'electron-updater';
  • Install the dependencies:
yarn install
  • Compile with tsc:
./node_modules/.bin/tsc

it throws:

node_modules/electron-builder-http/out/electron-builder-http.d.ts(257,31): error TS2503: Cannot find namespace 'debug'.

Workaround fix

I can "fix" it locally by:

  • Entering the module directory:
cd node_modules/electron-builder-http/
  • Installing the typings for debug:
yarn add @types/debug
  • Modifying the file electron-builder-http.d.ts, importing debug. Changing the section:
declare module "electron-builder-http" {
  /// <reference types="node" />
  import { EventEmitter } from "events"

to

declare module "electron-builder-http" {
  /// <reference types="node" />
  import * as debug from 'debug';
  import { EventEmitter } from "events"
  • And changing the type of the debug property below to use the @types interface. Changing the section:
  export abstract class HttpExecutor<REQUEST_OPTS, REQUEST> {
    protected readonly maxRedirects: number
    protected readonly debug: debug.Debugger
  export abstract class HttpExecutor<REQUEST_OPTS, REQUEST> {
    protected readonly maxRedirects: number
    protected readonly debug: debug.IDebugger

Solution ...?

I'm right now working on a PR but I'm not sure I'm approaching it as you (the maintainer) would want it.

I see that the problem is specific to the module electron-builder-http. I also see there are typings in the top level of the project.

And I see in the source that those types are included in the tsconfig.json with a:

...
  "files": [
    "../../typings/debug.d.ts"
  ]
...

but it seems like those types are not being exported to the final npm module, so I can't use them in a TypeScript based project.

My current approach is to add @types/debug as a dependency to electron-builder-http and change the interface to debug.IDebug.

@develar
Copy link
Member

develar commented Mar 24, 2017

Thanks for clear report.

Workaround — add

"skipLibCheck": true

to tsconfig.js as in the ts project https://github.com/develar/onshape-desktop-shell/blob/master/tsconfig.json

@tiangolo
Copy link
Author

Thanks @develar !

That works. 🎉


I almost finished the change to make all the parts use the semi-official types from @types/debug.

Is that something that interests you?


If doing that interests you, I'm having a problem running the tests with yarn run test.

I just resetted the files to remove all my changes with git checkout -- . but even with the pure source files the same error occurs.

I would like to know if I'm any step or if it's a genuine bug / issue.

I made all the changes to use @types/debug in several places that the library is used, but I woudn't like to create a PR while the tests are not passing yet.

@develar
Copy link
Member

develar commented Mar 24, 2017

Official types/debug is bad and must be not used, because global var debug is exported.

@develar
Copy link
Member

develar commented Mar 24, 2017

About tests — please provide error details. Did you clone project using git lfs?

@tiangolo
Copy link
Author

Oh! Sorry! I though I had copy-pasted the logs.

Here's the last part:

Building packages/electron-builder-util
Generating d.ts to /media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/out/electron-builder-util.d.ts
Building packages/electron-publish
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/node_modules/bluebird-lst/index.d.ts (803, 3): Duplicate identifier 'default'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/node_modules/fs-extra-p/index.d.ts (3, 12): Duplicate identifier 'WriteStream'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/node_modules/fs-extra-p/index.d.ts (3, 25): Duplicate identifier 'createReadStream'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/node_modules/fs-extra-p/index.d.ts (3, 43): Duplicate identifier 'createWriteStream'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/node_modules/fs-extra-p/index.d.ts (3, 62): Duplicate identifier 'FSWatcher'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/node_modules/fs-extra-p/index.d.ts (3, 73): Duplicate identifier 'Stats'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/node_modules/fs-extra-p/index.d.ts (5, 15): Duplicate identifier 'Filter'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/node_modules/bluebird-lst/index.d.ts (803, 3): Duplicate identifier 'default'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/node_modules/fs-extra-p/index.d.ts (3, 12): Duplicate identifier 'WriteStream'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/node_modules/fs-extra-p/index.d.ts (3, 25): Duplicate identifier 'createReadStream'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/node_modules/fs-extra-p/index.d.ts (3, 43): Duplicate identifier 'createWriteStream'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/node_modules/fs-extra-p/index.d.ts (3, 62): Duplicate identifier 'FSWatcher'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/node_modules/fs-extra-p/index.d.ts (3, 73): Duplicate identifier 'Stats'.
/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/node_modules/fs-extra-p/index.d.ts (5, 15): Duplicate identifier 'Filter'.
error Command failed with exit code 255.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.      

@tiangolo
Copy link
Author

OK, thanks for the clarification about the official @types/debug types. I'll just stick to your suggested workaround.

Then we can then close this now if that seems OK to you.

@develar
Copy link
Member

develar commented Mar 24, 2017

PR will be accepted. Later someone will fix official typIngs

@develar
Copy link
Member

develar commented Mar 24, 2017

Tests — do not install node modules for sub packages. Please remove all node_modules except root. Did you install it manually by mistake?

@develar
Copy link
Member

develar commented Mar 24, 2017

Yeah — we should use official types to avoid such issues for users.

@develar
Copy link
Member

develar commented Mar 24, 2017

I will hide debug member from http request lib, so, it will be not exposed.

@develar develar self-assigned this Mar 24, 2017
@tiangolo
Copy link
Author

Wow! You're fast @develar ! I was just going to "start to continue working on it" when I saw that you already did a lot about it.

Thanks for the great project and the commitment to it.


Nevertheless, I just installed the version you just released on that same demo project commented above, using yarn add [email protected] and the error is still present:

▶ ./node_modules/.bin/tsc
node_modules/electron-updater/node_modules/electron-builder-http/out/electron-builder-http.d.ts(257,31): error TS2503: Cannot find namespace 'debug'.

Do you want me to finish that PR I was trying and see if that would help (and if that would fit in the project)?

@tiangolo
Copy link
Author

I started trying to finish the PR just to see where it could take me.

I installed and used git lfs.

I removed any node_modules inside the projects.

I added a dev dependency to the top package of @types/debug.

I refactored the debug imports to be like import * as _debug from "debug", etc.

But while running yarn run test I'm gettting an error.

I thought it could be that the debug types where trying to declare a global debug and not allowing to import it as import * as _debug from debug, so I "refactored" everything to use debug and export debugUtil instead, but I still got the same error:

Building test
yarn schema v0.22.0
$ typescript-json-schema packages/electron-builder/tsconfig.json Config --out packages/electron-builder/scheme.json --noExtraProps --useTypeOfKeyword --strictNullChecks
Done in 3.82s.
Done in 25.69s.
yarn lint v0.22.0
$ node test/out/helpers/lint.js

/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/src/util.ts:13
export const debugUtil = debug("electron-builder")
                         ^
TypeError: (0 , (_debug || _load_debug(...))) is not a function
    at Object.<anonymous> (/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/packages/electron-builder-util/src/util.ts:13:26)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/media/user/DATA/temp-wd-code/wd-code/code2/electron-builder/test/src/helpers/lint.ts:7:23)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
error Command failed with exit code 1.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.               

I don't really understand what's happening (I actually don't understand how the full complex build works, without installing dependencies, etc).

Then I tried adding adding @types/debug as a dependency to the internal package, but I got the same error.

And then I tried to "install" the dependencies inside the internal package, but I got the same error as above in #1405 (comment)


I'm kind of lost now.

I just wish it was possible to bundle the used types (your original type declarations) in a sub package in its "out" type declarations file, but it seems that's not possible...

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 a pull request may close this issue.

2 participants