From 4ce02a3f77bb0225488f513c5f92e18abb29e144 Mon Sep 17 00:00:00 2001 From: "lithe-support[bot]" <356900028+lithe-support[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 03:09:19 +0000 Subject: [PATCH] fix(toolchain): ignore generated workspace ignore files (#1670) --- rust/contracts/rust-core-api.md | 10 +- rust/crates/toolchain/src/pin_sources.rs | 92 ++++++++++++++-- rust/fixtures/toolchain-pin-storage.json | 134 +++++++++++++++++++++-- 3 files changed, 211 insertions(+), 25 deletions(-) diff --git a/rust/contracts/rust-core-api.md b/rust/contracts/rust-core-api.md index fdb83f6d..1ea4ef58 100644 --- a/rust/contracts/rust-core-api.md +++ b/rust/contracts/rust-core-api.md @@ -8245,9 +8245,13 @@ entries reject with `plugin.toolchain.invalidDocument`. and returns `{status:"ok",edit:{document,documentChanged,ignoreDocument,ignoreChanged}}`. A semantic no-op preserves both exact sources, including absence. A changed pin document is serialized deterministically; the ignore matcher preserves -an already effective ignore rule or appends `/toolchains/local.json` after any -negation, preserving existing text and newline convention. Ignore parsing is -bounded to 4096 lines when preparing a changed pin document; NUL and invalid patterns reject with +already effective rules for `.gitignore` itself and `toolchains/local.json`, +appending only the missing `/.gitignore` and `/toolchains/local.json` rules after +any negation, in that order. These root-anchored rules do not hide team-shared +configuration or nested ignore files. Existing text, UTF-8 BOM and newline +convention are preserved; repeated maintenance does not append duplicate rules. +Ignore parsing is bounded to 4096 lines when preparing a changed pin document; +NUL and invalid patterns reject with `plugin.toolchain.invalidIgnore`. Input and output budgets reject with `plugin.toolchain.limitExceeded`; invalid request shapes use `plugin.toolchain.invalidParams`. The matcher never reads files or global rules. diff --git a/rust/crates/toolchain/src/pin_sources.rs b/rust/crates/toolchain/src/pin_sources.rs index 7389bba3..f75f7521 100644 --- a/rust/crates/toolchain/src/pin_sources.rs +++ b/rust/crates/toolchain/src/pin_sources.rs @@ -12,7 +12,7 @@ use serde_json::{json, Value}; const DOCUMENT_PATH: &str = ".lithe/toolchains/local.json"; const IGNORE_PATH: &str = ".lithe/.gitignore"; -const IGNORE_ENTRY: &str = "/toolchains/local.json"; +const IGNORE_ENTRIES: [&str; 2] = ["/.gitignore", "/toolchains/local.json"]; const MAX_DOCUMENT_BYTES: usize = 1024 * 1024; const MAX_IGNORE_BYTES: usize = 64 * 1024; const MAX_IGNORE_LINES: usize = 4096; @@ -163,33 +163,103 @@ fn prepare_ignore(original: Option<&str>) -> Validation<(String, bool)> { builder.add_line(None, line).map_err(|_| invalid_ignore())?; } let matcher = builder.build().map_err(|_| invalid_ignore())?; - if matcher - .matched_path_or_any_parents("toolchains/local.json", false) - .is_ignore() - { - return Ok((content.to_owned(), false)); - } let newline = if content.contains("\r\n") { "\r\n" } else { "\n" }; let mut updated = content.to_owned(); - if !updated.is_empty() && !updated.ends_with('\n') { + for entry in IGNORE_ENTRIES { + if matcher + .matched_path_or_any_parents(&entry[1..], false) + .is_ignore() + { + continue; + } + if !updated.is_empty() && !updated.ends_with('\n') { + updated.push_str(newline); + } + updated.push_str(entry); updated.push_str(newline); } - updated.push_str(IGNORE_ENTRY); - updated.push_str(newline); if updated.len() > MAX_IGNORE_BYTES || updated.lines().count() > MAX_IGNORE_LINES { return Err(limit("ignoreDocument")); } - Ok((updated, true)) + let changed = updated.len() != content.len(); + Ok((updated, changed)) } #[cfg(test)] mod tests { use super::*; + #[test] + fn generated_ignore_file_hides_itself_without_hiding_shared_configuration() { + let (content, changed) = prepare_ignore(None).unwrap(); + assert!(changed); + assert_eq!(content, "/.gitignore\n/toolchains/local.json\n"); + + let mut builder = GitignoreBuilder::new(""); + for line in content.lines() { + builder.add_line(None, line).unwrap(); + } + let matcher = builder.build().unwrap(); + for path in [".gitignore", "toolchains/local.json"] { + assert!(matcher.matched_path_or_any_parents(path, false).is_ignore()); + } + for path in [ + "run/shared.json", + "toolchains/shared.json", + "nested/.gitignore", + ] { + assert!(!matcher.matched_path_or_any_parents(path, false).is_ignore()); + } + assert_eq!(prepare_ignore(Some(&content)).unwrap(), (content, false)); + } + + #[test] + fn ignore_maintenance_preserves_text_and_only_appends_missing_effective_rules() { + for (before, appended) in [ + ("/toolchains/local.json\n", "/.gitignore\n"), + (".gitignore\n", "/toolchains/local.json\n"), + (".gitignore\n/toolchains/local.json\n", ""), + ("*\n", ""), + ("**/local.json\n", "/.gitignore\n"), + ( + "/.gitignore\n!/.gitignore\n/toolchains/local.json\n", + "/.gitignore\n", + ), + ("# user notes", "\n/.gitignore\n/toolchains/local.json\n"), + ("\u{feff}/toolchains/local.json\r\n", "/.gitignore\r\n"), + ("\u{feff}/.gitignore\r\n/toolchains/local.json\r\n", ""), + ] { + let expected = format!("{before}{appended}"); + let (content, changed) = prepare_ignore(Some(before)).unwrap(); + assert_eq!(content, expected, "source: {before:?}"); + assert_eq!(changed, !appended.is_empty(), "source: {before:?}"); + assert_eq!(prepare_ignore(Some(&content)).unwrap(), (content, false)); + } + } + + #[test] + fn self_ignore_growth_obeys_the_existing_byte_and_line_budgets() { + let at_line_limit = format!( + "/toolchains/local.json\n{}", + "#\n".repeat(MAX_IGNORE_LINES - 1) + ); + let at_byte_limit = format!( + "/toolchains/local.json\n#{}", + "x".repeat(MAX_IGNORE_BYTES - "/toolchains/local.json\n#".len()) + ); + assert_eq!(at_byte_limit.len(), MAX_IGNORE_BYTES); + for content in [at_line_limit, at_byte_limit] { + let Err(failure) = prepare_ignore(Some(&content)) else { + panic!("appending self-ignore must respect the source budget"); + }; + assert_eq!(failure.code(), "plugin.toolchain.limitExceeded"); + } + } + #[test] fn ignore_growth_cannot_publish_a_document_the_next_edit_would_refuse() { let full = "#\n".repeat(MAX_IGNORE_LINES); diff --git a/rust/fixtures/toolchain-pin-storage.json b/rust/fixtures/toolchain-pin-storage.json index 86f9a884..bf0429b6 100644 --- a/rust/fixtures/toolchain-pin-storage.json +++ b/rust/fixtures/toolchain-pin-storage.json @@ -189,7 +189,7 @@ "edit": { "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", "documentChanged": true, - "ignoreDocument": "/toolchains/local.json\n", + "ignoreDocument": "/.gitignore\n/toolchains/local.json\n", "ignoreChanged": true } } @@ -217,7 +217,7 @@ "edit": { "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", "documentChanged": true, - "ignoreDocument": "# user notes\n/toolchains/local.json\n", + "ignoreDocument": "# user notes\n/.gitignore\n/toolchains/local.json\n", "ignoreChanged": true } } @@ -245,13 +245,13 @@ "edit": { "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", "documentChanged": true, - "ignoreDocument": "/toolchains/local.json\n!/toolchains/local.json\n/toolchains/local.json\n", + "ignoreDocument": "/toolchains/local.json\n!/toolchains/local.json\n/.gitignore\n/toolchains/local.json\n", "ignoreChanged": true } } }, { - "name": "wildcard-already-ignores", + "name": "wildcard-already-ignores-pin-source", "command": "toolchains.editPinSources", "input": { "document": "{\"toolchains\":{},\"version\":1}", @@ -273,8 +273,8 @@ "edit": { "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", "documentChanged": true, - "ignoreDocument": "**/local.json\n", - "ignoreChanged": false + "ignoreDocument": "**/local.json\n/.gitignore\n", + "ignoreChanged": true } } }, @@ -301,7 +301,7 @@ "edit": { "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", "documentChanged": true, - "ignoreDocument": "# user notes\r\n/toolchains/local.json\r\n", + "ignoreDocument": "# user notes\r\n/.gitignore\r\n/toolchains/local.json\r\n", "ignoreChanged": true } } @@ -329,7 +329,7 @@ "edit": { "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", "documentChanged": true, - "ignoreDocument": "# user notes\n/toolchains/local.json\n", + "ignoreDocument": "# user notes\n/.gitignore\n/toolchains/local.json\n", "ignoreChanged": true } } @@ -357,8 +357,8 @@ "edit": { "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", "documentChanged": true, - "ignoreDocument": "/toolchains/local.json\n", - "ignoreChanged": false + "ignoreDocument": "/toolchains/local.json\n/.gitignore\n", + "ignoreChanged": true } } }, @@ -441,7 +441,119 @@ "edit": { "document": "{\"pins\":{\"nested\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{\"legacy\":{\"home\":\"/fixture/legacy\"}},\"version\":1}", "documentChanged": true, - "ignoreDocument": "/toolchains/local.json\n", + "ignoreDocument": "/toolchains/local.json\n/.gitignore\n", + "ignoreChanged": true + } + } + }, + { + "name": "self-ignore-already-effective", + "command": "toolchains.editPinSources", + "input": { + "document": "{\"toolchains\":{},\"version\":1}", + "ignoreDocument": ".gitignore\n/toolchains/local.json\n", + "directory": ".", + "edits": [ + { + "tool": "dev.example:compiler", + "pin": { + "home": "/fixture/tool", + "version": "1.0", + "provider": "host:system" + } + } + ] + }, + "expected": { + "status": "ok", + "edit": { + "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", + "documentChanged": true, + "ignoreDocument": ".gitignore\n/toolchains/local.json\n", + "ignoreChanged": false + } + } + }, + { + "name": "self-ignore-after-later-negation", + "command": "toolchains.editPinSources", + "input": { + "document": "{\"toolchains\":{},\"version\":1}", + "ignoreDocument": "/.gitignore\n!/.gitignore\n/toolchains/local.json\n", + "directory": ".", + "edits": [ + { + "tool": "dev.example:compiler", + "pin": { + "home": "/fixture/tool", + "version": "1.0", + "provider": "host:system" + } + } + ] + }, + "expected": { + "status": "ok", + "edit": { + "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", + "documentChanged": true, + "ignoreDocument": "/.gitignore\n!/.gitignore\n/toolchains/local.json\n/.gitignore\n", + "ignoreChanged": true + } + } + }, + { + "name": "both-ignored-by-existing-wildcard", + "command": "toolchains.editPinSources", + "input": { + "document": "{\"toolchains\":{},\"version\":1}", + "ignoreDocument": "*\n", + "directory": ".", + "edits": [ + { + "tool": "dev.example:compiler", + "pin": { + "home": "/fixture/tool", + "version": "1.0", + "provider": "host:system" + } + } + ] + }, + "expected": { + "status": "ok", + "edit": { + "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", + "documentChanged": true, + "ignoreDocument": "*\n", + "ignoreChanged": false + } + } + }, + { + "name": "bom-crlf-rules-already-effective", + "command": "toolchains.editPinSources", + "input": { + "document": "{\"toolchains\":{},\"version\":1}", + "ignoreDocument": "/.gitignore\r\n/toolchains/local.json\r\n", + "directory": ".", + "edits": [ + { + "tool": "dev.example:compiler", + "pin": { + "home": "/fixture/tool", + "version": "1.0", + "provider": "host:system" + } + } + ] + }, + "expected": { + "status": "ok", + "edit": { + "document": "{\"pins\":{\".\":{\"dev.example:compiler\":{\"home\":\"/fixture/tool\",\"provider\":\"host:system\",\"version\":\"1.0\"}}},\"toolchains\":{},\"version\":1}", + "documentChanged": true, + "ignoreDocument": "/.gitignore\r\n/toolchains/local.json\r\n", "ignoreChanged": false } }