Skip to content

Be more aware of the workspace during clean #7752

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

Merged
merged 49 commits into from
Aug 13, 2025

Conversation

nojaf
Copy link
Member

@nojaf nojaf commented Aug 2, 2025

This is an attempt to drive the conversation at #7707

In case of clean, we already do an upward traversal to find a root rescript json.
I'm adding some logic to check if the parent rescript json contains the current rescript json in "dependencies" (or "dev-dependencies").

If we did find a link, we would only remove the files from the current project and not everything.

Consider https://github.com/nojaf/rescript-kaplay/blob/main/packages/rescript-kaplay/rescript.json

We already find https://github.com/nojaf/rescript-kaplay/blob/d38649fcd409c174c39b31488f790e036f3b0a20/rescript.json#L8

If that name matches root_package (in the code, a bit confusing name) is "@nojaf/rescript-kaplay", so we only need to remove those files.

Files from https://github.com/nojaf/rescript-kaplay/blob/main/packages/samples/rescript.json are unaffected.

If we were to clean from the repo root, root_config would be the well the root package and everything gets cleaned.

Copy link
Member Author

@nojaf nojaf left a comment

Choose a reason for hiding this comment

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

Let me know if something doesn't make any sense.

Copy link

pkg-pr-new bot commented Aug 2, 2025

Open in StackBlitz

rescript

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

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/win32-x64

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

commit: fc66c24

@nojaf
Copy link
Member Author

nojaf commented Aug 4, 2025

I think it might make sense to rethink the format command as well.

Would it not be more consistent if we format either:

  • a specific file argument like format Foo.res
  • Format all projects in case of a monorepo when invoked at the root.
  • Format a specific project only when it is part of a workspace, but invoked at the project level and not the root.

The --all flag seems a bit off compared to clean.

@cknitt
Copy link
Member

cknitt commented Aug 7, 2025

Agreed, defaulting to all / keeping args similar to build/clean definitely makes sense!

jfrolich
jfrolich previously approved these changes Aug 7, 2025
@nojaf
Copy link
Member Author

nojaf commented Aug 7, 2025

There still is more work to do here.
In general the concept of current_package and workspace_package and their relation should be cemented more in all code paths.

my-monorepo/
  packages/
    a/
      rescript.json
    b/
      rescript.json
rescript.json

Running clean, build, format and compiler-args inside a or my-monorepo need to clearly defined.

repository (my-monorepo) package (a) expectation repository expectation project
compiler-args packages/a/src/File.res src/Files.res Get compiler args. Extension from repo is used Exact same result, extension from repo is used
format All files of all packages are formatted Files of current package are formatted
build All packages are built Only this package is built
clean All packages are cleaned Only this packaged is cleaned

I also think that if there is no link with package and repository rescript, workspace_path should be None.

For the time being if there is a workspace rescript.json, that "suffix" is primary.
Nested rescript.json files cannot define it again. Maybe we add a warning for that.

Post v12 we can explore the option to override suffix in child packages, but it is my understanding that it doesn't work that way today.

I'm also in favour of renaming root_package of current_package in code.

@nojaf nojaf requested a review from Copilot August 11, 2025 10:45
Copilot

This comment was marked as outdated.

@nojaf nojaf marked this pull request as ready for review August 12, 2025 07:42
@@ -1,4 +1,4 @@
{
"name": "rescript",
"dependencies": ["@tests/gentype-react-example"]
"dependencies": ["@tests/gentype-react-example", "playground"]
Copy link
Member Author

Choose a reason for hiding this comment

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

"playground" is part of the workspace, so I need to be listed here for rewatch to pick up the correct node_modules folder.

The package.json has rescript clean in the commands.
This will now accurately only clean the playground package.

But it needs that link to the parent rescript json.

bold "Rescript version"
(cd ../testrepo && ./node_modules/.bin/rescript -v)
(cd ../testrepo && ./node_modules/.bin/rescript --version)
Copy link
Member Author

Choose a reason for hiding this comment

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

-v is verbosity and this was trying to compile the testrepo with an older installed version of rescript.
I'm not sure how this wasn't leading to bigger problems.

// I don't think this linting rule matters very much as we only create a single instance of this enum.
// See https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
#[allow(clippy::large_enum_variant)]
pub enum ProjectContext {
Copy link
Member Author

Choose a reason for hiding this comment

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

This streamlines the many workspace: Option<string> checks that were being done.

It improves the readability and debugability of rewatch.

Copy link
Member

Choose a reason for hiding this comment

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

I would maybe do:

pub enum MonoRepoContext {
  Root {local_dependencies, local_dev_dependencies}
  Package {parent_config: Config, parent_path: RescriptJsonPath}
}

pub struct ProjectContext {
  config: Config,
  path: RescriptJsonPath,
  mono_repo_context: Option<MonoRepoContext>
}

I would like to be mindful about memory consumption - even if minor - you never know how much these will grow in the future - and I think we should try and keep a small footprint (smaller than we do now). I think the enum will otherwise always take the biggest size when allocating, even in smaller contexts.

@nojaf nojaf requested a review from jfrolich August 12, 2025 07:48
@nojaf nojaf linked an issue Aug 12, 2025 that may be closed by this pull request
@nojaf
Copy link
Member Author

nojaf commented Aug 12, 2025

For those overwhelmed by these changes: in short, this resolves the expectations described in #7707 comment.

This PR makes Rewatch more aware of its context:

  • single project
  • monorepo with Rewatch run at the root
  • monorepo with Rewatch run at the package level

In the last case, Rewatch will run build, clean, and format only for that package. This seems sensible.

I removed the --all flag for formatting; formatting now defaults to the current project.

I will add a changelog entry if there is consensus to move forward.

Please ask any questions you may have.
If you read this and don't have any thoughts or feedback, please leave a 👀 on the PR to indicate silent agreement.

@nojaf nojaf dismissed jfrolich’s stale review August 12, 2025 08:10

Much has changed since this review.

Copy link
Member

@rolandpeelen rolandpeelen left a comment

Choose a reason for hiding this comment

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

Really nice work! Left some comments on the Rust bits and pieces 👌 / some optimisations.

@@ -709,7 +699,7 @@ fn compile_file(
}

// copy js file
root_package.config.get_package_specs().iter().for_each(|spec| {
root_config.get_package_specs().iter().for_each(|spec| {
Copy link
Member

Choose a reason for hiding this comment

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

More of a note to myself for some future refactor - I think we can do better with memory management - the package specs / root_config and those things are essentially constant after reading. We should be able - for the most part - to just always return references rather than cloning - I think we can further smallen the footprint / make things quite a bit faster that way

Copy link
Member

Choose a reason for hiding this comment

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

This function also get's called a lot. I think we might benefit from some more abstraction, having an internal representation for a package / packagespec, rather than the parseTree we need for Serde

Copy link
Member Author

Choose a reason for hiding this comment

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

5 usages indeed, worth revisiting. Will consider this outside the scope of this PR, as it is already so large.

Copy link
Contributor

Choose a reason for hiding this comment

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

we pass references right? Where are we doing cloning?

// I don't think this linting rule matters very much as we only create a single instance of this enum.
// See https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
#[allow(clippy::large_enum_variant)]
pub enum ProjectContext {
Copy link
Member

Choose a reason for hiding this comment

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

I would maybe do:

pub enum MonoRepoContext {
  Root {local_dependencies, local_dev_dependencies}
  Package {parent_config: Config, parent_path: RescriptJsonPath}
}

pub struct ProjectContext {
  config: Config,
  path: RescriptJsonPath,
  mono_repo_context: Option<MonoRepoContext>
}

I would like to be mindful about memory consumption - even if minor - you never know how much these will grow in the future - and I think we should try and keep a small footprint (smaller than we do now). I think the enum will otherwise always take the biggest size when allocating, even in smaller contexts.

context
}

pub fn get_root_config(&self) -> &Config {
Copy link
Member

Choose a reason for hiding this comment

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

Breaking up the enum into a struct for the shared fields would remove the need for these too, which is nice imo :)

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not exactly the same, because we want to get different things (in a mono repo package, get the root config, otherwise get the config of the current package). So I think it's pretty nice like this - and with an implementation, it's easy to get the configuration.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, check you're right actually. Misread - these would still be there :) - still think having the overlapping fields in a struct would model the real world better though

Copy link
Member Author

Choose a reason for hiding this comment

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

@rolandpeelen I've refactored based on the feedback,

now I have

#[allow(clippy::large_enum_variant)]
pub enum ProjectContext {
    /// Single project - no workspace relationship
    SingleProject { config: Config },
    /// Monorepo root - contains local dependencies (symlinked in node_modules)
    MonorepoRoot {
        config: Config,
        local_dependencies: AHashSet<String>, // names of local deps
        local_dev_dependencies: AHashSet<String>,
    },
    /// Package within a monorepo - has a parent workspace
    MonorepoPackage { config: Config, parent_config: Config },
}

How would change that? They all share their own Config. How to continue?

Copy link
Member

Choose a reason for hiding this comment

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

I would personally still break it out to a struct + enum, all have the same config object at the root, and only when you have a monorepo, there are additional fields.

So a project always has a config, but it may have a parent config (in the case of a package within a monorepo), or may have local dependencies (in case it's the root package) - but have no strong opinion.

The functions could still be there, because the 'root' would change in the case of a monorepo-package, but you'd for instance won't need the get_current_rescript_config - it would just be x.config, which makes a lot more sense to me than having an abstraction over a field that exists for all cases.

Copy link
Member

Choose a reason for hiding this comment

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

Come to think of it - I think we can leave it like this - the one property you lose doing this is an easy (and very clear) distinction you can use everywhere, and perhaps that's worth more than this is.

// See https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
#[allow(clippy::large_enum_variant)]
pub enum ProjectContext {
/// Single project - no workspace relationship
Copy link
Contributor

Choose a reason for hiding this comment

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

Super nice, I like this a lot!

@nojaf
Copy link
Member Author

nojaf commented Aug 13, 2025

Okay, I'm going to merge this. I want to get it over it.
Happy to revisit things in follow-up PRs.

I didn't hear any strong objections to the CLI changes. I think this is fine, but if you're reading this after the merge and still want to discuss, please speak up.

@nojaf nojaf merged commit 2cd9996 into rescript-lang:master Aug 13, 2025
27 checks passed
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.

Rescript clean of individual project
4 participants