Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly require csv for for Ruby head #454

Merged
merged 2 commits into from
Jan 26, 2024
Merged

Explicitly require csv for for Ruby head #454

merged 2 commits into from
Jan 26, 2024

Conversation

Mangara
Copy link
Contributor

@Mangara Mangara commented Jan 23, 2024

Some bundled gems are being removed from Ruby and need to be specified in the Gemfile with Ruby 3.4: https://bugs.ruby-lang.org/issues/20187

This PR adds the CSV gem to our Gemfile for use in testing.

I don't think we need to add it to the Gemspec? If users don't use the CSVEnumerator, they shouldn't need the gem, right?

@Mangara Mangara self-assigned this Jan 23, 2024
@Mangara Mangara requested a review from sambostock January 23, 2024 22:21
Copy link
Contributor

@sambostock sambostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to add it to the Gemspec? If users don't use the CSVEnumerator, they shouldn't need the gem, right?

My gut feeling for this is that if we want to just work without csv, as long as you don't try to use it, then we'll need something more "clever" to explicitly deal with csv being missing.

While looking into that, I noticed that we actually don't require 'csv' anywhere in the app (we do it in test/unit/csv_enumerator_test.rb, but not lib), despite referencing the CSV constant, which I think is actually a bug (although in practice it's unlikely anyone has run into it, as I'll bet something in most host apps requires it).

I think one approach would be something like

# lib/job-iteration/csv_enumerator.rb

begin
  require "csv"
rescue LoadError
  return
end

module JobIteration
  class CsvEnumerator
    # ...

and an entry in the README about it.

@Mangara
Copy link
Contributor Author

Mangara commented Jan 24, 2024

We only use the CSV constant to check that csv.instance_of?(CSV). Should we instead change that to csv.class.name == "CSV"?

That would make

  1. People trying to call the enumerator without a CSV object get the error
CsvEnumerator.new takes CSV object (ArgumentError)
  1. People (after the first error?) trying to call the enumerator with a CSV object get the error:
uninitialized constant CSV (NameError)

originating from their own code, which should hopefully get them to require "csv" pretty quickly (even Googling it).

The class would still get loaded for apps without the csv gem, but that seems very minor.

This moves any uninitialized constant error to the calling code,
which hopefully makes it clear to devs that they should
`require "csv"` before using the CsvEnumerator.
@Mangara Mangara force-pushed the mangara-csv-gemfile branch from 43e6372 to d561d7e Compare January 25, 2024 20:35
@Mangara Mangara merged commit 922ee56 into main Jan 26, 2024
45 checks passed
@sambostock sambostock deleted the mangara-csv-gemfile branch February 6, 2024 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants