1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use crate::core::{GitReference, PackageId, Source, SourceId};
use crate::sources::{ReplacedSource, CRATES_IO_REGISTRY};
use crate::util::config::{self, ConfigRelativePath, OptValue};
use crate::util::errors::CargoResult;
use crate::util::{Config, IntoUrl};
use anyhow::{bail, Context as _};
use log::debug;
use std::collections::{HashMap, HashSet};
use url::Url;
#[derive(Clone)]
pub struct SourceConfigMap<'cfg> {
cfgs: HashMap<String, SourceConfig>,
id2name: HashMap<SourceId, String>,
config: &'cfg Config,
}
#[derive(Debug, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
struct SourceConfigDef {
replace_with: OptValue<String>,
directory: Option<ConfigRelativePath>,
registry: OptValue<String>,
local_registry: Option<ConfigRelativePath>,
git: OptValue<String>,
branch: OptValue<String>,
tag: OptValue<String>,
rev: OptValue<String>,
}
#[derive(Clone)]
struct SourceConfig {
id: SourceId,
replace_with: Option<(String, String)>,
}
impl<'cfg> SourceConfigMap<'cfg> {
pub fn new(config: &'cfg Config) -> CargoResult<SourceConfigMap<'cfg>> {
let mut base = SourceConfigMap::empty(config)?;
let sources: Option<HashMap<String, SourceConfigDef>> = config.get("source")?;
if let Some(sources) = sources {
for (key, value) in sources.into_iter() {
base.add_config(key, value)?;
}
}
Ok(base)
}
pub fn empty(config: &'cfg Config) -> CargoResult<SourceConfigMap<'cfg>> {
let mut base = SourceConfigMap {
cfgs: HashMap::new(),
id2name: HashMap::new(),
config,
};
base.add(
CRATES_IO_REGISTRY,
SourceConfig {
id: SourceId::crates_io(config)?,
replace_with: None,
},
)?;
Ok(base)
}
pub fn config(&self) -> &'cfg Config {
self.config
}
pub fn load(
&self,
id: SourceId,
yanked_whitelist: &HashSet<PackageId>,
) -> CargoResult<Box<dyn Source + 'cfg>> {
debug!("loading: {}", id);
let mut name = match self.id2name.get(&id) {
Some(name) => name,
None => return id.load(self.config, yanked_whitelist),
};
let mut cfg_loc = "";
let orig_name = name;
let new_id;
loop {
let cfg = match self.cfgs.get(name) {
Some(cfg) => cfg,
None => bail!(
"could not find a configured source with the \
name `{}` when attempting to lookup `{}` \
(configuration in `{}`)",
name,
orig_name,
cfg_loc
),
};
match &cfg.replace_with {
Some((s, c)) => {
name = s;
cfg_loc = c;
}
None if id == cfg.id => return id.load(self.config, yanked_whitelist),
None => {
new_id = cfg.id.with_precise(id.precise().map(|s| s.to_string()));
break;
}
}
debug!("following pointer to {}", name);
if name == orig_name {
bail!(
"detected a cycle of `replace-with` sources, the source \
`{}` is eventually replaced with itself \
(configuration in `{}`)",
name,
cfg_loc
)
}
}
let new_src = new_id.load(
self.config,
&yanked_whitelist
.iter()
.map(|p| p.map_source(id, new_id))
.collect(),
)?;
let old_src = id.load(self.config, yanked_whitelist)?;
if !new_src.supports_checksums() && old_src.supports_checksums() {
bail!(
"\
cannot replace `{orig}` with `{name}`, the source `{orig}` supports \
checksums, but `{name}` does not
a lock file compatible with `{orig}` cannot be generated in this situation
",
orig = orig_name,
name = name
);
}
if old_src.requires_precise() && id.precise().is_none() {
bail!(
"\
the source {orig} requires a lock file to be present first before it can be
used against vendored source code
remove the source replacement configuration, generate a lock file, and then
restore the source replacement configuration to continue the build
",
orig = orig_name
);
}
Ok(Box::new(ReplacedSource::new(id, new_id, new_src)))
}
fn add(&mut self, name: &str, cfg: SourceConfig) -> CargoResult<()> {
if let Some(old_name) = self.id2name.insert(cfg.id, name.to_string()) {
if name != CRATES_IO_REGISTRY {
bail!(
"source `{}` defines source {}, but that source is already defined by `{}`\n\
note: Sources are not allowed to be defined multiple times.",
name,
cfg.id,
old_name
);
}
}
self.cfgs.insert(name.to_string(), cfg);
Ok(())
}
fn add_config(&mut self, name: String, def: SourceConfigDef) -> CargoResult<()> {
let mut srcs = Vec::new();
if let Some(registry) = def.registry {
let url = url(®istry, &format!("source.{}.registry", name))?;
srcs.push(SourceId::for_registry(&url)?);
}
if let Some(local_registry) = def.local_registry {
let path = local_registry.resolve_path(self.config);
srcs.push(SourceId::for_local_registry(&path)?);
}
if let Some(directory) = def.directory {
let path = directory.resolve_path(self.config);
srcs.push(SourceId::for_directory(&path)?);
}
if let Some(git) = def.git {
let url = url(&git, &format!("source.{}.git", name))?;
let reference = match def.branch {
Some(b) => GitReference::Branch(b.val),
None => match def.tag {
Some(b) => GitReference::Tag(b.val),
None => match def.rev {
Some(b) => GitReference::Rev(b.val),
None => GitReference::DefaultBranch,
},
},
};
srcs.push(SourceId::for_git(&url, reference)?);
} else {
let check_not_set = |key, v: OptValue<String>| {
if let Some(val) = v {
bail!(
"source definition `source.{}` specifies `{}`, \
but that requires a `git` key to be specified (in {})",
name,
key,
val.definition
);
}
Ok(())
};
check_not_set("branch", def.branch)?;
check_not_set("tag", def.tag)?;
check_not_set("rev", def.rev)?;
}
if name == "crates-io" && srcs.is_empty() {
srcs.push(SourceId::crates_io(self.config)?);
}
match srcs.len() {
0 => bail!(
"no source location specified for `source.{}`, need \
`registry`, `local-registry`, `directory`, or `git` defined",
name
),
1 => {}
_ => bail!(
"more than one source location specified for `source.{}`",
name
),
}
let src = srcs[0];
let replace_with = def
.replace_with
.map(|val| (val.val, val.definition.to_string()));
self.add(
&name,
SourceConfig {
id: src,
replace_with,
},
)?;
return Ok(());
fn url(val: &config::Value<String>, key: &str) -> CargoResult<Url> {
let url = val.val.into_url().with_context(|| {
format!(
"configuration key `{}` specified an invalid \
URL (in {})",
key, val.definition
)
})?;
Ok(url)
}
}
}