Skip to content

Commit 5875a9c

Browse files
committed
Remove build.lock when finished
1 parent e1ba3c0 commit 5875a9c

3 files changed

Lines changed: 59 additions & 20 deletions

File tree

rewatch/src/build.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::build::compile::{mark_modules_with_deleted_deps_dirty, mark_modules_w
1414
use crate::build::compiler_info::{CompilerCheckResult, verify_compiler_info, write_compiler_info};
1515
use crate::helpers::emojis::*;
1616
use crate::helpers::{self};
17-
use crate::lock::{LockKind, get_lock_or_exit};
17+
use crate::lock::{LockKind, drop_lock, get_lock_or_exit};
1818
use crate::project_context::ProjectContext;
1919
use crate::sourcedirs;
2020
use anyhow::{Context, Result, anyhow};
@@ -147,7 +147,13 @@ pub fn initialize_build(
147147
return Err(anyhow!("Failed to validate package dependencies"));
148148
}
149149

150-
let mut build_state = BuildCommandState::new(project_context, packages, compiler, warn_error);
150+
let mut build_state = BuildCommandState::new(
151+
path.to_path_buf(),
152+
project_context,
153+
packages,
154+
compiler,
155+
warn_error,
156+
);
151157
packages::parse_packages(&mut build_state)?;
152158

153159
let compile_assets_state = read_compile_state::read(&mut build_state)?;
@@ -239,6 +245,10 @@ pub fn incremental_build(
239245
create_sourcedirs: bool,
240246
plain_output: bool,
241247
) -> Result<(), IncrementalBuildError> {
248+
let build_folder = build_state.root_folder.to_string_lossy().to_string();
249+
250+
let _lock = get_lock_or_exit(LockKind::Build, &build_folder);
251+
242252
logs::initialize(&build_state.packages);
243253
let num_dirty_modules = build_state.modules.values().filter(|m| is_dirty(m)).count() as u64;
244254
let pb = if !plain_output && show_progress {
@@ -282,6 +292,8 @@ pub fn incremental_build(
282292
}
283293

284294
eprintln!("{}", &err);
295+
let _lock = drop_lock(LockKind::Build, &build_folder);
296+
285297
return Err(IncrementalBuildError {
286298
kind: IncrementalBuildErrorKind::SourceFileParseError,
287299
plain_output,
@@ -344,9 +356,13 @@ pub fn incremental_build(
344356
|| pb.inc(1),
345357
|size| pb.set_length(size),
346358
)
347-
.map_err(|e| IncrementalBuildError {
348-
kind: IncrementalBuildErrorKind::CompileError(Some(e.to_string())),
349-
plain_output,
359+
.map_err(|e| {
360+
let _lock = drop_lock(LockKind::Build, &build_folder);
361+
362+
IncrementalBuildError {
363+
kind: IncrementalBuildErrorKind::CompileError(Some(e.to_string())),
364+
plain_output,
365+
}
350366
})?;
351367

352368
let compile_duration = start_compiling.elapsed();
@@ -380,6 +396,9 @@ pub fn incremental_build(
380396
if helpers::contains_ascii_characters(&compile_errors) {
381397
eprintln!("{}", &compile_errors);
382398
}
399+
400+
let _lock = drop_lock(LockKind::Build, &build_folder);
401+
383402
Err(IncrementalBuildError {
384403
kind: IncrementalBuildErrorKind::CompileError(None),
385404
plain_output,
@@ -410,6 +429,7 @@ pub fn incremental_build(
410429
// Write per-package compiler metadata to `lib/bs/compiler-info.json` (idempotent)
411430
write_compiler_info(build_state);
412431

432+
let _lock = drop_lock(LockKind::Build, &build_folder);
413433
Ok(())
414434
}
415435
}
@@ -470,8 +490,6 @@ pub fn build(
470490
plain_output: bool,
471491
warn_error: Option<String>,
472492
) -> Result<BuildCommandState> {
473-
let _lock = get_lock_or_exit(LockKind::Build, &path.to_string_lossy());
474-
475493
let default_timing: Option<std::time::Duration> = if no_timing {
476494
Some(std::time::Duration::new(0.0 as u64, 0.0 as u32))
477495
} else {

rewatch/src/build/build_types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub struct BuildState {
122122
/// - This prevents the "code smell" of optional fields that are None for some commands
123123
#[derive(Debug)]
124124
pub struct BuildCommandState {
125+
pub root_folder: PathBuf,
125126
pub build_state: BuildState,
126127
// Command-line --warn-error flag override (takes precedence over rescript.json config)
127128
pub warn_error_override: Option<String>,
@@ -171,12 +172,14 @@ impl BuildState {
171172

172173
impl BuildCommandState {
173174
pub fn new(
175+
root_folder: PathBuf,
174176
project_context: ProjectContext,
175177
packages: AHashMap<String, Package>,
176178
compiler: CompilerInfo,
177179
warn_error_override: Option<String>,
178180
) -> Self {
179181
Self {
182+
root_folder,
180183
build_state: BuildState::new(project_context, packages, compiler),
181184
warn_error_override,
182185
}

rewatch/src/lock.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub enum Error {
3535
Locked(u32),
3636
AwaitingLockFile(AwaitLockError),
3737
ParsingLockfile(std::num::ParseIntError),
38-
ReadingLockfile(std::io::Error),
38+
ReadingLockfile(LockKind, std::io::Error),
3939
WritingLockfile(std::io::Error),
4040
ProjectFolderMissing(std::path::PathBuf),
4141
}
@@ -49,8 +49,11 @@ impl std::fmt::Display for Error {
4949
Error::ParsingLockfile(e) => format!(
5050
"Could not parse lockfile: \n {e} \n (try removing it and running the command again)"
5151
),
52-
Error::ReadingLockfile(e) => {
53-
format!("Could not read lockfile: \n {e} \n (try removing it and running the command again)")
52+
Error::ReadingLockfile(kind, e) => {
53+
format!(
54+
"Could not read lockfile: {}, \n {e} \n (try removing it and running the command again)",
55+
kind.file_name()
56+
)
5457
}
5558
Error::WritingLockfile(e) => format!("Could not write lockfile: \n {e}"),
5659
Error::ProjectFolderMissing(path) => format!(
@@ -113,7 +116,7 @@ impl LockKind {
113116

114117
pub const TIMEOUT_SECONDS: u64 = 60;
115118

116-
pub fn await_lock_deletion(location: &Path) -> Result<(), Error> {
119+
pub fn await_lock_deletion(location: &Path, kind: LockKind) -> Result<(), Error> {
117120
let now = SystemTime::now();
118121
let queue = Arc::new(FifoQueue::<Result<Event, notify::Error>>::new());
119122
let producer = queue.clone();
@@ -127,12 +130,13 @@ pub fn await_lock_deletion(location: &Path) -> Result<(), Error> {
127130

128131
loop {
129132
while !queue.is_empty() {
130-
if let Ok(Event {
131-
kind: EventKind::Remove(_),
132-
..
133-
}) = queue.pop()
134-
{
135-
return Ok(());
133+
match queue.pop() {
134+
Ok(Event {
135+
kind: EventKind::Remove(_),
136+
paths,
137+
..
138+
}) if paths.iter().find(|p| p.ends_with(kind.file_name())).is_some() => return Ok(()),
139+
Ok(_) | Err(_) => (),
136140
}
137141
}
138142

@@ -167,7 +171,7 @@ pub fn get(kind: LockKind, folder: &str) -> Lock {
167171
Ok(parsed_pid) if pid_matches_current_process(parsed_pid) => match kind {
168172
LockKind::Build => {
169173
println!("Awaiting lockfile");
170-
match await_lock_deletion(&location) {
174+
match await_lock_deletion(&lib_dir, kind) {
171175
Ok(_) => {
172176
continue;
173177
}
@@ -180,7 +184,7 @@ pub fn get(kind: LockKind, folder: &str) -> Lock {
180184
Err(e) => return Lock::Error(Error::ParsingLockfile(e)),
181185
},
182186
Err(e) if e.kind() == std::io::ErrorKind::NotFound => break,
183-
Err(e) => return Lock::Error(Error::ReadingLockfile(e)),
187+
Err(e) => return Lock::Error(Error::ReadingLockfile(kind, e)),
184188
}
185189
}
186190

@@ -201,14 +205,28 @@ pub fn get(kind: LockKind, folder: &str) -> Lock {
201205
pub fn get_lock_or_exit(kind: LockKind, folder: &str) -> Lock {
202206
match get(kind, folder) {
203207
Lock::Error(error) => {
204-
eprintln!("Could not start ReScript build: {error}");
208+
eprintln!("Could not start Rescript build: {error}");
205209
std::process::exit(1);
206210
}
207211

208212
acquired_lock => acquired_lock,
209213
}
210214
}
211215

216+
pub fn drop_lock(kind: LockKind, folder: &str) -> Result<()> {
217+
let project_folder = Path::new(folder);
218+
if !project_folder.exists() {
219+
return Ok(());
220+
}
221+
222+
let lib_dir = project_folder.join("lib");
223+
let location = lib_dir.join(kind.file_name());
224+
225+
fs::remove_file(&location)?;
226+
227+
Ok(())
228+
}
229+
212230
#[cfg(test)]
213231
mod tests {
214232
use super::*;

0 commit comments

Comments
 (0)