Skip to content

Commit ce1adb7

Browse files
cauthmannKeavon
authored andcommitted
Do not silently install cargo-about. (#377)
1 parent 6e8f6d0 commit ce1adb7

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jobs:
2525
- name: 🚧 Install Node dependencies
2626
run: cd frontend && npm install
2727

28+
- name: 📦 Install cargo-about
29+
run: cargo install cargo-about
30+
2831
- name: 🌐 Build Graphite web code
2932
run: cd frontend && npm run build
3033

about.hbs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Be careful to prevent auto-formatting from breaking this file's indentation
2-
// Replace this file with JSON output once this is resolved: https://github.com/EmbarkStudios/cargo-about/issues/73
3-
4-
module.exports = [
1+
{{!
2+
Be careful to prevent auto-formatting from breaking this file's indentation
3+
Replace this file with JSON output once this is resolved: https://github.com/EmbarkStudios/cargo-about/issues/73
4+
}}
5+
GENERATED_BY_CARGO_ABOUT: [
56
{{#each licenses}}
67
{
78
licenseName: `{{name}}`,
@@ -18,4 +19,4 @@ module.exports = [
1819
],
1920
},
2021
{{/each}}
21-
];
22+
]

frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"private": true,
66
"scripts": {
77
"serve": "vue-cli-service serve || (npm install && vue-cli-service serve)",
8-
"build": "cd .. && cargo install cargo-about && cargo about generate about.hbs > frontend/rust-licenses.js && cd frontend && (vue-cli-service build || (npm install && vue-cli-service build))",
8+
"build": "vue-cli-service build || (npm install && vue-cli-service build)",
99
"lint": "vue-cli-service lint || (npm install && vue-cli-service lint)",
1010
"lint-no-fix": "vue-cli-service lint --no-fix || (echo 'There were lint errors. Please run `npm run lint` to fix auto-them. If the linter execution fails, try running `npm install` first.' && false)"
1111
},

frontend/vue.config.js

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1-
/* eslint-disable @typescript-eslint/no-var-requires */
1+
/* eslint-disable @typescript-eslint/no-var-requires, no-console */
22
const path = require("path");
3-
const { unlink } = require("fs");
3+
const { spawnSync } = require("child_process");
44

55
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
66
const LicenseCheckerWebpackPlugin = require("license-checker-webpack-plugin");
77

8-
let rustLicenses = [];
9-
let debugMode = false;
10-
try {
11-
// eslint-disable-next-line global-require, import/extensions, import/no-unresolved
12-
rustLicenses = require("./rust-licenses");
13-
} catch (_) {
14-
// Rust licenses are not generated by Cargo About except in release mode (`npm run build`)
15-
debugMode = true;
8+
function generateRustLicenses() {
9+
console.info("Generating license information for rust code");
10+
const { stdout, stderr, status } = spawnSync("cargo", ["about", "generate", "about.hbs"], {
11+
cwd: path.join(__dirname, ".."),
12+
encoding: "utf8",
13+
timeout: 60000, // one minute
14+
shell: true,
15+
windowsHide: true, // hide the DOS window on windows
16+
});
17+
18+
if (status !== 0) {
19+
if (status !== 101) {
20+
// cargo returns 101 when the subcommand wasn't found
21+
console.error("cargo-about failed", status, stderr);
22+
}
23+
return null;
24+
}
25+
26+
// Make sure the output starts as expected, we don't want to eval an error message.
27+
if (!stdout.trim().startsWith("GENERATED_BY_CARGO_ABOUT:")) {
28+
console.error("Unexpected output from cargo-about", stdout);
29+
return null;
30+
}
31+
32+
// Security-wise, eval() isn't any worse than require(), but it doesn't need a temporary file.
33+
// eslint-disable-next-line no-eval
34+
return eval(stdout);
1635
}
1736

1837
module.exports = {
@@ -78,8 +97,27 @@ module.exports = {
7897
};
7998

8099
function formatThirdPartyLicenses(jsLicenses) {
100+
let rustLicenses = null;
101+
if (process.env.NODE_ENV === "production") {
102+
try {
103+
rustLicenses = generateRustLicenses();
104+
} catch (e) {
105+
// Nothing to show. Error messages were printed above.
106+
}
107+
108+
if (rustLicenses === null) {
109+
// This is probably caused by cargo about not being installed
110+
console.error(`
111+
Could not run 'cargo about', which is required to generate license information.
112+
To install cargo-about on your system, you can run:
113+
cargo install cargo-about
114+
License information is required on production builds. Aborting.`);
115+
process.exit(1);
116+
}
117+
}
118+
81119
// Remove the HTML character encoding caused by Handlebars
82-
let licenses = rustLicenses.map((rustLicense) => ({
120+
let licenses = (rustLicenses || []).map((rustLicense) => ({
83121
licenseName: htmlDecode(rustLicense.licenseName),
84122
licenseText: trimBlankLines(htmlDecode(rustLicense.licenseText)),
85123
packages: rustLicense.packages.map((package) => ({
@@ -131,7 +169,7 @@ function formatThirdPartyLicenses(jsLicenses) {
131169

132170
// Generate the formatted text file
133171
let formattedLicenseNotice = "GRAPHITE THIRD-PARTY SOFTWARE LICENSE NOTICES\n\n";
134-
if (debugMode) formattedLicenseNotice += "WARNING: Licenses for Rust packages are excluded in debug mode to improve performance — do not release without their inclusion!\n\n";
172+
if (!rustLicenses) formattedLicenseNotice += "WARNING: Licenses for Rust packages are excluded in debug mode to improve performance — do not release without their inclusion!\n\n";
135173

136174
licenses.forEach((license) => {
137175
let packagesWithSameLicense = "";
@@ -153,9 +191,6 @@ ${license.licenseText}
153191
`;
154192
});
155193

156-
// Clean up by deleting the `rust-licenses.js` Rust licenses data file generated by Cargo About
157-
unlink("./rust-licenses.js", (_) => _);
158-
159194
return formattedLicenseNotice;
160195
}
161196

0 commit comments

Comments
 (0)