Skip to content

Do not silently install cargo-about. #377

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

Merged
merged 1 commit into from
Sep 19, 2021
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
- name: 🚧 Install Node dependencies
run: cd frontend && npm install

- name: 📦 Install cargo-about
run: cargo install cargo-about

- name: 🌐 Build Graphite web code
run: cd frontend && npm run build

Expand Down
11 changes: 6 additions & 5 deletions about.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Be careful to prevent auto-formatting from breaking this file's indentation
// Replace this file with JSON output once this is resolved: https://github.com/EmbarkStudios/cargo-about/issues/73

module.exports = [
{{!
Be careful to prevent auto-formatting from breaking this file's indentation
Replace this file with JSON output once this is resolved: https://github.com/EmbarkStudios/cargo-about/issues/73
}}
GENERATED_BY_CARGO_ABOUT: [
{{#each licenses}}
{
licenseName: `{{name}}`,
Expand All @@ -18,4 +19,4 @@ module.exports = [
],
},
{{/each}}
];
]
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
"scripts": {
"serve": "vue-cli-service serve || (npm install && vue-cli-service serve)",
"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))",
"build": "vue-cli-service build || (npm install && vue-cli-service build)",
"lint": "vue-cli-service lint || (npm install && vue-cli-service lint)",
"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)"
},
Expand Down
65 changes: 50 additions & 15 deletions frontend/vue.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-var-requires, no-console */
const path = require("path");
const { unlink } = require("fs");
const { spawnSync } = require("child_process");

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

let rustLicenses = [];
let debugMode = false;
try {
// eslint-disable-next-line global-require, import/extensions, import/no-unresolved
rustLicenses = require("./rust-licenses");
} catch (_) {
// Rust licenses are not generated by Cargo About except in release mode (`npm run build`)
debugMode = true;
function generateRustLicenses() {
console.info("Generating license information for rust code");
const { stdout, stderr, status } = spawnSync("cargo", ["about", "generate", "about.hbs"], {
cwd: path.join(__dirname, ".."),
encoding: "utf8",
timeout: 60000, // one minute
shell: true,
windowsHide: true, // hide the DOS window on windows
});

if (status !== 0) {
if (status !== 101) {
// cargo returns 101 when the subcommand wasn't found
console.error("cargo-about failed", status, stderr);
}
return null;
}

// Make sure the output starts as expected, we don't want to eval an error message.
if (!stdout.trim().startsWith("GENERATED_BY_CARGO_ABOUT:")) {
console.error("Unexpected output from cargo-about", stdout);
return null;
}

// Security-wise, eval() isn't any worse than require(), but it doesn't need a temporary file.
// eslint-disable-next-line no-eval
return eval(stdout);
}

module.exports = {
Expand Down Expand Up @@ -78,8 +97,27 @@ module.exports = {
};

function formatThirdPartyLicenses(jsLicenses) {
let rustLicenses = null;
if (process.env.NODE_ENV === "production") {
try {
rustLicenses = generateRustLicenses();
} catch (e) {
// Nothing to show. Error messages were printed above.
}

if (rustLicenses === null) {
// This is probably caused by cargo about not being installed
console.error(`
Could not run 'cargo about', which is required to generate license information.
To install cargo-about on your system, you can run:
cargo install cargo-about
License information is required on production builds. Aborting.`);
process.exit(1);
}
}

// Remove the HTML character encoding caused by Handlebars
let licenses = rustLicenses.map((rustLicense) => ({
let licenses = (rustLicenses || []).map((rustLicense) => ({
licenseName: htmlDecode(rustLicense.licenseName),
licenseText: trimBlankLines(htmlDecode(rustLicense.licenseText)),
packages: rustLicense.packages.map((package) => ({
Expand Down Expand Up @@ -131,7 +169,7 @@ function formatThirdPartyLicenses(jsLicenses) {

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

licenses.forEach((license) => {
let packagesWithSameLicense = "";
Expand All @@ -153,9 +191,6 @@ ${license.licenseText}
`;
});

// Clean up by deleting the `rust-licenses.js` Rust licenses data file generated by Cargo About
unlink("./rust-licenses.js", (_) => _);

return formattedLicenseNotice;
}

Expand Down