Skip to content

Bug Fix: store typescript_declarations config and rebuild if changed #4523

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 12 commits into
base: main
Choose a base branch
from

Conversation

daniellionel01
Copy link

@daniellionel01 daniellionel01 commented Apr 29, 2025

Code to fix #4520

It currently creates a file gleam-toml.lock where it stores the configuration for typescript_declarations. If that has changed since the last build, it rm -rfs the build directory to make sure that it emits typescript declaration files this time. Might be a bit overkill and only necessary to for example rm -rf the dev/javascript directory, I am not sure.

Has been tested locally in a dedicated gleam project. But I can add something to the test suite to make it part of CI/CD if that is a requirement.

So in summary, some aspects that should be discussed in more depth:

  • do we need to rm -rf the whole build directory or only parts of it? for example build/dev/javascript?
  • where to store the gleam-toml.lock file
  • what to call it (gleam-config.lock, gleam.toml, config.lock, ...)
  • the format to store it in (toml? json?)
  • add test to CI/CD?

Cheers!

@daniellionel01 daniellionel01 changed the title Bugfix: store typescript_declarations config and rebuild if changed Bug Fix: store typescript_declarations config and rebuild if changed Apr 29, 2025
Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you! I've left some notes inline.

Please un-draft and tag me when ready for a review.

@@ -370,6 +370,9 @@
segment would generate invalid code on the Erlang target.
([Surya Rose](https://github.com/GearsDatapacks))

- Fixed a bug where enabling `typescript_declarations = true` wouldn't generate TypeScript declarations unless the build directory was manually deleted. The compiler now automatically rebuilds the project when this configuration changes.
([daniellionel01](https://github.com/daniellionel01))
Copy link
Member

Choose a reason for hiding this comment

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

Wrap long lines to match the existing entries please

Copy link
Author

Choose a reason for hiding this comment

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

done 👍

@@ -0,0 +1,14 @@
name = "javascript_typescript_declarations"
Copy link
Member

Choose a reason for hiding this comment

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

This test project doesn't seem to be used anywhere? What is it for?

Could be a good foundation for an integration test in a CI workflow.

Copy link
Author

@daniellionel01 daniellionel01 May 3, 2025

Choose a reason for hiding this comment

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

I'm not very familiar with testing in rust, so I am not sure how to setup a testing workflow here. I've removed the project though

if !self.io.is_file(&path) {
self.io.write(&path, crate::javascript::PRELUDE_TS_DEF)?;
}
self.io.write(&path, crate::javascript::PRELUDE_TS_DEF)?;
Copy link
Member

Choose a reason for hiding this comment

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

Why has this been changed?

Copy link
Author

Choose a reason for hiding this comment

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

Has been reverted, not sure what I thought there

@@ -261,6 +264,62 @@ where
})
}

/// Checks if the TypeScript declarations setting has changed since the last build.
/// If it has changed, we clear the build directory to make sure it generates TypeScript definitions.
pub fn check_javascript_config(&self) -> Result<(), Error> {
Copy link
Member

Choose a reason for hiding this comment

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

Let's give this a more descriptive name that says what it does.

Copy link
Author

Choose a reason for hiding this comment

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

done 👍

pub fn check_javascript_config(&self) -> Result<(), Error> {
if !self.target().is_javascript() {
return Ok(());
}
Copy link
Member

Choose a reason for hiding this comment

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

We also only care about this if configured to perform code generation, otherwise it does nothing.

Copy link
Author

Choose a reason for hiding this comment

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

done 👍

.build_directory_for_target(self.mode(), self.target());
let config_path = build_path.join("gleam-toml.lock");

let current_setting = format!(
Copy link
Member

Choose a reason for hiding this comment

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

Always favour the declarative serde derive API over ad-hoc serde usage.

That said, we could just write an empty file and its existence can indicate that TS was asked for. Then we don't need to do any reading or parsing.

Copy link
Author

Choose a reason for hiding this comment

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

done 👍 the file is now called typescript-declarations.lock

kind: FileKind::File,
path: config_path,
err: Some(e.to_string()),
})
Copy link
Member

Choose a reason for hiding this comment

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

There's a bug here! The build directory doesn't have all the contents it needs to have after being reset.

We can fix it by not deleting the entire directory. It would be more proper for this compiler instance to only delete the sub-directory it owns.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not 100% sure if I understand this correctly. So right now it deletes the build/dev/javascript directory, but that shouldn't happen? By "sub-directory it owns" are you referring to build/dev/javascript/[project name]? Because from what I am able to tell we also need to generate the .d.mts files inside of the adjacent directories f.e. gleam_stdlib.
We could also delete all of the *.mjs files inside of the build/dev/javascript directory, would that be better?

@@ -188,6 +188,9 @@ where
// verify that this version is appropriate.
self.check_gleam_version()?;

// Check if JavaScript configuration has changed
Copy link
Member

Choose a reason for hiding this comment

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

More detail in the documentation please 🙏

Copy link
Author

Choose a reason for hiding this comment

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

done 👍

@lpil lpil marked this pull request as draft May 2, 2025 16:57
@daniellionel01
Copy link
Author

daniellionel01 commented May 3, 2025

Thank you! I've left some notes inline.

Please un-draft and tag me when ready for a review.

@lpil addressed all comments, thank you! Will be awaiting your feedback 🤘

I couldn't get the test for CI working yet, but have tested it locally quite a bit

@daniellionel01 daniellionel01 marked this pull request as ready for review May 3, 2025 19:26
@lpil
Copy link
Member

lpil commented May 5, 2025

Could you rebase to remove the merge commit please 🙏

@daniellionel01 daniellionel01 force-pushed the bugfix/typescript-declarations branch from 3f0c1c7 to b5af4fd Compare May 5, 2025 15:18
@daniellionel01
Copy link
Author

daniellionel01 commented May 5, 2025

Could you rebase to remove the merge commit please 🙏

@lpil I'll be honest, I've never rebased anything and am failing hard to do so 🥲 I've tried for half an hour with GPT and messed up the branch like 3 times already. Any help is appreciated.

It might be faster for me to just copy paste my changes (since it's just the one function) to a fresh branch, close this PR and open a new one

@daniellionel01
Copy link
Author

daniellionel01 commented May 5, 2025

Created a new PR with the identical changes, sorry about the confusion @lpil

You or I can close this one then I suppose

#4562

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.

Typescript declarations are not emitted with existing build directory
2 participants