packwerk-extensions 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 077e4a805c3061339f9c18e26d1768bbe3c3966e9e4c958b078ba1bcd0292c07
4
- data.tar.gz: c292cb8cb900a9746d20e426a94ca89fc2a55cea542662812cb14f450466ae55
3
+ metadata.gz: 39d34d165ec6218799d3416a91249917b0d5b64272af7a8ba60c66e3473876c5
4
+ data.tar.gz: 040f5a83a6fef23e26efef5149fce578329c5db46b9d8f3ebe0d380e56250683
5
5
  SHA512:
6
- metadata.gz: 601a651716181f5da48a302a7dd3b9a5eb3d46fa81a13ebd7138602aef8c11576344d724a2907b7bb950161ba11f0bb2a68207a9338de4b4e780c569320e8900
7
- data.tar.gz: 288a0af58508deebdc3904a4a716ff933c837f4ca1cf840ece8ef8f4509936dfc6839fa7737e0f6d5805a007a00803dcafde59054a8e4089a11228a1d4b2486e
6
+ metadata.gz: e6f3ac7caadd78432c6b5a45f593c63445e23bbc30784528b13c505175e4358f0efb0de65eeca41b6a049431d24855834d191b2a1b10892dda56016eb777e937
7
+ data.tar.gz: 7d4fbd68b612ce1cb185e574fca289bf8266133cda8f47a47d95a14adf098c5fa2805d6cdf15ba4c47287b5afcc4674328b7308e255c713cffc24b43c457cd8b
data/README.md CHANGED
@@ -56,9 +56,86 @@ Example:
56
56
  public_path: my/custom/path/
57
57
  ```
58
58
 
59
+ ### Defining public constants through sigil
60
+
61
+ > [!WARNING]
62
+ > This way of of defining the public API of a package should be considered WIP. It is not supported by all tooling in the RubyAtScale ecosystem, as @alexevanczuk pointed out in a [comment on the PR](https://github.com/rubyatscale/packwerk-extensions/pull/35#discussion_r1334331797):
63
+ >
64
+ > There are a couple of other places that will require changes related to this sigil. Namely, everything that is coupled to the public folder implementation of privacy.
65
+ >
66
+ > In the rubyatscale org:
67
+ >
68
+ > * pack_stats, example https://github.com/rubyatscale/pack_stats/blob/main/lib/pack_stats/private/metrics/public_usage.rb. (IMO though we can just remove this metric – it has never been useful)
69
+ > * Other places that mention public_path or app/public.
70
+ > * Org wide search for app/public link
71
+ > * Org wide search for public_path link
72
+ > * packs (the Rust port of packwerk – I could take this one over unless someone is interested in implementing whatever we come up with there
73
+
74
+
75
+
76
+ You may make individual files public withhin a private package by usage of a comment within the first 5 lines of the `.rb` file containing `pack_public: true`.
77
+
78
+ Example:
79
+
80
+ ```ruby
81
+ # pack_public: true
82
+ module Foo
83
+ class Update
84
+ end
85
+ end
86
+ ```
87
+ Now `Foo::Update` is considered public even though the `foo` package might be set to `enforce_private: (true || :strict)`.
88
+
89
+ It's important to note that when combining `public_api: true` with the declaration of `private_constants`,
90
+ `packwerk validate` will raise an exception if both are used for the same constant. This must be resolved by removing
91
+ the sigil from the `.rb` file or removing the constant from the list of `private_constants`.
92
+
93
+ If you are using rubocop, it may be configured in such a way that there must be an empty line after the magic keywords at the top of the file. Currently, this extension is not modifying rubocop in anyway so it does not recognize `public_pack: true` as a valid magic keyword option. That means placing it at the end of the magic keywords will throw a rubocop exception. However, you can place it first in the list to avoid an exception in rubocop.
94
+ ```
95
+ -----
96
+ # typed: ignore
97
+ # frozen_string_literal: true
98
+ # pack_public: true
99
+
100
+ class Foo
101
+ ...
102
+ end => Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
103
+
104
+ ------
105
+ # typed: ignore
106
+ # frozen_string_literal: true
107
+
108
+ # pack_public: true
109
+
110
+ class Foo
111
+ ...
112
+ end => Less than ideal. This won't raise an issue in rubocop, however, only the first 5 lines are scanned for the magic comment of public_pack so there is risk at it being missed. It also is requiring extra empty lines in the group of magic comments.
113
+
114
+ -----
115
+ # pack_public: true
116
+ # typed: ignore
117
+ # frozen_string_literal: true
118
+
119
+ class Foo
120
+ ...
121
+ end => Ideal solution. No exceptions from rubocop and very low risk of the magic comment being out of range since
122
+ ```
123
+
59
124
  ### Using specific private constants
60
125
  Sometimes it is desirable to only enforce privacy on a subset of constants in a package. You can do so by defining a `private_constants` list in your package.yml. Note that `enforce_privacy` must be set to `true` or `'strict'` for this to work.
61
126
 
127
+ ### Ignore strict mode for violation coming from specific path patterns
128
+ If you want to activate `'strict'` mode on you package but have a few privacy violations you know you will deal with later,
129
+ you can set a list of patterns to exclude.
130
+
131
+ ```yaml
132
+ enforce_privacy: strict
133
+ strict_privacy_ignored_patterns:
134
+ - engines/another_engine/test/**/*
135
+ ```
136
+
137
+ In this example, violations on constants of your engine referenced in those files `engines/another_engine/test/**/*` will not fail Packwerk checks.
138
+
62
139
  ### Package Privacy violation
63
140
  Packwerk thinks something is a privacy violation if you're referencing a constant, class, or module defined in the private implementation (i.e. not the public folder) of another package. We care about these because we want to make sure we only use parts of a package that have been exposed as public API.
64
141
 
@@ -12,6 +12,37 @@ module Packwerk
12
12
  include Packwerk::Checker
13
13
 
14
14
  VIOLATION_TYPE = T.let('privacy', String)
15
+ PUBLICIZED_SIGIL = T.let('pack_public: true', String)
16
+ PUBLICIZED_SIGIL_REGEX = T.let(/#.*pack_public:\s*true/, Regexp)
17
+ @publicized_locations = T.let({}, T::Hash[String, T::Boolean])
18
+
19
+ class << self
20
+ extend T::Sig
21
+
22
+ sig { returns(T::Hash[String, T::Boolean]) }
23
+ def publicized_locations
24
+ @publicized_locations
25
+ end
26
+
27
+ sig { params(location: String).returns(T::Boolean) }
28
+ def publicized_location?(location)
29
+ unless publicized_locations.key?(location)
30
+ publicized_locations[location] = check_for_publicized_sigil(location)
31
+ end
32
+
33
+ T.must(publicized_locations[location])
34
+ end
35
+
36
+ sig { params(location: String).returns(T::Boolean) }
37
+ def check_for_publicized_sigil(location)
38
+ content_contains_sigil?(File.readlines(location))
39
+ end
40
+
41
+ sig { params(lines: T::Array[String]).returns(T::Boolean) }
42
+ def content_contains_sigil?(lines)
43
+ T.must(lines[0..4]).any? { |l| l =~ PUBLICIZED_SIGIL_REGEX }
44
+ end
45
+ end
15
46
 
16
47
  sig { override.returns(String) }
17
48
  def violation_type
@@ -28,6 +59,7 @@ module Packwerk
28
59
  privacy_package = Package.from(constant_package)
29
60
 
30
61
  return false if privacy_package.public_path?(reference.constant.location)
62
+ return false if self.class.publicized_location?(reference.constant.location)
31
63
 
32
64
  privacy_option = privacy_package.enforce_privacy
33
65
  return false if enforcement_disabled?(privacy_option)
@@ -44,7 +76,14 @@ module Packwerk
44
76
  end
45
77
  def strict_mode_violation?(listed_offense)
46
78
  publishing_package = listed_offense.reference.constant.package
47
- publishing_package.config['enforce_privacy'] == 'strict'
79
+
80
+ return false unless publishing_package.config['enforce_privacy'] == 'strict'
81
+ return false if exclude_from_strict?(
82
+ publishing_package.config['strict_privacy_ignored_patterns'] || [],
83
+ Pathname.new(listed_offense.reference.relative_path).cleanpath
84
+ )
85
+
86
+ true
48
87
  end
49
88
 
50
89
  sig do
@@ -98,6 +137,13 @@ module Packwerk
98
137
 
99
138
  standard_message.chomp
100
139
  end
140
+
141
+ sig { params(globs: T::Array[String], path: Pathname).returns(T::Boolean) }
142
+ def exclude_from_strict?(globs, path)
143
+ globs.any? do |glob|
144
+ path.fnmatch(glob, File::FNM_EXTGLOB)
145
+ end
146
+ end
101
147
  end
102
148
  end
103
149
  end
@@ -11,6 +11,7 @@ module Packwerk
11
11
  const :enforce_privacy, T.nilable(T.any(T::Boolean, String))
12
12
  const :private_constants, T::Array[String]
13
13
  const :ignored_private_constants, T::Array[String]
14
+ const :strict_privacy_ignored_patterns, T::Array[String]
14
15
 
15
16
  sig { params(path: String).returns(T::Boolean) }
16
17
  def public_path?(path)
@@ -27,7 +28,8 @@ module Packwerk
27
28
  user_defined_public_path: user_defined_public_path(package),
28
29
  enforce_privacy: package.config['enforce_privacy'],
29
30
  private_constants: package.config['private_constants'] || [],
30
- ignored_private_constants: package.config['ignored_private_constants'] || []
31
+ ignored_private_constants: package.config['ignored_private_constants'] || [],
32
+ strict_privacy_ignored_patterns: package.config['strict_privacy_ignored_patterns'] || []
31
33
  )
32
34
  end
33
35
 
@@ -31,7 +31,7 @@ module Packwerk
31
31
 
32
32
  sig { override.returns(T::Array[String]) }
33
33
  def permitted_keys
34
- %w[public_path enforce_privacy private_constants ignored_private_constants]
34
+ %w[public_path enforce_privacy private_constants ignored_private_constants strict_privacy_ignored_patterns]
35
35
  end
36
36
 
37
37
  private
@@ -110,9 +110,8 @@ module Packwerk
110
110
  def check_private_constant_location(configuration, package_set, name, location, config_file_path)
111
111
  declared_package = package_set.package_from_path(relative_path(configuration, config_file_path))
112
112
  constant_package = package_set.package_from_path(location)
113
-
114
113
  if constant_package == declared_package
115
- Result.new(ok: true)
114
+ check_for_publicized_constant(location, constant_package, name)
116
115
  else
117
116
  Result.new(
118
117
  ok: false,
@@ -122,6 +121,23 @@ module Packwerk
122
121
  end
123
122
  end
124
123
 
124
+ sig { params(location: String, constant_package: Packwerk::Package, name: T.untyped).returns(Result) }
125
+ def check_for_publicized_constant(location, constant_package, name)
126
+ if Packwerk::Privacy::Checker.publicized_location?(location)
127
+ sigil = Packwerk::Privacy::Checker::PUBLICIZED_SIGIL
128
+ Result.new(
129
+ ok: false,
130
+ error_value: "'#{name}' is an explicitly publicized constant declared in #{location} through usage of " \
131
+ "'#{sigil}'. However, the package '#{constant_package}' is also declaring it as a private " \
132
+ "constant. This conflict must be resolved. Either remove '#{sigil}' from #{location} or " \
133
+ 'remove this constant from the list of private constants in the config for ' \
134
+ "'#{constant_package}'."
135
+ )
136
+ else
137
+ Result.new(ok: true)
138
+ end
139
+ end
140
+
125
141
  sig { params(constants: T.untyped, config_file_path: String).returns(T::Array[Result]) }
126
142
  def assert_constants_can_be_loaded(constants, config_file_path)
127
143
  constants.map do |constant|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: packwerk-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gusto Engineers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-23 00:00:00.000000000 Z
11
+ date: 2023-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: packwerk