ruby / csv

CSV Reading and Writing
https://ruby.github.io/csv/
BSD 2-Clause "Simplified" License
178 stars 113 forks source link

CSV.generate is not working with Rails 7 #245

Closed Barkha-03 closed 2 years ago

Barkha-03 commented 2 years ago

I currently updated my app to rails 7.0.2 and since unable to generate the CSV using this gem. I am getting the following error,

> CSV.generate(headers: true)
ArgumentError: Cannot parse nil as CSV
from /usr/local/bundle/gems/csv-3.2.3/lib/csv.rb:1893:in `initialize'

I have tested this and this is working fine with the previous version of rails.

Ruby version ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]

Rails version Rails 7.0.2.3

esparta commented 2 years ago

Can you provide an script to reproduce this issue? it will be easier to pip-point the issue. Probably this is not a CSV problem, specially if it's noted only when you upgrade Rails - so I'd suggest to review with and without Rails in your bundle.

Barkha-03 commented 2 years ago

Hey @esparta

> CSV.generate(headers: true)
ArgumentError: Cannot parse nil as CSV
from /usr/local/bundle/gems/csv-3.2.3/lib/csv.rb:1893:in `initialize'

I used this code. I used this with my Rails 6 and it is working with all the same gems. This is not working only after I updated my rails and ruby version. Thanks.

esparta commented 2 years ago

Sorry if I was not clear, but with script I mean a complete example which the maintainers (and supporters as myself) can just execute and be able to reproduce without having your bundle. A line with the error it's not enough.

Here's something I did, and it's clear ruby 3.1.2 with bundled CSV gem has not the problem executing this minimal script:

require 'csv'
CSV.generate(headers: true) do |csv|
  csv << ['id', 'date', 'amount']
  csv << 123, '2022/05/09', 12.40]
end

image

Barkha-03 commented 2 years ago

Thanks a lot @esparta Did you test this with rails 7.0.2?

esparta commented 2 years ago

Thanks a lot @esparta Did you test this with rails 7.0.2?

No, I didn't and that's what I'm gently asking you: create a reproducible script:

https://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#create-an-executable-test-case

Barkha-03 commented 2 years ago

Hey @esparta Thanks a lot for your suggestion. Honestly, I did not have any idea about these scripts. Here is my script,

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  ruby '3.1.2'
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem 'rails', "~> 7.0.2", "= 7.0.2.3"
  gem "csv"
end

require "active_support"
require "active_support/core_ext/object/blank"
require "minitest/autorun"
require "csv"

class BugTest < Minitest::Test
    def test_csv
        attributes = %i[id name version]

        all = [
            { id: 1, name: 'Ruby', value: '3.1.2' },
            { id: 2, name: 'Rails', value: '7.0.2' },
            { id: 3, name: 'Bootstrap', value: '4' }
        ]

        CSV.generate(headers: true) do |csv|
            csv << attributes

            all.each do |data|
                csv << [
                data[:id],
                data[:name],
                data[:value]
                ]
            end
        end
    end
end
Barkha-03 commented 2 years ago

Hey @esparta Thanks a lot for giving your time. I created a new app with specified ruby and rails version and it did work. I highly doubt if any of the gem I installed is causing this. I am still investigating this and dropping the gem file as well here in case any one wants to have a look.

# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.1.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', "~> 7.0.2", "= 7.0.2.3"
# Use Puma as the app server
gem 'puma'
# Use SCSS for stylesheets
gem 'sass-rails'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'mini_racer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
gem 'redis', '~> 4.0'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0' # , require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
  gem 'rb-readline'
end

gem 'pg'

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'listen'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'bullet'
  gem 'spring', '~> 3.0.0'
end

gem 'capybara-webkit', git: 'https://github.com/thoughtbot/capybara-webkit.git'

group :test do
  gem 'factory_bot_rails'
  gem 'headless', git: 'https://github.com/leonid-shevtsov/headless.git'
  gem 'rails-controller-testing'
  gem 'rspec-rails'
  gem 'stripe-ruby-mock', git: 'https://github.com/stripe-ruby-mock/stripe-ruby-mock.git', require: 'stripe_mock'
  gem 'webmock'
  gem 'webrick', git: 'https://github.com/ruby/webrick.git'
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

# Used for server-side processing of datatables.
gem 'ajax-datatables-rails', '~> 1.2.0'

# Ability to debug code in real time.
gem 'pry'

# Used for mass imports.
gem 'activerecord-import', git: 'https://github.com/zdennis/activerecord-import'

gem 'sidekiq', github: 'mperham/sidekiq'
# Sidekiq used for backgrounding tasks.
group :production do
  source 'https://gems.contribsys.com/' do
    gem 'sidekiq-pro'
  end
end
gem 'sidekiq-scheduler'

# CSV parsing.
gem 'csv'

# user authentication
gem 'devise'

# for document validation
gem 'activestorage-validator'

# Generates a visual diagram to represent model relationships.
gem 'railroady'

# pagination
gem 'will_paginate' # remove

# Convert number to words.
gem 'humanize'

# Slack-ruby-client
gem 'async-websocket', '~> 0.8.0'
gem 'eventmachine'
gem 'slack-ruby-client'
gem 'websocket-driver'

# Message implementation
gem 'twilio-ruby'

# For sidekiq web
gem 'rack'

# For detecting mime types
gem 'mime'

# to call the api from rails application
gem 'rest-client'

gem 'activerecord-session_store'

# To get the next available day after a provided day
gem 'chronic'

# To get current season for password attacks
gem 'ruby-seasons'

# active storage validation
gem 'active_storage_validations'

# necessary for AWS client
gem 'aws-sdk-ec2'
gem 'aws-sdk-s3'
gem 'aws-sdk-ssm'
gem 'aws-sdk-wafv2'

gem 'terminal-table'
gem 'time_difference'

# For PDF generation
gem 'grover'

# Dumping database
gem 'seed_dump'

# Amazon SQS
gem 'aws-sdk-sqs'

gem 'combine_pdf' # to combine the pdfs for report

# Establish Audit Trail
gem 'audited'#, '~> 4.9'

gem 'stripe'

# Ability to duplicate records
gem 'amoeba'

# Calculating percentage changes
gem 'percentage'

# XML/HTML Parser
gem 'nokogiri'

# Atomic Red Team
gem 'atomic-red-team', git: 'https://www.github.com/redcanaryco/atomic-red-team.git', branch: :master

# Better Rails C Colors
gem 'awesome_print'

gem 'jwt'

# Mailchimp
gem 'MailchimpMarketing'

gem 'cocoon'

# Handle expanding and compacting IP addresses and ranges.
gem 'ipaddr'

gem 'rotp'
gem 'rqrcode'

# Sentry.io
gem 'sentry-rails'
gem 'sentry-ruby'
gem 'sentry-sidekiq'

# rubocop
gem 'rubocop'
gem 'rubocop-rails'
gem 'rubocop-rspec'

# Brakeman static analysis tool
gem 'brakeman'

# Security checks
gem 'bundler-audit'

gem 'zeitwerk'

# Rail 7 upgrade
gem "sprockets-rails"
gem 'cssbundling-rails'
gem 'jsbundling-rails'
gem 'stimulus-rails'
kou commented 2 years ago

Could you also show the output of https://github.com/ruby/csv/issues/245#issuecomment-1121949544 ?

(I couldn't reproduce this...)

kou commented 2 years ago

Could you also show full backtrace of:

> CSV.generate(headers: true)
ArgumentError: Cannot parse nil as CSV
from /usr/local/bundle/gems/csv-3.2.3/lib/csv.rb:1893:in `initialize'
Barkha-03 commented 2 years ago

Hey @kou

[1] pry(RootCause)> e.backtrace
=> ["/usr/local/bundle/gems/csv-3.2.3/lib/csv.rb:1893:in `initialize'",
 "/usr/local/bundle/gems/csv-3.2.3/lib/csv.rb:1411:in `new'",
 "/usr/local/bundle/gems/csv-3.2.3/lib/csv.rb:1411:in `generate'",
 "/root/myapp/app/models/root_cause.rb:20:in `to_csv'",
 "/usr/local/bundle/gems/activerecord-7.0.2.3/lib/active_record/relation/delegation.rb:108:in `public_send'",
 "/usr/local/bundle/gems/activerecord-7.0.2.3/lib/active_record/relation/delegation.rb:108:in `block in method_missing'",
 "/usr/local/bundle/gems/activerecord-7.0.2.3/lib/active_record/relation.rb:880:in `_scoping'",
 "/usr/local/bundle/gems/activerecord-7.0.2.3/lib/active_record/relation.rb:428:in `scoping'",
 "/usr/local/bundle/gems/activerecord-7.0.2.3/lib/active_record/relation/delegation.rb:108:in `method_missing'",
 "/root/myapp/app/controllers/consultants/root_causes_controller.rb:64:in `block (2 levels) in export_root_causes'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_controller/metal/mime_responds.rb:214:in `respond_to'",
 "/root/myapp/app/controllers/consultants/root_causes_controller.rb:62:in `export_root_causes'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/abstract_controller/base.rb:214:in `process_action'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_controller/metal/rendering.rb:53:in `process_action'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/abstract_controller/callbacks.rb:234:in `block in process_action'",
 "/usr/local/bundle/gems/activesupport-7.0.2.3/lib/active_support/callbacks.rb:118:in `block in run_callbacks'",
 "/usr/local/bundle/gems/audited-5.0.2/lib/audited/sweeper.rb:16:in `around'",
 "/usr/local/bundle/gems/activesupport-7.0.2.3/lib/active_support/callbacks.rb:127:in `block in run_callbacks'",
 "/usr/local/bundle/gems/audited-5.0.2/lib/audited/sweeper.rb:16:in `around'",
 "/usr/local/bundle/gems/activesupport-7.0.2.3/lib/active_support/callbacks.rb:127:in `block in run_callbacks'",
 "/usr/local/bundle/gems/actiontext-7.0.2.3/lib/action_text/rendering.rb:20:in `with_renderer'",
 "/usr/local/bundle/gems/actiontext-7.0.2.3/lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'",
 "/usr/local/bundle/gems/activesupport-7.0.2.3/lib/active_support/callbacks.rb:127:in `instance_exec'",
 "/usr/local/bundle/gems/activesupport-7.0.2.3/lib/active_support/callbacks.rb:127:in `block in run_callbacks'",
 "/usr/local/bundle/gems/activesupport-7.0.2.3/lib/active_support/callbacks.rb:138:in `run_callbacks'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/abstract_controller/callbacks.rb:233:in `process_action'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_controller/metal/rescue.rb:22:in `process_action'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'",
 "/usr/local/bundle/gems/activesupport-7.0.2.3/lib/active_support/notifications.rb:206:in `block in instrument'",
 "/usr/local/bundle/gems/activesupport-7.0.2.3/lib/active_support/notifications/instrumenter.rb:24:in `instrument'",
 "/usr/local/bundle/gems/activesupport-7.0.2.3/lib/active_support/notifications.rb:206:in `instrument'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_controller/metal/instrumentation.rb:66:in `process_action'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'",
 "/usr/local/bundle/gems/activerecord-7.0.2.3/lib/active_record/railties/controller_runtime.rb:27:in `process_action'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/abstract_controller/base.rb:151:in `process'",
 "/usr/local/bundle/gems/actionview-7.0.2.3/lib/action_view/rendering.rb:39:in `process'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_controller/metal.rb:188:in `dispatch'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_controller/metal.rb:251:in `dispatch'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_dispatch/routing/route_set.rb:49:in `dispatch'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_dispatch/routing/route_set.rb:32:in `serve'",
 "/usr/local/bundle/gems/actionpack-7.0.2.3/lib/action_dispatch/journey/router.rb:50:in `block in serve'",
:
kou commented 2 years ago

Could you also show this line "/root/myapp/app/models/root_cause.rb:20:in `to_csv'" ?

Barkha-03 commented 2 years ago

CSV.generate(headers: true) do |csv|

This is at line @kou

kou commented 2 years ago

Could you add p str debug print before "/usr/local/bundle/gems/csv-3.2.3/lib/csv.rb:1411"?

kou commented 2 years ago

Could you also add p str.method(:nil?) debug print before "/usr/local/bundle/gems/csv-3.2.3/lib/csv.rb:1411"?

Barkha-03 commented 2 years ago

Hey @kou This is a gem file and I don't have any local file with this name so I won't be able to make any changes you have mentioned.

kou commented 2 years ago

You can use local csv by using gem "csv", path: "/local/path/to/csv" in your Gemfile:

$ git clone https:///github.com/ruby/csv.git /tmp/csv
$ editor Gemfile
(Replace 'gem "csv"' with 'gem "csv", path: "/tmp/csv".)
$ bundle instlal
$ ...
Barkha-03 commented 2 years ago

This is what I got @kou

str.nil? ----->   true
str.method(:nil?) -------> #<Method: String#nil?() /root/myapp/config/initializers/string.rb:25>
Barkha-03 commented 2 years ago

Hey @kou Thanks a lot for suggesting this. I found the issue. .nil? was overwritten in my string.rb file for some reason which caused this. I removed it and now it is working. Thanks a lot all. closing this out.