Skip to content

Commit

Permalink
fix excessive gleam.mjs generation
Browse files Browse the repository at this point in the history
This patch fixes excessive generation by checking for file existence.

An earlier attempt (gleam-lang#3238) had done this by checking for empty `modules`,
noting in its commit message that file existence checks will cause old
`gleam.mjs` to never be updated when a new version is available. I have
since learned that that was incorrect, as new versions of `gleam` delete
`build` folders created by previous versions.

closes gleam-lang#3178
  • Loading branch information
Ofek Doitch authored and Ofek Doitch committed Jun 20, 2024
1 parent c55475d commit 39a7960
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@
statements in a function when there is an error in a previous one.
([Ameen Radwan](https://github.com/Acepie))

- Fixed a bug where the compiler would unnecessarily generate `gleam.mjs`,
confusing build tools like Vite.
([Ofek Doitch](https://github.com/ofekd))

### Formatter

### Language Server
Expand Down
4 changes: 4 additions & 0 deletions compiler-cli/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ impl FileSystemWriter for ProjectIO {
fn write_bytes(&self, path: &Utf8Path, content: &[u8]) -> Result<(), Error> {
write_bytes(path, content)
}

fn exists(&self, path: &Utf8Path) -> bool {
path.exists()
}
}

impl CommandExecutor for ProjectIO {
Expand Down
15 changes: 13 additions & 2 deletions compiler-core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,22 @@ impl<'a> JavaScript<'a> {

fn write_prelude(&self, writer: &impl FileSystemWriter) -> Result<()> {
let rexport = format!("export * from \"{}\";\n", self.prelude_location);
writer.write(&self.output_directory.join("gleam.mjs"), &rexport)?;
let prelude_path = &self.output_directory.join("gleam.mjs");

// This check skips unnecessary `gleam.mjs` writes which confuse
// watchers and HMR build tools
if !writer.exists(prelude_path) {
writer.write(prelude_path, &rexport)?;
}

if self.typescript == TypeScriptDeclarations::Emit {
let rexport = rexport.replace(".mjs", ".d.mts");
writer.write(&self.output_directory.join("gleam.d.mts"), &rexport)?;
let prelude_declaration_path = &self.output_directory.join("gleam.d.mts");

// Type decleration may trigger badly configured watchers
if !writer.exists(prelude_declaration_path) {
writer.write(prelude_declaration_path, &rexport)?;
}
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions compiler-core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ pub trait FileSystemWriter {
fn hardlink(&self, from: &Utf8Path, to: &Utf8Path) -> Result<(), Error>;
fn symlink_dir(&self, from: &Utf8Path, to: &Utf8Path) -> Result<(), Error>;
fn delete_file(&self, path: &Utf8Path) -> Result<(), Error>;
fn exists(&self, path: &Utf8Path) -> bool;
}

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions compiler-core/src/io/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ impl FileSystemWriter for InMemoryFileSystem {
.insert(path.to_path_buf(), file);
Ok(())
}

fn exists(&self, path: &Utf8Path) -> bool {
self.files.deref().borrow().contains_key(path)
}
}

impl FileSystemReader for InMemoryFileSystem {
Expand Down
4 changes: 4 additions & 0 deletions compiler-core/src/language_server/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ where
fn delete_file(&self, path: &Utf8Path) -> Result<()> {
self.io.delete_file(path)
}

fn exists(&self, path: &Utf8Path) -> bool {
self.io.exists(path)
}
}

impl<IO> FileSystemReader for FileSystemProxy<IO>
Expand Down
4 changes: 4 additions & 0 deletions compiler-wasm/src/wasm_filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ impl FileSystemWriter for WasmFileSystem {
tracing::trace!("write_bytes {:?}", path);
self.imfs.write_bytes(path, content)
}

fn exists(&self, path: &Utf8Path) -> bool {
self.imfs.exists(path)
}
}

impl FileSystemReader for WasmFileSystem {
Expand Down

0 comments on commit 39a7960

Please sign in to comment.