# Alias for require "../../../../../../coolfile"?

**URL:** <https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633>\
**Category:** Community\
**Created:** [January 16, 2020, 10:29pm UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633 "2020-01-16T22:29:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![carcinocron](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/carcinocron/32/1022_2.png) [@carcinocron](https://forum.crystal-lang.org/u/carcinocron)\
**Post date:** [January 16, 2020, 10:29pm UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/1 "2020-01-16T22:29:59Z")

</div>

Is there syntax for something like shortening

```auto
require "../../../../../../coolfile"?

```

to

```auto
require "~/foo/coolfile"?

```

?

`~` (or another symbol) would be shorthand for project root or `./src/`

this is common in javascript.

---

<div class="post-metadata">

**Author:** ![paulcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/paulcsmith/32/301_2.png) [@paulcsmith](https://forum.crystal-lang.org/u/paulcsmith)\
**Post date:** [January 16, 2020, 10:51pm UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/2 "2020-01-16T22:51:58Z")

</div>

I suggested this and was shot down. I still don’t quite understand why because I think it’s a clear win. I’d love to see it in crystal!

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 16, 2020, 11:36pm UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/3 "2020-01-16T23:36:43Z")

</div>

Could you elaborate on use cases for this?  
The general idea is that required code is either from a library (i.e. shard) in `lib` folder or relative to the current file. What do you need relative to the project root for?

---

<div class="post-metadata">

**Author:** ![carcinocron](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/carcinocron/32/1022_2.png) [@carcinocron](https://forum.crystal-lang.org/u/carcinocron)\
**Post date:** [January 17, 2020, 12:09am UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/4 "2020-01-17T00:09:48Z")

</div>

Its useful because `require "../../../../../../coolfile"` is bad for maintenance in large projects.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [January 17, 2020, 10:07am UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/5 "2020-01-17T10:07:21Z")

</div>

Would that be relative to the project root, if you are in a project, and relative to the current shard, if you are in a shard? I’m having a bit of trouble understanding how to distinguish both cases when you have that in a project with shards. That is, a shard uses that path but you are in a project that uses that shard.

I’m almost sure it’s doable by checking something something relative to the crystal path, but I’d like to see a pseudo algorithm for this.

---

<div class="post-metadata">

**Author:** ![mavu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mavu/32/213_2.png) [@mavu](https://forum.crystal-lang.org/u/mavu)\
**Post date:** [January 17, 2020, 10:42am UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/6 "2020-01-17T10:42:52Z")

</div>

and if you just have a crystal file in a directory, is it relative to the file?  
Or relative to the place you started the compiler from?

---

<div class="post-metadata">

**Author:** ![carcinocron](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/carcinocron/32/1022_2.png) [@carcinocron](https://forum.crystal-lang.org/u/carcinocron)\
**Post date:** [January 17, 2020, 11:15am UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/7 "2020-01-17T11:15:19Z")

</div>

Maybe what I would do is put alias definitions in a file like shard.yml or alias.yml. So users could define aliases that only apply in their project or in their shard. Somebody might like ~ for their root, somebody else might like @server, @client, @tests.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 17, 2020, 12:57pm UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/8 "2020-01-17T12:57:27Z")

</div>

But why do you have such a long path in the first place? What’s the use case?

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 17, 2020, 1:00pm UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/9 "2020-01-17T13:00:27Z")

</div>

Custom aliases in shard.yml are most certainly not going to happen. I can imagine there might be uses cases. But there are probably only going to be a couple of really useful path references and then they should be hard coded, so they have the same meaning everywhere.

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [January 17, 2020, 1:59pm UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/10 "2020-01-17T13:59:36Z")

</div>

Some workarounds:

1. Any linked directory in `./lib` will be searchable as a shard. The [require logic](https://crystal-lang.org/reference/syntax_and_semantics/requiring_files.html) is flexible enough probably to find something that will suit your needs.

2. Any other directory can take part in this lookup. Change your `CRYSTAL_PATH` env variable and include your wanted sources. Use `$ crystal env` to check the current values.

3. Maybe a way to have that option right now in your project is to have a macro that will search for the first shard.yml file and use that as information.

Yet I find appealing the idea of `~` meaning root of the context shard. But I have not needed it so far. I usually don’t deal with such nested files.

---

<div class="post-metadata">

**Author:** ![Fryguy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fryguy/32/1181_2.png) [@Fryguy](https://forum.crystal-lang.org/u/Fryguy)\
**Post date:** [January 20, 2020, 5:36pm UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/11 "2020-01-20T17:36:39Z")

</div>

What if it were something like shards setting up a PROJECT\_ROOT env var that matches the location of the shards.yml. Then in the project the author could do something like `require “#{ENV[“PROJECT\_ROOT”]}/src/coolfile”.

Not sure how that would work for shards themselves that are in the lib dir. Maybe a separate SHARDS\_ROOT or something.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 20, 2020, 6:33pm UTC](https://forum.crystal-lang.org/t/alias-for-require-coolfile/1633/12 "2020-01-20T18:33:45Z")

</div>

> [@Fryguy](#):
>
> Not sure how that would work for shards themselves that are in the lib dir.

They can always be required as `shardname`.
