Skip to content

Namespace the version file on the file system#30

Merged
etagwerker merged 2 commits intofastruby:mainfrom
JoshCheek:namespace-version-file
Sep 10, 2021
Merged

Namespace the version file on the file system#30
etagwerker merged 2 commits intofastruby:mainfrom
JoshCheek:namespace-version-file

Conversation

@JoshCheek
Copy link
Copy Markdown
Contributor

The version file is at the toplevel dir in lib, which makes it not namespaced on the file system. Thus require "version" is really requiring dotenv_validator's version, despite the fact that this is unexpected. The solution that people have arrived at is to namespace files within a directory that matches the gem name. So, this movew lib/version.rb to lib/dotenv_validator/version.rb.

For a bit more context on this, I go into it here.

Here is a demonstration that requiring "version" winds up requiring this file:

gem install --no-document dotenv_validator -v 1.0.0 >/dev/null && ruby -e '
   features = $LOADED_FEATURES.dup
   require "version"
   puts $LOADED_FEATURES - features'
/Users/josh/.rubies/ruby-3.0.2/lib/ruby/3.0.0/rubygems/bundler_version_finder.rb
/Users/josh/.gem/ruby/3.0.2/gems/dotenv_validator-1.0.0/lib/version.rb

Additionally, I built the gem to verify that this works as expected, and then saw that the built gem wound up in a pkg directory, which git was aware of. This seemed precarious b/c it would be easy enough to add and commit previous gems into the repo. So I also added pkg to .gitignore.

I will abide by the code of conduct.

The version file is at the toplevel dir in `lib`, which makes it not
namespaced on the file system. Thus `require "version"` is really
requiring `dotenv_validator`'s version, despite the fact that this is
unexpected. The solution that people have arrived at is to namespace
files within a directory that matches the gem name. So, this movew
`lib/version.rb` to `lib/dotenv_validator/version.rb`.

For a bit more context on this, I go into it
[here](https://github.com/JoshCheek/elective-building-a-gem/blob/085283ee848287f2b57f8ec5da963c5d775eb3de/Day2.md#namespacing-our-gems-files-and-classes).

```sh
gem install --no-document dotenv_validator -v 1.0.0 >/dev/null && ruby -e '
   features = $LOADED_FEATURES.dup
   require "version"
   puts $LOADED_FEATURES - features'
/Users/josh/.rubies/ruby-3.0.2/lib/ruby/3.0.0/rubygems/bundler_version_finder.rb
/Users/josh/.gem/ruby/3.0.2/gems/dotenv_validator-1.0.0/lib/version.rb
```
Copy link
Copy Markdown
Member

@etagwerker etagwerker left a comment

Choose a reason for hiding this comment

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

@JoshCheek Looks good, thanks! 👍

@etagwerker etagwerker merged commit d447904 into fastruby:main Sep 10, 2021
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