4 ms·
Whatever you do, don't autoload Rails `lib/`
- deleted 4y ago[deleted]
- viraptor 4y agoI'm not sure the article really explains why. If one of the solutions is to just move lib to app/lib where it's autoloaded - what's the actual issue with autoloading lib?
- deleted 4y ago[deleted]
- nico 4y agoWhat’s the difference between lib and app/lib if they both autoload or both don’t? Or is the solution to just put everything in a folder that always get autoloaded? Wouldn’t that be the same as autoloading lib?
- wkdneidbwf 4y agoif they both did or didn’t autoload there would be no practical difference. but by default every dir under `app/` autoloads, and `lib/` does not. so using app/lib is really just a reasonable solution for not breaking the existing defaults and conventions.
- jrochkind1 4y agoThe issue OP has run into, if I understand it, is that there are some things that can't autoload and work properly, and when you configure lib/ with a non-default configuration to autoload, there's no place for these things, as lib/ is ordinarily just about the only place in a Rails app that is on the load path but doesn't autoload. No idea what about HN algorithm made this wind up on the front page! It's not an especially interesting post, I agree. Perhaps just a testament to the in fact continued interest in Rails?
- benatkin 4y agoIf you miss Rails but are slinging JavaScript these days there's Nuxt 3. It autoloads all the things.
- p-o 4y agoAt my current place of work, we have this strategy and I think it's a mistake. app/lib is just another way of writing app/service. It becomes, over time, a chaotic place where everything goes. If it doesn't fit for _any_ reason, app/lib becomes the place where those files live. The benefit of lib/ is clearer in my opinion. Files that live there are only explicitly loaded. Some might be loaded in development, some in production, some in test. The conversation about the structure becomes more important because you need to think about what is going to consume your class. But I think we could take this a step further and instead replace lib/ with engines/. This way, you can create multiple Rails engines that will have their own paradigm and environments' handling. Every Rails application I got to work on had a lib/ (or app/service) that could have been grouped in a few different engines/. The benefit of using those kind of engines is that conversation about architecture, pros/cons get more defined. At least that's been my experience.
- LesZedCB 4y agoservices are IMO the biggest misfire of an attempted rails convention. "service" is completely domain agnostic and because there's no standard API like there is for controllers or model, the "service" API ranges from `MyService.call` to `MyService.new.call` `MyService.do_thing` etc etc. service became the junk drawer of junk drawers even worse than lib because people at least 'believe' there is supposed to be a convention when there isn't. at least you know lib is kinda wild west. rails has failed to channel applications into domain logic once it scales past start-up rapid development to a huge team of ICs
- nightpool 4y agoI agree with the former, but I absolutely can't agree with the latter. Services aren't necessary to scale up Rails applications. Plenty of people get along just fine with plain old model-view-controller architectures.
- deleted 4y ago[deleted]
- 4y ago
- rco8786 4y agoEhhh I’ve autoloaded lib for like 10 years and never experienced any of the problems the author says that arise from doing so.
- buf 4y agoYep - me too. Don't know what the author's on about.
- inopinatus 4y agoMe neither, there’s some too-clever-for-it’s-own-good metaprogramming that enthusiastic Ruby devs sometimes indulge in, such as private inner DSLs, or baroque/arcane dicking around with eigenclasses, prepends, anonymous modules etc, that could certainly blow up spectacularly when part of the application code, and should either be extracted to a standalone gem or reconsidered entirely. Having shat out such over-engineered malarkey myself on occasion, I will say, there but for the grace of gods go I.
- grncdr 4y agoOnly ~6 years instead of 10, but I’m also not really sure what the problem is with autoloading lib. The only concrete problem I’m aware of is the presence of lib/tasks creating a Tasks constant. However I’d argue that lib/tasks (like so many other Rails-isms) is a poor choice that’s become enshrined as a holy convention.
- nightpool 4y agoWhy do people keep spelling it "Mastadon"? It's such a strange spelling to me, it's not how it's pronounced and it doesn't really make any sense to me (unlike some mispellings where the "wrong" spelling clearly makes more sense then the actual spelling). But this is just a bog-standard greek root, right up there with masticate and smilodon and all the other -odon prehistoric animals of the world. is there some kind of connection here i'm missing? It's so common that I feel like there must be some kind of underlying logic driving it
- kjeetgill 4y agoSome people just actually pronounce it as Mastadon over Mastodon.
- capableweb 4y agoI think there is a lot of topics one might discuss online that you don't talk with others AFK with, and then the connection between spelling<>pronunciation never gets cemented. I see the same thing all the time with Etherium/Ethereum as well for example. I had the same problem when NGINX first came out. It wasn't until maybe a year later after starting to use it that I actually talked with another person AFK about NGINX, and they said "engine-x" which I had never heard of.
- gjvc 4y agoI work with a Russian guy who still pronounces it the same as hijinx.
- deleted 4y ago[deleted]
- BugsJustFindMe 4y agoThe sounds are not as different as you believe they are. It's the same reason people write Tobasco.
- tdeck 4y ago> I's not how it's pronounced I pronounce it rougly /mæstədan/ and I imagine many other people do as well. That sounds is closer to "a" than "o".
- jack_riminton 4y agoTbh I'm getting tired of these "you must never do x" or "you've been doing y wrong" articles, they're always such overblown statements for mostly trivial things
- btown 4y agoWhile this is generally true, systems like Rails have so much magic going on that "best practices" are actually closer to being part of a specification. Whether that's noteworthy enough for a dedicated post, and why it can't just be part of Rails documentation, is a separate question - but there's nothing trivial about this kind of advice.
- stevebmark 4y agoAutoloading is one of the many weird and painful parts of Rails that favors magic over reliable programs. In development mode, Rails autoloads files ad-hoc. This means that as you navigate your website, you're loading new modules. And the Ruby community adores implicit metaprogramming, so the fundamental structure of your classes and data changes, and it's different depending on how you navigate your local website. We got some horrific non-deterministic errors in development that would require a local server restart to undo. It's really scary stuff. This blog post is about a problem that shouldn't exist, and doesn't exit in any other modern language ecosystem.
- SkyPuncher 4y agoI really like rails, but the "convention over configuration" absolutely drives me up the wall. It creates a lot of situations that are challenging to understand when explicit references would solve the issue. One thing I very much like about JS (among other languages) is the explicit import system. You can walk the dependency chain up and down to figure out exactly where and why certain code is called.
- byroot 4y agoYour code is supposed to be loadable in any order. If not, you are doing something wrong and you could experience just the same issues without it. But if you don't like autoloading, you can turn it off with just `config.eager_load = true`.
- stevebmark 4y agoSure, code isn't supposed to do lots of stuff. Ruby and Rails just make it hyper-easy to make difficult to debug errors.
- byroot 4y agoAll I’m saying is that autoloading isn’t the reason.
- chao- 4y agoI agree: don't autoload lib. Even better: Autoload discrete subdirectories in lib occasionally. Occasionally? If something is a library, why do you want autoloading anyway? The likely answer is that you are actively working on it, and autoloading allows quick feedback loops, and is a huge productivity boost. I would expect a "library" is a true, cross-cutting technical dependency, not behavior for the application domain (or else it ought to be in app). It is safe to autoload some number of things inside of lib some of the time, if you knowingly placed it there. One manner of achieving this: - Be specific, and don't use a wildcard or globbing notation. Maintain a list of exclusive sub-directories in lib that are capable of optionally being auto-loaded (in development only). - Set up an environment variable that controls when and which of these curated lib subdirectories are be autoloaded. Only enable one when actively working on it, keep disabled otherwise. As with many things in Rails, there are (a) a dozen other ways to achieve a very similar result, (b) people with different aesthetic preferences for one or the other, and (c) other people who chafe at there not being a "blessed" way to do this, or even think that making one's own conventions ought not be done.
- nightpool 4y ago> If something is a library, why do you want autoloading anyway? What? Bundler always "auto-loads" all of your gems automatically anyway—that's the whole point of the require: statement. If anything, thinking about lib/ code as library code makes it MORE obvious that it should be autoloaded—why should gem "papertrail" and lib/papertrail.rb ever behave any differently? Both should provide Papertrail constants to the rest of the application.
- chao- 4y agoI think we are using "autoload" to discuss different things, one of which is probably in line with what the article is about, and the other is not. And I may be the one misreading here! I take "autoloading" in the Rails context to mean "Rails.application.config.autoload_paths", which is separate from a Ruby "require", and which has always had all sorts of complexity, but is supposed to be recommended against as of Rails 6. You are, I believe, referring to "require" call to load a library once (which is what Bundler does). The Rails autoloading (once homegrown, now via zeitwerk) is used to hotswap to a new version of a constant when a file has changed. It does so by searching for missing constants on the file system based on "root paths", in this case using "config.autoload_paths" plus a few defaults. This hotswap nature of autoloading is why I reference autoloading a library specifically in the context where one is "actively working on it, and autoloading allows quick feedback loops". A simple "require" does not do that. It loads the library once, ala Bundler, and unless it lives in the tree of an autoload "root path", changes will not be available until one restarts the server/console/rake task, etc. I agree that, of course, that one's libraries ought to be loaded via "require" to be used. Otherwise, they are dead code, and might as well not be part of the project source. My suggestion of an ENV var is that it should swap from a single require at application boot to live autoloading.
- drdaeman 4y agoWhat’s the point of autoloading? Just being lazy about require statements? Rails always baffled me in this regard, things coming from seemingly nowhere, sometimes (in a large and messy codebases) hard to trace back and comprehend.
- graypegg 4y agoThings are indexed by name, so it’s normally pretty easy to trace back where something comes from based on that unique symbol name, especially with IDE tools. I at least don’t find it to be a problem. The big list of imports at the top of most files in my nodejs projects I find more annoying personally.
- bluepod4 4y agoidk. This was common knowledge/advice when I first worked with Rails professionally on a startup-size team over a decade ago. The writer says “That’s when people jump to googling ‘how to autoload lib/‘. Don’t do it! lib/ should not be autoloaded.” But from experience, that’s not what was commonly queried. The search terms or the question asked to senior engineers went something like “my Rails lib dependency is not loading in production” which always lead to the “require” solution and the advice against autoloading. Thinking about it more, I don’t recall ever googling the word “autoload”. Have all of those old StackOverflow posts been removed? Are newer devs not using linters that check for this? Is the audience for this article solo devs inexperienced with working with Rails?
- Fire-Dragon-DoL 4y agoI mean, the reason is right there: every rails developer with some experience knows that lib is not autoloaded. So yes, familiarity. Putting autoload on lib is equivalent to monkey patching. Creating a directory app/lib is a great way to achieve confusion and make the fuzzy finder fail more often though. Give it another name if you can