fix(filters): scope source filters per compilation unit#604
Closed
fix(filters): scope source filters per compilation unit#604
Conversation
Source filters installed via Filter::Util::Call::filter_add lived on a thread-local global stack and the "wasFilterInstalled" flag was never reset between files. As a result, a filter installed by file A leaked into any file A required. Concretely, `jcpan -t URI::git` failed because: use Spiffy -Base; # in Test/Base.pm calls Spiffy::spiffy_filter (which installs a regex filter that prepends "my $self = shift;" to every sub), then Spiffy's import calls Exporter::export(...), which lazily requires Exporter::Heavy. While Heavy.pm was being parsed, the Spiffy filter was still active and got applied to Heavy.pm's source, mangling heavy_export and producing the spurious 'my variable $self masks earlier declaration' warning at Exporter/Heavy.pm line 237. With heavy_export broken, `field` was never installed in Test::Base, so the next line field _filters => [qw(norm trim)]; parsed as two consecutive barewords and died with "syntax error near => [qw". Fix: treat the source-filter stack and the "installed during use" flag as part of the per-file compilation context. ModuleOperators now snapshots and resets that state before compiling a required/do'd file and restores it in finally, so filters installed by the caller cannot leak into the required file (and vice versa). Test plan: - `jcpan -t URI::git` now passes all 3 subtests - `make` (full unit-test suite) is green Generated with [Devin](https://cli.devin.ai/docs) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Owner
Author
|
Subsumed by #597 — unified into a single PR. #597 adopts this PR's cleaner design (wire-up at Verified after refactor:
#597 also includes:
|
7 tasks
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.
Summary
Source filters installed via
Filter::Util::Call::filter_addlived on a thread-local global stack and thewasFilterInstalledflag was never reset between files. As a result, a filter installed while compiling file A leaked into any file Arequire'd.This broke
jcpan -t URI::git:Test/Base.pmdoesuse Spiffy -Base;, which callsSpiffy::spiffy_filterto install a regex filter that prependsmy $self = shift;to every sub.importthen callsExporter::export(...), which lazilyrequiresExporter::Heavy.Heavy.pmwas being parsed, the Spiffy filter was still active and got applied to it, manglingheavy_exportand producing the spurious"my" variable $self masks earlier declarationwarning atExporter/Heavy.pmline 237.heavy_exportbroken,fieldwas never installed inTest::Base, and the next linefield _filters => [qw(norm trim)];parsed as two consecutive barewords and died withsyntax error near "=> [qw".Fix
Treat the source-filter stack and the "installed during use" flag as part of the per-file compilation context:
FilterUtilCall.java: addFilterStateSnapshot,saveAndResetFilterState(),restoreFilterState().ModuleOperators.java: snapshot and clear the filter state before compiling arequire/do'd file and restore it infinally, so filters installed by the caller cannot leak into the required file (and vice versa).Test plan
jcpan -t URI::gitpasses all 3 subtests (was: syntax error inTest::Base)make(full unit-test suite) greenGenerated with Devin