Skip to content

Reuse package resolution for ppx #7776

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

nojaf
Copy link
Member

@nojaf nojaf commented Aug 18, 2025

This is another attempt to solve ppx resolution in monorepos.

image

The gist is that some workspaces can have node_modules on the root and the package level.
We take this into account for finding dependencies but didn't for ppx.
In this PR, I've refactored the same resolution code and reused it when looking for ppx.

I didn't add a test as it is very difficult to reproduce this in a yarn workspace.
It is my understanding that yarn will only show this behaviour when it fails to resolve a single version for the entire workspace.

@tsnobip could you give this one a go?

Copy link

pkg-pr-new bot commented Aug 18, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7776

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/win32-x64

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

commit: 140aedb

Copy link
Member

@tsnobip tsnobip left a comment

Choose a reason for hiding this comment

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

thanks a lot @nojaf, this solution looks good, it works locally on my monorepo.

Supersedes #7740.

@tsnobip
Copy link
Member

tsnobip commented Aug 18, 2025

I didn't add a test as it is very difficult to reproduce this in a yarn workspace.

isn't this covered by the testrepo with-ppx that should install the ppx at the package level.

@nojaf
Copy link
Member Author

nojaf commented Aug 18, 2025

isn't this covered by the testrepo with-ppx that should install the ppx at the package level.

No, yarn installed sury at the root level. And you can't force it to install at the package level. You really need resolution problem before that can happen.

@nojaf nojaf requested a review from Copilot August 18, 2025 13:44
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors and reuses package resolution logic for ppx (preprocessor extension) handling in monorepos. Previously, dependency resolution and ppx resolution used different logic, which caused issues when packages existed at different node_modules levels in a workspace.

  • Extracted common package resolution logic into a reusable try_package_path function
  • Updated ppx flag processing to use the same resolution strategy as regular dependencies
  • Changed error handling from strings to proper anyhow::Result types throughout the parsing pipeline

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
rewatch/src/helpers.rs Added try_package_path function that implements multi-level package resolution logic
rewatch/src/config.rs Updated flatten_ppx_flags to use new resolution logic and proper error handling
rewatch/src/build/parse.rs Changed function signatures to return anyhow::Result and updated error handling
rewatch/src/build/packages.rs Replaced inline resolution logic with call to new try_package_path function
rewatch/src/build.rs Updated to handle new error-returning signature
CHANGELOG.md Added entry documenting the ppx resolution fix

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

let path = helpers::try_package_path(
package_config,
project_context,
format!("{}{}{}", &package_config.name, MAIN_SEPARATOR, y).as_str(),
Copy link
Preview

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

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

The string formatting with MAIN_SEPARATOR could be simplified using Path::join() which handles path separators correctly across platforms.

Suggested change
format!("{}{}{}", &package_config.name, MAIN_SEPARATOR, y).as_str(),
Path::new(&package_config.name).join(y).to_str().unwrap(),

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

I disagree with this really, making a Path and calling unwrap later seems worse.

Some('.') => helpers::try_package_path(
package_config,
project_context,
format!("{}{}{}", package_config.name, MAIN_SEPARATOR, &ys[0]).as_str(),
Copy link
Preview

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

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

Similar to the previous occurrence, using Path::join() would be more idiomatic and handle cross-platform path separators automatically.

Suggested change
format!("{}{}{}", package_config.name, MAIN_SEPARATOR, &ys[0]).as_str(),
Path::new(&package_config.name).join(&ys[0]).to_string_lossy().as_ref(),

Copilot uses AI. Check for mistakes.

@tsnobip
Copy link
Member

tsnobip commented Aug 18, 2025

Ok I think it did at some point and that's why the test used to fail, but well, yarn works in mysterious ways haha.

Let's merge this, I think this is the last preventing me from using rewatch everywhere without workarounds.

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 this pull request may close these issues.

2 participants