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

Fix "module defined in multiple files" AGAIN!!!! #237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/ghci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,15 +688,15 @@ impl Ghci {
path: &NormalPath,
log: &mut CompilationLog,
) -> miette::Result<()> {
let (import_name, _target_kind) =
self.targets.module_import_name(&self.search_paths, path)?;
let module = self.targets.module_import_name(&self.search_paths, path)?;

self.stdin
.interpret_module(&mut self.stdout, &import_name, log)
.interpret_module(&mut self.stdout, &module.name, log)
.await?;

self.targets
.insert_source_path(path.clone(), TargetKind::Path);
if !module.loaded {
self.targets.insert_source_path(path.clone(), module.kind);
}

self.refresh_eval_commands_for_paths(std::iter::once(path))
.await?;
Expand Down
46 changes: 37 additions & 9 deletions src/ghci/parse/module_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ impl ModuleSet {
///
/// Returns whether the value was newly inserted.
pub fn insert_source_path(&mut self, path: NormalPath, kind: TargetKind) -> bool {
self.modules.insert(path, kind).is_some()
match self.modules.insert(path, kind) {
Some(old_kind) => {
assert!(kind == old_kind, "`ghciwatch` failed to track how modules were imported in `ghci`; please report this as a bug");
true
}
None => false,
}
}

/// Get the name used to refer to the given module path when importing it.
Expand All @@ -68,18 +74,27 @@ impl ModuleSet {
&self,
show_paths: &ShowPaths,
path: &NormalPath,
) -> miette::Result<(String, TargetKind)> {
) -> miette::Result<ImportInfo> {
match self.modules.get(path) {
Some(kind) => match kind {
TargetKind::Path => Ok((path.relative().to_string(), *kind)),
TargetKind::Module => {
let module = show_paths.path_to_module(path)?;
Ok((module, *kind))
}
Some(&kind) => match kind {
TargetKind::Path => Ok(ImportInfo {
name: path.relative().to_string(),
kind,
loaded: true,
}),
TargetKind::Module => Ok(ImportInfo {
name: show_paths.path_to_module(path)?,
kind,
loaded: true,
}),
},
None => {
let path = show_paths.make_relative(path)?;
Ok((path.into_relative().into_string(), TargetKind::Path))
Ok(ImportInfo {
name: path.into_relative().into_string(),
kind: TargetKind::Path,
loaded: false,
})
}
}
}
Expand All @@ -89,3 +104,16 @@ impl ModuleSet {
self.modules.keys()
}
}

/// Information about a module to be imported into a `ghci` session.
pub struct ImportInfo {
/// The name to refer to the module by.
///
/// This may either be a dotted module name like `My.Cool.Module` or a path like
/// `src/My/Cool/Module.hs`.
pub name: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor thing, but I think making the String a field for each of the Path and Module constructors for TargetKind would make it less likely to accidentally confuse the two types of Strings (i.e. using a module where a path was expected or vice versa). However, I realize that's a more extensive refactor

/// Whether the `name` is a name or path.
pub kind: TargetKind,
/// Whether the module is already loaded in the `ghci` session.
pub loaded: bool,
}
55 changes: 55 additions & 0 deletions tests/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,58 @@ async fn can_eval_commands_in_non_interpreted_modules() {
.await
.expect("ghciwatch evals commands");
}

/// Test that `ghciwatch` can eval commands in a module loaded at startup, and then test that it
/// can eval commands in that module a second time.
///
/// See: <https://github.com/MercuryTechnologies/ghciwatch/issues/234>
#[test]
async fn can_eval_commands_twice() {
let module_path = "src/MyModule.hs";
let cmd = "-- $> example ++ example";
let mut session = GhciWatchBuilder::new("tests/data/simple")
.with_arg("--enable-eval")
.before_start(move |path| async move {
Fs::new()
.append(path.join(module_path), format!("\n{cmd}\n"))
.await
})
.start()
.await
.expect("ghciwatch starts");
let module_path = session.path(module_path);

// Adds the module succesfully.
let ok_reload = BaseMatcher::message("All good!")
// Evals the command.
.and(BaseMatcher::message(
r"MyModule.hs:\d+:\d+: example \+\+ example",
))
// Reads eval output.
.and(BaseMatcher::message("Read line").with_field("line", "exampleexample"))
// Finishes the reload.
.and(BaseMatcher::reload_completes())
.but_not(
BaseMatcher::message("Read stderr line")
.with_field("line", "defined in multiple files"),
);

session
.wait_until_ready()
.await
.expect("ghciwatch didn't start in time");

session.checkpoint();
session.fs().touch(&module_path).await.unwrap();
session
.assert_logged_or_wait(ok_reload.clone())
.await
.expect("ghciwatch evals commands");

session.checkpoint();
session.fs().touch(&module_path).await.unwrap();
session
.assert_logged_or_wait(ok_reload)
.await
.expect("ghciwatch evals commands");
}
Loading