bundler 4.1.0.beta2 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bundler/build_metadata.rb +2 -2
  3. data/lib/bundler/checksum.rb +8 -2
  4. data/lib/bundler/cli/clean.rb +19 -3
  5. data/lib/bundler/cli/exec.rb +1 -1
  6. data/lib/bundler/cli/install.rb +1 -1
  7. data/lib/bundler/cli/lock.rb +17 -1
  8. data/lib/bundler/cli/update.rb +1 -1
  9. data/lib/bundler/cli.rb +2 -0
  10. data/lib/bundler/definition.rb +170 -12
  11. data/lib/bundler/dsl.rb +40 -23
  12. data/lib/bundler/errors.rb +8 -0
  13. data/lib/bundler/fetcher/downloader.rb +15 -5
  14. data/lib/bundler/fetcher.rb +4 -1
  15. data/lib/bundler/index.rb +7 -2
  16. data/lib/bundler/inline.rb +16 -8
  17. data/lib/bundler/installer.rb +19 -0
  18. data/lib/bundler/lazy_specification.rb +42 -25
  19. data/lib/bundler/lockfile_generator.rb +27 -9
  20. data/lib/bundler/lockfile_parser.rb +38 -6
  21. data/lib/bundler/man/bundle-lock.1 +7 -1
  22. data/lib/bundler/man/bundle-lock.1.ronn +10 -0
  23. data/lib/bundler/man/bundle-plugin.1 +61 -0
  24. data/lib/bundler/man/bundle-plugin.1.ronn +84 -0
  25. data/lib/bundler/man/gemfile.5 +21 -10
  26. data/lib/bundler/man/gemfile.5.ronn +39 -17
  27. data/lib/bundler/match_platform.rb +15 -6
  28. data/lib/bundler/mirror.rb +7 -5
  29. data/lib/bundler/override.rb +60 -7
  30. data/lib/bundler/resolver.rb +3 -1
  31. data/lib/bundler/ruby_dsl.rb +2 -1
  32. data/lib/bundler/rubygems_ext.rb +56 -13
  33. data/lib/bundler/rubygems_integration.rb +2 -4
  34. data/lib/bundler/runtime.rb +24 -10
  35. data/lib/bundler/source/git/git_proxy.rb +18 -6
  36. data/lib/bundler/source/git.rb +0 -2
  37. data/lib/bundler/source/rubygems.rb +2 -2
  38. data/lib/bundler/source_map.rb +1 -1
  39. data/lib/bundler/spec_set.rb +26 -7
  40. data/lib/bundler/stub_specification.rb +4 -0
  41. data/lib/bundler/vendor/fileutils/lib/fileutils.rb +54 -29
  42. data/lib/bundler/version.rb +1 -1
  43. data/lib/bundler/worker.rb +4 -0
  44. data/lib/bundler.rb +1 -1
  45. data/lib/rubygems/compact_index_client/http_fetcher.rb +3 -1
  46. data/lib/rubygems/compact_index_client/parser.rb +1 -0
  47. data/lib/rubygems/vendor/uri/lib/uri/common.rb +1 -1
  48. data/lib/rubygems/vendor/uri/lib/uri/generic.rb +15 -3
  49. data/lib/rubygems/vendor/uri/lib/uri/version.rb +1 -1
  50. data/lib/rubygems/yaml_serializer.rb +247 -137
  51. metadata +2 -2
@@ -16,34 +16,37 @@ end
16
16
 
17
17
  module Gem
18
18
  module YAMLSerializer
19
- Scalar = Struct.new(:value, :tag, :anchor, keyword_init: true)
20
-
21
- Mapping = Struct.new(:pairs, :tag, :anchor, keyword_init: true) do
22
- def initialize(pairs: [], tag: nil, anchor: nil)
23
- super
24
- end
25
- end
26
-
27
- Sequence = Struct.new(:items, :tag, :anchor, keyword_init: true) do
28
- def initialize(items: [], tag: nil, anchor: nil)
29
- super
30
- end
31
- end
32
-
33
- AliasRef = Struct.new(:name, keyword_init: true)
19
+ Scalar = Struct.new(:value, :tag, :anchor)
20
+ Mapping = Struct.new(:pairs, :tag, :anchor)
21
+ Sequence = Struct.new(:items, :tag, :anchor)
22
+ AliasRef = Struct.new(:name)
34
23
 
35
24
  class Parser
36
25
  # A plain (unquoted) mapping key followed by ":". "#" inside a key is
37
26
  # literal unless preceded by a space (which would start a comment).
38
- MAPPING_KEY_RE = /^((?:[^#:\s]|:[^ ])(?:[^#:]|:[^ ]|(?<=[^ ])#)*):(?:[ ]+(.*))?$/
27
+ # Like Psych, spaces and tabs may separate the ":" from the value.
28
+ MAPPING_KEY_RE = /\G((?:[^#:\s]|:[^ \t])(?:[^#:]|:[^ \t]|(?<=[^ ])#)*):(?:[ \t]+(.*))?$/
39
29
  # A quoted mapping key followed by ":". A whole-line quoted scalar
40
30
  # cannot match because its closing quote is at the end of the line.
41
- QUOTED_KEY_RE = /^("(?:[^"\\]|\\.)*"|'(?:[^']|'')*'):(?:[ ]+(.*))?$/
31
+ QUOTED_KEY_RE = /\G("(?:[^"\\]|\\.)*"|'(?:[^']|'')*'):(?:[ \t]+(.*))?$/
32
+ # An entry of a flow sequence. A quoted entry may contain commas.
33
+ FLOW_SEQUENCE_ENTRY_RE = /"(?:[^"\\]|\\.)*"|'(?:[^']|'')*'|[^,\s](?:[^,]*[^,\s])?/
34
+ SEQUENCE_ENTRY_RE = /\G-(?: |\z)/
35
+ # A quoted scalar whose closing quote is on a later line.
36
+ OPEN_QUOTED_RE = /\A(?:! )?(?:"(?>(?:[^"\\]|\\.)*)(\\)?|'(?>(?:[^']|'')*))\z/
37
+ RUBY_OBJECT_TAG_RE = %r{\G!ruby/object:}
38
+ LSTRIP_BYTES = [0, 9, 10, 11, 12, 13, 32].freeze
39
+ UTF8_BOM_BYTES = [0xEF, 0xBB, 0xBF].freeze
42
40
  MAX_NESTING_DEPTH = 1_000
41
+ NULL_SCALARS = %w[~ null].freeze
42
+ BLANK_RE = /\A[\0\s]*\z/
43
+ BLANK_OR_COMMENT_RE = /\A[\0\s]*(?:#|\z)/
44
+ DOCUMENT_END_RE = /\A\.\.\.(?:[ \t]|\z)/
43
45
 
44
46
  STRING_UNESCAPES = {
45
47
  "\\\\" => "\\",
46
48
  "\\\"" => "\"",
49
+ "\\ " => " ",
47
50
  "\\0" => "\0",
48
51
  "\\a" => "\a",
49
52
  "\\b" => "\b",
@@ -53,12 +56,25 @@ module Gem
53
56
  "\\f" => "\f",
54
57
  "\\r" => "\r",
55
58
  "\\e" => "\e",
59
+ "\\N" => "\u0085",
60
+ "\\_" => "\u00A0",
61
+ "\\L" => "\u2028",
62
+ "\\P" => "\u2029",
56
63
  }.freeze
57
64
 
58
65
  def initialize(source)
59
- @lines = source.split(/\r?\n/)
66
+ raise TypeError, "no implicit conversion of nil into String" if source.nil?
67
+
68
+ source = strip_bom(source)
69
+ # Keep trailing empty lines, which a block scalar with keep chomping
70
+ # ("|+") preserves, but not the empty string after the final line
71
+ # break.
72
+ @lines = source.split(/\r?\n/, -1)
73
+ @lines.pop if @lines.last == ""
74
+ @lines.each(&:freeze)
60
75
  @anchors = {}
61
76
  @depth = 0
77
+ strip_document_end
62
78
  strip_document_prefix
63
79
  end
64
80
 
@@ -82,6 +98,19 @@ module Gem
82
98
 
83
99
  private
84
100
 
101
+ # Byte-wise, as checksums.yaml is read as binary unlike config files,
102
+ # and binary cannot be compared with a UTF-8 string holding the BOM.
103
+ def strip_bom(source)
104
+ return source unless UTF8_BOM_BYTES.each_with_index.all? {|byte, i| source.getbyte(i) == byte }
105
+
106
+ source.byteslice(UTF8_BOM_BYTES.size..)
107
+ end
108
+
109
+ def strip_document_end
110
+ index = @lines.index {|line| DOCUMENT_END_RE.match?(line) }
111
+ @lines.slice!(index..) if index
112
+ end
113
+
85
114
  def strip_document_prefix
86
115
  return if @lines.empty?
87
116
  return unless @lines[0]&.start_with?("---")
@@ -101,37 +130,38 @@ module Gem
101
130
  return nil if @lines.empty?
102
131
 
103
132
  line = @lines[0]
104
- stripped = line.lstrip
105
- indent = line.size - stripped.size
133
+ indent = indent_of(line)
106
134
  return nil if indent < base_indent
107
135
 
108
- return parse_alias_ref if stripped.start_with?("*")
109
-
110
- anchor = consume_anchor
136
+ return parse_alias_ref if /\G\*/.match?(line, indent)
111
137
 
112
- if anchor
138
+ pos = indent
139
+ if /\G&/.match?(line, indent) && (anchor = consume_anchor)
140
+ # A lone anchor line, e.g. "--- &1", anchors the node on the next line.
141
+ skip_blank_and_comments
142
+ return register_anchor(anchor, nil) if @lines.empty? || current_indent < base_indent
113
143
  line = @lines[0]
114
- stripped = line.lstrip
144
+ indent = pos = indent_of(line)
115
145
  end
116
146
 
117
- if stripped.start_with?("- ") || stripped == "-"
147
+ if SEQUENCE_ENTRY_RE.match?(line, pos)
118
148
  parse_sequence(indent, anchor)
119
- elsif QUOTED_KEY_RE.match?(stripped)
149
+ elsif QUOTED_KEY_RE.match?(line, pos)
120
150
  # A mapping whose key is quoted, e.g. '"have: colon": value'.
121
151
  parse_mapping(indent, anchor)
122
- elsif (stripped.start_with?("\"") && stripped.end_with?("\"")) ||
123
- (stripped.start_with?("'") && stripped.end_with?("'"))
152
+ elsif (/\G"/.match?(line, pos) && line.end_with?("\"")) ||
153
+ (/\G'/.match?(line, pos) && line.end_with?("'"))
124
154
  # A whole-line quoted scalar, e.g. '"system: foo: bar"'. It may
125
155
  # contain ": ", so it must be checked before MAPPING_KEY_RE.
126
156
  parse_plain_scalar(indent, anchor)
127
- elsif stripped =~ MAPPING_KEY_RE && !stripped.start_with?("!ruby/object:")
157
+ elsif MAPPING_KEY_RE.match?(line, pos) && !RUBY_OBJECT_TAG_RE.match?(line, pos)
128
158
  parse_mapping(indent, anchor)
129
- elsif stripped.start_with?("!ruby/object:")
159
+ elsif RUBY_OBJECT_TAG_RE.match?(line, pos)
130
160
  parse_tagged_node(indent, anchor)
131
- elsif stripped.start_with?("|")
132
- modifier = stripped[1..].to_s.strip
161
+ elsif /\G\|/.match?(line, pos)
162
+ modifier = line[pos + 1, line.size].strip
133
163
  @lines.shift
134
- register_anchor(anchor, Scalar.new(value: parse_block_scalar(indent, modifier)))
164
+ register_anchor(anchor, Scalar.new(parse_block_scalar(indent, modifier)))
135
165
  else
136
166
  parse_plain_scalar(indent, anchor)
137
167
  end
@@ -142,23 +172,27 @@ module Gem
142
172
  def parse_sequence(indent, anchor)
143
173
  items = []
144
174
  while @lines.any?
175
+ skip_blank_and_comments
176
+ break if @lines.empty?
145
177
  line = @lines[0]
146
- stripped = line.lstrip
147
- break unless line.size - stripped.size == indent &&
148
- (stripped.start_with?("- ") || stripped == "-")
149
- content = @lines.shift.lstrip[1..].strip
150
- item_anchor, content = extract_item_anchor(content)
178
+ break unless indent_of(line) == indent && SEQUENCE_ENTRY_RE.match?(line, indent)
179
+ @lines.shift
180
+ content = line[indent + 1, line.size]
181
+ content.strip!
182
+ item_anchor = nil
183
+ item_anchor, content = extract_anchor(content) if content.start_with?("&")
151
184
  item = parse_sequence_item(content, indent)
152
185
  items << register_anchor(item_anchor, item)
153
186
  end
154
- register_anchor(anchor, Sequence.new(items: items))
187
+ register_anchor(anchor, Sequence.new(items))
155
188
  end
156
189
 
157
190
  def parse_sequence_item(content, indent)
158
191
  if content.start_with?("*")
159
192
  parse_inline_alias(content)
160
193
  elsif content.empty?
161
- @lines.any? && current_indent > indent ? parse_node(indent) : nil
194
+ skip_blank_and_comments
195
+ @lines.any? && current_indent > indent ? parse_node(indent + 1) : nil
162
196
  elsif content.start_with?("!ruby/object:")
163
197
  parse_tagged_content(content.strip, indent)
164
198
  elsif content.start_with?("!binary ")
@@ -167,11 +201,12 @@ module Gem
167
201
  @lines.unshift("#{" " * (indent + 2)}#{content}")
168
202
  parse_node(indent)
169
203
  elsif QUOTED_KEY_RE.match?(content) ||
170
- (content =~ MAPPING_KEY_RE && !content.start_with?("!ruby/object:"))
204
+ (MAPPING_KEY_RE.match?(content) && !content.start_with?("!ruby/object:") &&
205
+ !OPEN_QUOTED_RE.match?(content))
171
206
  @lines.unshift("#{" " * (indent + 2)}#{content}")
172
207
  parse_node(indent)
173
208
  elsif content.start_with?("|")
174
- Scalar.new(value: parse_block_scalar(indent, content[1..].to_s.strip))
209
+ Scalar.new(parse_block_scalar(indent, content[1..].to_s.strip))
175
210
  else
176
211
  parse_inline_scalar(content, indent)
177
212
  end
@@ -180,28 +215,33 @@ module Gem
180
215
  def parse_mapping(indent, anchor)
181
216
  pairs = []
182
217
  while @lines.any?
218
+ skip_blank_and_comments
219
+ break if @lines.empty?
183
220
  line = @lines[0]
184
- stripped = line.lstrip
185
- break unless line.size - stripped.size == indent
221
+ break unless indent_of(line) == indent
186
222
 
187
- if (match = QUOTED_KEY_RE.match(stripped))
223
+ if (match = QUOTED_KEY_RE.match(line, indent))
188
224
  key = coerce(match[1])
189
- elsif (match = MAPPING_KEY_RE.match(stripped)) && !stripped.start_with?("!ruby/object:")
190
- key = match[1].strip
225
+ elsif (match = MAPPING_KEY_RE.match(line, indent)) && !RUBY_OBJECT_TAG_RE.match?(line, indent)
226
+ key = match[1]
227
+ key.strip!
191
228
  key = decode_binary_tag(key) if key.start_with?("!binary ")
192
229
  else
193
230
  break
194
231
  end
195
232
  @lines.shift
196
- val = strip_comment(match[2].to_s.strip)
233
+ val = match[2]
234
+ val&.strip!
235
+ val = strip_comment(val || "")
197
236
 
198
- val_anchor, val = consume_value_anchor(val)
237
+ val_anchor = nil
238
+ val_anchor, val = extract_anchor(val) if val.start_with?("&")
199
239
  value = parse_mapping_value(val, indent)
200
240
  value = register_anchor(val_anchor, value) if val_anchor
201
241
 
202
- pairs << [Scalar.new(value: key), value]
242
+ pairs << key << value
203
243
  end
204
- register_anchor(anchor, Mapping.new(pairs: pairs))
244
+ register_anchor(anchor, Mapping.new(pairs))
205
245
  end
206
246
 
207
247
  def parse_mapping_value(val, indent)
@@ -212,25 +252,19 @@ module Gem
212
252
  elsif val.start_with?("!binary ")
213
253
  parse_binary_value(val, indent)
214
254
  elsif val.empty?
215
- next_stripped = nil
216
- next_indent = nil
217
- if @lines.any?
218
- next_stripped = @lines[0].lstrip
219
- next_indent = @lines[0].size - next_stripped.size
220
- end
221
- if next_stripped &&
222
- (next_stripped.start_with?("- ") || next_stripped == "-") &&
223
- next_indent == indent
255
+ skip_blank_and_comments
256
+ line = @lines[0]
257
+ if line && indent_of(line) == indent && SEQUENCE_ENTRY_RE.match?(line, indent)
224
258
  parse_node(indent)
225
259
  else
226
260
  parse_node(indent + 1)
227
261
  end
228
262
  elsif val == "[]"
229
- Sequence.new
263
+ Sequence.new([])
230
264
  elsif val == "{}"
231
- Mapping.new
265
+ Mapping.new([])
232
266
  elsif val.start_with?("|")
233
- Scalar.new(value: parse_block_scalar(indent, val[1..].to_s.strip))
267
+ Scalar.new(parse_block_scalar(indent, val[1..].to_s.strip))
234
268
  else
235
269
  parse_inline_scalar(val, indent)
236
270
  end
@@ -253,21 +287,24 @@ module Gem
253
287
  node.anchor = anchor
254
288
  node
255
289
  else
256
- Mapping.new(pairs: [[Scalar.new(value: "value"), node]], tag: tag, anchor: anchor)
290
+ Mapping.new(["value", node], tag, anchor)
257
291
  end
258
292
  end
259
293
 
260
294
  def parse_block_scalar(base_indent, modifier)
261
295
  parts = []
262
- block_indent = nil
296
+ indentation = modifier[/\A[+-]?([1-9])/, 1]
297
+ chomping = modifier[/\A[1-9]?([+-])/, 1]
298
+ block_indent = indentation && base_indent + indentation.to_i
263
299
 
264
300
  while @lines.any?
265
301
  line = @lines[0]
266
- if line.strip.empty?
302
+ if BLANK_RE.match?(line)
303
+ parts << line[block_indent..] if block_indent && line.size > block_indent
267
304
  parts << "\n"
268
305
  @lines.shift
269
306
  else
270
- line_indent = line.size - line.lstrip.size
307
+ line_indent = indent_of(line)
271
308
  break if line_indent <= base_indent
272
309
  block_indent ||= line_indent
273
310
  parts << @lines.shift[block_indent..].to_s << "\n"
@@ -275,30 +312,54 @@ module Gem
275
312
  end
276
313
 
277
314
  res = parts.join
278
- res.chomp! if modifier == "-" && res.end_with?("\n")
315
+ unless chomping == "+"
316
+ res.delete_suffix!("\n") while res.end_with?("\n")
317
+ # Clipping keeps the final line break but drops trailing empty lines.
318
+ res << "\n" if chomping.nil? && !res.empty?
319
+ end
279
320
  res
280
321
  end
281
322
 
282
323
  def parse_plain_scalar(indent, anchor)
283
- result = coerce(@lines.shift.strip)
324
+ val = @lines.shift.strip
325
+ result = coerce(val)
284
326
  return register_anchor(anchor, result) if result.is_a?(Mapping) || result.is_a?(Sequence)
285
327
 
286
- while result.is_a?(String) && @lines.any? &&
287
- !@lines[0].strip.empty? && current_indent > indent
288
- result << " " << @lines.shift.strip
289
- end
290
- register_anchor(anchor, Scalar.new(value: result))
328
+ fold_continuation_lines(result, val, indent)
329
+ register_anchor(anchor, Scalar.new(result))
291
330
  end
292
331
 
293
332
  def parse_inline_scalar(val, indent)
294
333
  result = coerce(val)
295
334
  return result if result.is_a?(Mapping) || result.is_a?(Sequence)
296
335
 
297
- while result.is_a?(String) && @lines.any? &&
298
- !@lines[0].strip.empty? && current_indent > indent
336
+ fold_continuation_lines(result, val, indent)
337
+ Scalar.new(result)
338
+ end
339
+
340
+ def fold_continuation_lines(result, val, indent)
341
+ return unless result.is_a?(String)
342
+
343
+ return result.replace(fold_quoted_lines(val.delete_prefix("! "), indent)) if OPEN_QUOTED_RE.match?(val)
344
+
345
+ while @lines.any? && !BLANK_OR_COMMENT_RE.match?(@lines[0]) && current_indent > indent
299
346
  result << " " << @lines.shift.strip
300
347
  end
301
- Scalar.new(value: result)
348
+ end
349
+
350
+ def fold_quoted_lines(val, indent)
351
+ open = OPEN_QUOTED_RE.match(val)
352
+ breaks = +""
353
+ while open && @lines.any? && (BLANK_RE.match?(@lines[0]) || current_indent > indent)
354
+ line = @lines.shift.strip
355
+ next breaks << "\n" if line.empty?
356
+
357
+ val.chop! if open[1]
358
+ val << (breaks.empty? && !open[1] ? " " : breaks) << line
359
+ breaks.clear
360
+ open = OPEN_QUOTED_RE.match("#{val[0]}#{line}")
361
+ end
362
+ coerce(val)
302
363
  end
303
364
 
304
365
  def coerce(val, depth = 0)
@@ -306,25 +367,30 @@ module Gem
306
367
 
307
368
  val = val.sub(/^! /, "") if val.start_with?("! ")
308
369
 
309
- if val =~ /^"(.*)"$/
310
- $1.gsub(/\\(?:["\\0abtnvfre]|x\h{2})/) do |m|
311
- STRING_UNESCAPES[m] || m[2..].to_i(16).chr(Encoding::UTF_8)
370
+ if val.size >= 2 && val.start_with?("\"") && val.end_with?("\"")
371
+ inner = val[1..-2]
372
+ return inner unless inner.include?("\\")
373
+ inner.gsub(/\\(?:["\\ 0abtnvfreN_LP]|x\h{2}|u\h{4}|U\h{8})/) do |m|
374
+ char = STRING_UNESCAPES[m] || unescape_code_point(m)
375
+ # Metadata is read as binary, where non-ASCII text stays as UTF-8 bytes.
376
+ Encoding.compatible?(inner, char) ? char : char.b
312
377
  end
313
- elsif val =~ /^'(.*)'$/
314
- $1.gsub(/''/, "'")
378
+ elsif val.size >= 2 && val.start_with?("'") && val.end_with?("'")
379
+ inner = val[1..-2]
380
+ inner.include?("''") ? inner.gsub("''", "'") : inner
315
381
  elsif val == "true"
316
382
  true
317
383
  elsif val == "false"
318
384
  false
319
- elsif ["~", "null"].include?(val)
385
+ elsif NULL_SCALARS.include?(val)
320
386
  nil
321
387
  elsif val == "{}"
322
- Mapping.new
388
+ Mapping.new([])
323
389
  elsif val =~ /^\[(.*)\]$/
324
390
  inner = $1.strip
325
- return Sequence.new if inner.empty?
326
- items = inner.split(/\s*,\s*/).reject(&:empty?).map {|e| Scalar.new(value: coerce(e, depth + 1)) }
327
- Sequence.new(items: items)
391
+ return Sequence.new([]) if inner.empty?
392
+ items = inner.scan(FLOW_SEQUENCE_ENTRY_RE).map {|e| Scalar.new(coerce(e, depth + 1)) }
393
+ Sequence.new(items)
328
394
  elsif /\A\d{4}-\d{2}-\d{2}([ T]\d{2}:\d{2}:\d{2})?/.match?(val)
329
395
  begin
330
396
  Time.new(val)
@@ -343,6 +409,12 @@ module Gem
343
409
  end
344
410
  end
345
411
 
412
+ def unescape_code_point(escape)
413
+ code = escape[2..].hex
414
+ return escape if code > 0x10FFFF || code.between?(0xD800, 0xDFFF)
415
+ code.chr(Encoding::UTF_8)
416
+ end
417
+
346
418
  def decode_binary_tag(str)
347
419
  content = str.sub(/\A!binary\s+/, "")
348
420
  content = $1 if content =~ /\A"(.*)"\z/ || content =~ /\A'(.*)'\z/
@@ -353,51 +425,55 @@ module Gem
353
425
  rest = val.sub(/\A!binary\s+/, "")
354
426
  if rest.start_with?("|")
355
427
  content = parse_block_scalar(indent, rest[1..].to_s.strip)
356
- Scalar.new(value: content.unpack1("m"))
428
+ Scalar.new(content.unpack1("m"))
357
429
  else
358
- Scalar.new(value: decode_binary_tag(val))
430
+ Scalar.new(decode_binary_tag(val))
359
431
  end
360
432
  end
361
433
 
362
434
  def parse_alias_ref
363
- AliasRef.new(name: @lines.shift.lstrip[1..].strip)
435
+ AliasRef.new(@lines.shift.lstrip[1..].strip)
364
436
  end
365
437
 
366
438
  def parse_inline_alias(content)
367
- AliasRef.new(name: content[1..].strip)
439
+ AliasRef.new(content[1..].strip)
368
440
  end
369
441
 
370
442
  def current_indent
371
- line = @lines[0]
372
- line.size - line.lstrip.size
443
+ indent_of(@lines[0])
444
+ end
445
+
446
+ def indent_of(line)
447
+ i = 0
448
+ i += 1 while LSTRIP_BYTES.include?(line.getbyte(i))
449
+ i
373
450
  end
374
451
 
375
452
  def consume_anchor
376
453
  line = @lines[0]
377
454
  stripped = line.lstrip
378
- return nil unless stripped.start_with?("&") && stripped =~ /^&(\S+)\s+/
455
+ return nil unless stripped.start_with?("&") && stripped =~ /^&(\S+)\s*/
379
456
 
380
457
  anchor = $1
381
- @lines[0] = line.sub(/&#{Regexp.escape(anchor)}\s+/, "")
458
+ if $'.empty?
459
+ @lines.shift
460
+ else
461
+ @lines[0] = line.sub(/&#{Regexp.escape(anchor)}\s+/, "")
462
+ end
382
463
  anchor
383
464
  end
384
465
 
385
- def extract_item_anchor(content)
466
+ def extract_anchor(content)
386
467
  return [nil, content] unless content =~ /^&(\S+)/
387
468
 
388
469
  anchor = $1
389
470
  [anchor, content.sub(/^&#{Regexp.escape(anchor)}\s*/, "")]
390
471
  end
391
472
 
392
- def consume_value_anchor(val)
393
- return [nil, val] unless val =~ /^&(\S+)\s+/
394
-
395
- anchor = $1
396
- [anchor, val.sub(/^&#{Regexp.escape(anchor)}\s+/, "")]
397
- end
398
-
399
473
  def register_anchor(name, node)
400
474
  if name
475
+ # An anchored empty node still defines the anchor, as a null.
476
+ node ||= Scalar.new(nil)
401
477
  @anchors[name] = node
402
478
  node.anchor = name if node.respond_to?(:anchor=)
403
479
  end
@@ -414,12 +490,7 @@ module Gem
414
490
  end
415
491
 
416
492
  def skip_blank_and_comments
417
- while @lines.any?
418
- line = @lines[0]
419
- stripped = line.lstrip
420
- break unless stripped.empty? || stripped.start_with?("#")
421
- @lines.shift
422
- end
493
+ @lines.shift while @lines.any? && BLANK_OR_COMMENT_RE.match?(@lines[0])
423
494
  end
424
495
 
425
496
  def strip_comment(val)
@@ -462,14 +533,16 @@ module Gem
462
533
  class Builder
463
534
  VALID_OPS = %w[= != > < >= <= ~>].freeze
464
535
  ARRAY_FIELDS = %w[files test_files executables extra_rdoc_files].freeze
465
- MAX_ALIAS_RESOLUTIONS = 1_000
536
+ MAX_ALIAS_EXPANSION = 100_000
466
537
 
467
538
  def initialize(permitted_classes: [], permitted_symbols: [], aliases: true)
468
539
  @permitted_classes = permitted_classes.map {|c| "!ruby/object:#{c}" }
469
540
  @permitted_symbols = permitted_symbols
470
541
  @aliases = aliases
471
542
  @anchor_values = {}
472
- @alias_count = 0
543
+ @anchor_sizes = {}
544
+ @expanded_size = 0
545
+ @alias_expansion = 0
473
546
  end
474
547
 
475
548
  def build(node)
@@ -487,31 +560,40 @@ module Gem
487
560
  private
488
561
 
489
562
  def build_node(node)
563
+ start = @expanded_size
490
564
  case node
491
565
  when nil then nil
492
566
  when AliasRef then resolve_alias(node)
493
- when Scalar then store_anchor(node.anchor, node.value)
494
- when Mapping then build_mapping(node)
495
- when Sequence then store_anchor(node.anchor, node.items.map {|item| build_node(item) })
567
+ when Scalar then store_anchor(node.anchor, node.value, start)
568
+ when Mapping then store_anchor(node.anchor, build_mapping(node), start)
569
+ when Sequence then store_anchor(node.anchor, node.items.map {|item| build_node(item) }, start)
496
570
  else node # already a Ruby object
497
571
  end
498
572
  end
499
573
 
500
574
  def resolve_alias(node)
501
575
  raise Psych::AliasesNotEnabled unless @aliases
502
- @alias_count += 1
503
- if @alias_count > MAX_ALIAS_RESOLUTIONS
504
- raise Psych::BadAlias, "exceeded maximum alias resolutions (#{MAX_ALIAS_RESOLUTIONS})"
505
- end
506
576
  unless @anchor_values.key?(node.name)
507
- klass = defined?(Psych::AnchorNotDefined) ? Psych::AnchorNotDefined : Psych::BadAlias
508
- raise klass, "An alias referenced an unknown anchor: #{node.name}"
577
+ raise Psych::AnchorNotDefined, node.name if defined?(Psych::AnchorNotDefined)
578
+ raise Psych::BadAlias, "An alias referenced an unknown anchor: #{node.name}"
579
+ end
580
+ # Charge the expanded size of the anchor, weighing scalars by length,
581
+ # so neither nested aliases nor aliases to a long string blow up.
582
+ size = @anchor_sizes.fetch(node.name)
583
+ @expanded_size += size
584
+ @alias_expansion += size
585
+ if @alias_expansion > MAX_ALIAS_EXPANSION
586
+ raise Psych::BadAlias, "exceeded maximum alias expansion (#{MAX_ALIAS_EXPANSION})"
509
587
  end
510
588
  @anchor_values.fetch(node.name)
511
589
  end
512
590
 
513
- def store_anchor(name, value)
514
- @anchor_values[name] = value if name
591
+ def store_anchor(name, value, start)
592
+ @expanded_size += value.is_a?(String) ? 1 + value.bytesize : 1
593
+ if name
594
+ @anchor_values[name] = value
595
+ @anchor_sizes[name] = @expanded_size - start
596
+ end
515
597
  value
516
598
  end
517
599
 
@@ -537,13 +619,12 @@ module Gem
537
619
  raise ArgumentError, "undefined class/module #{node.tag.sub("!ruby/object:", "")}"
538
620
  end
539
621
 
540
- store_anchor(node.anchor, result)
622
+ result
541
623
  end
542
624
 
543
625
  def build_hash(node)
544
626
  result = {}
545
- node.pairs.each do |key_node, value_node|
546
- key = key_node.is_a?(Scalar) ? key_node.value.to_s : build_node(key_node).to_s
627
+ each_pair(node) do |key, value_node|
547
628
  value = build_node(value_node)
548
629
 
549
630
  if ARRAY_FIELDS.include?(key)
@@ -643,13 +724,21 @@ module Gem
643
724
 
644
725
  def pairs_to_hash(node)
645
726
  result = {}
646
- node.pairs.each do |key_node, value_node|
647
- key = key_node.is_a?(Scalar) ? key_node.value.to_s : build_node(key_node).to_s
727
+ each_pair(node) do |key, value_node|
648
728
  result[key] = build_node(value_node)
649
729
  end
650
730
  result
651
731
  end
652
732
 
733
+ def each_pair(node)
734
+ pairs = node.pairs
735
+ i = 0
736
+ while i < pairs.size
737
+ yield pairs[i], pairs[i + 1]
738
+ i += 2
739
+ end
740
+ end
741
+
653
742
  def validate_tag!(tag)
654
743
  return if @permitted_classes.include?(tag)
655
744
  raise_disallowed_class!(tag)
@@ -713,7 +802,9 @@ module Gem
713
802
  }.freeze
714
803
 
715
804
  def emit(obj)
716
- "---#{emit_node(obj, 0)}"
805
+ res = "---#{emit_node(obj, 0)}"
806
+ res << "...\n" if @open_ended
807
+ res
717
808
  end
718
809
 
719
810
  private
@@ -831,15 +922,35 @@ module Gem
831
922
  end
832
923
 
833
924
  def emit_block_scalar(str, indent)
834
- parts = [str.end_with?("\n") ? " |\n" : " |-\n"]
925
+ # The root node has no parent to be indented from, so indent its
926
+ # content by one level like libyaml does.
927
+ indent = 2 if indent.zero?
928
+ parts = [" |#{block_scalar_header(str)}\n"]
835
929
  str.each_line do |line|
836
- parts << "#{pad(indent + 2)}#{line}"
930
+ parts << (line == "\n" ? line : "#{pad(indent)}#{line}")
837
931
  end
838
932
  res = parts.join
839
933
  res << "\n" unless res.end_with?("\n")
840
934
  res
841
935
  end
842
936
 
937
+ # Mirrors libyaml: an explicit indentation indicator when the first
938
+ # line is empty or starts with a space (a parser would otherwise take
939
+ # that whitespace as the block indentation and drop it), "-" when the
940
+ # content does not end with a line break and "+" when it ends with
941
+ # more than one, which also requires a document end marker.
942
+ def block_scalar_header(str)
943
+ header = +""
944
+ header << "2" if str.start_with?(" ", "\n")
945
+ if !str.end_with?("\n")
946
+ header << "-"
947
+ elsif str.end_with?("\n\n") || str == "\n"
948
+ header << "+"
949
+ @open_ended = true
950
+ end
951
+ header
952
+ end
953
+
843
954
  def needs_quoting?(str, quote = false)
844
955
  quote || str.empty? || str != str.strip || str =~ /[[:cntrl:]]/ ||
845
956
  str =~ /^[!*&:@%$"'|`]/ || str =~ /^-?\d+(\.\d+)?$/ || str =~ /^[<>=-]/ ||
@@ -864,17 +975,16 @@ module Gem
864
975
  end
865
976
 
866
977
  def load(str, permitted_classes: [], permitted_symbols: [], aliases: true)
867
- raise TypeError, "no implicit conversion of nil into String" if str.nil?
868
- return nil if str.empty?
869
-
870
978
  ast = Parser.new(str).parse
871
- return nil if ast.nil?
872
-
873
- Builder.new(
979
+ result = Builder.new(
874
980
  permitted_classes: permitted_classes,
875
981
  permitted_symbols: permitted_symbols,
876
982
  aliases: aliases
877
983
  ).build(ast)
984
+ # Bundler 4.0 and older call Hash methods on what this returns for
985
+ # .bundle/config, which is just "---" once its last key is unset, so
986
+ # nil stays a Hash here as it did in RubyGems 4.0.
987
+ result.nil? ? {} : result
878
988
  end
879
989
  end
880
990
  end