Skip to content

Commit

Permalink
Only key by Cargo.toml and Cargo.lock files of workspace members (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
max-heller authored Dec 3, 2023
1 parent b1db5f9 commit d30f114
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 62 deletions.
48 changes: 28 additions & 20 deletions dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86960,6 +86960,7 @@ var cache_lib_cache = __nccwpck_require__(7799);




function reportError(e) {
const { commandFailed } = e;
if (commandFailed) {
Expand Down Expand Up @@ -87007,6 +87008,15 @@ function getCacheProvider() {
cache: cache,
};
}
async function utils_exists(path) {
try {
await external_fs_default().promises.access(path);
return true;
}
catch {
return false;
}
}

;// CONCATENATED MODULE: ./src/workspace.ts

Expand All @@ -87018,25 +87028,30 @@ class Workspace {
this.root = root;
this.target = target;
}
async getPackages() {
async getPackages(filter, ...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"], {
const meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1", ...extraArgs], {
cwd: this.root,
}));
lib_core.debug(`workspace "${this.root}" has ${meta.packages.length} packages`);
for (const pkg of meta.packages) {
if (pkg.manifest_path.startsWith(this.root)) {
continue;
}
for (const pkg of meta.packages.filter(filter)) {
const targets = pkg.targets.filter((t) => t.kind.some((kind) => SAVE_TARGETS.has(kind))).map((t) => t.name);
packages.push({ name: pkg.name, version: pkg.version, targets, path: external_path_default().dirname(pkg.manifest_path) });
}
}
catch { }
catch (err) {
console.error(err);
}
return packages;
}
async getPackagesOutsideWorkspaceRoot() {
return await this.getPackages(pkg => !pkg.manifest_path.startsWith(this.root));
}
async getWorkspaceMembers() {
return await this.getPackages(_ => true, "--no-deps");
}
}

;// CONCATENATED MODULE: ./src/config.ts
Expand Down Expand Up @@ -87152,7 +87167,8 @@ 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 cargo_manifests = sort_and_uniq(await globFiles(`${root}/**/Cargo.toml`));
const workspaceMembers = await workspace.getWorkspaceMembers();
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 {
const content = await promises_default().readFile(cargo_manifest, { encoding: "utf8" });
Expand Down Expand Up @@ -87193,8 +87209,8 @@ class CacheConfig {
keyFiles.push(cargo_manifest);
}
}
const cargo_locks = sort_and_uniq(await globFiles(`${root}/**/Cargo.lock`));
for (const cargo_lock of cargo_locks) {
const cargo_lock = external_path_default().join(workspace.root, "Cargo.lock");
if (await utils_exists(cargo_lock)) {
try {
const content = await promises_default().readFile(cargo_lock, { encoding: "utf8" });
const parsed = parse(content);
Expand Down Expand Up @@ -87357,6 +87373,7 @@ function sort_and_uniq(a) {




async function cleanTargetDir(targetDir, packages, checkTimestamp = false) {
lib_core.debug(`cleaning target directory "${targetDir}"`);
// remove all *files* from the profile directory
Expand All @@ -87365,7 +87382,7 @@ async function cleanTargetDir(targetDir, packages, checkTimestamp = false) {
if (dirent.isDirectory()) {
let dirName = external_path_default().join(dir.path, dirent.name);
// is it a profile dir, or a nested target dir?
let isNestedTarget = (await exists(external_path_default().join(dirName, "CACHEDIR.TAG"))) || (await exists(external_path_default().join(dirName, ".rustc_info.json")));
let isNestedTarget = (await utils_exists(external_path_default().join(dirName, "CACHEDIR.TAG"))) || (await utils_exists(external_path_default().join(dirName, ".rustc_info.json")));
try {
if (isNestedTarget) {
await cleanTargetDir(dirName, packages, checkTimestamp);
Expand Down Expand Up @@ -87638,15 +87655,6 @@ async function rmRF(dirName) {
core.debug(`deleting "${dirName}"`);
await io.rmRF(dirName);
}
async function exists(path) {
try {
await external_fs_default().promises.access(path);
return true;
}
catch {
return false;
}
}

;// CONCATENATED MODULE: ./src/restore.ts

Expand Down
48 changes: 28 additions & 20 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86960,6 +86960,7 @@ var cache_lib_cache = __nccwpck_require__(7799);




function reportError(e) {
const { commandFailed } = e;
if (commandFailed) {
Expand Down Expand Up @@ -87007,6 +87008,15 @@ function getCacheProvider() {
cache: cache,
};
}
async function exists(path) {
try {
await external_fs_default().promises.access(path);
return true;
}
catch {
return false;
}
}

;// CONCATENATED MODULE: ./src/workspace.ts

Expand All @@ -87018,25 +87028,30 @@ class Workspace {
this.root = root;
this.target = target;
}
async getPackages() {
async getPackages(filter, ...extraArgs) {
let packages = [];
try {
core.debug(`collecting metadata for "${this.root}"`);
const meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1"], {
const meta = JSON.parse(await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1", ...extraArgs], {
cwd: this.root,
}));
core.debug(`workspace "${this.root}" has ${meta.packages.length} packages`);
for (const pkg of meta.packages) {
if (pkg.manifest_path.startsWith(this.root)) {
continue;
}
for (const pkg of meta.packages.filter(filter)) {
const targets = pkg.targets.filter((t) => t.kind.some((kind) => SAVE_TARGETS.has(kind))).map((t) => t.name);
packages.push({ name: pkg.name, version: pkg.version, targets, path: external_path_default().dirname(pkg.manifest_path) });
}
}
catch { }
catch (err) {
console.error(err);
}
return packages;
}
async getPackagesOutsideWorkspaceRoot() {
return await this.getPackages(pkg => !pkg.manifest_path.startsWith(this.root));
}
async getWorkspaceMembers() {
return await this.getPackages(_ => true, "--no-deps");
}
}

;// CONCATENATED MODULE: ./src/config.ts
Expand Down Expand Up @@ -87152,7 +87167,8 @@ 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 cargo_manifests = sort_and_uniq(await globFiles(`${root}/**/Cargo.toml`));
const workspaceMembers = await workspace.getWorkspaceMembers();
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 {
const content = await promises_default().readFile(cargo_manifest, { encoding: "utf8" });
Expand Down Expand Up @@ -87193,8 +87209,8 @@ class CacheConfig {
keyFiles.push(cargo_manifest);
}
}
const cargo_locks = sort_and_uniq(await globFiles(`${root}/**/Cargo.lock`));
for (const cargo_lock of cargo_locks) {
const cargo_lock = external_path_default().join(workspace.root, "Cargo.lock");
if (await exists(cargo_lock)) {
try {
const content = await promises_default().readFile(cargo_lock, { encoding: "utf8" });
const parsed = parse(content);
Expand Down Expand Up @@ -87357,6 +87373,7 @@ function sort_and_uniq(a) {




async function cleanTargetDir(targetDir, packages, checkTimestamp = false) {
core.debug(`cleaning target directory "${targetDir}"`);
// remove all *files* from the profile directory
Expand Down Expand Up @@ -87638,15 +87655,6 @@ async function rmRF(dirName) {
core.debug(`deleting "${dirName}"`);
await io.rmRF(dirName);
}
async function exists(path) {
try {
await external_fs_default().promises.access(path);
return true;
}
catch {
return false;
}
}

;// CONCATENATED MODULE: ./src/save.ts

Expand Down Expand Up @@ -87678,7 +87686,7 @@ async function run() {
await macOsWorkaround();
const allPackages = [];
for (const workspace of config.workspaces) {
const packages = await workspace.getPackages();
const packages = await workspace.getPackagesOutsideWorkspaceRoot();
allPackages.push(...packages);
try {
core.info(`... Cleaning ${workspace.target} ...`);
Expand Down
10 changes: 1 addition & 9 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from "fs";
import path from "path";

import { CARGO_HOME } from "./config";
import { exists } from "./utils";
import { Packages } from "./workspace";

export async function cleanTargetDir(targetDir: string, packages: Packages, checkTimestamp = false) {
Expand Down Expand Up @@ -308,12 +309,3 @@ async function rmRF(dirName: string) {
core.debug(`deleting "${dirName}"`);
await io.rmRF(dirName);
}

async function exists(path: string) {
try {
await fs.promises.access(path);
return true;
} catch {
return false;
}
}
11 changes: 6 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path from "path";
import * as toml from "smol-toml";

import { getCargoBins } from "./cleanup";
import { CacheProvider, getCmdOutput } from "./utils";
import { CacheProvider, exists, getCmdOutput } from "./utils";
import { Workspace } from "./workspace";

const HOME = os.homedir();
Expand Down Expand Up @@ -142,7 +142,9 @@ export class CacheConfig {
)),
);

const cargo_manifests = sort_and_uniq(await globFiles(`${root}/**/Cargo.toml`));
const workspaceMembers = await workspace.getWorkspaceMembers();

const cargo_manifests = sort_and_uniq(workspaceMembers.map(member => path.join(member.path, "Cargo.toml")));

for (const cargo_manifest of cargo_manifests) {
try {
Expand Down Expand Up @@ -189,9 +191,8 @@ export class CacheConfig {
}
}

const cargo_locks = sort_and_uniq(await globFiles(`${root}/**/Cargo.lock`));

for (const cargo_lock of cargo_locks) {
const cargo_lock = path.join(workspace.root, "Cargo.lock");
if (await exists(cargo_lock)) {
try {
const content = await fs_promises.readFile(cargo_lock, { encoding: "utf8" });
const parsed = toml.parse(content);
Expand Down
2 changes: 1 addition & 1 deletion src/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function run() {

const allPackages = [];
for (const workspace of config.workspaces) {
const packages = await workspace.getPackages();
const packages = await workspace.getPackagesOutsideWorkspaceRoot();
allPackages.push(...packages);
try {
core.info(`... Cleaning ${workspace.target} ...`);
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as buildjetCache from "@actions/buildjet-cache";
import * as ghCache from "@actions/cache";
import fs from "fs";

export function reportError(e: any) {
const { commandFailed } = e;
Expand Down Expand Up @@ -61,3 +62,12 @@ export function getCacheProvider(): CacheProvider {
cache: cache,
};
}

export async function exists(path: string) {
try {
await fs.promises.access(path);
return true;
} catch {
return false;
}
}
21 changes: 14 additions & 7 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,33 @@ const SAVE_TARGETS = new Set(["lib", "proc-macro"]);
export class Workspace {
constructor(public root: string, public target: string) {}

public async getPackages(): Promise<Packages> {
async getPackages(filter: ((p: Meta['packages'][0]) => boolean), ...extraArgs: string[]): Promise<Packages> {
let packages: Packages = [];
try {
core.debug(`collecting metadata for "${this.root}"`);
const meta: Meta = JSON.parse(
await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1"], {
await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1", ...extraArgs], {
cwd: this.root,
}),
);
core.debug(`workspace "${this.root}" has ${meta.packages.length} packages`);
for (const pkg of meta.packages) {
if (pkg.manifest_path.startsWith(this.root)) {
continue;
}
for (const pkg of meta.packages.filter(filter)) {
const targets = pkg.targets.filter((t) => t.kind.some((kind) => SAVE_TARGETS.has(kind))).map((t) => t.name);
packages.push({ name: pkg.name, version: pkg.version, targets, path: path.dirname(pkg.manifest_path) });
}
} catch {}
} catch (err) {
console.error(err);
}
return packages;
}

public async getPackagesOutsideWorkspaceRoot(): Promise<Packages> {
return await this.getPackages(pkg => !pkg.manifest_path.startsWith(this.root));
}

public async getWorkspaceMembers(): Promise<Packages> {
return await this.getPackages(_ => true, "--no-deps");
}
}

export interface PackageDefinition {
Expand Down

0 comments on commit d30f114

Please sign in to comment.