{"action":"created","issue":{"url":"https://api.github.com/repos/rails/sprockets/issues/209","repository_url":"https://api.github.com/repos/rails/sprockets","labels_url":"https://api.github.com/repos/rails/sprockets/issues/209/labels{/name}","comments_url":"https://api.github.com/repos/rails/sprockets/issues/209/comments","events_url":"https://api.github.com/repos/rails/sprockets/issues/209/events","html_url":"https://github.com/rails/sprockets/issues/209","id":124597829,"number":209,"title":"Couldn't find file when using require_asset","user":{"login":"mikem","id":62569,"avatar_url":"https://avatars.githubusercontent.com/u/62569?v=3","gravatar_id":"","url":"https://api.github.com/users/mikem","html_url":"https://github.com/mikem","followers_url":"https://api.github.com/users/mikem/followers","following_url":"https://api.github.com/users/mikem/following{/other_user}","gists_url":"https://api.github.com/users/mikem/gists{/gist_id}","starred_url":"https://api.github.com/users/mikem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikem/subscriptions","organizations_url":"https://api.github.com/users/mikem/orgs","repos_url":"https://api.github.com/users/mikem/repos","events_url":"https://api.github.com/users/mikem/events{/privacy}","received_events_url":"https://api.github.com/users/mikem/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2016-01-02T16:23:56Z","updated_at":"2016-08-27T05:07:50Z","closed_at":null,"body":"I have an app using the [react-rails](https://github.com/reactjs/react-rails) gem. That gem [registers the `application/jsx` mime type and engine](https://github.com/reactjs/react-rails/blob/3b21569f5defb1705172c4a9a09b6ed0a1a9d38e/lib/react/rails/engine.rb#L7-L8) as follows:\r\n\r\n```ruby\r\nsprockets_env.register_mime_type(\"application/jsx\", extensions: [\".jsx\", \".js.jsx\", \".es.jsx\", \".es6.jsx\"])\r\nsprockets_env.register_transformer(\"application/jsx\", \"application/javascript\", React::JSX::Processor)\r\n```\r\n\r\nI can successfully load JSX files if I use `require` directives in another JS file, but it doesn't work if I use `require_asset` from within an ERB file.\r\n\r\nI created a [minimal Rails app](https://github.com/mikem/sprockets_jsx_bug) to reproduce this bug. That app has two \"components:\"\r\n\r\n- [`some_component.js`](https://github.com/mikem/sprockets_jsx_bug/blob/master/app/assets/javascripts/some_component.js)\r\n- [`another_component.js.jsx`](https://github.com/mikem/sprockets_jsx_bug/blob/master/app/assets/javascripts/another_component.js.jsx)\r\n\r\nThere's also a [`main.js.erb`](https://github.com/mikem/sprockets_jsx_bug/blob/master/app/assets/javascripts/main.js.erb) which uses `require_asset` to load the two components. You can visit http://localhost:3000/some_component/index and note in the console that `some_component.js` logs to the console successfully. Uncomment [line 6](https://github.com/mikem/sprockets_jsx_bug/blob/master/app/assets/javascripts/main.js.erb#L6) to also load `another_component.js.jsx` and see the error.\r\n\r\nI can include `another_component.js.jsx` without any problems if I don't use ERB. To verify this, comment out `main.js.erb`'s line 6 and add an `=` to [line 17 of `application.js`](https://github.com/mikem/sprockets_jsx_bug/blob/master/app/assets/javascripts/application.js#L17). Both components will write to the console now.\r\n\r\nI'm bumping up against this in the [jasmine-rails](https://github.com/searls/jasmine-rails) gem, which uses [ERB to generate a JS file which loads all the Jasmine specs](https://github.com/searls/jasmine-rails/blob/ea97098844bef373470739f2927e895450e5e9a9/lib/assets/javascripts/jasmine-specs.js.erb#L11-L13).\r\n\r\nI tried to resolve this myself and discovered the following:\r\n- in both the ERB `require_asset` case and the JS `//= require` case, [`Sprockets::Resolve#resolve`](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L23) is executed\r\n- in the JS `//= require` case, the provided `path` is absolute, so [`resolve_absolute_path`](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L31) is taken on line 31 and everything is smooth\r\n- in the ERB `require_asset` case, the provided `path` is not absolute nor relative (ie: it's just `another_component.js.jsx`), so `resolve_logical_path` is called in the [last branch of the if-statement on line 35](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L35)\r\n- `resolve_logical_path` first [determines the `mime_type` for the `path` using `parse_path_extnames`](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L104) and gets `application/jsx`\r\n- next, [`mime_type` and `accept` are used to get `parsed_accept`](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L105) which is an empty array, then [`transformed_accept`](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L106) is also empty\r\n- because `transformed_accept` is empty, [`resolve_under_paths` returns a `nil` `filename`](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L107) (see [this guard clause](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L120))\r\n- the `nil` `filename` [prevents a `uri` from being built](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L38-L44) and in turn [triggers the `FileNotFound` exception](https://github.com/rails/sprockets/blob/f7bfd95445d648da766755388f6085b77a79ad25/lib/sprockets/resolve.rb#L54-L65)\r\n\r\nSince the absolute path case seems to work just fine, I'm not sure why the logical path shouldn't also work."},"comment":{"url":"https://api.github.com/repos/rails/sprockets/issues/comments/242896612","html_url":"https://github.com/rails/sprockets/issues/209#issuecomment-242896612","issue_url":"https://api.github.com/repos/rails/sprockets/issues/209","id":242896612,"user":{"login":"gilesbowkett","id":974,"avatar_url":"https://avatars.githubusercontent.com/u/974?v=3","gravatar_id":"","url":"https://api.github.com/users/gilesbowkett","html_url":"https://github.com/gilesbowkett","followers_url":"https://api.github.com/users/gilesbowkett/followers","following_url":"https://api.github.com/users/gilesbowkett/following{/other_user}","gists_url":"https://api.github.com/users/gilesbowkett/gists{/gist_id}","starred_url":"https://api.github.com/users/gilesbowkett/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gilesbowkett/subscriptions","organizations_url":"https://api.github.com/users/gilesbowkett/orgs","repos_url":"https://api.github.com/users/gilesbowkett/repos","events_url":"https://api.github.com/users/gilesbowkett/events{/privacy}","received_events_url":"https://api.github.com/users/gilesbowkett/received_events","type":"User","site_admin":false},"created_at":"2016-08-27T05:07:49Z","updated_at":"2016-08-27T05:07:49Z","body":"I dug into this a tiny bit. PR #370 shows an awful hack which gets rid of the problem for the example app. The goal of the PR is not actually to be accepted, just to show what I found. TLDR: `@content_type` isn't getting set correctly."}}
{"actor":{"display_login":"gilesbowkett"}}