Skip to content
Open
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
53 changes: 9 additions & 44 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,23 +337,7 @@ To pass the arguments to the subcommand, remove `--`",
}
}
if commands::run::is_manifest_command(cmd) {
if gctx.cli_unstable().script {
return Ok((args, GlobalArgs::default()));
} else {
gctx.shell().print_report(
&[
Level::WARNING.secondary_title(
format!("user-defined alias `{cmd}` has the appearance of a manifest-command")
).element(
Level::NOTE.message(
"this was previously accepted but will be phased out when `-Zscript` is stabilized; \
see <https://github.com/rust-lang/cargo/issues/12207>"
)
)
],
false
)?;
}
return Ok((args, GlobalArgs::default()));
}

let mut alias = alias
Expand Down Expand Up @@ -490,31 +474,12 @@ impl Exec {
match self {
Self::Builtin(exec) => exec(gctx, subcommand_args),
Self::Manifest(cmd) => {
let ext_path = super::find_external_subcommand(gctx, &cmd);
if !gctx.cli_unstable().script && ext_path.is_some() {
gctx.shell().print_report(
&[
Level::WARNING.secondary_title(
format!("external subcommand `{cmd}` has the appearance of a manifest-command")
).element(
Level::NOTE.message(
"this was previously accepted but will be phased out when `-Zscript` is stabilized; \
see <https://github.com/rust-lang/cargo/issues/12207>"
)
)
],
false
)?;

Self::External(cmd).exec(gctx, subcommand_args)
} else {
let ext_args: Vec<OsString> = subcommand_args
.get_many::<OsString>("")
.unwrap_or_default()
.cloned()
.collect();
commands::run::exec_manifest_command(gctx, &cmd, &ext_args)
}
let ext_args: Vec<OsString> = subcommand_args
.get_many::<OsString>("")
.unwrap_or_default()
.cloned()
.collect();
commands::run::exec_manifest_command(gctx, &cmd, &ext_args)
}
Self::External(cmd) => {
let mut ext_args = vec![OsStr::new(&cmd)];
Expand Down Expand Up @@ -581,11 +546,11 @@ pub fn cli(gctx: &GlobalContext) -> Command {

let usage = if is_rustup() {
color_print::cstr!(
"<bright-cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS] [COMMAND]</>\n <bright-cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS]</> <bright-cyan,bold>-Zscript</> <cyan><<MANIFEST_RS>> [ARGS]...</>"
"<bright-cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS] [COMMAND]</>\n <bright-cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS]</> <cyan><<MANIFEST_RS>> [ARGS]...</>"
)
} else {
color_print::cstr!(
"<bright-cyan,bold>cargo</> <cyan>[OPTIONS] [COMMAND]</>\n <bright-cyan,bold>cargo</> <cyan>[OPTIONS]</> <bright-cyan,bold>-Zscript</> <cyan><<MANIFEST_RS>> [ARGS]...</>"
"<bright-cyan,bold>cargo</> <cyan>[OPTIONS] [COMMAND]</>\n <bright-cyan,bold>cargo</> <cyan>[OPTIONS]</> <cyan><<MANIFEST_RS>> [ARGS]...</>"
)
};

Expand Down
102 changes: 30 additions & 72 deletions src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,84 +96,42 @@ pub fn is_manifest_command(arg: &str) -> bool {

pub fn exec_manifest_command(gctx: &mut GlobalContext, cmd: &str, args: &[OsString]) -> CliResult {
let manifest_path = Path::new(cmd);
match (manifest_path.is_file(), gctx.cli_unstable().script) {
(true, true) => {}
(true, false) => {
return Err(anyhow::anyhow!("running the file `{cmd}` requires `-Zscript`").into());
}
(false, true) => {
let possible_commands = crate::list_commands(gctx);
let is_dir = if manifest_path.is_dir() {
format!(": `{cmd}` is a directory")
} else {
"".to_owned()
};
let suggested_command = if let Some(suggested_command) = possible_commands
.keys()
.filter(|c| cmd.starts_with(c.as_str()))
.max_by_key(|c| c.len())
{
let actual_args = cmd.strip_prefix(suggested_command).unwrap();
let args = if args.is_empty() {
"".to_owned()
} else {
format!(
" {}",
args.into_iter().map(|os| os.to_string_lossy()).join(" ")
)
};
format!(
"\nhelp: there is a command with a similar name: `{suggested_command} {actual_args}{args}`"
)
} else {
if !manifest_path.is_file() {
let possible_commands = crate::list_commands(gctx);
let is_dir = if manifest_path.is_dir() {
format!(": `{cmd}` is a directory")
} else {
"".to_owned()
};
let suggested_command = if let Some(suggested_command) = possible_commands
.keys()
.filter(|c| cmd.starts_with(c.as_str()))
.max_by_key(|c| c.len())
{
let actual_args = cmd.strip_prefix(suggested_command).unwrap();
let args = if args.is_empty() {
"".to_owned()
};
let suggested_script = if let Some(suggested_script) = suggested_script(cmd) {
format!("\nhelp: there is a script with a similar name: `{suggested_script}`")
} else {
"".to_owned()
};
return Err(anyhow::anyhow!(
"no such file or subcommand `{cmd}`{is_dir}{suggested_command}{suggested_script}"
)
.into());
}
(false, false) => {
// HACK: duplicating the above for minor tweaks but this will all go away on
// stabilization
let possible_commands = crate::list_commands(gctx);
let suggested_command = if let Some(suggested_command) = possible_commands
.keys()
.filter(|c| cmd.starts_with(c.as_str()))
.max_by_key(|c| c.len())
{
let actual_args = cmd.strip_prefix(suggested_command).unwrap();
let args = if args.is_empty() {
"".to_owned()
} else {
format!(
" {}",
args.into_iter().map(|os| os.to_string_lossy()).join(" ")
)
};
format!(
"\nhelp: there is a command with a similar name: `{suggested_command} {actual_args}{args}`"
" {}",
args.into_iter().map(|os| os.to_string_lossy()).join(" ")
)
} else {
"".to_owned()
};
let suggested_script = if let Some(suggested_script) = suggested_script(cmd) {
format!(
"\nhelp: there is a script with a similar name: `{suggested_script}` (requires `-Zscript`)"
)
} else {
"".to_owned()
};
return Err(anyhow::anyhow!(
"no such subcommand `{cmd}`{suggested_command}{suggested_script}"
format!(
"\nhelp: there is a command with a similar name: `{suggested_command} {actual_args}{args}`"
)
.into());
}
} else {
"".to_owned()
};
let suggested_script = if let Some(suggested_script) = suggested_script(cmd) {
format!("\nhelp: there is a script with a similar name: `{suggested_script}`")
} else {
"".to_owned()
};
return Err(anyhow::anyhow!(
"no such file or subcommand `{cmd}`{is_dir}{suggested_command}{suggested_script}"
)
.into());
}

let manifest_path = root_manifest(Some(manifest_path), gctx)?;
Expand Down
4 changes: 1 addition & 3 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ fn execute_external_subcommand(gctx: &GlobalContext, cmd: &str, args: &[&OsStr])
let command = match path {
Some(command) => command,
None => {
let script_suggestion = if gctx.cli_unstable().script
&& std::path::Path::new(cmd).is_file()
{
let script_suggestion = if std::path::Path::new(cmd).is_file() {
let sep = std::path::MAIN_SEPARATOR;
format!(
"\nhelp: To run the file `{cmd}`, provide a relative path like `.{sep}{cmd}`"
Expand Down
14 changes: 2 additions & 12 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,8 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
.compilation
.rustc_process(unit, is_primary, is_workspace)?;
build_base_args(build_runner, &mut base, unit)?;
// TODO: remove once frontmatter hits nightly
if unit.pkg.manifest().is_embedded() {
Comment on lines +797 to 798
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Blocked on rust-lang/rust#148051 hitting nightly

if !gctx.cli_unstable().script {
anyhow::bail!(
"parsing `{}` requires `-Zscript`",
unit.pkg.manifest_path().display()
);
}
base.arg("-Z").arg("crate-attr=feature(frontmatter)");
}

Expand Down Expand Up @@ -849,13 +844,8 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
let bcx = build_runner.bcx;
// script_metadata is not needed here, it is only for tests.
let mut rustdoc = build_runner.compilation.rustdoc_process(unit, None)?;
// TODO: remove once frontmatter hits nightly
if unit.pkg.manifest().is_embedded() {
Comment on lines +847 to 848
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Blocked on rust-lang/rust#148051 hitting nightly

if !bcx.gctx.cli_unstable().script {
anyhow::bail!(
"parsing `{}` requires `-Zscript`",
unit.pkg.manifest_path().display()
);
}
rustdoc.arg("-Z").arg("crate-attr=feature(frontmatter)");
}
rustdoc.inherit_jobserver(&build_runner.jobserver);
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,6 @@ unstable_cli_options!(
rustdoc_mergeable_info: bool = ("Use rustdoc mergeable cross-crate-info files"),
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
sbom: bool = ("Enable the `sbom` option in build config in .cargo/config.toml file"),
script: bool = ("Enable support for single-file, `.rs` packages"),
section_timings: bool = ("Enable support for extended compilation sections in --timings output"),
separate_nightlies: bool,
skip_rustdoc_fingerprint: bool,
Expand Down Expand Up @@ -985,6 +984,8 @@ const STABILIZED_BUILD_DIR: &str = "build.build-dir is now always enabled.";

const STABILIZED_CONFIG_INCLUDE: &str = "The `include` config key is now always available";

const STABILIZED_SCRIPT: &str = "Cargo scripts are now always available";

fn deserialize_comma_separated_list<'de, D>(
deserializer: D,
) -> Result<Option<Vec<String>>, D::Error>
Expand Down Expand Up @@ -1373,6 +1374,7 @@ impl CliUnstable {
"package-workspace" => stabilized_warn(k, "1.89", STABILIZED_PACKAGE_WORKSPACE),
"build-dir" => stabilized_warn(k, "1.91", STABILIZED_BUILD_DIR),
"config-include" => stabilized_warn(k, "1.93", STABILIZED_CONFIG_INCLUDE),
"script" => stabilized_warn(k, "1.95", STABILIZED_SCRIPT),

// Unstable features
// Sorted alphabetically:
Expand Down Expand Up @@ -1436,7 +1438,6 @@ impl CliUnstable {
"separate-nightlies" => self.separate_nightlies = parse_empty(k, v)?,
"checksum-freshness" => self.checksum_freshness = parse_empty(k, v)?,
"skip-rustdoc-fingerprint" => self.skip_rustdoc_fingerprint = parse_empty(k, v)?,
"script" => self.script = parse_empty(k, v)?,
"target-applies-to-host" => self.target_applies_to_host = parse_empty(k, v)?,
"panic-immediate-abort" => self.panic_immediate_abort = parse_empty(k, v)?,
"unstable-options" => self.unstable_options = parse_empty(k, v)?,
Expand Down
3 changes: 0 additions & 3 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,9 +1097,6 @@ pub fn root_manifest(manifest_path: Option<&Path>, gctx: &GlobalContext) -> Carg
manifest_path.display()
)
}
if crate::util::toml::is_embedded(&path) && !gctx.cli_unstable().script {
anyhow::bail!("embedded manifest `{}` requires `-Zscript`", path.display())
}
Ok(path)
} else {
find_root_manifest_for_wd(gctx.cwd())
Expand Down
3 changes: 0 additions & 3 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ pub fn read_manifest(
fn read_toml_string(path: &Path, is_embedded: bool, gctx: &GlobalContext) -> CargoResult<String> {
let mut contents = paths::read(path).map_err(|err| ManifestError::new(err, path.into()))?;
if is_embedded {
if !gctx.cli_unstable().script {
anyhow::bail!("parsing `{}` requires `-Zscript`", path.display());
}
contents = embedded::expand_manifest(&contents)
.map_err(|e| emit_frontmatter_diagnostic(e, &contents, path, gctx))?;
}
Expand Down
7 changes: 6 additions & 1 deletion src/doc/man/cargo.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cargo --- The Rust package manager
## SYNOPSIS

`cargo` [_options_] _command_ [_args_]\
`cargo` [_options_] _script_ [_args_]\
`cargo` [_options_] `--version`\
`cargo` [_options_] `--list`\
`cargo` [_options_] `--help`\
Expand Down Expand Up @@ -232,7 +233,11 @@ stable yet and may be subject to change.
mkdir foo && cd foo
cargo init .

6. Learn about a command's options and usage:
6. Run a script

cargo task.rs --action

7. Learn about a command's options and usage:

cargo help clean

Expand Down
5 changes: 3 additions & 2 deletions src/doc/man/generated_txt/cargo-add.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ OPTIONS

Manifest Options
--manifest-path path
Path to the Cargo.toml file. By default, Cargo searches for the
Cargo.toml file in the current directory or any parent directory.
Path to the Cargo.toml or cargo script file. By default, Cargo
searches for the Cargo.toml file in the current directory or any
parent directory.

-p spec, --package spec
Add dependencies to only the specified package.
Expand Down
5 changes: 3 additions & 2 deletions src/doc/man/generated_txt/cargo-bench.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,9 @@ OPTIONS

Manifest Options
--manifest-path path
Path to the Cargo.toml file. By default, Cargo searches for the
Cargo.toml file in the current directory or any parent directory.
Path to the Cargo.toml or cargo script file. By default, Cargo
searches for the Cargo.toml file in the current directory or any
parent directory.

--ignore-rust-version
Ignore rust-version specification in packages.
Expand Down
5 changes: 3 additions & 2 deletions src/doc/man/generated_txt/cargo-build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ OPTIONS

Manifest Options
--manifest-path path
Path to the Cargo.toml file. By default, Cargo searches for the
Cargo.toml file in the current directory or any parent directory.
Path to the Cargo.toml or cargo script file. By default, Cargo
searches for the Cargo.toml file in the current directory or any
parent directory.

--ignore-rust-version
Ignore rust-version specification in packages.
Expand Down
5 changes: 3 additions & 2 deletions src/doc/man/generated_txt/cargo-check.txt
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ OPTIONS

Manifest Options
--manifest-path path
Path to the Cargo.toml file. By default, Cargo searches for the
Cargo.toml file in the current directory or any parent directory.
Path to the Cargo.toml or cargo script file. By default, Cargo
searches for the Cargo.toml file in the current directory or any
parent directory.

--ignore-rust-version
Ignore rust-version specification in packages.
Expand Down
5 changes: 3 additions & 2 deletions src/doc/man/generated_txt/cargo-clean.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ OPTIONS

Manifest Options
--manifest-path path
Path to the Cargo.toml file. By default, Cargo searches for the
Cargo.toml file in the current directory or any parent directory.
Path to the Cargo.toml or cargo script file. By default, Cargo
searches for the Cargo.toml file in the current directory or any
parent directory.

--locked
Asserts that the exact same dependencies and versions are used as
Expand Down
5 changes: 3 additions & 2 deletions src/doc/man/generated_txt/cargo-doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ OPTIONS

Manifest Options
--manifest-path path
Path to the Cargo.toml file. By default, Cargo searches for the
Cargo.toml file in the current directory or any parent directory.
Path to the Cargo.toml or cargo script file. By default, Cargo
searches for the Cargo.toml file in the current directory or any
parent directory.

--ignore-rust-version
Ignore rust-version specification in packages.
Expand Down
5 changes: 3 additions & 2 deletions src/doc/man/generated_txt/cargo-fetch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ OPTIONS

Manifest Options
--manifest-path path
Path to the Cargo.toml file. By default, Cargo searches for the
Cargo.toml file in the current directory or any parent directory.
Path to the Cargo.toml or cargo script file. By default, Cargo
searches for the Cargo.toml file in the current directory or any
parent directory.

--locked
Asserts that the exact same dependencies and versions are used as
Expand Down
Loading