fix: recognize sourced bash library usage in unused-import detector - #699
Open
daBOB wants to merge 1 commit into
Open
fix: recognize sourced bash library usage in unused-import detector#699daBOB wants to merge 1 commit into
daBOB wants to merge 1 commit into
Conversation
The bash unused-import check treated a source directive as used only when the sourced file's basename reappeared in the script body. Scripts that source a function library (source ./lib.sh) and then call its functions or read its variables never repeat the basename, so every such script was flagged as an unused import. Resolve the sourced path relative to the sourcing script, parse it, and collect the symbols it defines: function definitions in both name() and 'function name' forms plus top-level variable assignments, including export/declare wrappers while excluding function-local declarations. The import counts as used when any defined symbol is referenced in the script. Unresolvable paths keep the existing basename heuristic, and per-run caching avoids reparsing a library shared by many scripts.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The bash unused-import detector flags
source ./lib.shdirectives in scripts that demonstrably use the sourced library. Usage was recognized only when the sourced file's basename reappeared in the script body — but scripts that source a function library call its functions and read its variables; they never repeat the basename.Repro (project with 30 shell scripts, 28 of which
source ./lib-acceptance.shand then call functions it defines, e.g.require_server, or read variables like$API):Before this fix, all 28 sourcing scripts are flagged, e.g.:
...even though line 8 of that script calls
require_server, defined inscripts/lib-acceptance.sh. In other words the detector produced a false positive for essentially every bash script that sources a shared function library.Fix
In
detect_unused_imports, forspec.grammar == "bash": resolve the sourced path relative to the sourcing script (reusing the spec'sresolve_import/resolve_bash_source), parse the sourced file, and collect the symbols it defines:name() {}andfunction name {}forms (global regardless of nesting), andexport FOO=.../declare -r FOO=...wrappers, while excluding function-locallocal/declareassignments.The source directive counts as used when any defined symbol is referenced in the rest of the script. When the sourced path cannot be resolved (variable-based paths, missing files), the existing basename heuristic is kept, so current behavior — including all existing tests — is unchanged for unresolvable sources. Defined-symbol sets are cached per run since many scripts typically source the same library.
Verified on the repro project: 28
unused_importfindings before, 0 after; the remaining 111 work items from other detectors are unaffected. Full test suite passes (the two pre-existingtest_do_run_batches_dry_run_generates_packet_and_promptsfailures reproduce identically on a clean checkout ofmainin this environment).Note: while investigating I also checked a suspected companion issue — findings surviving rescans after their file is deleted. That is already handled on current
main(verify_disappearedauto-resolves with "source file no longer exists"); confirmed empirically by deleting a file with 11 open findings and rescanning.