Skip to content

Commit 2378828

Browse files
authored
fix: Audit 06/01 (#2550)
* Updates. * Polish. * Add caching. * Update. * Rework. * Fix cache strategy. * Rework pid. * Fix tests. * Reenable locks. * Rework loader. * Update deps. * Fixes. * Add client cache. * Start daemon earlier. * Skip connect for non pipeline. * Better server stuff. * Polish. * Debug docker. * Fixes. * Fixes.
1 parent 4ccc661 commit 2378828

27 files changed

Lines changed: 429 additions & 189 deletions

File tree

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ jobs:
143143
if: ${{ vars.ENABLE_SCCACHE == 'true' }}
144144
# Fixes issues where proto can't find a version because nothing is pinned globally
145145
- run: cp .prototools ~/.proto/.prototools
146-
- run: bash ./scripts/ci/warmPluginsCache.sh
146+
# - run: bash ./scripts/ci/warmPluginsCache.sh
147147
- name: Build plugins
148148
run: just build-wasm
149149
- name: Run tests

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@
44

55
#### 🚀 Updates
66

7+
- Added in-memory caching to certain toolchain operations, primarily around locating executables.
8+
- Improved daemon startup performance by loading the workspace graph in the background after the
9+
server is ready.
710
- Updated plugin distribution to use ghcr.io instead of raw URLs, which should improve reliability
811
and performance of plugin downloads.
912

13+
#### 🐞 Fixes
14+
15+
- Reworked the daemon connect/ready logic to possibly fix some Windows connection issues.
16+
- Fixed an issue where the task dependency `cacheStrategy` inferrence was not working correctly
17+
based on what experiments are enabled.
18+
- Fixed an issue where locks created at `.moon/cache/locks` would not be cleaned up.
19+
1020
#### ⚙️ Internal
1121

1222
- Updated proto to [v0.57.4](https://github.com/moonrepo/proto/releases/tag/v0.57.4) from 0.57.3.

Cargo.lock

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/app/src/commands/daemon/restart.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub async fn restart(session: MoonSession) -> AppResult {
2121

2222
connector.stop_daemon().await?;
2323

24-
let pid = connector.start_daemon().await?;
24+
let pid = connector.start_daemon(true).await?.unwrap_or_default();
2525
let message = format!("Daemon has been restarted with process ID {pid}");
2626

2727
session.console.render(element! {

crates/app/src/commands/daemon/server.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ pub async fn server(session: MoonSession) -> AppResult {
77
start_daemon_server(
88
DaemonState {
99
app_context: session.get_app_context().await?,
10-
workspace_graph: session.get_workspace_graph().await?,
10+
// Loaded in the background within the workspace watcher,
11+
// otherwise it causes this command to block for too long
12+
workspace_graph: Default::default(),
1113
},
1214
vec![Box::new(WorkspaceWatcher::new(session))],
1315
)

crates/app/src/commands/daemon/start.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ pub async fn start(session: MoonSession) -> AppResult {
1616
return Ok(None);
1717
}
1818

19-
let pid = session.get_daemon_connector()?.start_daemon().await?;
19+
let pid = session
20+
.get_daemon_connector()?
21+
.start_daemon(true)
22+
.await?
23+
.unwrap_or_default();
2024

2125
session.console.render(element! {
2226
Container {

crates/app/src/commands/daemon/status.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,19 @@ pub async fn status(session: MoonSession) -> AppResult {
2020
return Ok(None);
2121
}
2222

23-
let status = connector.connect().await?.status().await?;
23+
let Some(mut client) = connector.connect().await? else {
24+
session.console.render(element! {
25+
Container {
26+
Notice(variant: Variant::Caution) {
27+
StyledText(content: "Unable to connect to the daemon")
28+
}
29+
}
30+
})?;
31+
32+
return Ok(None);
33+
};
34+
35+
let status = client.status().await?;
2436

2537
session.console.render(element! {
2638
Container {

crates/app/src/session.rs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use moon_api::Launchpad;
77
use moon_app_context::AppContext;
88
use moon_cache::CacheEngine;
99
use moon_codegen::CodeGenerator;
10-
use moon_common::is_formatted_output;
10+
use moon_common::{is_docker, is_formatted_output, is_remote};
1111
use moon_config::{ExtensionsConfig, InheritedTasksManager, ToolchainsConfig, WorkspaceConfig};
1212
use moon_config_loader::ConfigLoader;
1313
use moon_console::{Console, MoonReporter, create_console_theme};
@@ -46,6 +46,7 @@ pub struct MoonSession {
4646

4747
// Lazy components
4848
pub(crate) cache_engine: OnceLock<Arc<CacheEngine>>,
49+
// pub(crate) daemon_client: OnceCell<Option<DaemonClient>>,
4950
pub(crate) extension_registry: OnceCell<Arc<ExtensionRegistry>>,
5051
pub(crate) project_graph: OnceLock<Arc<ProjectGraph>>,
5152
pub(crate) task_graph: OnceLock<Arc<TaskGraph>>,
@@ -75,6 +76,7 @@ impl MoonSession {
7576
config_dir: PathBuf::new(),
7677
config_loader: ConfigLoader::default(),
7778
console: Console::new(cli.quiet || is_formatted_output()),
79+
// daemon_client: OnceCell::new(),
7880
extensions_config: Arc::new(ExtensionsConfig::default()),
7981
extension_registry: OnceCell::new(),
8082
moon_env: Arc::new(MoonEnvironment::default()),
@@ -126,13 +128,18 @@ impl MoonSession {
126128
}
127129

128130
pub async fn connect_to_daemon(&self) -> miette::Result<Option<DaemonClient>> {
129-
if !self.workspace_config.daemon {
131+
if !self.is_daemon_allowed() {
130132
return Ok(None);
131133
}
132134

133-
let client = self.get_daemon_connector()?.connect().await?;
135+
// let client = self
136+
// .daemon_client
137+
// .get_or_try_init(async move || self.get_daemon_connector()?.connect().await)
138+
// .await?;
134139

135-
Ok(Some(client))
140+
// Ok(client.clone())
141+
142+
self.get_daemon_connector()?.connect().await
136143
}
137144

138145
pub async fn create_workspace_graph_context(&self) -> miette::Result<WorkspaceBuilderContext> {
@@ -271,6 +278,21 @@ impl MoonSession {
271278
.map(Arc::clone)
272279
}
273280

281+
pub fn is_daemon_allowed(&self) -> bool {
282+
self.workspace_config.daemon && self.is_pipeline_command() && !is_docker()
283+
}
284+
285+
pub fn is_pipeline_command(&self) -> bool {
286+
matches!(
287+
self.cli.command,
288+
Commands::Ci(_)
289+
| Commands::Check(_)
290+
| Commands::Exec(_)
291+
| Commands::Run(_)
292+
| Commands::Sync { .. }
293+
)
294+
}
295+
274296
pub fn is_telemetry_enabled(&self) -> bool {
275297
self.workspace_config.telemetry
276298
}
@@ -389,39 +411,37 @@ impl AppSession for MoonSession {
389411

390412
analyze::extract_repo_info(&vcs).await?;
391413

392-
// Preload
414+
// Preload components
393415
if self.requires_workspace_configured() {
394416
let _ = self.get_cache_engine()?;
395417
}
396418

419+
// Start the daemon in the background
420+
if self.is_daemon_allowed() {
421+
self.get_daemon_connector()?.start_daemon(false).await?;
422+
}
423+
397424
Ok(None)
398425
}
399426

400427
async fn execute(&mut self) -> AppResult {
401-
let is_exec_command = matches!(
402-
self.cli.command,
403-
Commands::Ci(_)
404-
| Commands::Check(_)
405-
| Commands::Exec(_)
406-
| Commands::Run(_)
407-
| Commands::Sync { .. }
408-
);
409-
410428
// Check for a new version and log to the console
411-
if self.is_telemetry_enabled() && is_exec_command {
429+
if self.is_telemetry_enabled() && self.is_pipeline_command() {
412430
execute::check_for_new_version(&self, &self.toolchains_config.moon.manifest_url)
413431
.await?;
414432
}
415433

416-
// Start the daemon in the background
417-
if self.workspace_config.daemon && is_exec_command {
418-
self.get_daemon_connector()?.start_daemon().await?;
419-
}
420-
421434
Ok(None)
422435
}
423436

424437
async fn shutdown(&mut self) -> AppResult {
438+
// Stop the daemon if it's running
439+
if is_remote()
440+
&& let Ok(Some(mut daemon)) = self.connect_to_daemon().await
441+
{
442+
daemon.stop().await?;
443+
}
444+
425445
// Ensure all child processes have finished running
426446
ProcessRegistry::instance()
427447
.wait_for_running_to_shutdown()

crates/app/src/watchers/workspace_watcher.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ impl WorkspaceWatcher {
4242

4343
#[async_trait]
4444
impl FileWatcher<AtomicDaemonState> for WorkspaceWatcher {
45+
async fn on_init(&mut self, _state: AtomicDaemonState) -> miette::Result<()> {
46+
Ok(())
47+
}
48+
4549
async fn on_file_event(
4650
&mut self,
4751
state: AtomicDaemonState,
@@ -51,8 +55,8 @@ impl FileWatcher<AtomicDaemonState> for WorkspaceWatcher {
5155
return Ok(());
5256
}
5357

54-
// Handle root `.prototools` changes
55-
if event.path.as_str() == ".prototools" {
58+
// Handle `.prototools` changes
59+
if event.path.ends_with(".prototools") {
5660
self.reset_proto(&state).await?;
5761

5862
return Ok(());

crates/cache/src/cache_engine.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ impl CacheEngine {
120120
bytes_saved: 0,
121121
};
122122

123-
let mut dirs = vec![&self.hash.hashes_dir, &self.hash.outputs_dir];
123+
let locks_dir = self.cache_dir.join("locks");
124+
let mut dirs = vec![&self.hash.hashes_dir, &self.hash.outputs_dir, &locks_dir];
124125

125126
if all {
126127
dirs.push(&self.state.states_dir);
@@ -156,7 +157,8 @@ impl CacheEngine {
156157
name.push_str(".lock");
157158
}
158159

159-
let guard = fs::lock_file(self.cache_dir.join("locks").join(name))?;
160+
let mut guard = fs::lock_file(self.cache_dir.join("locks").join(name))?;
161+
guard.remove_on_unlock();
160162

161163
Ok(guard)
162164
}

0 commit comments

Comments
 (0)