Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Check Web CI coverage of WASM dependencies

Manav Rathi9caa61ec03f2
diff --git a/.github/checks/run.sh b/.github/checks/run.sh
index 8a37903a3d..a535d99300 100755
--- a/.github/checks/run.sh
+++ b/.github/checks/run.sh
@@ -7,3 +7,5 @@ node .github/checks/documentation-comments/check.mjs
node .github/checks/documentation-comments/test.mjs
ruby .github/checks/workflow-paths/check.rb
ruby .github/checks/workflow-paths/test.rb
+ruby .github/checks/wasm-ci-paths/check.rb
+ruby .github/checks/wasm-ci-paths/test.rb
diff --git a/.github/checks/wasm-ci-paths/check.rb b/.github/checks/wasm-ci-paths/check.rb
new file mode 100644
index 0000000000..37281fa8f7
--- /dev/null
+++ b/.github/checks/wasm-ci-paths/check.rb
@@ -0,0 +1,52 @@
+require "json"
+require "open3"
+require "pathname"
+require "set"
+require "yaml"
+
+Dir.chdir(ARGV.fetch(0, "."))
+root = Pathname.pwd
+workflow_path = ".github/workflows/web-lint.yml"
+workflow = YAML.safe_load(File.read(workflow_path), aliases: true)
+events = workflow["on"] || workflow[true]
+config = events.fetch("pull_request") || {}
+abort "#{workflow_path}: WASM coverage does not support paths-ignore" if config.key?("paths-ignore")
+patterns = config["paths"]
+exit 0 unless patterns
+abort "#{workflow_path}: WASM coverage does not support negated paths" if patterns.any? { |p| p.start_with?("!") }
+
+output, status = Open3.capture2(
+ "cargo", "metadata", "--no-deps", "--format-version", "1", "--locked", "--offline",
+ chdir: "rust",
+)
+exit status.exitstatus unless status.success?
+packages = JSON.parse(output).fetch("packages")
+by_name = packages.to_h { |package| [package.fetch("name"), package] }
+by_directory = packages.to_h { |package| [Pathname.new(package.fetch("manifest_path")).dirname, package] }
+pending = Dir["web/packages/wasm/*/package.json"].map do |path|
+ name = JSON.parse(File.read(path)).fetch("name")
+ package = by_name.fetch(name) { abort "#{path}: no matching Cargo package for #{name}" }
+ Pathname.new(package.fetch("manifest_path")).dirname
+end
+abort "No Web WASM packages found" if pending.empty?
+
+directories = Set.new
+until pending.empty?
+ directory = pending.pop
+ next unless directories.add?(directory)
+
+ package = by_directory.fetch(directory) { abort "Local dependency #{directory} is outside the Rust workspace" }
+ # Include optional and target-specific edges to avoid fetching Cargo's resolved graph.
+ package.fetch("dependencies").each do |dependency|
+ next if dependency["kind"] == "dev" || !dependency["path"]
+
+ pending << Pathname.new(dependency.fetch("path"))
+ end
+end
+
+required = %w[rust/Cargo.lock rust/Cargo.toml] + directories.map { |dir| "#{dir.relative_path_from(root)}/**" }
+# Literal directory prefixes prove coverage of the entire crate, including future files.
+prefixes = patterns.grep(%r{\A[\w./-]+/\*\*\z}).map { |pattern| pattern.delete_suffix("**") }
+missing = required.reject { |path| patterns.include?(path) || prefixes.any? { |prefix| path.start_with?(prefix) } }
+missing.sort.each { |path| warn "#{workflow_path}: missing WASM dependency path #{path.inspect}" }
+exit(missing.empty? ? 0 : 1)
diff --git a/.github/checks/wasm-ci-paths/test.rb b/.github/checks/wasm-ci-paths/test.rb
new file mode 100644
index 0000000000..970cfd103b
--- /dev/null
+++ b/.github/checks/wasm-ci-paths/test.rb
@@ -0,0 +1,70 @@
+require "fileutils"
+require "json"
+require "open3"
+require "rbconfig"
+require "tmpdir"
+require "yaml"
+
+CHECK = File.expand_path("check.rb", __dir__)
+WORKFLOW = ".github/workflows/web-lint.yml"
+
+def write(root, path, content)
+ FileUtils.mkdir_p(File.dirname("#{root}/#{path}"))
+ File.write("#{root}/#{path}", content)
+end
+
+def crate(root, path, name, dependencies = "")
+ write(root, "rust/#{path}/src/lib.rs", "")
+ write(root, "rust/#{path}/Cargo.toml", <<~TOML)
+ [package]
+ name = "#{name}"
+ version = "0.1.0"
+ edition = "2024"
+ #{dependencies}
+ TOML
+end
+
+def check(root, patterns, errors = [])
+ config = patterns ? { "paths" => patterns } : nil
+ write(root, WORKFLOW, { "on" => { "pull_request" => config } }.to_yaml)
+ stdout, stderr, status = Open3.capture3(RbConfig.ruby, CHECK, root)
+ expected = errors.map { |error| "#{WORKFLOW}: #{error}\n" }.join
+ raise "#{stdout}#{stderr}" unless stdout.empty? && stderr == expected && status.exitstatus == (errors.empty? ? 0 : 1)
+end
+
+Dir.mktmpdir("ente-wasm-ci-paths-") do |root|
+ write(root, "rust/Cargo.toml", <<~TOML)
+ [workspace]
+ resolver = "2"
+ members = ["bindings/wasm/*", "crates/*"]
+ [workspace.dependencies]
+ shared-alias = { package = "shared", path = "crates/shared" }
+ TOML
+ write(root, "web/packages/wasm/app/package.json", { name: "app-wasm" }.to_json)
+ crate(root, "bindings/wasm/app", "app-wasm", <<~TOML)
+ [dependencies]
+ shared-alias.workspace = true
+ [build-dependencies]
+ builder = { path = "../../../crates/builder" }
+ [dev-dependencies]
+ test-only = { path = "../../../crates/test-only" }
+ TOML
+ crate(root, "crates/shared", "shared", <<~TOML)
+ [target.'cfg(windows)'.dependencies]
+ leaf = { path = "../leaf", optional = true }
+ TOML
+ %w[builder leaf test-only native-only].each { |name| crate(root, "crates/#{name}", name) }
+ crate(root, "bindings/wasm/unused", "unused-wasm", <<~TOML)
+ [dependencies]
+ native-only = { path = "../../../crates/native-only" }
+ TOML
+
+ base = %w[rust/Cargo.lock rust/Cargo.toml rust/bindings/wasm/**]
+ needed = %w[builder leaf shared].map { |name| "rust/crates/#{name}/**" }
+ incomplete = base + %w[rust/crates/shared-other/** rust/crates/leaf/Cargo.toml]
+ check(root, incomplete, needed.map { |path| "missing WASM dependency path #{path.inspect}" })
+ check(root, base + needed)
+ check(root, %w[rust/**])
+ check(root, nil)
+ check(root, base + needed + ["!rust/crates/leaf/private/**"], ["WASM coverage does not support negated paths"])
+end
diff --git a/.github/workflows/web-lint.yml b/.github/workflows/web-lint.yml
index 15201a36b3..f39623888b 100644
--- a/.github/workflows/web-lint.yml
+++ b/.github/workflows/web-lint.yml
@@ -7,10 +7,13 @@ on:
- "rust/Cargo.lock"
- "rust/Cargo.toml"
- "rust/bindings/wasm/**"
+ - "rust/crates/accounts/**"
- "rust/crates/cast/**"
- "rust/crates/collections/**"
- "rust/crates/contacts/**"
- "rust/crates/core/**"
+ - "rust/crates/ensu-crypto/**"
+ - "rust/crates/legacy/**"
- "rust/crates/locker/**"
- "rust/crates/paste/**"
- "rust/crates/space/**"