Skip to content

Commit 11b5493

Browse files
committed
Support target-spec json file extension in various cases (case-insensitive)
1 parent be1db6c commit 11b5493

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/cargo/core/compiler/compile_kind.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl CompileTarget {
131131
if name.is_empty() {
132132
anyhow::bail!("target was empty");
133133
}
134-
if !name.ends_with(".json") {
134+
if !Self::name_ends_with_json(name) {
135135
return Ok(CompileTarget { name: name.into() });
136136
}
137137

@@ -170,7 +170,7 @@ impl CompileTarget {
170170
// name without ".json") as a short name for this target. Note that the
171171
// `unwrap()` here should never trigger since we have a nonempty name
172172
// and it starts as utf-8 so it's always utf-8
173-
if self.name.ends_with(".json") {
173+
if self.is_json_file() {
174174
Path::new(&self.name).file_stem().unwrap().to_str().unwrap()
175175
} else {
176176
&self.name
@@ -180,11 +180,7 @@ impl CompileTarget {
180180
/// See [`CompileKind::fingerprint_hash`].
181181
pub fn fingerprint_hash(&self) -> u64 {
182182
let mut hasher = StableHasher::new();
183-
match self
184-
.name
185-
.ends_with(".json")
186-
.then(|| fs::read_to_string(self.name))
187-
{
183+
match self.is_json_file().then(|| fs::read_to_string(self.name)) {
188184
Some(Ok(contents)) => {
189185
// This may have some performance concerns, since it is called
190186
// fairly often. If that ever seems worth fixing, consider
@@ -197,4 +193,25 @@ impl CompileTarget {
197193
}
198194
hasher.finish()
199195
}
196+
197+
/// Checks if name of `self` contains a filename with json ext.
198+
/// Does not check if the path exists.
199+
///
200+
/// Same as [`Self::name_ends_with_json`], use it to check strings if create `CompileTarget` isn't a way.
201+
#[inline]
202+
pub fn is_json_file(&self) -> bool {
203+
Self::name_ends_with_json(self.name)
204+
}
205+
206+
/// Helper function to check if the `name` ends with ".json" (case-insensitive).
207+
/// Does not check if the path exists.
208+
pub fn name_ends_with_json<S: AsRef<str>>(name: S) -> bool {
209+
const EXT: &'static str = ".json";
210+
name.as_ref().ends_with(EXT) || {
211+
let s = name.as_ref();
212+
s.get((s.len() - 5)..)
213+
.map(|ext| ext.eq_ignore_ascii_case(EXT))
214+
.unwrap_or_default()
215+
}
216+
}
200217
}

src/cargo/util/context/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use std::time::Instant;
6868

6969
use self::ConfigValue as CV;
7070
use crate::core::compiler::rustdoc::RustdocExternMap;
71+
use crate::core::compiler::CompileTarget;
7172
use crate::core::global_cache_tracker::{DeferredGlobalLastUse, GlobalCacheTracker};
7273
use crate::core::shell::Verbosity;
7374
use crate::core::{features, CliUnstable, Shell, SourceId, Workspace, WorkspaceRootConfig};
@@ -2651,7 +2652,7 @@ impl BuildTargetConfig {
26512652
/// Gets values of `build.target` as a list of strings.
26522653
pub fn values(&self, gctx: &GlobalContext) -> CargoResult<Vec<String>> {
26532654
let map = |s: &String| {
2654-
if s.ends_with(".json") {
2655+
if CompileTarget::name_ends_with_json(s) {
26552656
// Path to a target specification file (in JSON).
26562657
// <https://doc.rust-lang.org/rustc/targets/custom.html>
26572658
self.inner

0 commit comments

Comments
 (0)