Skip to content
This repository was archived by the owner on Feb 28, 2023. It is now read-only.

Commit 7ede5c4

Browse files
authored
Merge pull request #51 from paritytech/igor-add-cargo-toml
Include well-known source input dependencies for Rust compilation
2 parents 7d81ebe + 8aaf775 commit 7ede5c4

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/compiler/rust.rs

+39-2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ where
229229
let _dep_info = run_input_output(cmd, None).await?;
230230
// Parse the dep-info file, then hash the contents of those files.
231231
let cwd = cwd.to_owned();
232+
let cwd2 = cwd.to_owned();
232233
let name2 = crate_name.to_owned();
233234
let parsed = pool
234235
.spawn_blocking(move || {
@@ -237,13 +238,49 @@ where
237238
})
238239
.await?;
239240

240-
parsed.map(move |files| {
241-
trace!(
241+
parsed.map(move |mut files| {
242+
// HACK: Ideally, if we're compiling a Cargo package, we should be
243+
// compiling it with the same files included in the published .crate
244+
// package to ensure maximum compatibility. While it's possible for
245+
// the crate developers to mark files as required for the compilation
246+
// via mechanisms such as `include_bytes!`, this is also a hack and
247+
// may leave an undesired footprint in the compilation outputs.
248+
// An upstream mechanism to only track required additional files is
249+
// pending at https://github.com/rust-lang/rust/pull/84029.
250+
// Until then, unconditionally include Cargo.toml manifest to provide
251+
// a minimal support and to keep some crates compiling that may
252+
// read additional info directly from the manifest file.
253+
let cargo_manifest_dir = env_vars
254+
.iter()
255+
.find(|(key, _)| key.to_str() == Some("CARGO_MANIFEST_DIR"))
256+
.map(|(_, value)| Path::new(value));
257+
const KNOWN_AUX_DEP_FILES: &[(Option<&str>, &str)] = &[
258+
(None, "Cargo.toml"),
259+
(Some("libp2p_wasm_ext"), "src/websockets.js"),
260+
];
261+
for (target_crate_name, file_path) in KNOWN_AUX_DEP_FILES {
262+
let name_matches = target_crate_name.map_or(true, |x| x == crate_name);
263+
// If this is a Cargo package, try to get the same path prefix as
264+
// other paths are probably using. If, let's say, a package registry
265+
// is symlinked, it might be troublesome for the path transformer to
266+
// correctly figure out which files to include.
267+
let base_dir = cargo_manifest_dir.unwrap_or(&cwd2);
268+
let file_path = base_dir.join(file_path);
269+
if name_matches && file_path.exists() {
270+
files.push(file_path);
271+
}
272+
}
273+
debug!(
242274
"[{}]: got {} source files from dep-info in {}",
243275
crate_name,
244276
files.len(),
245277
fmt_duration_as_secs(&start.elapsed())
246278
);
279+
trace!(
280+
"[{}]: source files calculated from dep-info: {:?}",
281+
crate_name,
282+
files
283+
);
247284
// Just to make sure we capture temp_dir.
248285
drop(temp_dir);
249286
files

0 commit comments

Comments
 (0)