Skip to content

Commit fff88a6

Browse files
authored
fix: make duplicate URL behavior check for inconsistency (#335)
* fix: make duplicate URL behavior check for inconsistency * fix: make errors easier to follow and resolve
1 parent 90ff0c9 commit fff88a6

File tree

6 files changed

+673
-95
lines changed

6 files changed

+673
-95
lines changed

docs/bibliography.md

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ The bibliography feature allows guideline authors to include references to exter
1313
Contributors can reference citations within guideline text using the `:cite:` role:
1414

1515
```rst
16-
As documented in :cite:`gui_Abc123XyzQrs:RUST-REF-UNION`, union types have specific safety requirements.
16+
As documented in :cite:`gui_Kx9mPq2nL7Yz:RUST-REF-UNION`, union types have specific safety requirements.
1717
```
1818

1919
**Format:** `:cite:`{guideline_id}:{CITATION-KEY}``
2020

21-
- `guideline_id` is the parent guideline's ID (e.g., `gui_Abc123XyzQrs`)
21+
- `guideline_id` is the parent guideline's ID (e.g., `gui_Kx9mPq2nL7Yz`)
2222
- `CITATION-KEY` is the citation key in UPPERCASE-WITH-HYPHENS format
2323

2424
The `:cite:` role renders as a clickable `[CITATION-KEY]` link that navigates to the bibliography entry.
@@ -29,23 +29,23 @@ Each guideline can include an optional bibliography section with entries defined
2929

3030
```rst
3131
.. guideline:: Union Field Validity
32-
:id: gui_Abc123XyzQrs
32+
:id: gui_Kx9mPq2nL7Yz
3333
...
3434
35-
As documented in :cite:`gui_Abc123XyzQrs:RUST-REF-UNION`, unions have requirements.
35+
As documented in :cite:`gui_Kx9mPq2nL7Yz:RUST-REF-UNION`, unions have requirements.
3636
3737
.. bibliography::
38-
:id: bib_Abc123XyzQrs
38+
:id: bib_Kx9mPq2nL7Yz
3939
:status: draft
4040
4141
.. list-table::
4242
:header-rows: 0
4343
:widths: auto
4444
:class: bibliography-table
4545
46-
* - :bibentry:`gui_Abc123XyzQrs:RUST-REF-UNION`
46+
* - :bibentry:`gui_Kx9mPq2nL7Yz:RUST-REF-UNION`
4747
- The Rust Reference. "Unions." https://doc.rust-lang.org/reference/items/unions.html
48-
* - :bibentry:`gui_Abc123XyzQrs:CERT-C-INT34`
48+
* - :bibentry:`gui_Kx9mPq2nL7Yz:CERT-C-INT34`
4949
- SEI CERT C Coding Standard. "INT34-C." https://wiki.sei.cmu.edu/confluence/x/ItcxBQ
5050
```
5151

@@ -55,7 +55,7 @@ The `:bibentry:` role creates an anchor that the `:cite:` role links to, and ren
5555

5656
### 3. Why Namespacing?
5757

58-
The guideline ID prefix (`gui_Abc123XyzQrs:`) is required to avoid conflicts when multiple guidelines use the same citation key (e.g., both guidelines might cite `RUST-REF-UNION`). The prefix ensures each citation anchor is unique across the entire documentation.
58+
The guideline ID prefix (`gui_Kx9mPq2nL7Yz:`) is required to avoid conflicts when multiple guidelines use the same citation key (e.g., both guidelines might cite `RUST-REF-UNION`). The prefix ensures each citation anchor is unique across the entire documentation.
5959

6060
When using `generate_guideline_templates.py`, the guideline ID is automatically included in all `:cite:` and `:bibentry:` roles.
6161

@@ -83,10 +83,16 @@ Citation keys must follow these rules:
8383

8484
The bibliography validator extension checks:
8585
- URL accessibility (HTTP status)
86-
- Duplicate URLs across guidelines
86+
- URL consistency across guidelines (same URL must use identical citation key and description)
8787
- Citation key format compliance
8888
- Citation references match definitions
8989

90+
**Duplicate URL Consistency:** When the same URL appears in multiple guidelines, all instances must use:
91+
- The same citation key (e.g., all use `RUST-REF-UNION`)
92+
- The same description text (e.g., all use `The Rust Reference. "Unions."`)
93+
94+
This ensures readers see consistent references throughout the documentation.
95+
9096
## Configuration
9197

9298
### conf.py Settings
@@ -101,8 +107,9 @@ bibliography_url_timeout = 10
101107
# Whether broken URLs should fail the build
102108
bibliography_fail_on_broken = True
103109

104-
# Whether duplicate URLs should fail the build
105-
bibliography_fail_on_duplicates = True
110+
# Whether inconsistent duplicate URLs should fail the build
111+
# (same URL with different citation keys or descriptions)
112+
bibliography_fail_on_inconsistent = True
106113
```
107114

108115
### Build Commands
@@ -200,35 +207,84 @@ The `guidelines-ids.json` now includes bibliography data. All IDs follow the sam
200207
}
201208
```
202209

203-
## Error Messages
210+
## Error Messages and Build Failures
211+
212+
All bibliography validation errors will **fail the build**. This ensures that invalid citations and mismatched guideline IDs are caught during development and CI.
213+
214+
The validation runs in two places:
215+
1. **During document parsing**: The `:cite:` and `:bibentry:` roles check format and log errors
216+
2. **During consistency check**: The `bibliography_validator` scans all guidelines and raises `BibliographyValidationError` if any errors are found
217+
218+
All error messages include copy-pasteable fixes to help contributors quickly resolve issues.
204219

205220
### Broken URL
206221
```
207-
WARNING: [bibliography_validator] Broken URL in src/gui_Foo.rst:
222+
WARNING: [bibliography_validator] Broken URL in src/gui_Rm4kWp8sN2Qx.rst:
208223
URL: https://example.com/broken
209-
Guideline: gui_Foo
224+
Guideline: gui_Rm4kWp8sN2Qx
210225
Error: HTTP 404
211226
Action: Update or remove this reference
212227
```
213228

214-
### Duplicate URL
229+
### Inconsistent Duplicate URL
215230
```
216-
WARNING: [bibliography_validator] Duplicate URL detected:
231+
ERROR: Inconsistent bibliography entry for URL:
217232
URL: https://doc.rust-lang.org/reference/items/unions.html
218-
Found in: gui_UnionFieldValidity, gui_UnionLayout
219-
Action: Consider if both guidelines need this reference
233+
Canonical entry (from gui_Kx9mPq2nL7Yz):
234+
Citation key: [RUST-REF-UNION]
235+
Description: The Rust Reference. "Unions."
236+
Guidelines with inconsistent entries: gui_Ht5vBn3mJ9Lw, gui_Qp8xZc4kR6Fy
237+
238+
Copy-paste fixes:
239+
In gui_Ht5vBn3mJ9Lw, replace the bibliography entry with:
240+
* - :bibentry:`gui_Ht5vBn3mJ9Lw:RUST-REF-UNION`
241+
- The Rust Reference. "Unions." https://doc.rust-lang.org/reference/items/unions.html
242+
243+
In gui_Qp8xZc4kR6Fy, replace the bibliography entry with:
244+
* - :bibentry:`gui_Qp8xZc4kR6Fy:RUST-REF-UNION`
245+
- The Rust Reference. "Unions." https://doc.rust-lang.org/reference/items/unions.html
220246
```
221247

222248
### Invalid Citation Key
223249
```
224-
ERROR: Invalid citation key format: 'lowercase-key'.
225-
Expected: UPPERCASE-WITH-HYPHENS (e.g., RUST-REF-UNION, CERT-C-INT34)
250+
ERROR: Invalid citation key in :cite: role (coding-guidelines/expressions/gui_Bob7x9KmPq2nL):
251+
Key: CERT-C-int34
252+
Must be UPPERCASE-WITH-HYPHENS (e.g., RUST-REF-UNION)
253+
Copy-paste fix: :cite:`gui_Bob7x9KmPq2nL:CERT-C-INT34`
226254
```
227255

228256
### Invalid Role Format
257+
258+
The error message automatically detects the guideline ID from context and includes it in the suggested fix:
259+
260+
```
261+
ERROR: Invalid :cite: format: "RUST-REF-UNION".
262+
Expected format: :cite:`gui_XxxYyyZzz:CITATION-KEY`
263+
Suggested fix: :cite:`gui_Kx9mPq2nL7Yz:RUST-REF-UNION`
264+
```
265+
266+
```
267+
ERROR: Invalid :bibentry: format: "[RUST-REF-UNION]".
268+
Expected format: :bibentry:`gui_XxxYyyZzz:CITATION-KEY`
269+
Suggested fix: :bibentry:`gui_Kx9mPq2nL7Yz:RUST-REF-UNION`
270+
```
271+
272+
### Guideline ID Mismatch
273+
274+
If the guideline ID in a role doesn't match the current guideline:
275+
276+
```
277+
ERROR: Guideline ID mismatch in :cite: role.
278+
Role uses: gui_Bib7x9KmPq2nL
279+
Current guideline: gui_Bob7x9KmPq2nL
280+
Copy-paste fix: :cite:`gui_Bob7x9KmPq2nL:RUST-REF-UNION`
281+
```
282+
229283
```
230-
ERROR: Invalid :cite: format: "RUST-REF-UNION".
231-
Expected format: :cite:`gui_XxxYyyZzz:CITATION-KEY`
284+
ERROR: Guideline ID mismatch in :bibentry: role.
285+
Role uses: gui_Bib7x9KmPq2nL
286+
Current guideline: gui_Bob7x9KmPq2nL
287+
Copy-paste fix: :bibentry:`gui_Bob7x9KmPq2nL:RUST-REF-UNION`
232288
```
233289

234290
## Migration
@@ -249,8 +305,8 @@ As documented in [RUST-REF-UNION], ...
249305

250306
**After:**
251307
```rst
252-
As documented in :cite:`gui_YourGuidelineId:RUST-REF-UNION`, ...
308+
As documented in :cite:`gui_Kx9mPq2nL7Yz:RUST-REF-UNION`, ...
253309
254-
* - :bibentry:`gui_YourGuidelineId:RUST-REF-UNION`
310+
* - :bibentry:`gui_Kx9mPq2nL7Yz:RUST-REF-UNION`
255311
- The Rust Reference...
256312
```

exts/coding_guidelines/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class CodingGuidelinesDomain(Domain):
2222
label = "Rust Standard Library"
2323
roles = {
2424
"std": std_role.StdRefRole(),
25+
"cite": citation_roles.CiteRole(),
26+
"bibentry": citation_roles.BibEntryRole(),
2527
}
2628
directives = {}
2729
object_types = {}

0 commit comments

Comments
 (0)