Skip to content

Commit 25365d9

Browse files
committed
Use syntax_prefix to improve error message
Signed-off-by: hi-rustin <[email protected]>
1 parent 56fdb1d commit 25365d9

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ impl BuildOutput {
719719
whence: &str,
720720
line: &str,
721721
data: &'a str,
722+
old_syntax: bool,
722723
) -> CargoResult<(&'a str, &'a str)> {
723724
let mut iter = data.splitn(2, "=");
724725
let key = iter.next();
@@ -727,9 +728,10 @@ impl BuildOutput {
727728
(Some(a), Some(b)) => Ok((a, b.trim_end())),
728729
_ => bail!(
729730
"invalid output in {whence}: `{line}`\n\
730-
Expected a line with `cargo::KEY=VALUE` with an `=` character, \
731+
Expected a line with `{syntax}KEY=VALUE` with an `=` character, \
731732
but none was found.\n\
732733
{DOCS_LINK_SUGGESTION}",
734+
syntax = if old_syntax { "cargo:" } else { "cargo::" },
733735
),
734736
}
735737
}
@@ -738,6 +740,7 @@ impl BuildOutput {
738740
whence: &str,
739741
line: &str,
740742
data: &'a str,
743+
old_syntax: bool,
741744
) -> CargoResult<(&'a str, &'a str)> {
742745
let mut iter = data.splitn(2, "=");
743746
let key = iter.next();
@@ -746,9 +749,14 @@ impl BuildOutput {
746749
(Some(a), Some(b)) => Ok((a, b.trim_end())),
747750
_ => bail!(
748751
"invalid output in {whence}: `{line}`\n\
749-
Expected a line with `cargo::metadata=KEY=VALUE` with an `=` character, \
752+
Expected a line with `{syntax}KEY=VALUE` with an `=` character, \
750753
but none was found.\n\
751754
{DOCS_LINK_SUGGESTION}",
755+
syntax = if old_syntax {
756+
"cargo:"
757+
} else {
758+
"cargo::metadata="
759+
},
752760
),
753761
}
754762
}
@@ -758,16 +766,18 @@ impl BuildOutput {
758766
Ok(line) => line.trim(),
759767
Err(..) => continue,
760768
};
769+
let mut old_syntax = false;
761770
let (key, value) = if let Some(data) = line.strip_prefix("cargo::") {
762771
// For instance, `cargo::rustc-flags=foo` or `cargo::metadata=foo=bar`.
763-
parse_directive(whence.as_str(), line, data)?
772+
parse_directive(whence.as_str(), line, data, old_syntax)?
764773
} else if let Some(data) = line.strip_prefix("cargo:") {
774+
old_syntax = true;
765775
// For instance, `cargo:rustc-flags=foo`.
766776
if RESERVED_PREFIXES
767777
.iter()
768778
.any(|prefix| data.starts_with(prefix))
769779
{
770-
parse_directive(whence.as_str(), line, data)?
780+
parse_directive(whence.as_str(), line, data, old_syntax)?
771781
} else {
772782
// For instance, `cargo:foo=bar`.
773783
("metadata", data)
@@ -782,12 +792,14 @@ impl BuildOutput {
782792
script_out_dir.to_str().unwrap(),
783793
);
784794

795+
let syntax_prefix = if old_syntax { "cargo:" } else { "cargo::" };
785796
macro_rules! check_and_add_target {
786797
($target_kind: expr, $is_target_kind: expr, $link_type: expr) => {
787798
if !targets.iter().any(|target| $is_target_kind(target)) {
788799
bail!(
789-
"invalid instruction `cargo::{}` from {}\n\
800+
"invalid instruction `{}{}` from {}\n\
790801
The package {} does not have a {} target.",
802+
syntax_prefix,
791803
key,
792804
whence,
793805
pkg_descr,
@@ -810,14 +822,14 @@ impl BuildOutput {
810822
"rustc-link-arg-cdylib" | "rustc-cdylib-link-arg" => {
811823
if !targets.iter().any(|target| target.is_cdylib()) {
812824
warnings.push(format!(
813-
"cargo::{} was specified in the build script of {}, \
825+
"{}{} was specified in the build script of {}, \
814826
but that package does not contain a cdylib target\n\
815827
\n\
816828
Allowing this was an unintended change in the 1.50 \
817829
release, and may become an error in the future. \
818830
For more information, see \
819831
<https://github.com/rust-lang/cargo/issues/9562>.",
820-
key, pkg_descr
832+
syntax_prefix, key, pkg_descr
821833
));
822834
}
823835
linker_args.push((LinkArgTarget::Cdylib, value))
@@ -828,11 +840,13 @@ impl BuildOutput {
828840
"rustc-link-arg-bin" => {
829841
let (bin_name, arg) = value.split_once('=').ok_or_else(|| {
830842
anyhow::format_err!(
831-
"invalid instruction `cargo::{}={}` from {}\n\
832-
The instruction should have the form cargo::{}=BIN=ARG",
843+
"invalid instruction `{}{}={}` from {}\n\
844+
The instruction should have the form {}{}=BIN=ARG",
845+
syntax_prefix,
833846
key,
834847
value,
835848
whence,
849+
syntax_prefix,
836850
key
837851
)
838852
})?;
@@ -841,8 +855,9 @@ impl BuildOutput {
841855
.any(|target| target.is_bin() && target.name() == bin_name)
842856
{
843857
bail!(
844-
"invalid instruction `cargo::{}` from {}\n\
858+
"invalid instruction `{}{}` from {}\n\
845859
The package {} does not have a bin target with the name `{}`.",
860+
syntax_prefix,
846861
key,
847862
whence,
848863
pkg_descr,
@@ -871,7 +886,10 @@ impl BuildOutput {
871886
if extra_check_cfg {
872887
check_cfgs.push(value.to_string());
873888
} else {
874-
warnings.push(format!("cargo::{} requires -Zcheck-cfg flag", key));
889+
warnings.push(format!(
890+
"{}{} requires -Zcheck-cfg flag",
891+
syntax_prefix, key
892+
));
875893
}
876894
}
877895
"rustc-env" => {
@@ -929,7 +947,7 @@ impl BuildOutput {
929947
"rerun-if-changed" => rerun_if_changed.push(PathBuf::from(value)),
930948
"rerun-if-env-changed" => rerun_if_env_changed.push(value.to_string()),
931949
"metadata" => {
932-
let (key, value) = parse_metadata(whence.as_str(), line, &value)?;
950+
let (key, value) = parse_metadata(whence.as_str(), line, &value, old_syntax)?;
933951
metadata.push((key.to_owned(), value.to_owned()));
934952
}
935953
_ => bail!(

tests/testsuite/build_script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5169,7 +5169,7 @@ fn wrong_output() {
51695169
"\
51705170
[COMPILING] foo [..]
51715171
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo:example`
5172-
Expected a line with `cargo::metadata=KEY=VALUE` with an `=` character, but none was found.
5172+
Expected a line with `cargo:KEY=VALUE` with an `=` character, but none was found.
51735173
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
51745174
for more information about build script outputs.
51755175
",

tests/testsuite/build_script_extra_link_arg.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn link_arg_missing_target() {
126126
.with_status(101)
127127
.with_stderr("\
128128
[COMPILING] foo [..]
129-
error: invalid instruction `cargo::rustc-link-arg-bins` from build script of `foo v0.0.1 ([ROOT]/foo)`
129+
error: invalid instruction `cargo:rustc-link-arg-bins` from build script of `foo v0.0.1 ([ROOT]/foo)`
130130
The package foo v0.0.1 ([ROOT]/foo) does not have a bin target.
131131
")
132132
.run();
@@ -141,7 +141,7 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target.
141141
.with_stderr(
142142
"\
143143
[COMPILING] foo [..]
144-
error: invalid instruction `cargo::rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)`
144+
error: invalid instruction `cargo:rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)`
145145
The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `abc`.
146146
",
147147
)
@@ -157,8 +157,8 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `ab
157157
.with_stderr(
158158
"\
159159
[COMPILING] foo [..]
160-
error: invalid instruction `cargo::rustc-link-arg-bin=abc` from build script of `foo v0.0.1 ([ROOT]/foo)`
161-
The instruction should have the form cargo::rustc-link-arg-bin=BIN=ARG
160+
error: invalid instruction `cargo:rustc-link-arg-bin=abc` from build script of `foo v0.0.1 ([ROOT]/foo)`
161+
The instruction should have the form cargo:rustc-link-arg-bin=BIN=ARG
162162
",
163163
)
164164
.run();
@@ -204,7 +204,7 @@ fn cdylib_link_arg_transitive() {
204204
[COMPILING] bar v1.0.0 [..]
205205
[RUNNING] `rustc --crate-name build_script_build bar/build.rs [..]
206206
[RUNNING] `[..]build-script-build[..]
207-
warning: [email protected]: cargo::rustc-link-arg-cdylib was specified in the build script of bar v1.0.0 \
207+
warning: [email protected]: cargo:rustc-link-arg-cdylib was specified in the build script of bar v1.0.0 \
208208
([ROOT]/foo/bar), but that package does not contain a cdylib target
209209
210210
Allowing this was an unintended change in the 1.50 release, and may become an error in \

tests/testsuite/check_cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ fn build_script_feature_gate() {
540540
.build();
541541

542542
p.cargo("check")
543-
.with_stderr_contains("warning[..]cargo::rustc-check-cfg requires -Zcheck-cfg flag")
543+
.with_stderr_contains("warning[..]cargo:rustc-check-cfg requires -Zcheck-cfg flag")
544544
.with_status(0)
545545
.run();
546546
}

0 commit comments

Comments
 (0)