Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 5995f20

Browse files
authored
Merge pull request #858 from rust-lang-nursery/workspace-mode
Remove workspace mode, change some options
2 parents b9c8dfd + e96e9ed commit 5995f20

File tree

6 files changed

+98
-249
lines changed

6 files changed

+98
-249
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,6 @@ project.
9393

9494
Currently we accept the following options:
9595

96-
* `build_lib` (`bool`, defaults to `false`) checks the project as if you passed
97-
the `--lib` argument to cargo. Mutually exclusive with, and preferred over,
98-
`build_bin`.
99-
* `build_bin` (`String`, defaults to `""`) checks the project as if you passed
100-
`-- bin <build_bin>` argument to cargo. Mutually exclusive with `build_lib`.
101-
* `cfg_test` (`bool`, defaults to `false`) checks the project as if you were
102-
running `cargo test` rather than `cargo build`. I.e., compiles (but does not
103-
run) test code.
10496
* `unstable_features` (`bool`, defaults to `false`) enables unstable features.
10597
Currently no option requires this flag.
10698
* `sysroot` (`String`, defaults to `""`) if the given string is not empty, use
@@ -110,7 +102,7 @@ Currently we accept the following options:
110102
the given target triple for all rustc invocations
111103
* `wait_to_build` (`u64`, defaults to `1500`) time in milliseconds between
112104
receiving a change notification and starting build
113-
* `all_targets` (`bool`, defaults to `false`) checks the project as if you were
105+
* `all_targets` (`bool`, defaults to `true`) checks the project as if you were
114106
running `cargo check --all-targets`. I.e., check all targets and integration
115107
tests too
116108
* `use_crate_blacklist` (`bool`, defaults to `true`) if disabled, also indexes
@@ -129,6 +121,18 @@ Currently we accept the following options:
129121
- `"opt-in"` Clippy lints are shown when crates specify `#![warn(clippy)]`.
130122
- `"on"` Clippy lints enabled for all crates in workspace.
131123

124+
and the following unstable options:
125+
126+
* `build_lib` (`bool`, defaults to `false`) checks the project as if you passed
127+
the `--lib` argument to cargo. Mutually exclusive with, and preferred over,
128+
`build_bin`.
129+
* `build_bin` (`String`, defaults to `""`) checks the project as if you passed
130+
`-- bin <build_bin>` argument to cargo. Mutually exclusive with `build_lib`.
131+
* `cfg_test` (`bool`, defaults to `false`) checks the project as if you were
132+
running `cargo test` rather than `cargo build`. I.e., compiles (but does not
133+
run) test code.
134+
135+
132136
## Troubleshooting
133137

134138
For tips on debugging and troubleshooting, see [debugging.md](debugging.md).

src/build/cargo.rs

Lines changed: 34 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ use std::thread;
3636

3737
// Runs an in-process instance of Cargo.
3838
pub(super) fn cargo(internals: &Internals, package_arg: PackageArg, progress_sender: Sender<ProgressUpdate>) -> BuildResult {
39-
let workspace_mode = internals.config.lock().unwrap().workspace_mode;
40-
4139
let compilation_cx = internals.compilation_cx.clone();
4240
let config = internals.config.clone();
4341
let vfs = internals.vfs.clone();
@@ -73,7 +71,7 @@ pub(super) fn cargo(internals: &Internals, package_arg: PackageArg, progress_sen
7371
.map_err(|_| failure::err_msg("thread panicked"))
7472
.and_then(|res| res)
7573
{
76-
Ok(ref cwd) if workspace_mode => {
74+
Ok(ref cwd) => {
7775
let diagnostics = Arc::try_unwrap(diagnostics_clone)
7876
.unwrap()
7977
.into_inner()
@@ -84,7 +82,6 @@ pub(super) fn cargo(internals: &Internals, package_arg: PackageArg, progress_sen
8482
.unwrap();
8583
BuildResult::Success(cwd.clone(), diagnostics, analysis, true)
8684
}
87-
Ok(cwd) => BuildResult::Success(cwd, vec![], vec![], true),
8885
Err(err) => {
8986
// This message goes like this to the UI via showMessage. In VSCode
9087
// this ends up on one single line, so it's important to keep it concise.
@@ -164,22 +161,9 @@ fn run_cargo(
164161
let rustflags = prepare_cargo_rustflags(&rls_config);
165162

166163

167-
if rls_config.workspace_mode {
168-
for package in &packages {
169-
if ws.members().find(|x| *x.name() == *package).is_none() {
170-
warn!("cargo - couldn't find member package `{}` specified in `analyze_package` configuration", package);
171-
}
172-
}
173-
} else {
174-
// Warn about invalid specified bin target or package depending on current mode
175-
// TODO: Return client notifications along with diagnostics to inform the user
176-
let cur_pkg_targets = ws.current()?.targets();
177-
178-
if let Some(ref build_bin) = *rls_config.build_bin.as_ref() {
179-
let mut bins = cur_pkg_targets.iter().filter(|x| x.is_bin());
180-
if bins.find(|x| x.name() == build_bin).is_none() {
181-
warn!("cargo - couldn't find binary `{}` specified in `build_bin` configuration", build_bin);
182-
}
164+
for package in &packages {
165+
if ws.members().find(|x| *x.name() == *package).is_none() {
166+
warn!("cargo - couldn't find member package `{}` specified in `analyze_package` configuration", package);
183167
}
184168
}
185169

@@ -254,15 +238,13 @@ fn run_cargo(
254238

255239
struct RlsExecutor {
256240
compilation_cx: Arc<Mutex<CompilationContext>>,
257-
cur_package_id: Mutex<Option<PackageId>>,
258241
config: Arc<Mutex<Config>>,
259242
/// Because of the Cargo API design, we first acquire outer lock before creating the executor
260243
/// and calling the compilation function. This, resulting, inner lock is used to synchronize
261244
/// env var access during underlying `rustc()` calls during parallel `exec()` callback threads.
262245
env_lock: environment::InnerLock,
263246
vfs: Arc<Vfs>,
264247
analysis: Arc<Mutex<Vec<Analysis>>>,
265-
workspace_mode: bool,
266248
/// Packages which are directly a member of the workspace, for which
267249
/// analysis and diagnostics will be provided
268250
member_packages: Mutex<HashSet<PackageId>>,
@@ -282,26 +264,14 @@ impl RlsExecutor {
282264
analysis: Arc<Mutex<Vec<Analysis>>>,
283265
progress_sender: Sender<ProgressUpdate>,
284266
) -> RlsExecutor {
285-
let workspace_mode = config.lock().unwrap().workspace_mode;
286-
let (cur_package_id, member_packages) = if workspace_mode {
287-
let member_packages = ws.members().map(|x| x.package_id().clone()).collect();
288-
(None, member_packages)
289-
} else {
290-
let pkg_id = ws.current_opt()
291-
.expect("No current package in Cargo")
292-
.package_id()
293-
.clone();
294-
(Some(pkg_id), HashSet::new())
295-
};
267+
let member_packages = ws.members().map(|x| x.package_id().clone()).collect();
296268

297269
RlsExecutor {
298270
compilation_cx,
299-
cur_package_id: Mutex::new(cur_package_id),
300271
config,
301272
env_lock,
302273
vfs,
303274
analysis,
304-
workspace_mode,
305275
member_packages: Mutex::new(member_packages),
306276
compiler_messages,
307277
progress_sender: Mutex::new(progress_sender),
@@ -311,15 +281,7 @@ impl RlsExecutor {
311281
/// Returns whether a given package is a primary one (every member of the
312282
/// workspace is considered as such).
313283
fn is_primary_crate(&self, id: &PackageId) -> bool {
314-
if self.workspace_mode {
315-
self.member_packages.lock().unwrap().contains(id)
316-
} else {
317-
let cur_package_id = self.cur_package_id.lock().unwrap();
318-
id
319-
== cur_package_id
320-
.as_ref()
321-
.expect("Executor has not been initialized")
322-
}
284+
self.member_packages.lock().unwrap().contains(id)
323285
}
324286
}
325287

@@ -339,7 +301,7 @@ impl Executor for RlsExecutor {
339301
}
340302

341303
fn force_rebuild(&self, unit: &Unit) -> bool {
342-
// In workspace_mode we need to force rebuild every package in the
304+
// We need to force rebuild every package in the
343305
// workspace, even if it's not dirty at a time, to cache compiler
344306
// invocations in the build plan.
345307
// We only do a cargo build if we want to force rebuild the last
@@ -463,46 +425,13 @@ impl Executor for RlsExecutor {
463425

464426
{
465427
let config = self.config.lock().unwrap();
466-
let crate_type = parse_arg(cargo_args, "--crate-type");
467-
// Because we only try to emulate `cargo test` using `cargo check`, so for now
468-
// assume crate_type arg (i.e. in `cargo test` it isn't specified for --test targets)
469-
// and build test harness only for final crate type
470-
let crate_type = if config.all_targets || config.cfg_test {
471-
// Crate type may be undefined when `all_targets` is true, for example for integration tests
472-
crate_type.unwrap_or_else(|| "undefined".to_owned())
473-
} else {
474-
// Panic if crate type undefined for other cases
475-
crate_type.expect("no crate-type in rustc command line")
476-
};
477-
let build_lib = *config.build_lib.as_ref();
478-
let is_final_crate_type = crate_type == "bin" || (crate_type == "lib" && build_lib);
479428

480429
if config.sysroot.is_none() {
481430
args.push("--sysroot".to_owned());
482431
args.push(sysroot);
483432
}
484433

485-
// We can't omit compilation here, because Cargo is going to expect to get
486-
// dep-info for this crate, so we shell out to rustc to get that.
487-
// This is not really ideal, because we are going to
488-
// compute this info anyway when we run rustc ourselves, but we don't do
489-
// that before we return to Cargo.
490-
// FIXME Don't do this. Start our build here rather than on another thread
491-
// so the dep-info is ready by the time we return from this callback.
492-
// NB: In `workspace_mode` regular compilation is performed here (and we don't
493-
// only calculate dep-info) so it should fix the problem mentioned above.
494-
let modified = args.iter()
495-
.map(|a| {
496-
// Emitting only dep-info is possible only for final crate type, as
497-
// as others may emit required metadata for dependent crate types
498-
if a.starts_with("--emit") && is_final_crate_type && !self.workspace_mode {
499-
"--emit=dep-info"
500-
} else {
501-
a
502-
}
503-
})
504-
.collect::<Vec<_>>();
505-
cmd.args_replace(&modified);
434+
cmd.args_replace(&args);
506435
}
507436

508437
// Cache executed command for the build plan
@@ -524,30 +453,26 @@ impl Executor for RlsExecutor {
524453
compilation_cx.cwd = cargo_cmd.get_cwd().map(|p| p.to_path_buf());
525454
}
526455

527-
if self.workspace_mode {
528-
let build_dir = {
529-
let cx = self.compilation_cx.lock().unwrap();
530-
cx.build_dir.clone().unwrap()
531-
};
456+
let build_dir = {
457+
let cx = self.compilation_cx.lock().unwrap();
458+
cx.build_dir.clone().unwrap()
459+
};
532460

533-
if let BuildResult::Success(_, mut messages, mut analysis, success) = super::rustc::rustc(
534-
&self.vfs,
535-
&args,
536-
&envs,
537-
cargo_cmd.get_cwd(),
538-
&build_dir,
539-
Arc::clone(&self.config),
540-
&self.env_lock.as_facade(),
541-
) {
542-
self.compiler_messages.lock().unwrap().append(&mut messages);
543-
self.analysis.lock().unwrap().append(&mut analysis);
544-
545-
if !success {
546-
return Err(format_err!("Build error"));
547-
}
461+
if let BuildResult::Success(_, mut messages, mut analysis, success) = super::rustc::rustc(
462+
&self.vfs,
463+
&args,
464+
&envs,
465+
cargo_cmd.get_cwd(),
466+
&build_dir,
467+
Arc::clone(&self.config),
468+
&self.env_lock.as_facade(),
469+
) {
470+
self.compiler_messages.lock().unwrap().append(&mut messages);
471+
self.analysis.lock().unwrap().append(&mut analysis);
472+
473+
if !success {
474+
return Err(format_err!("Build error"));
548475
}
549-
} else {
550-
cmd.exec()?;
551476
}
552477

553478
Ok(())
@@ -585,40 +510,14 @@ impl Default for CargoOptions {
585510

586511
impl CargoOptions {
587512
fn new(config: &Config) -> CargoOptions {
588-
if config.workspace_mode {
589-
CargoOptions {
590-
target: config.target.clone(),
591-
features: config.features.clone(),
592-
all_features: config.all_features,
593-
no_default_features: config.no_default_features,
594-
jobs: config.jobs,
595-
all_targets: config.all_targets,
596-
..CargoOptions::default()
597-
}
598-
} else {
599-
// In single-crate mode we currently support only one crate target,
600-
// and if lib is set, then we ignore bin target config
601-
let (lib, bin) = if *config.build_lib.as_ref() {
602-
(true, vec![])
603-
} else {
604-
let bin = match *config.build_bin.as_ref() {
605-
Some(ref bin) => vec![bin.clone()],
606-
None => vec![],
607-
};
608-
(false, bin)
609-
};
610-
611-
CargoOptions {
612-
lib,
613-
bin,
614-
target: config.target.clone(),
615-
features: config.features.clone(),
616-
all_features: config.all_features,
617-
no_default_features: config.no_default_features,
618-
jobs: config.jobs,
619-
all_targets: config.all_targets,
620-
..CargoOptions::default()
621-
}
513+
CargoOptions {
514+
target: config.target.clone(),
515+
features: config.features.clone(),
516+
all_features: config.all_features,
517+
no_default_features: config.no_default_features,
518+
jobs: config.jobs,
519+
all_targets: config.all_targets,
520+
..CargoOptions::default()
622521
}
623522
}
624523
}

src/build/mod.rs

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -534,53 +534,28 @@ impl Internals {
534534

535535
// Don't hold this lock when we run Cargo.
536536
let needs_to_run_cargo = self.compilation_cx.lock().unwrap().args.is_empty();
537-
let workspace_mode = self.config.lock().unwrap().workspace_mode;
538-
539-
if workspace_mode {
540-
// If the build plan has already been cached, use it, unless Cargo
541-
// has to be specifically rerun (e.g. when build scripts changed)
542-
let work = {
543-
let modified: Vec<_> = self.dirty_files.lock().unwrap().keys().cloned().collect();
544-
let mut cx = self.compilation_cx.lock().unwrap();
545-
let manifest_path = important_paths::find_root_manifest_for_wd(cx.build_dir.as_ref().unwrap());
546-
let manifest_path = match manifest_path {
547-
Ok(mp) => mp,
548-
Err(e) => {
549-
let msg = format!("Error reading manifest path: {:?}", e);
550-
return BuildResult::Err(msg, None);
551-
}
552-
};
553-
cx.build_plan.prepare_work(&manifest_path, &modified, needs_to_run_cargo)
554-
};
555-
return match work {
556-
// In workspace_mode, cargo performs the full build and returns
557-
// appropriate diagnostics/analysis data
558-
WorkStatus::NeedsCargo(package_arg) => cargo::cargo(self, package_arg, progress_sender),
559-
WorkStatus::Execute(job_queue) => job_queue.execute(self, progress_sender),
537+
538+
// If the build plan has already been cached, use it, unless Cargo
539+
// has to be specifically rerun (e.g. when build scripts changed)
540+
let work = {
541+
let modified: Vec<_> = self.dirty_files.lock().unwrap().keys().cloned().collect();
542+
let mut cx = self.compilation_cx.lock().unwrap();
543+
let manifest_path = important_paths::find_root_manifest_for_wd(cx.build_dir.as_ref().unwrap());
544+
let manifest_path = match manifest_path {
545+
Ok(mp) => mp,
546+
Err(e) => {
547+
let msg = format!("Error reading manifest path: {:?}", e);
548+
return BuildResult::Err(msg, None);
549+
}
560550
};
561-
// In single package mode Cargo needs to be run to cache args/envs for
562-
// future rustc calls
563-
} else if needs_to_run_cargo {
564-
if let e @ BuildResult::Err(..) = cargo::cargo(self, PackageArg::Unknown, progress_sender) {
565-
return e;
566-
}
551+
cx.build_plan.prepare_work(&manifest_path, &modified, needs_to_run_cargo)
552+
};
553+
match work {
554+
// Cargo performs the full build and returns
555+
// appropriate diagnostics/analysis data
556+
WorkStatus::NeedsCargo(package_arg) => cargo::cargo(self, package_arg, progress_sender),
557+
WorkStatus::Execute(job_queue) => job_queue.execute(self, progress_sender),
567558
}
568-
569-
let compile_cx = self.compilation_cx.lock().unwrap();
570-
let args = &compile_cx.args;
571-
assert!(!args.is_empty());
572-
let envs = &compile_cx.envs;
573-
let build_dir = compile_cx.build_dir.as_ref().unwrap();
574-
let env_lock = self.env_lock.as_facade();
575-
rustc::rustc(
576-
&self.vfs,
577-
args,
578-
envs,
579-
compile_cx.cwd.as_ref().map(|x| &**x),
580-
build_dir,
581-
Arc::clone(&self.config),
582-
&env_lock,
583-
)
584559
}
585560
}
586561

0 commit comments

Comments
 (0)