Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(build): read a crate's license-file and fail when cargo cannot list the crates

EtienneLescotb3d37c95e85f
diff --git a/scripts/before-pack.cjs b/scripts/before-pack.cjs
index fe45fa3b0..94b9ccf0e 100644
--- a/scripts/before-pack.cjs
+++ b/scripts/before-pack.cjs
@@ -1115,8 +1115,9 @@ function checkCompositorAddonFreshness(
// electron-builder.json5 ships this as resources/THIRD-PARTY-LICENSES.txt, but for a missing
// extraResources source electron-builder only logs "file source doesn't exist" and packages
// without it. A packaging path that skipped `npm run licenses:generate` would ship an installer
-// with no licence texts for the code it bundles, silently. The generator itself never fails over
-// a missing licence file or cargo, so only absence is refused here.
+// with no licence texts for the code it bundles, silently. The generator writes nothing when cargo
+// cannot list the addon's crates, and never fails over a missing licence file, so only absence is
+// refused here.
const THIRD_PARTY_LICENSES = path.join(ROOT, "build-licenses", "THIRD-PARTY-LICENSES.txt");
function checkThirdPartyLicenses() {
diff --git a/scripts/generate-third-party-licenses.mjs b/scripts/generate-third-party-licenses.mjs
index 9e2dd700e..ea9b5774c 100644
--- a/scripts/generate-third-party-licenses.mjs
+++ b/scripts/generate-third-party-licenses.mjs
@@ -7,9 +7,10 @@
// code is: Vite bundles it into dist/ and dist-electron/.
// - Rust: every crate compiled into compositor_view.node, from `cargo metadata`.
//
-// It never fails a release build over a licence. A package without a licence file is recorded
-// with its declared licence, and a missing or failing cargo drops the crates part with a warning;
-// the file is written either way. before-pack.cjs refuses to package only when it is absent.
+// A package without a licence file is recorded with its declared licence, with a warning; that
+// never fails a build. A missing or failing cargo does, and nothing is written: every installer
+// ships the addon, so a file without its crates would be incomplete, and before-pack.cjs, which
+// refuses to package when the file is absent, would accept it.
//
// Run: node scripts/generate-third-party-licenses.mjs [output path]
@@ -25,22 +26,35 @@ const ADDON_CRATE = "compositor-view-napi";
const LICENSE_FILE = /^(licen[cs]e|copying|notice)/i;
const NO_FILE = "(no licence file in the package)";
-/** The licence and NOTICE files at the top of a package directory, in file-name order. */
-function readLicenseTexts(dir) {
- return fs
+/**
+ * The licence and NOTICE files at the top of a package directory, in file-name order, then the
+ * `extra` files that exist. A text read twice is printed once: renderSection merges it.
+ */
+function readLicenseTexts(dir, extra = []) {
+ const names = fs
.readdirSync(dir, { withFileTypes: true })
.filter((entry) => entry.isFile() && LICENSE_FILE.test(entry.name))
.map((entry) => entry.name)
- .sort()
- .map((name) => fs.readFileSync(path.join(dir, name), "utf8"))
+ .sort();
+ return [
+ ...names.map((name) => path.join(dir, name)),
+ ...extra.filter((file) => fs.existsSync(file)),
+ ]
+ .map((file) => fs.readFileSync(file, "utf8"))
.filter((text) => text.trim());
}
-/** SPDX expression from a package.json or a cargo package; tolerates the legacy object forms. */
-function declaredLicense({ license, licenses }) {
+/**
+ * SPDX expression from a package.json or a cargo package; tolerates the legacy object forms.
+ * A crate may declare only a `license_file`, a custom licence, which is then named instead.
+ */
+function declaredLicense({ license, licenses, license_file }) {
const values = [license ?? licenses].flat();
const ids = values.map((value) => (typeof value === "string" ? value : value?.type));
- return ids.filter(Boolean).join(" OR ") || "no declared licence";
+ return (
+ ids.filter(Boolean).join(" OR ") ||
+ (license_file ? `see ${license_file}` : "no declared licence")
+ );
}
function readJson(file) {
@@ -102,10 +116,15 @@ export function collectCrates(metadata) {
return [...seen]
.filter((id) => !members.has(id))
.map((id) => packages.get(id))
- .map((pkg) => ({
- label: `${pkg.name} ${pkg.version} (${declaredLicense(pkg)})`,
- texts: readLicenseTexts(path.dirname(pkg.manifest_path)),
- }));
+ .map((pkg) => {
+ const dir = path.dirname(pkg.manifest_path);
+ // Cargo's `license-file` is relative to the manifest and may sit below the top level.
+ const declared = pkg.license_file ? [path.resolve(dir, pkg.license_file)] : [];
+ return {
+ label: `${pkg.name} ${pkg.version} (${declaredLicense(pkg)})`,
+ texts: readLicenseTexts(dir, declared),
+ };
+ });
}
// Code-unit order, not localeCompare: the same input must give the same bytes on every runner.
@@ -135,8 +154,7 @@ function summary(noun, entries) {
return `${noun}: ${entries.length} (${missing} without a licence file).`;
}
-/** `crates` is undefined when cargo could not list them; `cratesError` then says why. */
-export function renderLicenses({ npm, crates, cratesError }) {
+export function renderLicenses({ npm, crates }) {
const header = [
"OpenScreen: third-party licences",
"",
@@ -150,15 +168,13 @@ export function renderLicenses({ npm, crates, cratesError }) {
"file. Generated at build time by scripts/generate-third-party-licenses.mjs.",
"",
summary("npm packages", npm),
- crates
- ? summary("Rust crates", crates)
- : `Rust crates: not listed, cargo metadata failed on the build machine (${cratesError}).`,
+ summary("Rust crates", crates),
"",
];
- const sections = [renderSection("npm packages", npm)];
- if (crates) {
- sections.push(renderSection("Rust crates compiled into the compositor addon", crates));
- }
+ const sections = [
+ renderSection("npm packages", npm),
+ renderSection("Rust crates compiled into the compositor addon", crates),
+ ];
return `${[...header, ...sections].join("\n").trimEnd()}\n`;
}
@@ -173,10 +189,10 @@ function cargoMetadata() {
// The JSON is ~1 MB already, which is spawnSync's default buffer.
{ cwd: crates, encoding: "utf8", maxBuffer: 256 * 1024 * 1024 },
);
- if (result.error) throw result.error;
+ if (result.error) throw new Error(`cargo metadata could not run: ${result.error.message}`);
if (result.status !== 0) {
const last = result.stderr.trim().split("\n").pop();
- throw new Error(`exit code ${result.status}: ${last}`);
+ throw new Error(`cargo metadata failed, exit code ${result.status}: ${last}`);
}
return JSON.parse(result.stdout);
}
@@ -189,15 +205,9 @@ const warn = (message) =>
function main() {
const output = process.argv[2] ? path.resolve(process.argv[2]) : OUTPUT;
const { entries: npm, skipped } = collectNpmPackages();
- let crates;
- let cratesError;
- try {
- crates = collectCrates(cargoMetadata());
- } catch (err) {
- cratesError = err instanceof Error ? err.message : String(err);
- warn(`third-party licences: Rust crates omitted, cargo metadata failed (${cratesError}).`);
- }
- const missing = [...npm, ...(crates ?? [])].filter((entry) => entry.texts.length === 0);
+ // Throws without cargo: no fallback, see the top of this file.
+ const crates = collectCrates(cargoMetadata());
+ const missing = [...npm, ...crates].filter((entry) => entry.texts.length === 0);
if (missing.length > 0) {
warn(
`third-party licences: ${missing.length} packages have no licence file; ` +
@@ -205,12 +215,12 @@ function main() {
);
}
- const text = renderLicenses({ npm, crates, cratesError });
+ const text = renderLicenses({ npm, crates });
fs.mkdirSync(path.dirname(output), { recursive: true });
fs.writeFileSync(output, text);
console.log(
`Wrote ${path.relative(ROOT, output)} (${Math.round(Buffer.byteLength(text) / 1024)} KB): ` +
- `${npm.length} npm packages, ${crates?.length ?? 0} Rust crates` +
+ `${npm.length} npm packages, ${crates.length} Rust crates` +
(skipped.length > 0 ? `; ${skipped.length} lock entries not installed here skipped.` : "."),
);
}
diff --git a/scripts/generate-third-party-licenses.test.mjs b/scripts/generate-third-party-licenses.test.mjs
index eed452f35..d872ed435 100644
--- a/scripts/generate-third-party-licenses.test.mjs
+++ b/scripts/generate-third-party-licenses.test.mjs
@@ -157,6 +157,25 @@ describe("collectCrates", () => {
const serde = collectCrates(METADATA).find((entry) => entry.label.startsWith("serde "));
expect(serde?.texts).toEqual(["serde licence"]);
});
+
+ it("reads a declared license-file below the top level, and names it without a licence", () => {
+ const custom = { ...crate("custom"), license: null, license_file: "legal/TERMS.txt" };
+ write(path.dirname(custom.manifest_path), { "legal/TERMS.txt": "custom terms" });
+ const metadata = {
+ packages: [crate("compositor-view-napi", "cdylib"), custom],
+ workspace_members: ["compositor-view-napi 1.0.0"],
+ resolve: {
+ nodes: [
+ { id: "compositor-view-napi 1.0.0", deps: [dep("custom", null)] },
+ { id: "custom 1.0.0", deps: [] },
+ ],
+ },
+ };
+
+ expect(collectCrates(metadata)).toEqual([
+ { label: "custom 1.0.0 (see legal/TERMS.txt)", texts: ["custom licence", "custom terms"] },
+ ]);
+ });
});
describe("renderLicenses", () => {
@@ -176,13 +195,4 @@ describe("renderLicenses", () => {
expect(reversed).toBe(renderLicenses({ npm, crates }));
expect(reversed).not.toContain("\r");
});
-
- it("still writes the npm part, and says why, when cargo could not list the crates", () => {
- const text = renderLicenses({ npm, cratesError: "spawnSync cargo ENOENT" });
-
- expect(text).toContain("Rust crates: not listed, cargo metadata failed on the build machine");
- expect(text).toContain("(spawnSync cargo ENOENT)");
- expect(text).toContain("alpha 1.0.0 (MIT)");
- expect(text).not.toContain("Rust crates compiled into the compositor addon");
- });
});