Skip to content

Commit

Permalink
CARGO: Support main.rs as crate root in various subfolder
Browse files Browse the repository at this point in the history
This feature was added after cargo 0.21
(not released with any stable Rust version yet)
rust-lang/cargo#4496
  • Loading branch information
kumbayo committed Oct 4, 2017
1 parent 3c026d7 commit a9e33fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class CargoTomlWatcher(
"/src/bin", "/examples", "/tests", "/benches"
)

private val MAIN = "main.rs"

override fun before(events: List<VFileEvent>) {
}

Expand All @@ -38,9 +40,12 @@ class CargoTomlWatcher(
if (isCargoTomlChange(event)) return true
if (event.path.endsWith(RustToolchain.CARGO_LOCK)) return true
if (event is VFileContentChangeEvent || PathUtil.getFileExtension(event.path) != "rs") return false
val parent = PathUtil.getParentPath(event.path)
val grandParent = PathUtil.getParentPath(parent)
val name = PathUtil.getFileName(event.path)

if (IMPLICIT_TARGET_FILES.any { event.path.endsWith(it) }) return true
return IMPLICIT_TARGET_DIRS.any { PathUtil.getParentPath(event.path).endsWith(it) }
return IMPLICIT_TARGET_DIRS.any { parent.endsWith(it) || (name == MAIN && grandParent.endsWith(it)) }
}

if (events.any(::isInterestingEvent)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class CargoTomlWatcherTest : RsTestBase() {
watcher.checkTriggered(createEvent)
watcher.checkNotTriggered(newChangeEvent(binFile))

// src/bin/*/main.rs
watcher.checkTriggered(newCreateEvent("src/bin/foo/main.rs").second)

// src/main.rs
watcher.checkTriggered(newCreateEvent("src/main.rs").second)
watcher.checkNotTriggered(newCreateEvent("prefix_src/main.rs").second)
Expand All @@ -59,6 +62,11 @@ class CargoTomlWatcherTest : RsTestBase() {
watcher.checkTriggered(newCreateEvent("tests/foo.rs").second)
watcher.checkNotTriggered(newCreateEvent("prefix_tests/foo.rs").second)

// benches/*/main.rs, examples/*/main.rs, tests/*/main.rs
watcher.checkTriggered(newCreateEvent("benches/foo/main.rs").second)
watcher.checkTriggered(newCreateEvent("examples/foo/main.rs").second)
watcher.checkTriggered(newCreateEvent("tests/foo/main.rs").second)

// build.rs
watcher.checkTriggered(newCreateEvent("build.rs").second)
watcher.checkNotTriggered(newCreateEvent("prefix_build.rs").second)
Expand Down

0 comments on commit a9e33fe

Please sign in to comment.