Repository navigation
Enable Style/FrozenStringLiteralComment - #39
Merged
Merged
Conversation
Remove the .rubocop.yml entry that disabled Style/FrozenStringLiteralComment so the cop runs with its default EnforcedStyle (always). Add "# frozen_string_literal: true" to the 8 files the cop flagged: Gemfile, Rakefile, packs-specification.gemspec, lib/packs-specification.rb, lib/packs/pack.rb, lib/packs/rspec/support.rb, lib/packs/specification.rb and lib/packs/specification/configuration.rb. In files with a Sorbet sigil the pragma goes on the line after "# typed:", matching lib/packs/rspec/fixture_helper.rb. Drop the now-redundant .freeze on Packs::PACKAGE_FILE (Style/RedundantFreeze). The constant is still frozen via the pragma. No mutation fixes were needed. Specs pass on Ruby 3.3, 3.4 and 4.0 with -W:deprecated and produce no chilled-string warnings. A static sweep of lib/, bin/, spec/, the Gemfile, the Rakefile and the gemspec found no in-place mutation of string literals.
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.
Why
With
# frozen_string_literal: true, an accidental in-place mutation of a string literal raisesFrozenErrorright where it happens. Without it, the mutation silently changes shared state. The pragma also lets Ruby deduplicate literals, so the same literal is not allocated again on every evaluation. Ruby 3.4+ already warns about mutating chilled literals, and this change settles the question for this gem ahead of that default flipping.Config change
.rubocop.yml: removed the block that disabled the cop, together with its comment ("Disabling for now until it's clearer why we want this"):The cop now runs with its default
EnforcedStyle: always. NoExcludeentries were needed.Files that got the pragma
The pragma was added to 8 files:
Gemfile,Rakefile,packs-specification.gemspec,lib/packs-specification.rb,lib/packs/pack.rb,lib/packs/specification.rb,lib/packs/specification/configuration.rbandlib/packs/rspec/support.rb.bin/*,spec/*andlib/packs/rspec/fixture_helper.rbalready had it.# typed:stays on line 1 and the pragma goes on line 2, the same layoutfixture_helper.rbalready uses. I confirmed Ruby honors the pragma on line 2 on 3.3, 3.4 and 4.0.sorbet/rbi/todo.rbiis generated, has no string literals, and rubocop doesn't inspect it, so it was left alone.Runtime fixes
None were needed. No spec or dynamic check found any code that mutates a literal that is now frozen.
The only code change besides the pragmas:
lib/packs-specification.rb:14: dropped the now-redundant.freezefromPACKAGE_FILE = T.let('package.yml', String), which the pragma makesStyle/RedundantFreeze. The constant is still frozen (checkedPacks::PACKAGE_FILE.frozen?on 3.3, 3.4 and 4.0).One visible behavior change, left in on purpose:
lib/packs/specification/configuration.rb:30:DEFAULT_PACK_PATHS.dupis a shallow copy. So when there is nopacks.yml, or it has nopack_pathskey,Configuration.fetch.pack_pathsnow returns frozen strings.DEFAULT_PACK_PATHS, and that was already true onmain. So onmain, a caller that mutated one would have silently corrupted the shared default. Now that raisesFrozenErrorinstead.packsonly passes them toFile.fnmatch,Dir.globand interpolation.packs.ymlstill come back unfrozen, as before.Verification
main(Ruby 4.0.5): 28 examples, 0 failures.Pack#inspect.RUBYOPT=-W:deprecatedand printed no chilled-string warnings.RUBYOPT=--enable-frozen-string-literalwith aTracePoint(:raise)hook that records everyFrozenError, including rescued ones. It recorded none.Style/RedundantFreezeare clean. The full run reports the same 6 pre-existing offenses in 3 files asmain(Naming/PredicatePrefixonis_gem?and related directive cops,Style/AccessModifierDeclarations,Layout/EmptyLinesAfterModuleInclusion,Style/RegexpLiteral). None of them come from this change, and CI doesn't run rubocop.srb tc: "No errors!"<<,concat, bang methods,[]=,force_encoding,replace, buffer arguments,StringIO) and traced every string literal to where it is used, including library calls (Pathname, Dir.mktmpdir, YAML, sorbet-runtimeT::Struct, RubyGems setters, Bundler DSL). Nothing mutates a literal.skip_chdir_to_tmpdir,delete_app_file, the defaultwrite_filecontent, andfor_filegiven a frozen String or a Pathname. I ran each of those directly.mainand this branch on 3.3, 3.4 and 4.0. The only difference is thepack_pathschange described above.gem build,rake build,rake -T,Gem::Specification.load+validate,bin/rubocop --version, and pipedbin/console.lib: packs (119), packs-rails (7), rubocop-packs (90), pack_stats (19), query_packwerk (60), danger-packwerk (67) and visualize_packs (181). All passed with no failures.origin/main): 0 blockers, 0 major, 0 minor, 0 info.