Fix auto-link not updating BibEntry when associated files are found (#15084)#15124
Fix auto-link not updating BibEntry when associated files are found (#15084)#15124shubh1978 wants to merge 39 commits intoJabRef:mainfrom
Conversation
|
Hey @shubh1978! 👋 Thank you for contributing to JabRef! We have automated checks in place, based on which you will soon get feedback if any of them are failing. We also use Qodo for review assistance. It will update your pull request description with a review help and offer suggestions to improve the pull request. After all automated checks pass, a maintainer will also review your contribution. Once that happens, you can go through their comments in the "Files changed" tab and act on them, or reply to the conversation if you have further inputs. You can read about the whole pull request process in our contribution guide. Please ensure that your pull request is in line with our AI Usage Policy and make necessary disclosures. |
Review Summary by QodoFix auto-link not updating BibEntry when associated files found
WalkthroughsDescription• Fixed auto-link not updating BibEntry file list when associated files found • Ensured LinkedFile objects are updated instead of replaced during repair • Added explicit file field update mechanism in UI thread execution • Improved file discovery logic to properly handle directory traversal • Added fallback AutoLinkPreferences initialization for robustness Diagramflowchart LR
A["Auto-link Action"] -->|"Execute with preferences"| B["AutoSetFileLinksUtil"]
B -->|"Find associated files"| C["File Discovery"]
C -->|"Match files by citation key"| D["LinkedFile Objects"]
D -->|"Update or replace existing"| E["BibEntry File List"]
E -->|"Set field + notify UI"| F["UI Update"]
File Changes1. jabgui/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java
|
✅ All tests passed ✅🏷️ Commit: fbf5207 Learn more about TestLens at testlens.app. |
Code Review by Qodo
✅ 1.
|
| System.out.println("DEBUG citationKey = " + citationKey); | ||
| System.out.println("DEBUG found file = " + path); | ||
| System.out.println("DEBUG basename = " + FileUtil.getBaseName(path)); | ||
|
|
There was a problem hiding this comment.
1. system.out.println debug logs 📘 Rule violation ⛨ Security
New code writes unstructured debug output to stdout and includes file paths/citation keys, which can leak sensitive user/library information and is not compliant with secure logging practices. Replace these statements with the project logger (or remove them) and avoid logging sensitive values.
Agent Prompt
## Issue description
New code uses `System.out.println(...)` for debugging and prints potentially sensitive information (citation keys, filesystem paths). This violates secure logging expectations and bypasses log levels/formatting.
## Issue Context
JabRef should use its logging framework (e.g., `LOGGER.debug/info/warn/error`) and avoid emitting sensitive data in logs. Stdout prints are also typically disallowed by style/lint checks.
## Fix Focus Areas
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[158-159]
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[181-182]
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[204-206]
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[214-216]
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[332-335]
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[381-383]
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java[67-68]
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java[106-108]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| try { | ||
|
|
||
| Path baseDir = directories.get(0); | ||
|
|
||
| relativePath = baseDir.relativize(associatedFile); | ||
|
|
||
| } catch (Exception e) { | ||
|
|
||
| relativePath = associatedFile; | ||
|
|
||
| } |
There was a problem hiding this comment.
2. Broad catch (exception e) 📘 Rule violation ⛯ Reliability
The new catch (Exception e) is overly broad and silently degrades behavior by swallowing the exception without logging context. This makes failures harder to diagnose and violates the requirement for specific, properly-logged exception handling.
Agent Prompt
## Issue description
`buildLinkedFileFromPath` introduces `catch (Exception e)` and swallows it without logging. This is overly broad and hides real failures.
## Issue Context
The method uses `directories.get(0)` and `Path.relativize`, both of which can fail for specific reasons. Compliance requires catching specific exceptions and logging them properly (exception object passed as the last argument).
## Fix Focus Areas
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[369-379]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| List<Path> dirs = new ArrayList<>(databaseContext.getFileDirectories(filePreferences)); | ||
|
|
||
| databaseContext.getDatabasePath().ifPresent(dbPath -> { | ||
| Path parent = dbPath.getParent(); | ||
| if (parent != null) { | ||
| try (Stream<Path> walk = Files.walk(parent)) { | ||
| walk.filter(Files::isDirectory) | ||
| .forEach(dir -> { | ||
| if (!dirs.contains(dir)) { | ||
| dirs.add(dir); | ||
| } | ||
| }); | ||
| } catch (IOException e) { | ||
| LOGGER.error("Error walking directories", e); | ||
| } | ||
| } | ||
| }); |
There was a problem hiding this comment.
3. Recursive directory explosion 🐞 Bug ➹ Performance
The AutoSetFileLinksUtil constructor now walks the database parent directory and adds every subdirectory, which can be huge and redundant. This runs on the JavaFX/UI thread in the menu action and can severely degrade performance when combined with recursive file searching.
Agent Prompt
### Issue description
`AutoSetFileLinksUtil` constructor currently walks the entire database parent tree and adds every subdirectory as a search root. This can be extremely slow and redundant, and in `AutoLinkFilesAction.execute` it happens on the JavaFX/UI thread.
### Issue Context
Existing `FileFinder` implementations already recurse within each configured directory root (e.g., `Files.find(..., Integer.MAX_VALUE, ...)`). Adding all subdirectories as additional roots multiplies traversal work.
### Fix Focus Areas
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[66-112]
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java[52-76]
- jablib/src/main/java/org/jabref/logic/util/io/CitationKeyBasedFileFinder.java[74-92]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| for (LinkedFile existing : currentFiles) { | ||
|
|
||
| Optional<LinkedFile> replacement = | ||
| foundFiles.values().stream() | ||
| .filter(newFile -> | ||
| FileUtil.getBaseName(newFile.getLink()) | ||
| .equals(FileUtil.getBaseName(existing.getLink()))) | ||
| .findFirst(); | ||
|
|
||
| if (replacement.isPresent()) { | ||
|
|
||
| LinkedFile newFile = replacement.get(); | ||
|
|
||
| System.out.println("DEBUG: REPLACING FILE → " + existing.getLink()); | ||
|
|
||
| updatedFiles.add(newFile); | ||
|
|
||
| updated = true; | ||
|
|
||
| } else { | ||
|
|
||
| updatedFiles.add(existing); | ||
|
|
||
| } |
There was a problem hiding this comment.
4. Overwrites valid file links 🐞 Bug ✓ Correctness
Auto-link now replaces existing linked files purely by basename match, without checking whether the existing link is broken or ensuring correct matching rules. This can overwrite a valid user-selected link when another file with the same basename is found.
Agent Prompt
### Issue description
`doLinkAssociatedFiles` currently replaces existing linked files based solely on basename matching, without checking if the existing link is broken. This can overwrite correct user links and violates the intended broken-link repair behavior.
### Issue Context
The codebase contains explicit broken-link-only behavior (`BrokenLinkedFileNameBasedFileFinder`) and repair logic with extension matching (`autoLinkBrokenLinkedFiles`). Unit tests expect that copies do not trigger relinking.
### Fix Focus Areas
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[149-225]
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[234-258]
- jablib/src/main/java/org/jabref/logic/util/io/BrokenLinkedFileNameBasedFileFinder.java[59-64]
- jabgui/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java[633-660]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
|
|
||
| String fileType = checkAndGetFileType(associatedFile); | ||
|
|
||
| // CRITICAL FIX: use RELATIVE TO DATABASE DIRECTORY ONLY | ||
| Path relativePath; | ||
|
|
||
| try { | ||
|
|
||
| Path baseDir = directories.get(0); | ||
|
|
||
| relativePath = baseDir.relativize(associatedFile); | ||
|
|
||
| } catch (Exception e) { | ||
|
|
||
| relativePath = associatedFile; | ||
|
|
||
| } | ||
|
|
||
| System.out.println("DEBUG RELATIVE PATH = " + relativePath); | ||
|
|
||
| return new LinkedFile( | ||
| "", | ||
| relativePath, | ||
| fileType | ||
| ); |
There was a problem hiding this comment.
5. Bad path relativization 🐞 Bug ✓ Correctness
Auto-linked files now compute relative paths using directories.get(0).relativize(...), which may be unrelated to the found file’s actual base directory. This can produce incorrect relative paths or fall back to absolute paths, harming portability and link resolution.
Agent Prompt
### Issue description
`buildLinkedFileFromPath` now relativizes against `directories.get(0)` which is not guaranteed to be the correct base directory. This can generate incorrect relative paths or absolute paths.
### Issue Context
`FileUtil.relativize` already implements correct selection of the base directory by checking prefixes (and real paths). `BibDatabaseContext.getFileDirectories` order can put user/library metadata directories first.
### Fix Focus Areas
- jabgui/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java[362-388]
- jablib/src/main/java/org/jabref/logic/util/io/FileUtil.java[287-317]
- jablib/src/main/java/org/jabref/model/database/BibDatabaseContext.java[147-196]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
|
Your pull request conflicts with the target branch. Please merge with your code. For a step-by-step guide to resolve merge conflicts, see https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/resolving-a-merge-conflict-using-the-command-line. |
We fixed an issue where auto-linking files did not update the BibEntry file list when associated files were found. Fixes JabRef#15084
2aad931 to
6ed9f7b
Compare
|
Your pull request modified git submodules. Please follow our FAQ on submodules to fix. |
Closes #15084
PR Description
We fixed an issue where auto-linking files did not update the BibEntry file list when associated files were found.
Previously, the file finder correctly detected matching files but the BibEntry file field was not updated properly, causing the UI and saved library to not reflect the linked files. This fix ensures that linked files are correctly added or updated in the BibEntry so the UI and persistence layer reflect the changes.
Steps to test
test-support/src/manual-tests/issue-9798/example1/issue-9798_1.bib
Quality → Automatically set file links
Proof of issue being fixed
Verification screenshot (required by contribution guidelines)
Library with a single entry having author as myself and title as the issue number (#15084):
Checklist