Skip to content

Commit 6fa8618

Browse files
committed
Modify the original cargo toml
1 parent 65e20f7 commit 6fa8618

File tree

2 files changed

+94
-53
lines changed

2 files changed

+94
-53
lines changed

orchestrator/src/coordinator.rs

Lines changed: 94 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,23 @@ impl<B> Coordinator<B>
5050
where
5151
B: Backend,
5252
{
53-
pub fn new(backend: B) -> Result<Self, Error> {
53+
pub async fn new(backend: B) -> Result<Self, Error> {
5454
let token = CancellationToken::new();
5555

5656
let [stable, beta, nightly] =
5757
Channel::ALL.map(|channel| Container::new(channel, token.clone(), &backend));
5858

59+
let (stable, beta, nightly) = join!(stable, beta, nightly);
60+
61+
let stable = stable?;
62+
let beta = beta?;
63+
let nightly = nightly?;
64+
5965
Ok(Self {
6066
backend,
61-
stable: stable?,
62-
beta: beta?,
63-
nightly: nightly?,
67+
stable,
68+
beta,
69+
nightly,
6470
token,
6571
})
6672
}
@@ -110,19 +116,24 @@ where
110116
}
111117

112118
impl Coordinator<DockerBackend> {
113-
pub fn new_docker() -> Result<Self, Error> {
114-
Self::new(DockerBackend(()))
119+
pub async fn new_docker() -> Result<Self, Error> {
120+
Self::new(DockerBackend(())).await
115121
}
116122
}
117123

118124
#[derive(Debug)]
119125
pub struct Container {
120126
task: JoinHandle<Result<()>>,
127+
modify_cargo_toml: ModifyCargoToml,
121128
commander: Commander,
122129
}
123130

124131
impl Container {
125-
fn new(channel: Channel, token: CancellationToken, backend: &impl Backend) -> Result<Self> {
132+
async fn new(
133+
channel: Channel,
134+
token: CancellationToken,
135+
backend: &impl Backend,
136+
) -> Result<Self> {
126137
let (mut child, stdin, stdout) = backend.run_worker_in_background(channel)?;
127138
let IoQueue {
128139
mut tasks,
@@ -151,7 +162,15 @@ impl Container {
151162
id: Default::default(),
152163
};
153164

154-
Ok(Container { task, commander })
165+
let modify_cargo_toml = ModifyCargoToml::new(commander.clone())
166+
.await
167+
.context(CouldNotLoadCargoTomlSnafu)?;
168+
169+
Ok(Container {
170+
task,
171+
modify_cargo_toml,
172+
commander,
173+
})
155174
}
156175

157176
pub async fn compile(
@@ -195,7 +214,7 @@ impl Container {
195214
};
196215

197216
let write_main = self.commander.one(write_main);
198-
let modify_cargo_toml = modify_cargo_toml(&self.commander, &request);
217+
let modify_cargo_toml = self.modify_cargo_toml.modify_for(&request);
199218

200219
let (write_main, modify_cargo_toml) = join!(write_main, modify_cargo_toml);
201220

@@ -257,8 +276,13 @@ impl Container {
257276
}
258277

259278
pub async fn shutdown(self) -> Result<()> {
260-
let Self { task, commander } = self;
279+
let Self {
280+
task,
281+
modify_cargo_toml,
282+
commander,
283+
} = self;
261284
drop(commander);
285+
drop(modify_cargo_toml);
262286
task.await.context(ContainerTaskPanickedSnafu)?
263287
}
264288
}
@@ -302,41 +326,61 @@ struct Commander {
302326
id: Arc<AtomicU64>,
303327
}
304328

305-
// Consider making this into a top-level message to the worker to avoid the roundtrip.
306-
async fn modify_cargo_toml(
307-
commander: &Commander,
308-
request: &CompileRequest,
309-
) -> Result<(), ModifyCargoTomlError> {
310-
use modify_cargo_toml_error::*;
329+
#[derive(Debug)]
330+
struct ModifyCargoToml {
331+
commander: Commander,
332+
cargo_toml: toml::Value,
333+
}
311334

335+
impl ModifyCargoToml {
312336
const PATH: &str = "Cargo.toml";
313337

314-
let read_request = ReadFileRequest {
315-
path: PATH.to_owned(),
316-
};
317-
let cargo_toml = commander
318-
.one(read_request)
319-
.await
320-
.context(CouldNotReadSnafu)?;
338+
async fn new(commander: Commander) -> Result<Self, ModifyCargoTomlError> {
339+
let cargo_toml = Self::read(&commander).await?;
340+
Ok(Self {
341+
commander,
342+
cargo_toml,
343+
})
344+
}
321345

322-
let cargo_toml = String::from_utf8(cargo_toml.0)?;
323-
let cargo_toml = toml::from_str(&cargo_toml)?;
346+
async fn modify_for(&self, request: &CompileRequest) -> Result<(), ModifyCargoTomlError> {
347+
let cargo_toml = self.cargo_toml.clone();
348+
let cargo_toml = request.modify_cargo_toml(cargo_toml);
349+
Self::write(&self.commander, cargo_toml).await
350+
}
351+
352+
async fn read(commander: &Commander) -> Result<toml::Value, ModifyCargoTomlError> {
353+
use modify_cargo_toml_error::*;
324354

325-
let cargo_toml = request.modify_cargo_toml(cargo_toml);
355+
let path = Self::PATH.to_owned();
356+
let cargo_toml = commander
357+
.one(ReadFileRequest { path })
358+
.await
359+
.context(CouldNotReadSnafu)?;
326360

327-
let cargo_toml = toml::to_string(&cargo_toml)?;
328-
let cargo_toml = cargo_toml.into_bytes();
361+
let cargo_toml = String::from_utf8(cargo_toml.0)?;
362+
let cargo_toml = toml::from_str(&cargo_toml)?;
329363

330-
let write_request = WriteFileRequest {
331-
path: PATH.to_owned(),
332-
content: cargo_toml,
333-
};
334-
commander
335-
.one(write_request)
336-
.await
337-
.context(CouldNotWriteSnafu)?;
364+
Ok(cargo_toml)
365+
}
366+
367+
async fn write(
368+
commander: &Commander,
369+
cargo_toml: toml::Value,
370+
) -> Result<(), ModifyCargoTomlError> {
371+
use modify_cargo_toml_error::*;
372+
373+
let cargo_toml = toml::to_string(&cargo_toml)?;
374+
let content = cargo_toml.into_bytes();
375+
376+
let path = Self::PATH.to_owned();
377+
commander
378+
.one(WriteFileRequest { path, content })
379+
.await
380+
.context(CouldNotWriteSnafu)?;
338381

339-
Ok(())
382+
Ok(())
383+
}
340384
}
341385

342386
#[derive(Debug, Snafu)]
@@ -646,6 +690,9 @@ pub enum Error {
646690

647691
#[snafu(display("Failed to send worker message through channel"))]
648692
UnableToSendWorkerMessage { source: mpsc::error::SendError<()> },
693+
694+
#[snafu(display("Unable to load original Cargo.toml"))]
695+
CouldNotLoadCargoToml { source: ModifyCargoTomlError },
649696
}
650697

651698
struct IoQueue {
@@ -779,9 +826,9 @@ mod tests {
779826
}
780827
}
781828

782-
fn new_coordinator() -> Result<Coordinator<impl Backend>> {
783-
Coordinator::new(TestBackend::new())
784-
//Coordinator::new_docker()
829+
async fn new_coordinator() -> Result<Coordinator<impl Backend>> {
830+
Coordinator::new(TestBackend::new()).await
831+
//Coordinator::new_docker().await
785832
}
786833

787834
fn new_compile_request() -> CompileRequest {
@@ -864,7 +911,7 @@ mod tests {
864911
#[tokio::test]
865912
#[snafu::report]
866913
async fn test_compile_response() -> Result<()> {
867-
let coordinator = new_coordinator()?;
914+
let coordinator = new_coordinator().await?;
868915

869916
let response = coordinator
870917
.compile(new_compile_request())
@@ -884,7 +931,7 @@ mod tests {
884931
#[tokio::test]
885932
#[snafu::report]
886933
async fn test_compile_streaming() -> Result<()> {
887-
let coordinator = new_coordinator()?;
934+
let coordinator = new_coordinator().await?;
888935

889936
let ActiveCompilation {
890937
task,
@@ -919,7 +966,7 @@ mod tests {
919966
#[snafu::report]
920967
async fn test_compile_edition() -> Result<()> {
921968
for edition in Edition::ALL {
922-
let coordinator = new_coordinator()?;
969+
let coordinator = new_coordinator().await?;
923970

924971
let response = coordinator
925972
.compile(new_compile_hir_request_for(edition))
@@ -941,7 +988,7 @@ mod tests {
941988
#[tokio::test]
942989
#[snafu::report]
943990
async fn test_compile_assembly() -> Result<()> {
944-
let coordinator = new_coordinator()?;
991+
let coordinator = new_coordinator().await?;
945992

946993
let response = coordinator
947994
.compile(new_compile_assembly_request())
@@ -966,7 +1013,7 @@ mod tests {
9661013
#[tokio::test]
9671014
#[snafu::report]
9681015
async fn test_compile_hir() -> Result<()> {
969-
let coordinator = new_coordinator()?;
1016+
let coordinator = new_coordinator().await?;
9701017

9711018
let response = coordinator
9721019
.compile(new_compile_hir_request())
@@ -985,7 +1032,7 @@ mod tests {
9851032
#[tokio::test]
9861033
#[snafu::report]
9871034
async fn test_compile_llvm_ir() -> Result<()> {
988-
let coordinator = new_coordinator()?;
1035+
let coordinator = new_coordinator().await?;
9891036

9901037
let response = coordinator
9911038
.compile(new_compile_llvm_ir_request())
@@ -1005,7 +1052,7 @@ mod tests {
10051052
#[snafu::report]
10061053
async fn test_compile_wasm() -> Result<()> {
10071054
// cargo-wasm only exists inside the container
1008-
let coordinator = Coordinator::new_docker()?;
1055+
let coordinator = Coordinator::new_docker().await?;
10091056

10101057
let response = coordinator
10111058
.compile(new_compile_wasm_request())

orchestrator/src/sandbox.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,10 @@ impl CompileRequest {
192192
pub(crate) fn modify_cargo_toml(&self, mut cargo_toml: toml::Value) -> toml::Value {
193193
cargo_toml = modify_cargo_toml::set_edition(cargo_toml, self.edition.to_cargo_toml_key());
194194

195-
// TODO: When we boot the container, grab information like
196-
// `Cargo.toml` (and the `rustc` version, dependency
197-
// versions?). Then we always start from that version.
198-
199-
// TODO: We need to undo these changes when switching back from a library
200195
if let Some(crate_type) = self.crate_type.to_library_cargo_toml_key() {
201196
cargo_toml = modify_cargo_toml::set_crate_type(cargo_toml, crate_type);
202197
}
203198

204-
// TODO: We need to undo these changes when switching back from Wasm
205199
if CompileTarget::Wasm == self.target {
206200
cargo_toml = modify_cargo_toml::remove_dependencies(cargo_toml);
207201
cargo_toml = modify_cargo_toml::set_release_lto(cargo_toml, true);

0 commit comments

Comments
 (0)