-
Notifications
You must be signed in to change notification settings - Fork 3
ci: have the generator declare which paths it owns, and check it wrote them #392
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
Open
thedavidmeister
wants to merge
3
commits into
main
Choose a base branch
from
codegen-witness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| name: codegen-declaration | ||
| description: >- | ||
| Fails `rainix-copy-artifacts` when a codegen hook declared a generated path it then did not write. Re-running the generators and diffing proves the committed CONTENT is current, but a generator that has STOPPED emitting a file writes nothing, so the already-correct committed copy is left alone, nothing differs and the job is green over a dead emitter (rainlanguage/rain.factory.deploy#35). Only the generator knows which paths it owns rather than deliberately leaves frozen, so it declares them on stdout — `rainix-codegen owns <path>` and `rainix-codegen wrote <path>` — and the workflow tees that into the log this reads. A repo whose hooks declare nothing is a no-op. | ||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Check the codegen declaration | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| # path: ref runs the check from THIS composite's own checkout, so the | ||
| # check version always matches the action version (same pattern as | ||
| # mutation-ledger) regardless of any RAINIX_SHA the caller pins. | ||
| nix run "path:$(cd "$GITHUB_ACTION_PATH/../../.." && pwd)#rainix-static" -- \ | ||
| codegen-declaration --log "$RUNNER_TEMP/rainix-codegen.log" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| use std::collections::BTreeSet; | ||
| use std::path::Path; | ||
|
|
||
| /// Line prefix a codegen hook prints to declare a path. `forge script` indents | ||
| /// `console.log` output under `== Logs ==`, so lines are matched trimmed. | ||
| pub(crate) const SENTINEL: &str = "rainix-codegen "; | ||
|
|
||
| #[derive(Default, PartialEq, Eq, Debug)] | ||
| pub(crate) struct Declaration { | ||
| pub(crate) owns: BTreeSet<String>, | ||
| pub(crate) wrote: BTreeSet<String>, | ||
| pub(crate) malformed: Vec<String>, | ||
| } | ||
|
|
||
| pub(crate) fn parse(log: &str) -> Declaration { | ||
| let mut out = Declaration::default(); | ||
| for line in log.lines() { | ||
| let Some(rest) = line.trim().strip_prefix(SENTINEL) else { | ||
| continue; | ||
| }; | ||
| match rest.split_once(char::is_whitespace) { | ||
| Some(("owns", path)) if !path.trim().is_empty() => { | ||
| out.owns.insert(path.trim().to_string()); | ||
| } | ||
| Some(("wrote", path)) if !path.trim().is_empty() => { | ||
| out.wrote.insert(path.trim().to_string()); | ||
| } | ||
| // Fail-closed: a verb this check does not know is a declaration it | ||
| // is silently not making, which is the defect one level up. | ||
| _ => out.malformed.push(line.trim().to_string()), | ||
| } | ||
| } | ||
| out | ||
| } | ||
|
|
||
| pub(crate) fn offences( | ||
| owns: &BTreeSet<String>, | ||
| wrote: &BTreeSet<String>, | ||
| present: &BTreeSet<String>, | ||
| ) -> Vec<String> { | ||
| let mut out = Vec::new(); | ||
| for path in owns { | ||
| match (wrote.contains(path), present.contains(path)) { | ||
| (false, true) => out.push(format!( | ||
| "the codegen hooks declare {path} generated, but nothing wrote it on this run. \ | ||
| The committed copy is left exactly as it was, so regenerating and diffing \ | ||
| passes without ever checking it — its emitter is dead. Restore the emitter, or \ | ||
| stop declaring the path if it is genuinely no longer generated." | ||
| )), | ||
| (false, false) => out.push(format!( | ||
| "the codegen hooks declare {path} generated, but nothing wrote it and no such \ | ||
| file exists after the run." | ||
| )), | ||
| (true, false) => out.push(format!( | ||
| "a codegen hook reported writing {path}, but no such file exists after the run." | ||
| )), | ||
| (true, true) => {} | ||
| } | ||
| } | ||
| out | ||
| } | ||
|
|
||
| pub(crate) fn run(root: &Path, log: &Path) { | ||
| let text = match std::fs::read_to_string(log) { | ||
| Ok(text) => text, | ||
| Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(), | ||
| Err(e) => crate::fail(&format!( | ||
| "codegen-declaration: failed to read {}: {e}", | ||
| log.display() | ||
| )), | ||
| }; | ||
| let declaration = parse(&text); | ||
|
|
||
| if !declaration.malformed.is_empty() { | ||
| for line in &declaration.malformed { | ||
| eprintln!("::error::codegen-declaration: unreadable declaration: {line}"); | ||
| } | ||
| std::process::exit(1); | ||
| } | ||
|
|
||
| if declaration.owns.is_empty() { | ||
| println!( | ||
| "codegen-declaration: clean — this repo's codegen declares no generated paths, so \ | ||
| nothing here can tell a live emitter from one that has died" | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| let present: BTreeSet<String> = declaration | ||
| .owns | ||
| .union(&declaration.wrote) | ||
| .filter(|path| root.join(path).exists()) | ||
| .cloned() | ||
| .collect(); | ||
| let offences = offences(&declaration.owns, &declaration.wrote, &present); | ||
|
|
||
| if !offences.is_empty() { | ||
| for line in &offences { | ||
| eprintln!("::error::codegen-declaration: {line}"); | ||
| } | ||
| std::process::exit(1); | ||
| } | ||
|
|
||
| println!( | ||
| "codegen-declaration: clean — {} declared generated paths, each written this run", | ||
| declaration.owns.len() | ||
| ); | ||
| for path in declaration.wrote.difference(&declaration.owns) { | ||
| println!("codegen-declaration: note — {path} was written but not declared, so nothing will notice if its emitter dies"); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn set(paths: &[&str]) -> BTreeSet<String> { | ||
| paths.iter().map(|p| p.to_string()).collect() | ||
| } | ||
|
|
||
| #[test] | ||
| fn forge_indented_lines_parse_into_both_sets() { | ||
| let log = "== Logs ==\n rainix-codegen owns src/a.sol\n rainix-codegen wrote src/a.sol\n"; | ||
| let d = parse(log); | ||
| assert_eq!(d.owns, set(&["src/a.sol"])); | ||
| assert_eq!(d.wrote, set(&["src/a.sol"])); | ||
| assert!(d.malformed.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_line_that_merely_mentions_the_sentinel_is_not_a_declaration() { | ||
| let d = parse("error: expected `rainix-codegen owns src/a.sol`\n"); | ||
| assert_eq!(d, Declaration::default()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn repeated_declarations_of_one_path_are_one_path() { | ||
| let d = parse("rainix-codegen wrote src/a.sol\nrainix-codegen wrote src/a.sol\n"); | ||
| assert_eq!(d.wrote, set(&["src/a.sol"])); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_unknown_verb_is_malformed_rather_than_ignored() { | ||
| let d = parse("rainix-codegen skipped src/a.sol\n"); | ||
| assert_eq!(d.malformed, vec!["rainix-codegen skipped src/a.sol"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_verb_with_no_path_is_malformed() { | ||
| let d = parse("rainix-codegen owns\nrainix-codegen wrote \n"); | ||
| assert_eq!(d.malformed.len(), 2); | ||
| assert!(d.owns.is_empty()); | ||
| assert!(d.wrote.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_declared_path_nothing_wrote_is_an_offence_though_it_is_on_disk() { | ||
| let owns = set(&["src/lib/LibReleasedSuites.sol", "src/generated/A.sol"]); | ||
| let wrote = set(&["src/generated/A.sol"]); | ||
| let off = offences(&owns, &wrote, &owns); | ||
| assert_eq!(off.len(), 1); | ||
| assert!(off[0].contains("src/lib/LibReleasedSuites.sol")); | ||
| assert!(off[0].contains("emitter is dead")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_declared_path_that_is_not_on_disk_says_so_instead() { | ||
| let owns = set(&["src/generated/Gone.sol"]); | ||
| let off = offences(&owns, &BTreeSet::new(), &BTreeSet::new()); | ||
| assert_eq!(off.len(), 1); | ||
| assert!(off[0].contains("no such file exists")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_path_reported_written_that_is_not_on_disk_is_an_offence() { | ||
| let all = set(&["src/a.sol"]); | ||
| let off = offences(&all, &all, &BTreeSet::new()); | ||
| assert_eq!(off.len(), 1); | ||
| assert!(off[0].contains("reported writing")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn every_declared_path_written_is_clean() { | ||
| let all = set(&["src/a.sol", "src/b.sol"]); | ||
| assert!(offences(&all, &all, &all).is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_written_but_undeclared_path_is_not_an_offence() { | ||
| let owns = set(&["src/a.sol"]); | ||
| let wrote = set(&["src/a.sol", "soldeer.lock"]); | ||
| assert!(offences(&owns, &wrote, &wrote).is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn declaring_nothing_is_not_an_offence() { | ||
| let wrote = set(&["src/a.sol"]); | ||
| assert!(offences(&BTreeSet::new(), &wrote, &wrote).is_empty()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: rainlanguage/rainix
Length of output: 14146
🏁 Script executed:
Repository: rainlanguage/rainix
Length of output: 18708
🏁 Script executed:
Repository: rainlanguage/rainix
Length of output: 27036
🤖 get_repo_knowledge executed:
get_repo_knowledge rainlanguage/rainix /tmp/coderabbit-repo-knowledge/rainlanguage-rainix-96e9c804/architecture /tmp/coderabbit-repo-knowledge/rainlanguage-rainix-96e9c804/learningsLength of output: 38179
Constrain declaration paths to the repository.
root.join(path).exists()allows an absolute path to replacerootand allows..components to traverse outside it. A hook can then reportownsandwrotefor an existing external target, so this check passes without checking a repository artifact.Accept only normalized relative paths. Reject root, prefix, and parent components before checking existence. The declaration check is a trusted generator-output consistency check, not a security boundary, and the repository contract does not establish a separate requirement to reject resolved symlink targets.
🤖 Prompt for AI Agents