Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compile): make output more deterministic #25092

Merged
Merged
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
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ deno_emit = "=0.44.0"
deno_graph = { version = "=0.81.2" }
deno_lint = { version = "=0.63.1", features = ["docs"] }
deno_lockfile.workspace = true
deno_npm = "=0.22.0"
deno_npm = "=0.23.0"
deno_package_json.workspace = true
deno_runtime = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_semver = "=0.5.10"
deno_task_shell = "=0.17.0"
deno_terminal.workspace = true
eszip = "=0.74.0"
eszip = "=0.75.0"
libsui = "0.3.0"
napi_sym.workspace = true
node_resolver.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion cli/mainrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::tokio_util::create_and_run_current_thread_with_maybe_metrics;
pub use deno_runtime::UNSTABLE_GRANULAR_FLAGS;
use deno_terminal::colors;
use indexmap::IndexMap;

use std::borrow::Cow;
use std::collections::HashMap;
Expand Down Expand Up @@ -73,7 +74,7 @@ fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
}
}

fn load_env_vars(env_vars: &HashMap<String, String>) {
fn load_env_vars(env_vars: &IndexMap<String, String>) {
env_vars.iter().for_each(|env_var| {
if env::var(env_var.0).is_err() {
std::env::set_var(env_var.0, env_var.1);
Expand Down
24 changes: 15 additions & 9 deletions cli/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub struct SerializedWorkspaceResolver {
pub pkg_json_resolution: PackageJsonDepResolution,
}

// Note: Don't use hashmaps/hashsets. Ensure the serialization
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Note: Don't use hashmaps/hashsets. Ensure the serialization
// NOTE(@dsherret): Don't use hashmaps/hashsets. Ensure the serialization

Copy link
Member Author

Choose a reason for hiding this comment

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

Why should this be attributed?

// is deterministic.
#[derive(Deserialize, Serialize)]
pub struct Metadata {
pub argv: Vec<String>,
Expand All @@ -111,7 +113,7 @@ pub struct Metadata {
pub ca_stores: Option<Vec<String>>,
pub ca_data: Option<Vec<u8>>,
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
pub env_vars_from_env_file: HashMap<String, String>,
pub env_vars_from_env_file: IndexMap<String, String>,
pub workspace_resolver: SerializedWorkspaceResolver,
pub entrypoint_key: String,
pub node_modules: Option<NodeModules>,
Expand Down Expand Up @@ -667,8 +669,10 @@ impl<'a> DenoCompileBinaryWriter<'a> {
// but also don't make this dependent on the registry url
let root_path = npm_resolver.global_cache_root_folder();
let mut builder = VfsBuilder::new(root_path)?;
for package in npm_resolver.all_system_packages(&self.npm_system_info)
{
let mut packages =
npm_resolver.all_system_packages(&self.npm_system_info);
packages.sort_by(|a, b| a.id.cmp(&b.id)); // determinism
for package in packages {
let folder =
npm_resolver.resolve_pkg_folder_from_pkg_id(&package.id)?;
builder.add_dir_recursive(&folder)?;
Expand Down Expand Up @@ -729,11 +733,13 @@ impl<'a> DenoCompileBinaryWriter<'a> {
cli_options.workspace().root_dir().to_file_path().unwrap(),
);
while let Some(pending_dir) = pending_dirs.pop_front() {
let entries = fs::read_dir(&pending_dir).with_context(|| {
format!("Failed reading: {}", pending_dir.display())
})?;
let mut entries = fs::read_dir(&pending_dir)
.with_context(|| {
format!("Failed reading: {}", pending_dir.display())
})?
.collect::<Result<Vec<_>, _>>()?;
entries.sort_by_cached_key(|entry| entry.file_name()); // determinism
for entry in entries {
let entry = entry?;
let path = entry.path();
if !path.is_dir() {
continue;
Expand All @@ -755,8 +761,8 @@ impl<'a> DenoCompileBinaryWriter<'a> {
/// in the passed environment file.
fn get_file_env_vars(
filename: String,
) -> Result<HashMap<String, String>, dotenvy::Error> {
let mut file_env_vars = HashMap::new();
) -> Result<IndexMap<String, String>, dotenvy::Error> {
let mut file_env_vars = IndexMap::new();
for item in dotenvy::from_filename_iter(filename)? {
let Ok((key, val)) = item else {
continue; // this failure will be warned about on load
Expand Down
7 changes: 5 additions & 2 deletions cli/standalone/virtual_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ impl VfsBuilder {
let read_dir = std::fs::read_dir(path)
.with_context(|| format!("Reading {}", path.display()))?;

for entry in read_dir {
let entry = entry?;
let mut dir_entries =
read_dir.into_iter().collect::<Result<Vec<_>, _>>()?;
dir_entries.sort_by_cached_key(|entry| entry.file_name()); // determinism

for entry in dir_entries {
let file_type = entry.file_type()?;
let path = entry.path();

Expand Down
28 changes: 28 additions & 0 deletions tests/specs/compile/determinism/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tempDir": true,
"steps": [{
"if": "unix",
"args": "compile --output main1 main.ts",
"output": "[WILDCARD]"
}, {
"if": "unix",
"args": "compile --output main2 main.ts",
"output": "[WILDCARD]"
}, {
"if": "unix",
"args": "run --allow-read=. assert_equal.ts main1 main2",
"output": "Same\n"
}, {
"if": "windows",
"args": "compile --output main1.exe main.ts",
"output": "[WILDCARD]"
}, {
"if": "windows",
"args": "compile --output main2.exe main.ts",
"output": "[WILDCARD]"
}, {
"if": "windows",
"args": "run --allow-read=. assert_equal.ts main1.exe main2.exe",
"output": "Same\n"
}]
}
15 changes: 15 additions & 0 deletions tests/specs/compile/determinism/assert_equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const file1 = Deno.readFileSync(Deno.args[0]);
const file2 = Deno.readFileSync(Deno.args[1]);

if (file1.length !== file2.length) {
console.error("File lengths are different");
Deno.exit(1);
}
for (let i = 0; i < file1.length; i++) {
if (file1[i] !== file2[i]) {
console.error("Files are different");
Deno.exit(1);
}
}

console.error("Same");
4 changes: 4 additions & 0 deletions tests/specs/compile/determinism/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "npm:@denotest/esm-basic";
import "npm:@denotest/add";
import "npm:@denotest/subtract";
import "npm:preact";
Loading