Skip to content

Fix rewatch panic when package.json has no "name" field#8291

Merged
cknitt merged 2 commits into
masterfrom
fix-rewatch-panic
Mar 12, 2026
Merged

Fix rewatch panic when package.json has no "name" field#8291
cknitt merged 2 commits into
masterfrom
fix-rewatch-panic

Conversation

@cknitt

@cknitt cknitt commented Mar 12, 2026

Copy link
Copy Markdown
Member

Fix rewatch panic when package.json has no "name" field

Fixes #8184

Problem

Rewatch panicked when package.json existed but lacked a "name" field, even though rescript.json contained the name. This occurred in CI's installation test where npm i creates a package.json without a name field.

thread 'main' panicked at src/build/packages.rs:483:56:
Could not read package name: No name field found in package.json

Solution

Modified read_package_name to fall back to rescript.json when package.json has no "name" field, instead of immediately erroring.

  • Check package.json first for "name" field
  • If missing, check rescript.json
  • Only error if neither file has a "name" field
  • Fixed error message to reference both files

Testing

Removed tests/package_tests/installation_test/package.json to restore the original test scenario (no package.json, name read from rescript.json). Verified build succeeds in this configuration.

Added a rewatch unit test for the remaining error path where both package.json and rescript.json exist but neither contains a name field.

Comment thread rewatch/src/build/packages.rs Outdated
@cknitt cknitt force-pushed the fix-rewatch-panic branch 2 times, most recently from 69e7008 to 11ad226 Compare March 12, 2026 09:18
Comment thread rewatch/src/build/packages.rs Outdated
let contents = fs::read_to_string(&rescript_json_path)
.map_err(|e| anyhow!("Could not read rescript.json: {}", e))?;
let json: serde_json::Value =
serde_json::from_str(&contents).map_err(|e| anyhow!("Could not parse rescript.json: {}", e))?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the changes in read_package_name bring anything new here?
Seems like a rewrite just for fun.
Notice how the original code had the file_name in .map_err(|e| anyhow!("Could not parse {}: {}", file_name, e))?;, in this version you no longer have this.

The real fix in is in make_package.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_package_name still needs a change because without a change, this case still fails:

  • package.json exists
  • package.json has no name
  • rescript.json does have name

The make_package change only fixes the panic.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, good catch!

@cknitt cknitt force-pushed the fix-rewatch-panic branch from 11ad226 to 5dc5618 Compare March 12, 2026 09:23
@pkg-pr-new

pkg-pr-new Bot commented Mar 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@8291

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@8291

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@8291

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@8291

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@8291

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@8291

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@8291

commit: 7c0b9cc

@cknitt cknitt merged commit a673a5c into master Mar 12, 2026
25 checks passed
@cknitt cknitt deleted the fix-rewatch-panic branch March 12, 2026 11:23
nojaf pushed a commit to nojaf/rescript that referenced this pull request Mar 16, 2026
…g#8291)

* Fix rewatch panic when package.json has no "name" field

* CHANGELOG
cknitt added a commit that referenced this pull request May 16, 2026
* Fix rewatch panic when package.json has no "name" field

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

Labels

None yet

2 participants