electron-userland / electron-builder

A complete solution to package and build a ready for distribution Electron app with “auto update” support out of the box
https://www.electron.build
MIT License
13.62k stars 1.74k forks source link

TypeScript configuration compilation not working under Windows #7572

Closed jsimomaa closed 1 year ago

jsimomaa commented 1 year ago

Tried to convert my electron-builder.js to TypeScript and it seems the TS-config requires compilation. However the compilation (or saving the resulting extended sources after compiling) fails:

compiling: [
  'C:\\app\\myapp\\electron\\.electron-builder.config.ts'
]
Could not write file 'C:/Users/ContainerAdministrator/.cache/config-file-ts/:-app-myapp-electron-.electron-builder.config.ts/app/myapp/electron/.electron-builder.config.js': EINVAL: invalid argument, mkdir 'C:/Users/ContainerAdministrator/.cache/config-file-ts/:-app-myapp-electron-.electron-builder.config.ts'.
  ⨯ ENOENT: no such file or directory, open 'C:\Users\ContainerAdministrator\.cache\config-file-ts\:-app-myapp-electron-.electron-builder.config.ts\_sources'  failedTask=build stackTrace=Error: ENOENT: no such file or directory, open 'C:\Users\ContainerAdministrator\.cache\config-file-ts\:-app-myapp-electron-.electron-builder.config.ts\_sources'
    at Object.openSync (node:fs:585:3)
    at Object.writeFileSync (node:fs:2229:35)
    at saveExtendedSources (C:\app\myapp\electron\node_modules\config-file-ts\src\compileUtil.ts:98:6)
    at compileIfNecessary (C:\app\myapp\electron\node_modules\config-file-ts\src\compileUtil.ts:74:7)
    at compileConfigIfNecessary (C:\app\myapp\electron\node_modules\config-file-ts\src\compileUtil.ts:173:19)
    at Object.loadTsConfig (C:\app\myapp\electron\node_modules\config-file-ts\src\loadTsConfig.ts:18:20)
    at readConfig (C:\app\myapp\electron\node_modules\read-config-file\src\main.ts:30:14)
    at getConfig (C:\app\myapp\electron\node_modules\electron-builder\node_modules\app-builder-lib\src\util\config.ts:44:34)
    at Packager.build (C:\app\myapp\electron\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:314:27)
    at executeFinally (C:\app\myapp\electron\node_modules\builder-util\src\promise.ts:12:14)
error Command failed with exit code 1.

This is because Windows does not allow : in path. The package config-files-ts function defaultOutDir produces an output path with : in it:

https://github.com/mighdoll/config-file-ts/blob/25600267e7da114c5a12b1e9b30759d7f4b7c16c/src/loadTsConfig.ts#L29-L40

Unfortunately package read-config-file does not supply the optional output dir for loadTsConfig:

https://github.com/develar/read-config-file/blob/482fb9fad1a789150064928a80b2a30485ba1f94/src/main.ts#L29-L31

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

jsimomaa commented 1 year ago

Is not stale

jeeanribeiro commented 1 year ago

What has worked for me is transpiling the config and then executing the electron-builder command using the output file path for the config flag, probably not the ideal solution but something that just works!

compile.js

const { execSync } = require('child_process')

// Get stage and OS flag from command line arguments
const stage = process.argv[2]
const osFlag = process.argv[3]

// Set environment variables
process.env.STAGE = stage

// Transpile TypeScript to JavaScript
try {
    execSync('tsc --esModuleInterop electron-builder.config.ts', { stdio: 'inherit' })

    // Build the command based on the stage and OS flag
    const command = `cross-env STAGE=${stage} electron-builder ${osFlag} --x64 --config electron-builder.config.js`

    // Run the electron-builder command
    execSync(command, { stdio: 'inherit' })
} catch (error) {
    console.error('Electron Builder transpilation or execution failed')
    process.exit(1)
}

package.json

"scripts": {
    "compile": "node compile.js"
},

terminal

npm run compile alpha --win
ncaq commented 1 year ago

I use JavaScript and ts-check for workaround.

mmaietta commented 1 year ago

Might be worth opening an issue with config-files-ts to fix the : path to work on windows, then we can update electron-builder accordingly once a new release is published?

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

jsimomaa commented 1 year ago

Not stale

mmaietta commented 1 year ago

Closing this issue. It's not an electron-builder issue. Please open an issue with upstream npm dependency https://www.npmjs.com/package/config-file-ts

Ref: https://github.com/electron-userland/electron-builder/issues/7572#issuecomment-1648295495

roberto-naharro commented 9 months ago

To check this issue status in config-file-ts, the ticket is: https://github.com/mighdoll/config-file-ts/issues/19 It seems they have a PR ready to fix it soon

MichaelRenslayer commented 8 months ago

This is a fake fix. We should re-open it https://github.com/mighdoll/config-file-ts/pull/22

mmaietta commented 8 months ago

Feel free to post in that github project. There's not much I can do from this side and the workaround is to just use the other approach in a .js file with

// Let's get that intellisense working
/**
* @type {import('electron-builder').Configuration}
* @see https://www.electron.build/configuration/configuration
*/

https://www.electron.build/api/programmatic-usage

KaminoRyo commented 7 months ago

I am saddened by this fact. "The configuration file now supports TS" is hype.😞