-
Notifications
You must be signed in to change notification settings - Fork 296
Description
Bug Report: manifestUrl undefined causing build failures + package.json formatting issues
Hi! 👋
Firstly, thanks for your work on this project! 🙂
Today I used patch-package to patch nw-builder@4.17.1 for the project I'm working on.
Problem Description
nw-builder fails with TypeError: Cannot read properties of undefined (reading 'startsWith') when manifestUrl is not properly passed through the build process. Additionally, the build process minifies
package.json files, making them unreadable.
Error Details
TypeError: Cannot read properties of undefined (reading 'startsWith')
at get (file:///node_modules/@nwutils/getter/src/main.js:44:27)
at nwbuild (file:///node_modules/nw-builder/src/index.js:96:11)
Root Cause Analysis
- Options Overwriting Issue: In src/index.js line 60, when manifest.json.nwbuild exists, the entire options object is replaced instead of merged, losing all default values including manifestUrl
- Missing Null Checks: The getManifest function in src/util.js doesn't handle undefined manifestUrl parameter
- Package.json Minification: src/bld.js writes package.json without formatting, creating unreadable one-liners
Impact
- Build process fails completely with manifestUrl errors
- Package.json becomes unreadable one-liner after build
- Affects all platforms (macOS, Windows, Linux)
- Makes nw-builder unusable when package.json contains nwbuild configuration
Reproduction Steps
- Create a project with package.json containing nwbuild configuration
- Run any nw-builder command
- Observe the manifestUrl undefined error and minified package.json
Here is the diff that solved my problem:
diff --git a/node_modules/nw-builder/src/bld.js b/node_modules/nw-builder/src/bld.js
index 766263b..4c5fb31 100644
--- a/node_modules/nw-builder/src/bld.js
+++ b/node_modules/nw-builder/src/bld.js
@@ -136,7 +136,7 @@ async function bld({
/* Set `product_string` in manifest for MacOS. This is used in renaming the Helper apps. */
if (platform === 'osx') {
manifest.json.product_string = app.name;
- await fs.promises.writeFile(manifest.path, JSON.stringify(manifest.json));
+ await fs.promises.writeFile(manifest.path, JSON.stringify(manifest.json, null, 2));
}
if (glob) {
diff --git a/node_modules/nw-builder/src/index.js b/node_modules/nw-builder/src/index.js
index da13d19..237391f 100644
--- a/node_modules/nw-builder/src/index.js
+++ b/node_modules/nw-builder/src/index.js
@@ -55,7 +55,7 @@ async function nwbuild(options) {
util.log('debug', 'info', 'Get node manifest...');
manifest = await util.getNodeManifest({ srcDir: options.srcDir, glob: options.glob });
if (typeof manifest.json?.nwbuild === 'object') {
- options = manifest.json.nwbuild;
+ options = { ...options, ...manifest.json.nwbuild };
}
util.log('info', options.logLevel, 'Parse final options using node manifest');
diff --git a/node_modules/nw-builder/src/util.js b/node_modules/nw-builder/src/util.js
index 47a5fee..faf6c7d 100644
--- a/node_modules/nw-builder/src/util.js
+++ b/node_modules/nw-builder/src/util.js
@@ -15,7 +15,7 @@ import * as GlobModule from 'glob';
function getManifest(manifestUrl) {
let chunks = '';
- if (manifestUrl.startsWith('file://')) {
+ if (manifestUrl && manifestUrl.startsWith('file://')) {
const filePath = url.fileURLToPath(manifestUrl);
return fs.readFileSync(filePath, 'utf-8');
}Fix Summary
- Preserve default options: Use spread operator to merge instead of replace options
- Add null safety: Check manifestUrl exists before calling startsWith()
- Maintain JSON formatting: Add proper indentation when writing package.json
Environment
- nw-builder version: 4.17.1
- Node.js version: 22.21.1+
- Affects all platforms
This patch resolves both the critical build failure and improves the developer experience by maintaining readable package.json files.
This issue body was partially generated by patch-package.