Skip to content

Commit 358f83f

Browse files
authored
Merge pull request #560 from michaelglass/michaelglass/warning-on-multiple-manifests
Warn if there are multiple manifest files
2 parents f7d9150 + df972cf commit 358f83f

6 files changed

+26
-5
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket
66

77
- Minimum Ruby version for Sprockets 4 is now 2.5+ which matches minimum ruby verision of Rails [#604]
88
- Fix threading bug introduced in Sprockets 4 [#603]
9+
- Warn when two potential manifest files exist. [#560]
910

1011
## 4.0.0.beta8
1112

@@ -54,4 +55,3 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket
5455
- Initial release of Sprockets 4
5556

5657
Please upgrade to the latest Sprockets 3 version before upgrading to Sprockets 4. Check the 3.x branch for previous changes https://github.com/rails/sprockets/blob/3.x/CHANGELOG.md.
57-

lib/sprockets/manifest.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def initialize(*args)
5353

5454
# If directory is given w/o filename, pick a random manifest location
5555
if @directory && @filename.nil?
56-
@filename = find_directory_manifest(@directory)
56+
@filename = find_directory_manifest(@directory, logger)
5757
end
5858

5959
unless @directory && @filename

lib/sprockets/manifest_utils.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22
require 'securerandom'
3+
require 'logger'
34

45
module Sprockets
56
# Public: Manifest utilities.
@@ -33,10 +34,13 @@ def generate_manifest_path
3334
# # => "/app/public/assets/.sprockets-manifest-abc123.json"
3435
#
3536
# Returns String filename.
36-
def find_directory_manifest(dirname)
37+
def find_directory_manifest(dirname, logger = Logger.new($stderr))
3738
entries = File.directory?(dirname) ? Dir.entries(dirname) : []
38-
entry = entries.find { |e| e =~ MANIFEST_RE } ||
39-
generate_manifest_path
39+
manifest_entries = entries.select { |e| e =~ MANIFEST_RE }
40+
if manifest_entries.length > 1
41+
logger.warn("Found multiple manifests: #{manifest_entries}. Choosing the first alphabetically: #{manifest_entries.first}")
42+
end
43+
entry = manifest_entries.first || generate_manifest_path
4044
File.join(dirname, entry)
4145
end
4246
end

test/fixtures/manifest_utils/with_two_manifests/.sprockets-manifest-00000000000000000000000000000000.json

Whitespace-only changes.

test/fixtures/manifest_utils/with_two_manifests/.sprockets-manifest-f4bf345974645583d284686ddfb7625e.json

Whitespace-only changes.

test/test_manifest_utils.rb

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22
require 'minitest/autorun'
33
require 'sprockets/manifest_utils'
4+
require 'logger'
45

56
class TestManifestUtils < MiniTest::Test
67
include Sprockets::ManifestUtils
@@ -16,5 +17,21 @@ def test_find_directory_manifest
1617

1718
assert_equal "#{root}/default/.sprockets-manifest-f4bf345974645583d284686ddfb7625e.json",
1819
find_directory_manifest("#{root}/default")
20+
21+
end
22+
23+
def test_warn_on_two
24+
root = File.expand_path("../fixtures/manifest_utils", __FILE__)
25+
26+
assert_match MANIFEST_RE, File.basename(find_directory_manifest(root))
27+
28+
r, w = IO.pipe
29+
logger = Logger.new(w)
30+
# finds the first one alphabetically
31+
assert_equal "#{root}/with_two_manifests/.sprockets-manifest-00000000000000000000000000000000.json",
32+
find_directory_manifest("#{root}/with_two_manifests", logger)
33+
output = r.gets
34+
35+
assert_match(/W, \[[^\]]+\] WARN -- : Found multiple manifests: .+ Choosing the first alphabetically: \.sprockets-manifest-00000000000000000000000000000000\.json/, output)
1936
end
2037
end

0 commit comments

Comments
 (0)