Skip to content
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

Handle absolute paths in typegen #7104

Merged
merged 10 commits into from
Oct 21, 2024
Merged

Conversation

Bushuo
Copy link
Contributor

@Bushuo Bushuo commented Oct 16, 2024

Like discussed in rescript-lang/rewatch#102 this PR enables gentype to handle passing of absolute file paths.
Instead of manipulating the path to the .cmt file it gets the source file path from the .cmt file first.
Then constructs the output file paths from there.

@cristianoc
Copy link
Collaborator

Would you do make format and then run tests locally:

make test
make test-gentype

The last command should produce no difference in the output.

@Bushuo
Copy link
Contributor Author

Bushuo commented Oct 17, 2024

@cristianoc Yes, will do. Tests are failing locally. I will keep working on it.

@Bushuo
Copy link
Contributor Author

Bushuo commented Oct 17, 2024

@cristianoc I added the resolved project_root and bsb_project_root to the default config.
They where empty strings before.

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

some comments

@@ -105,6 +105,26 @@ let set_debug ~gtconf =
let compiler_config_file = "rescript.json"
let legacy_compiler_config_file = "bsconfig.json"

let default_config ~project_root ~bsb_project_root =
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

{ default with project_root; bsb_project_root }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made it into a local function instead. with the syntactic improvement.

compiler/gentype/GenTypeMain.ml Show resolved Hide resolved
@Bushuo Bushuo force-pushed the gentype-abs-paths branch from 9f29767 to 10a5d70 Compare October 17, 2024 18:11
@cristianoc cristianoc requested a review from mununki October 18, 2024 04:36
Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

I think it's pretty much ready to go, modulo a couple of comments.
And, some testing with rewatch: @mununki

compiler/gentype/Paths.ml Outdated Show resolved Hide resolved
compiler/gentype/Paths.ml Outdated Show resolved Hide resolved
@@ -29,17 +29,43 @@ let find_name_space cmt =
cmt |> Filename.basename |> (Filename.chop_extension [@doesNotRaise])
|> keep_after_dash

let get_output_file_relative ~config cmt =
(cmt |> handle_namespace) ^ ModuleExtension.ts_input_file_suffix ~config
let remove_project_root_from_absolute_path ~(config : Config.t) source_path =
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the bit that I haven't really reviewed properly yet.
Might not be a robust way to get a relative path. Especially Windows vs linux etc.
Can't remember if the code already does this somewhere else.
Or, poking around in the Filename module, there's more one can use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I refactored the function to use Filename.concat for appending the directory separator when missing. Additionally, it now verifies if the prefix exists and returns the original path if not. I also renamed the function for better clarity.

@mununki
Copy link
Member

mununki commented Oct 18, 2024

This may not be related to this PR, but the genType output for v12 still differs from v11 in this way.

// Title.res
@genType @react.component
let make = (~text) => {
  <div className="title"> {text->React.string} </div>
}

v11

/* TypeScript file generated from Title.res by genType. */

/* eslint-disable */
/* tslint:disable */

import * as TitleJS from './Title.mjs';

export type props<text> = { readonly text: text };

export const make: React.ComponentType<{ readonly text: string }> = TitleJS.make as any;

v12

/* TypeScript file generated from Title.res by genType. */

/* eslint-disable */
/* tslint:disable */

import * as TitleJS from './Title.mjs';

import type {element as Jsx_element} from './Jsx.gen.tsx'; // Error: module not found

export type props<text> = { readonly text: text };

export const make: (_1:props<string>) => Jsx_element = TitleJS.make as any;

@cristianoc
Copy link
Collaborator

This may not be related to this PR, but the genType output for v12 still differs from v11 in this way.

// Title.res
@genType @react.component
let make = (~text) => {
  <div className="title"> {text->React.string} </div>
}

v11

/* TypeScript file generated from Title.res by genType. */

/* eslint-disable */
/* tslint:disable */

import * as TitleJS from './Title.mjs';

export type props<text> = { readonly text: text };

export const make: React.ComponentType<{ readonly text: string }> = TitleJS.make as any;

v12

/* TypeScript file generated from Title.res by genType. */

/* eslint-disable */
/* tslint:disable */

import * as TitleJS from './Title.mjs';

import type {element as Jsx_element} from './Jsx.gen.tsx'; // Error: module not found

export type props<text> = { readonly text: text };

export const make: (_1:props<string>) => Jsx_element = TitleJS.make as any;

would you open an issue for this
looks like in v12 we expose Jsx types differently, and need to be handled specially by gentype as built in types. Otherwise, as the code generated show, it's just a mysterious unknown type.

@mununki
Copy link
Member

mununki commented Oct 18, 2024

#7106

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

Looks ready to go -- would you update the CHAGELOG?

Also, has it been tested on some rewatch project?

@zth
Copy link
Collaborator

zth commented Oct 19, 2024

CI seems to fail still.

@cristianoc
Copy link
Collaborator

CI seems to fail still.

Yes looks like there's a little annotation missing after refactoring to please the static exception checker.

@Bushuo
Copy link
Contributor Author

Bushuo commented Oct 19, 2024

Nice 🥳 I could swear that the static checker previously ran without having to explicitly make reanalyze.

@Bushuo
Copy link
Contributor Author

Bushuo commented Oct 19, 2024

Also, has it been tested on some rewatch project?

I only tested the initial version of this PR while I had it based on the v11 branch.
Currently my project fails to build with v12.

@cristianoc
Copy link
Collaborator

@mununki @zth who has a rewatch project at hand to try this?

@zth
Copy link
Collaborator

zth commented Oct 19, 2024

@Bushuo this will need backporting to v11 anyway so if you want you could do a PR on top of this that just backports this to v11 and try it that way.

@Bushuo
Copy link
Contributor Author

Bushuo commented Oct 19, 2024

@Bushuo this will need backporting to v11 anyway so if you want you could do a PR on top of this that just backports this to v11 and try it that way.

@zth Sure, I can do it. I haven't backported before, though, so if you could give me a quick rundown of the ideal process, that'd be great. I might not get to it today though.

@zth
Copy link
Collaborator

zth commented Oct 19, 2024

@Bushuo this will need backporting to v11 anyway so if you want you could do a PR on top of this that just backports this to v11 and try it that way.

@zth Sure, I can do it. I haven't backported before, though, so if you could give me a quick rundown of the ideal process, that'd be great. I might not get to it today though.

Sure! It's really simple, and basically what you did before - just base a separate PR of this work off of 11.0_release instead of master. I think there are some formatting differences to watch out for, but it should be fairly straight forward.

So, after that, you'll have 2 PRs:

  1. This, based on master, that'll go into v12
  2. A separate PR based on 11.0_release, that'll go into the next v11 patch release

Clear?

@Bushuo
Copy link
Contributor Author

Bushuo commented Oct 20, 2024

@cristianoc @zth
I assumed FindSourceFile.ml cmt cmt_annots would always return an absolute path, allowing it to be used for both output_file and output_file_relative. However, this only seems true for v12, not v11.

On the v11 branch, the function returns test/variantsMatching.res for the file jscomp/test/variantsMatching.res.
I suspect this is related to how sources is configured in bsconfig.json.

The config file is at the root level.

"sources": {
    "dir": "jscomp/test/"
}

In contrast, configuring a similar test in v12 still returns an absolute path.
Do you know why this behavior differs and could you provide some guidance on how to proceed?

@Bushuo Bushuo mentioned this pull request Oct 20, 2024
@cristianoc
Copy link
Collaborator

@zth why do we need v11 support?

@zth
Copy link
Collaborator

zth commented Oct 20, 2024

@zth why do we need v11 support?

Because it's likely a large portion of users will remain on v11 for a good while. Backporting this will mean that those can move to rewatch if they're using gentype as well before it ships in the compiler itself only.

@Bushuo Bushuo force-pushed the gentype-abs-paths branch from 9759b80 to 5780c0a Compare October 20, 2024 20:22
@Bushuo Bushuo force-pushed the gentype-abs-paths branch from 5780c0a to 5bb3db9 Compare October 20, 2024 20:26
@Bushuo Bushuo requested a review from cristianoc October 21, 2024 06:25
@Bushuo
Copy link
Contributor Author

Bushuo commented Oct 21, 2024

I made some changes and now it also seems to work on v11. Could you please take a look again?

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@zth
Copy link
Collaborator

zth commented Oct 21, 2024

@Bushuo great work! ⭐

@zth zth merged commit 47870d0 into rescript-lang:master Oct 21, 2024
20 checks passed
zth pushed a commit that referenced this pull request Oct 21, 2024
* refactor: extract fun from process_cmt_file

* refactor: read the .cmt earlier

* feat: handle paths via source_file instead of cmt_file

* add project root to default config

* refactor: extract funs to remove duplication

* fix: consider file seperators

* fix: please static checker

* make find source file return only absolute paths

* refactor: cleanup getOutputFile and getOutputFileRelative

* chore: update changelog
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.

4 participants