3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2025-12-24 22:06:54 +00:00

Add support for running rust-cache commands from within a Nix shell (#290)

This commit is contained in:
marc0246 2025-12-01 19:56:12 +01:00 committed by GitHub
parent 779680da71
commit 151eeee51b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 167 additions and 43 deletions

41
dist/restore/index.js vendored
View file

@ -150675,11 +150675,12 @@ function reportError(e) {
lib_core.error(`${e.stack}`);
}
}
async function getCmdOutput(cmd, args = [], options = {}) {
async function getCmdOutput(cmdFormat, cmd, options = {}) {
cmd = cmdFormat.replace("{0}", cmd);
let stdout = "";
let stderr = "";
try {
await exec.exec(cmd, args, {
await exec.exec(cmd, [], {
silent: true,
listeners: {
stdout(data) {
@ -150694,7 +150695,7 @@ async function getCmdOutput(cmd, args = [], options = {}) {
}
catch (e) {
e.commandFailed = {
command: `${cmd} ${args.join(" ")}`,
command: cmd,
stderr,
};
throw e;
@ -150742,11 +150743,12 @@ class Workspace {
this.root = root;
this.target = target;
}
async getPackages(filter, ...extraArgs) {
async getPackages(cmdFormat, filter, extraArgs) {
const cmd = "cargo metadata --all-features --format-version 1" + (extraArgs ? ` ${extraArgs}` : "");
let packages = [];
try {
lib_core.debug(`collecting metadata for "${this.root}"`);
const meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1", ...extraArgs], {
const meta = JSON.parse(await getCmdOutput(cmdFormat, cmd, {
cwd: this.root,
env: { ...process.env, "CARGO_ENCODED_RUSTFLAGS": "" },
}));
@ -150761,11 +150763,11 @@ class Workspace {
}
return packages;
}
async getPackagesOutsideWorkspaceRoot() {
return await this.getPackages((pkg) => !pkg.manifest_path.startsWith(this.root));
async getPackagesOutsideWorkspaceRoot(cmdFormat) {
return await this.getPackages(cmdFormat, (pkg) => !pkg.manifest_path.startsWith(this.root));
}
async getWorkspaceMembers() {
return await this.getPackages((_) => true, "--no-deps");
async getWorkspaceMembers(cmdFormat) {
return await this.getPackages(cmdFormat, (_) => true, "--no-deps");
}
}
@ -150787,6 +150789,8 @@ const STATE_CONFIG = "RUST_CACHE_CONFIG";
const HASH_LENGTH = 8;
class CacheConfig {
constructor() {
/** A format string for running commands */
this.cmdFormat = "";
/** All the paths we want to cache */
this.cachePaths = [];
/** The primary cache key */
@ -150815,6 +150819,17 @@ class CacheConfig {
*/
static async new() {
const self = new CacheConfig();
let cmdFormat = lib_core.getInput("cmd-format");
if (cmdFormat) {
const placeholderMatches = cmdFormat.match(/\{0\}/g);
if (!placeholderMatches || placeholderMatches.length !== 1) {
cmdFormat = "{0}";
}
}
else {
cmdFormat = "{0}";
}
self.cmdFormat = cmdFormat;
// Construct key prefix:
// This uses either the `shared-key` input,
// or the `key` input combined with the `job` key.
@ -150845,7 +150860,7 @@ class CacheConfig {
// The env vars are sorted, matched by prefix and hashed into the
// resulting environment hash.
let hasher = external_crypto_default().createHash("sha1");
const rustVersion = await getRustVersion();
const rustVersion = await getRustVersion(cmdFormat);
let keyRust = `${rustVersion.release} ${rustVersion.host}`;
hasher.update(keyRust);
hasher.update(rustVersion["commit-hash"]);
@ -150895,7 +150910,7 @@ class CacheConfig {
for (const workspace of workspaces) {
const root = workspace.root;
keyFiles.push(...(await globFiles(`${root}/**/.cargo/config.toml\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`)));
const workspaceMembers = await workspace.getWorkspaceMembers();
const workspaceMembers = await workspace.getWorkspaceMembers(cmdFormat);
const cargo_manifests = sort_and_uniq(workspaceMembers.map((member) => external_path_default().join(member.path, "Cargo.toml")));
for (const cargo_manifest of cargo_manifests) {
try {
@ -151071,8 +151086,8 @@ function isCacheUpToDate() {
function digest(hasher) {
return hasher.digest("hex").substring(0, HASH_LENGTH);
}
async function getRustVersion() {
const stdout = await getCmdOutput("rustc", ["-vV"]);
async function getRustVersion(cmdFormat) {
const stdout = await getCmdOutput(cmdFormat, "rustc -vV");
let splits = stdout
.split(/[\n\r]+/)
.filter(Boolean)

45
dist/save/index.js vendored
View file

@ -150675,11 +150675,12 @@ function reportError(e) {
core.error(`${e.stack}`);
}
}
async function getCmdOutput(cmd, args = [], options = {}) {
async function getCmdOutput(cmdFormat, cmd, options = {}) {
cmd = cmdFormat.replace("{0}", cmd);
let stdout = "";
let stderr = "";
try {
await exec.exec(cmd, args, {
await exec.exec(cmd, [], {
silent: true,
listeners: {
stdout(data) {
@ -150694,7 +150695,7 @@ async function getCmdOutput(cmd, args = [], options = {}) {
}
catch (e) {
e.commandFailed = {
command: `${cmd} ${args.join(" ")}`,
command: cmd,
stderr,
};
throw e;
@ -150742,11 +150743,12 @@ class Workspace {
this.root = root;
this.target = target;
}
async getPackages(filter, ...extraArgs) {
async getPackages(cmdFormat, filter, extraArgs) {
const cmd = "cargo metadata --all-features --format-version 1" + (extraArgs ? ` ${extraArgs}` : "");
let packages = [];
try {
core.debug(`collecting metadata for "${this.root}"`);
const meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1", ...extraArgs], {
const meta = JSON.parse(await getCmdOutput(cmdFormat, cmd, {
cwd: this.root,
env: { ...process.env, "CARGO_ENCODED_RUSTFLAGS": "" },
}));
@ -150761,11 +150763,11 @@ class Workspace {
}
return packages;
}
async getPackagesOutsideWorkspaceRoot() {
return await this.getPackages((pkg) => !pkg.manifest_path.startsWith(this.root));
async getPackagesOutsideWorkspaceRoot(cmdFormat) {
return await this.getPackages(cmdFormat, (pkg) => !pkg.manifest_path.startsWith(this.root));
}
async getWorkspaceMembers() {
return await this.getPackages((_) => true, "--no-deps");
async getWorkspaceMembers(cmdFormat) {
return await this.getPackages(cmdFormat, (_) => true, "--no-deps");
}
}
@ -150787,6 +150789,8 @@ const STATE_CONFIG = "RUST_CACHE_CONFIG";
const HASH_LENGTH = 8;
class CacheConfig {
constructor() {
/** A format string for running commands */
this.cmdFormat = "";
/** All the paths we want to cache */
this.cachePaths = [];
/** The primary cache key */
@ -150815,6 +150819,17 @@ class CacheConfig {
*/
static async new() {
const self = new CacheConfig();
let cmdFormat = core.getInput("cmd-format");
if (cmdFormat) {
const placeholderMatches = cmdFormat.match(/\{0\}/g);
if (!placeholderMatches || placeholderMatches.length !== 1) {
cmdFormat = "{0}";
}
}
else {
cmdFormat = "{0}";
}
self.cmdFormat = cmdFormat;
// Construct key prefix:
// This uses either the `shared-key` input,
// or the `key` input combined with the `job` key.
@ -150845,7 +150860,7 @@ class CacheConfig {
// The env vars are sorted, matched by prefix and hashed into the
// resulting environment hash.
let hasher = external_crypto_default().createHash("sha1");
const rustVersion = await getRustVersion();
const rustVersion = await getRustVersion(cmdFormat);
let keyRust = `${rustVersion.release} ${rustVersion.host}`;
hasher.update(keyRust);
hasher.update(rustVersion["commit-hash"]);
@ -150895,7 +150910,7 @@ class CacheConfig {
for (const workspace of workspaces) {
const root = workspace.root;
keyFiles.push(...(await globFiles(`${root}/**/.cargo/config.toml\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`)));
const workspaceMembers = await workspace.getWorkspaceMembers();
const workspaceMembers = await workspace.getWorkspaceMembers(cmdFormat);
const cargo_manifests = sort_and_uniq(workspaceMembers.map((member) => external_path_default().join(member.path, "Cargo.toml")));
for (const cargo_manifest of cargo_manifests) {
try {
@ -151071,8 +151086,8 @@ function isCacheUpToDate() {
function digest(hasher) {
return hasher.digest("hex").substring(0, HASH_LENGTH);
}
async function getRustVersion() {
const stdout = await getCmdOutput("rustc", ["-vV"]);
async function getRustVersion(cmdFormat) {
const stdout = await getCmdOutput(cmdFormat, "rustc -vV");
let splits = stdout
.split(/[\n\r]+/)
.filter(Boolean)
@ -151428,9 +151443,9 @@ async function run() {
const workspaceCrates = core.getInput("cache-workspace-crates").toLowerCase() || "false";
const allPackages = [];
for (const workspace of config.workspaces) {
const packages = await workspace.getPackagesOutsideWorkspaceRoot();
const packages = await workspace.getPackagesOutsideWorkspaceRoot(config.cmdFormat);
if (workspaceCrates === "true") {
const wsMembers = await workspace.getWorkspaceMembers();
const wsMembers = await workspace.getWorkspaceMembers(config.cmdFormat);
packages.push(...wsMembers);
}
allPackages.push(...packages);